blob: 22c630b7ba6ca48f94aa5a24bf2c345353345c52 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/buffer_head.h> /* for invalidate_bdev */
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080042#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110043#include <linux/hdreg.h>
44#include <linux/proc_fs.h>
45#include <linux/random.h>
46#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070047#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110048#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110049#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110050#include <linux/raid/md_p.h>
51#include <linux/raid/md_u.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110052#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110053#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#define DEBUG 0
56#define dprintk(x...) ((void)(DEBUG && printk(x)))
57
58
59#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110060static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#endif
62
NeilBrown2604b702006-01-06 00:20:36 -080063static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static DEFINE_SPINLOCK(pers_lock);
65
Adrian Bunk5e563412006-06-26 00:27:42 -070066static void md_print_devices(void);
67
Bernd Schubert90b08712008-05-23 13:04:38 -070068static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
69
Adrian Bunk5e563412006-06-26 00:27:42 -070070#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
Robert Becker1e509152009-12-14 12:49:58 +110073 * Default number of read corrections we'll attempt on an rdev
74 * before ejecting it from the array. We divide the read error
75 * count by 2 for every hour elapsed between read errors.
76 */
77#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
78/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
80 * is 1000 KB/sec, so the extra system load does not show up that much.
81 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070082 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 * subsystem is idle. There is also an 'absolute maximum' reconstruction
84 * speed limit - in case reconstruction slows down your system despite
85 * idle IO detection.
86 *
87 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080088 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 */
90
91static int sysctl_speed_limit_min = 1000;
92static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080093static inline int speed_min(mddev_t *mddev)
94{
95 return mddev->sync_speed_min ?
96 mddev->sync_speed_min : sysctl_speed_limit_min;
97}
98
99static inline int speed_max(mddev_t *mddev)
100{
101 return mddev->sync_speed_max ?
102 mddev->sync_speed_max : sysctl_speed_limit_max;
103}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105static struct ctl_table_header *raid_table_header;
106
107static ctl_table raid_table[] = {
108 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 .procname = "speed_limit_min",
110 .data = &sysctl_speed_limit_min,
111 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700112 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800113 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 },
115 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 .procname = "speed_limit_max",
117 .data = &sysctl_speed_limit_max,
118 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700119 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800120 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800122 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123};
124
125static ctl_table raid_dir_table[] = {
126 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 .procname = "raid",
128 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700129 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 .child = raid_table,
131 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800132 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133};
134
135static ctl_table raid_root_table[] = {
136 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .procname = "dev",
138 .maxlen = 0,
139 .mode = 0555,
140 .child = raid_dir_table,
141 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800142 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700145static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
NeilBrownf91de922005-11-08 21:39:36 -0800147static int start_readonly;
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/*
NeilBrownd7603b72006-01-06 00:20:30 -0800150 * We have a system wide 'event count' that is incremented
151 * on any 'interesting' event, and readers of /proc/mdstat
152 * can use 'poll' or 'select' to find out when the event
153 * count increases.
154 *
155 * Events are:
156 * start array, stop array, error, add device, remove device,
157 * start build, activate spare
158 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800159static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800160static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800161void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800162{
163 atomic_inc(&md_event_count);
164 wake_up(&md_event_waiters);
165}
NeilBrown29269552006-03-27 01:18:10 -0800166EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800167
NeilBrownc331eb02006-05-30 21:27:13 -0700168/* Alternate version that can be called from interrupts
169 * when calling sysfs_notify isn't needed.
170 */
Adrian Bunk05381952006-06-26 00:28:01 -0700171static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700172{
173 atomic_inc(&md_event_count);
174 wake_up(&md_event_waiters);
175}
176
NeilBrownd7603b72006-01-06 00:20:30 -0800177/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * Enables to iterate over all existing md arrays
179 * all_mddevs_lock protects this list.
180 */
181static LIST_HEAD(all_mddevs);
182static DEFINE_SPINLOCK(all_mddevs_lock);
183
184
185/*
186 * iterates through all used mddevs in the system.
187 * We take care to grab the all_mddevs_lock whenever navigating
188 * the list, and to always hold a refcount when unlocked.
189 * Any code which breaks out of this loop while own
190 * a reference to the current mddev and must mddev_put it.
191 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800192#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 \
194 for (({ spin_lock(&all_mddevs_lock); \
195 tmp = all_mddevs.next; \
196 mddev = NULL;}); \
197 ({ if (tmp != &all_mddevs) \
198 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
199 spin_unlock(&all_mddevs_lock); \
200 if (mddev) mddev_put(mddev); \
201 mddev = list_entry(tmp, mddev_t, all_mddevs); \
202 tmp != &all_mddevs;}); \
203 ({ spin_lock(&all_mddevs_lock); \
204 tmp = tmp->next;}) \
205 )
206
207
NeilBrown409c57f2009-03-31 14:39:39 +1100208/* Rather than calling directly into the personality make_request function,
209 * IO requests come here first so that we can check if the device is
210 * being suspended pending a reconfiguration.
211 * We hold a refcount over the call to ->make_request. By the time that
212 * call has finished, the bio has been linked into some internal structure
213 * and so is visible to ->quiesce(), so we don't need the refcount any more.
214 */
215static int md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
NeilBrown409c57f2009-03-31 14:39:39 +1100217 mddev_t *mddev = q->queuedata;
218 int rv;
219 if (mddev == NULL || mddev->pers == NULL) {
220 bio_io_error(bio);
221 return 0;
222 }
223 rcu_read_lock();
NeilBrowna2826aa2009-12-14 12:49:49 +1100224 if (mddev->suspended || mddev->barrier) {
NeilBrown409c57f2009-03-31 14:39:39 +1100225 DEFINE_WAIT(__wait);
226 for (;;) {
227 prepare_to_wait(&mddev->sb_wait, &__wait,
228 TASK_UNINTERRUPTIBLE);
NeilBrowna2826aa2009-12-14 12:49:49 +1100229 if (!mddev->suspended && !mddev->barrier)
NeilBrown409c57f2009-03-31 14:39:39 +1100230 break;
231 rcu_read_unlock();
232 schedule();
233 rcu_read_lock();
234 }
235 finish_wait(&mddev->sb_wait, &__wait);
236 }
237 atomic_inc(&mddev->active_io);
238 rcu_read_unlock();
239 rv = mddev->pers->make_request(q, bio);
240 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
241 wake_up(&mddev->sb_wait);
242
243 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
NeilBrown409c57f2009-03-31 14:39:39 +1100246static void mddev_suspend(mddev_t *mddev)
247{
248 BUG_ON(mddev->suspended);
249 mddev->suspended = 1;
250 synchronize_rcu();
251 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
252 mddev->pers->quiesce(mddev, 1);
253 md_unregister_thread(mddev->thread);
254 mddev->thread = NULL;
255 /* we now know that no code is executing in the personality module,
256 * except possibly the tail end of a ->bi_end_io function, but that
257 * is certain to complete before the module has a chance to get
258 * unloaded
259 */
260}
261
262static void mddev_resume(mddev_t *mddev)
263{
264 mddev->suspended = 0;
265 wake_up(&mddev->sb_wait);
266 mddev->pers->quiesce(mddev, 0);
267}
268
NeilBrown3fa841d2009-09-23 18:10:29 +1000269int mddev_congested(mddev_t *mddev, int bits)
270{
NeilBrowna2826aa2009-12-14 12:49:49 +1100271 if (mddev->barrier)
272 return 1;
NeilBrown3fa841d2009-09-23 18:10:29 +1000273 return mddev->suspended;
274}
275EXPORT_SYMBOL(mddev_congested);
276
NeilBrowna2826aa2009-12-14 12:49:49 +1100277/*
278 * Generic barrier handling for md
279 */
280
281#define POST_REQUEST_BARRIER ((void*)1)
282
283static void md_end_barrier(struct bio *bio, int err)
284{
285 mdk_rdev_t *rdev = bio->bi_private;
286 mddev_t *mddev = rdev->mddev;
287 if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER)
288 set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags);
289
290 rdev_dec_pending(rdev, mddev);
291
292 if (atomic_dec_and_test(&mddev->flush_pending)) {
293 if (mddev->barrier == POST_REQUEST_BARRIER) {
294 /* This was a post-request barrier */
295 mddev->barrier = NULL;
296 wake_up(&mddev->sb_wait);
297 } else
298 /* The pre-request barrier has finished */
299 schedule_work(&mddev->barrier_work);
300 }
301 bio_put(bio);
302}
303
304static void submit_barriers(mddev_t *mddev)
305{
306 mdk_rdev_t *rdev;
307
308 rcu_read_lock();
309 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
310 if (rdev->raid_disk >= 0 &&
311 !test_bit(Faulty, &rdev->flags)) {
312 /* Take two references, one is dropped
313 * when request finishes, one after
314 * we reclaim rcu_read_lock
315 */
316 struct bio *bi;
317 atomic_inc(&rdev->nr_pending);
318 atomic_inc(&rdev->nr_pending);
319 rcu_read_unlock();
320 bi = bio_alloc(GFP_KERNEL, 0);
321 bi->bi_end_io = md_end_barrier;
322 bi->bi_private = rdev;
323 bi->bi_bdev = rdev->bdev;
324 atomic_inc(&mddev->flush_pending);
325 submit_bio(WRITE_BARRIER, bi);
326 rcu_read_lock();
327 rdev_dec_pending(rdev, mddev);
328 }
329 rcu_read_unlock();
330}
331
332static void md_submit_barrier(struct work_struct *ws)
333{
334 mddev_t *mddev = container_of(ws, mddev_t, barrier_work);
335 struct bio *bio = mddev->barrier;
336
337 atomic_set(&mddev->flush_pending, 1);
338
339 if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags))
340 bio_endio(bio, -EOPNOTSUPP);
341 else if (bio->bi_size == 0)
342 /* an empty barrier - all done */
343 bio_endio(bio, 0);
344 else {
345 bio->bi_rw &= ~(1<<BIO_RW_BARRIER);
346 if (mddev->pers->make_request(mddev->queue, bio))
347 generic_make_request(bio);
348 mddev->barrier = POST_REQUEST_BARRIER;
349 submit_barriers(mddev);
350 }
351 if (atomic_dec_and_test(&mddev->flush_pending)) {
352 mddev->barrier = NULL;
353 wake_up(&mddev->sb_wait);
354 }
355}
356
357void md_barrier_request(mddev_t *mddev, struct bio *bio)
358{
359 spin_lock_irq(&mddev->write_lock);
360 wait_event_lock_irq(mddev->sb_wait,
361 !mddev->barrier,
362 mddev->write_lock, /*nothing*/);
363 mddev->barrier = bio;
364 spin_unlock_irq(&mddev->write_lock);
365
366 atomic_set(&mddev->flush_pending, 1);
367 INIT_WORK(&mddev->barrier_work, md_submit_barrier);
368
369 submit_barriers(mddev);
370
371 if (atomic_dec_and_test(&mddev->flush_pending))
372 schedule_work(&mddev->barrier_work);
373}
374EXPORT_SYMBOL(md_barrier_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376static inline mddev_t *mddev_get(mddev_t *mddev)
377{
378 atomic_inc(&mddev->active);
379 return mddev;
380}
381
Dan Williams5fd3a172009-03-04 00:57:25 -0700382static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384static void mddev_put(mddev_t *mddev)
385{
386 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
387 return;
NeilBrownd3374822009-01-09 08:31:10 +1100388 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100389 mddev->ctime == 0 && !mddev->hold_active) {
390 /* Array is not configured at all, and not held active,
391 * so destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 list_del(&mddev->all_mddevs);
NeilBrownd3374822009-01-09 08:31:10 +1100393 if (mddev->gendisk) {
394 /* we did a probe so need to clean up.
395 * Call schedule_work inside the spinlock
396 * so that flush_scheduled_work() after
397 * mddev_find will succeed in waiting for the
398 * work to be done.
399 */
400 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
401 schedule_work(&mddev->del_work);
402 } else
403 kfree(mddev);
404 }
405 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
408static mddev_t * mddev_find(dev_t unit)
409{
410 mddev_t *mddev, *new = NULL;
411
412 retry:
413 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
NeilBrownefeb53c2009-01-09 08:31:10 +1100415 if (unit) {
416 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
417 if (mddev->unit == unit) {
418 mddev_get(mddev);
419 spin_unlock(&all_mddevs_lock);
420 kfree(new);
421 return mddev;
422 }
423
424 if (new) {
425 list_add(&new->all_mddevs, &all_mddevs);
426 spin_unlock(&all_mddevs_lock);
427 new->hold_active = UNTIL_IOCTL;
428 return new;
429 }
430 } else if (new) {
431 /* find an unused unit number */
432 static int next_minor = 512;
433 int start = next_minor;
434 int is_free = 0;
435 int dev = 0;
436 while (!is_free) {
437 dev = MKDEV(MD_MAJOR, next_minor);
438 next_minor++;
439 if (next_minor > MINORMASK)
440 next_minor = 0;
441 if (next_minor == start) {
442 /* Oh dear, all in use. */
443 spin_unlock(&all_mddevs_lock);
444 kfree(new);
445 return NULL;
446 }
447
448 is_free = 1;
449 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
450 if (mddev->unit == dev) {
451 is_free = 0;
452 break;
453 }
454 }
455 new->unit = dev;
456 new->md_minor = MINOR(dev);
457 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 list_add(&new->all_mddevs, &all_mddevs);
459 spin_unlock(&all_mddevs_lock);
460 return new;
461 }
462 spin_unlock(&all_mddevs_lock);
463
NeilBrown9ffae0c2006-01-06 00:20:32 -0800464 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (!new)
466 return NULL;
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 new->unit = unit;
469 if (MAJOR(unit) == MD_MAJOR)
470 new->md_minor = MINOR(unit);
471 else
472 new->md_minor = MINOR(unit) >> MdpMinorShift;
473
NeilBrownc8c00a62009-08-10 12:50:52 +1000474 mutex_init(&new->open_mutex);
NeilBrowndf5b89b2006-03-27 01:18:20 -0800475 mutex_init(&new->reconfig_mutex);
NeilBrownc3d97142009-12-14 12:49:52 +1100476 mutex_init(&new->bitmap_info.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 INIT_LIST_HEAD(&new->disks);
478 INIT_LIST_HEAD(&new->all_mddevs);
479 init_timer(&new->safemode_timer);
480 atomic_set(&new->active, 1);
NeilBrownf2ea68c2008-07-21 17:05:25 +1000481 atomic_set(&new->openers, 0);
NeilBrown409c57f2009-03-31 14:39:39 +1100482 atomic_set(&new->active_io, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700483 spin_lock_init(&new->write_lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100484 atomic_set(&new->flush_pending, 0);
NeilBrown3d310eb2005-06-21 17:17:26 -0700485 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700486 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700487 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000488 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800489 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700490 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 goto retry;
493}
494
495static inline int mddev_lock(mddev_t * mddev)
496{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800497 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Dan Williamsb522adc2009-03-31 15:00:31 +1100500static inline int mddev_is_locked(mddev_t *mddev)
501{
502 return mutex_is_locked(&mddev->reconfig_mutex);
503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505static inline int mddev_trylock(mddev_t * mddev)
506{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800507 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
NeilBrownb6eb1272010-04-15 10:13:47 +1000510static struct attribute_group md_redundancy_group;
511
NeilBrowna64c8762010-04-14 17:15:37 +1000512static void mddev_unlock(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
NeilBrowna64c8762010-04-14 17:15:37 +1000514 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000515 /* These cannot be removed under reconfig_mutex as
516 * an access to the files will try to take reconfig_mutex
517 * while holding the file unremovable, which leads to
518 * a deadlock.
519 * So hold open_mutex instead - we are allowed to take
520 * it while holding reconfig_mutex, and md_run can
521 * use it to wait for the remove to complete.
522 */
NeilBrowna64c8762010-04-14 17:15:37 +1000523 struct attribute_group *to_remove = mddev->to_remove;
524 mddev->to_remove = NULL;
NeilBrownb6eb1272010-04-15 10:13:47 +1000525 mutex_lock(&mddev->open_mutex);
526 mutex_unlock(&mddev->reconfig_mutex);
527
NeilBrowna64c8762010-04-14 17:15:37 +1000528 if (to_remove != &md_redundancy_group)
529 sysfs_remove_group(&mddev->kobj, to_remove);
530 if (mddev->pers == NULL ||
531 mddev->pers->sync_request == NULL) {
532 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
533 if (mddev->sysfs_action)
534 sysfs_put(mddev->sysfs_action);
535 mddev->sysfs_action = NULL;
536 }
NeilBrownb6eb1272010-04-15 10:13:47 +1000537 mutex_unlock(&mddev->open_mutex);
538 } else
539 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
NeilBrown005eca52005-08-22 13:11:08 -0700541 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
NeilBrown2989ddb2006-01-06 00:20:43 -0800544static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100546 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100548 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (rdev->desc_nr == nr)
550 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return NULL;
553}
554
555static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
556{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 mdk_rdev_t *rdev;
558
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100559 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (rdev->bdev->bd_dev == dev)
561 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return NULL;
564}
565
NeilBrownd9d166c2006-01-06 00:20:51 -0800566static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800567{
568 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800569 list_for_each_entry(pers, &pers_list, list) {
570 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800571 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800572 if (strcmp(pers->name, clevel)==0)
573 return pers;
574 }
NeilBrown2604b702006-01-06 00:20:36 -0800575 return NULL;
576}
577
Andre Nollb73df2d2008-07-11 22:02:23 +1000578/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700579static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Andre Nollb73df2d2008-07-11 22:02:23 +1000581 sector_t num_sectors = bdev->bd_inode->i_size / 512;
582 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585static int alloc_disk_sb(mdk_rdev_t * rdev)
586{
587 if (rdev->sb_page)
588 MD_BUG();
589
590 rdev->sb_page = alloc_page(GFP_KERNEL);
591 if (!rdev->sb_page) {
592 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000593 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595
596 return 0;
597}
598
599static void free_disk_sb(mdk_rdev_t * rdev)
600{
601 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800602 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 rdev->sb_loaded = 0;
604 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000605 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100606 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608}
609
610
NeilBrown6712ecf2007-09-27 12:47:43 +0200611static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700612{
613 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800614 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700615
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700616 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
617 printk("md: super_written gets error=%d, uptodate=%d\n",
618 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
619 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800620 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700621 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700622
NeilBrowna9701a32005-11-08 21:39:34 -0800623 if (atomic_dec_and_test(&mddev->pending_writes))
624 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700625 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700626}
627
NeilBrown6712ecf2007-09-27 12:47:43 +0200628static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800629{
630 struct bio *bio2 = bio->bi_private;
631 mdk_rdev_t *rdev = bio2->bi_private;
632 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800633
634 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
635 error == -EOPNOTSUPP) {
636 unsigned long flags;
637 /* barriers don't appear to be supported :-( */
638 set_bit(BarriersNotsupp, &rdev->flags);
639 mddev->barriers_work = 0;
640 spin_lock_irqsave(&mddev->write_lock, flags);
641 bio2->bi_next = mddev->biolist;
642 mddev->biolist = bio2;
643 spin_unlock_irqrestore(&mddev->write_lock, flags);
644 wake_up(&mddev->sb_wait);
645 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200646 } else {
647 bio_put(bio2);
648 bio->bi_private = rdev;
649 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800650 }
NeilBrowna9701a32005-11-08 21:39:34 -0800651}
652
NeilBrown7bfa19f2005-06-21 17:17:28 -0700653void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
654 sector_t sector, int size, struct page *page)
655{
656 /* write first size bytes of page to sector of rdev
657 * Increment mddev->pending_writes before returning
658 * and decrement it on completion, waking up sb_wait
659 * if zero is reached.
660 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800661 *
662 * As we might need to resubmit the request if BIO_RW_BARRIER
663 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700664 */
665 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Jens Axboe93dbb392009-02-16 10:25:40 +0100666 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700667
668 bio->bi_bdev = rdev->bdev;
669 bio->bi_sector = sector;
670 bio_add_page(bio, page, size, 0);
671 bio->bi_private = rdev;
672 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800673 bio->bi_rw = rw;
674
NeilBrown7bfa19f2005-06-21 17:17:28 -0700675 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800676 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
677 struct bio *rbio;
678 rw |= (1<<BIO_RW_BARRIER);
679 rbio = bio_clone(bio, GFP_NOIO);
680 rbio->bi_private = bio;
681 rbio->bi_end_io = super_written_barrier;
682 submit_bio(rw, rbio);
683 } else
684 submit_bio(rw, bio);
685}
686
687void md_super_wait(mddev_t *mddev)
688{
689 /* wait for all superblock writes that were scheduled to complete.
690 * if any had to be retried (due to BARRIER problems), retry them
691 */
692 DEFINE_WAIT(wq);
693 for(;;) {
694 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
695 if (atomic_read(&mddev->pending_writes)==0)
696 break;
697 while (mddev->biolist) {
698 struct bio *bio;
699 spin_lock_irq(&mddev->write_lock);
700 bio = mddev->biolist;
701 mddev->biolist = bio->bi_next ;
702 bio->bi_next = NULL;
703 spin_unlock_irq(&mddev->write_lock);
704 submit_bio(bio->bi_rw, bio);
705 }
706 schedule();
707 }
708 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700709}
710
NeilBrown6712ecf2007-09-27 12:47:43 +0200711static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
NeilBrowna654b9d82005-06-21 17:17:27 -0700716int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 struct page *page, int rw)
718{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700719 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 struct completion event;
721 int ret;
722
Jens Axboe93dbb392009-02-16 10:25:40 +0100723 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 bio->bi_bdev = bdev;
726 bio->bi_sector = sector;
727 bio_add_page(bio, page, size, 0);
728 init_completion(&event);
729 bio->bi_private = &event;
730 bio->bi_end_io = bi_complete;
731 submit_bio(rw, bio);
732 wait_for_completion(&event);
733
734 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
735 bio_put(bio);
736 return ret;
737}
NeilBrowna8745db2006-01-06 00:20:34 -0800738EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
NeilBrown0002b272005-09-09 16:23:53 -0700740static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 char b[BDEVNAME_SIZE];
743 if (!rdev->sb_page) {
744 MD_BUG();
745 return -EINVAL;
746 }
747 if (rdev->sb_loaded)
748 return 0;
749
750
Andre Noll0f420352008-07-11 22:02:23 +1000751 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 goto fail;
753 rdev->sb_loaded = 1;
754 return 0;
755
756fail:
757 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
758 bdevname(rdev->bdev,b));
759 return -EINVAL;
760}
761
762static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
763{
Andre Noll05710462008-07-11 22:02:20 +1000764 return sb1->set_uuid0 == sb2->set_uuid0 &&
765 sb1->set_uuid1 == sb2->set_uuid1 &&
766 sb1->set_uuid2 == sb2->set_uuid2 &&
767 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
771{
772 int ret;
773 mdp_super_t *tmp1, *tmp2;
774
775 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
776 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
777
778 if (!tmp1 || !tmp2) {
779 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100780 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 goto abort;
782 }
783
784 *tmp1 = *sb1;
785 *tmp2 = *sb2;
786
787 /*
788 * nr_disks is not constant
789 */
790 tmp1->nr_disks = 0;
791 tmp2->nr_disks = 0;
792
Andre Nollce0c8e02008-07-11 22:02:20 +1000793 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700795 kfree(tmp1);
796 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return ret;
798}
799
NeilBrown4d167f02007-05-09 02:35:37 -0700800
801static u32 md_csum_fold(u32 csum)
802{
803 csum = (csum & 0xffff) + (csum >> 16);
804 return (csum & 0xffff) + (csum >> 16);
805}
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807static unsigned int calc_sb_csum(mdp_super_t * sb)
808{
NeilBrown4d167f02007-05-09 02:35:37 -0700809 u64 newcsum = 0;
810 u32 *sb32 = (u32*)sb;
811 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 unsigned int disk_csum, csum;
813
814 disk_csum = sb->sb_csum;
815 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700816
817 for (i = 0; i < MD_SB_BYTES/4 ; i++)
818 newcsum += sb32[i];
819 csum = (newcsum & 0xffffffff) + (newcsum>>32);
820
821
822#ifdef CONFIG_ALPHA
823 /* This used to use csum_partial, which was wrong for several
824 * reasons including that different results are returned on
825 * different architectures. It isn't critical that we get exactly
826 * the same return value as before (we always csum_fold before
827 * testing, and that removes any differences). However as we
828 * know that csum_partial always returned a 16bit value on
829 * alphas, do a fold to maximise conformity to previous behaviour.
830 */
831 sb->sb_csum = md_csum_fold(disk_csum);
832#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700834#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return csum;
836}
837
838
839/*
840 * Handle superblock details.
841 * We want to be able to handle multiple superblock formats
842 * so we have a common interface to them all, and an array of
843 * different handlers.
844 * We rely on user-space to write the initial superblock, and support
845 * reading and updating of superblocks.
846 * Interface methods are:
847 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
848 * loads and validates a superblock on dev.
849 * if refdev != NULL, compare superblocks on both devices
850 * Return:
851 * 0 - dev has a superblock that is compatible with refdev
852 * 1 - dev has a superblock that is compatible and newer than refdev
853 * so dev should be used as the refdev in future
854 * -EINVAL superblock incompatible or invalid
855 * -othererror e.g. -EIO
856 *
857 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
858 * Verify that dev is acceptable into mddev.
859 * The first time, mddev->raid_disks will be 0, and data from
860 * dev should be merged in. Subsequent calls check that dev
861 * is new enough. Return 0 or -EINVAL
862 *
863 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
864 * Update the superblock for rdev with data in mddev
865 * This does not write to disc.
866 *
867 */
868
869struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000870 char *name;
871 struct module *owner;
872 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
873 int minor_version);
874 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
875 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
876 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000877 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878};
879
880/*
Andre Noll0894cc32009-06-18 08:49:23 +1000881 * Check that the given mddev has no bitmap.
882 *
883 * This function is called from the run method of all personalities that do not
884 * support bitmaps. It prints an error message and returns non-zero if mddev
885 * has a bitmap. Otherwise, it returns 0.
886 *
887 */
888int md_check_no_bitmap(mddev_t *mddev)
889{
NeilBrownc3d97142009-12-14 12:49:52 +1100890 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000891 return 0;
892 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
893 mdname(mddev), mddev->pers->name);
894 return 1;
895}
896EXPORT_SYMBOL(md_check_no_bitmap);
897
898/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 * load_super for 0.90.0
900 */
901static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
902{
903 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
904 mdp_super_t *sb;
905 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 /*
Andre Noll0f420352008-07-11 22:02:23 +1000908 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 * it's at the end of the disk.
910 *
911 * It also happens to be a multiple of 4Kb.
912 */
Andre Noll0f420352008-07-11 22:02:23 +1000913 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
NeilBrown0002b272005-09-09 16:23:53 -0700915 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (ret) return ret;
917
918 ret = -EINVAL;
919
920 bdevname(rdev->bdev, b);
921 sb = (mdp_super_t*)page_address(rdev->sb_page);
922
923 if (sb->md_magic != MD_SB_MAGIC) {
924 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
925 b);
926 goto abort;
927 }
928
929 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800930 sb->minor_version < 90 ||
931 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
933 sb->major_version, sb->minor_version,
934 b);
935 goto abort;
936 }
937
938 if (sb->raid_disks <= 0)
939 goto abort;
940
NeilBrown4d167f02007-05-09 02:35:37 -0700941 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
943 b);
944 goto abort;
945 }
946
947 rdev->preferred_minor = sb->md_minor;
948 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700949 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 if (sb->level == LEVEL_MULTIPATH)
952 rdev->desc_nr = -1;
953 else
954 rdev->desc_nr = sb->this_disk.number;
955
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700956 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700958 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 __u64 ev1, ev2;
960 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
961 if (!uuid_equal(refsb, sb)) {
962 printk(KERN_WARNING "md: %s has different UUID to %s\n",
963 b, bdevname(refdev->bdev,b2));
964 goto abort;
965 }
966 if (!sb_equal(refsb, sb)) {
967 printk(KERN_WARNING "md: %s has same UUID"
968 " but different superblock to %s\n",
969 b, bdevname(refdev->bdev, b2));
970 goto abort;
971 }
972 ev1 = md_event(sb);
973 ev2 = md_event(refsb);
974 if (ev1 > ev2)
975 ret = 1;
976 else
977 ret = 0;
978 }
NeilBrown8190e752009-06-18 08:48:58 +1000979 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Andre Nolldd8ac332009-03-31 14:33:13 +1100981 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -0800982 /* "this cannot possibly happen" ... */
983 ret = -EINVAL;
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 abort:
986 return ret;
987}
988
989/*
990 * validate_super for 0.90.0
991 */
992static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
993{
994 mdp_disk_t *desc;
995 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700996 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
NeilBrown41158c72005-06-21 17:17:25 -0700998 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800999 clear_bit(Faulty, &rdev->flags);
1000 clear_bit(In_sync, &rdev->flags);
1001 clear_bit(WriteMostly, &rdev->flags);
1002 clear_bit(BarriersNotsupp, &rdev->flags);
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (mddev->raid_disks == 0) {
1005 mddev->major_version = 0;
1006 mddev->minor_version = sb->minor_version;
1007 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001008 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001009 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 mddev->ctime = sb->ctime;
1011 mddev->utime = sb->utime;
1012 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001013 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 mddev->layout = sb->layout;
1015 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +11001016 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001017 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001018 mddev->bitmap_info.offset = 0;
1019 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
NeilBrownf6705572006-03-27 01:18:11 -08001021 if (mddev->minor_version >= 91) {
1022 mddev->reshape_position = sb->reshape_position;
1023 mddev->delta_disks = sb->delta_disks;
1024 mddev->new_level = sb->new_level;
1025 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001026 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001027 } else {
1028 mddev->reshape_position = MaxSector;
1029 mddev->delta_disks = 0;
1030 mddev->new_level = mddev->level;
1031 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001032 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001033 }
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (sb->state & (1<<MD_SB_CLEAN))
1036 mddev->recovery_cp = MaxSector;
1037 else {
1038 if (sb->events_hi == sb->cp_events_hi &&
1039 sb->events_lo == sb->cp_events_lo) {
1040 mddev->recovery_cp = sb->recovery_cp;
1041 } else
1042 mddev->recovery_cp = 0;
1043 }
1044
1045 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1046 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1047 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1048 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1049
1050 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001051
1052 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001053 mddev->bitmap_info.file == NULL)
1054 mddev->bitmap_info.offset =
1055 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001056
NeilBrown41158c72005-06-21 17:17:25 -07001057 } else if (mddev->pers == NULL) {
1058 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 ++ev1;
1060 if (ev1 < mddev->events)
1061 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001062 } else if (mddev->bitmap) {
1063 /* if adding to array with a bitmap, then we can accept an
1064 * older device ... but not too old.
1065 */
NeilBrown41158c72005-06-21 17:17:25 -07001066 if (ev1 < mddev->bitmap->events_cleared)
1067 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001068 } else {
1069 if (ev1 < mddev->events)
1070 /* just a hot-add of a new device, leave raid_disk at -1 */
1071 return 0;
1072 }
NeilBrown41158c72005-06-21 17:17:25 -07001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 desc = sb->disks + rdev->desc_nr;
1076
1077 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001078 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001079 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1080 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001081 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001083 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1084 /* active but not in sync implies recovery up to
1085 * reshape position. We don't know exactly where
1086 * that is, so set to zero for now */
1087 if (mddev->minor_version >= 91) {
1088 rdev->recovery_offset = 0;
1089 rdev->raid_disk = desc->raid_disk;
1090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001092 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1093 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001094 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001095 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 return 0;
1097}
1098
1099/*
1100 * sync_super for 0.90.0
1101 */
1102static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1103{
1104 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 mdk_rdev_t *rdev2;
1106 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 /* make rdev->sb match mddev data..
1110 *
1111 * 1/ zero out disks
1112 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1113 * 3/ any empty disks < next_spare become removed
1114 *
1115 * disks[0] gets initialised to REMOVED because
1116 * we cannot be sure from other fields if it has
1117 * been initialised or not.
1118 */
1119 int i;
1120 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1121
NeilBrown61181562005-09-09 16:24:02 -07001122 rdev->sb_size = MD_SB_BYTES;
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 sb = (mdp_super_t*)page_address(rdev->sb_page);
1125
1126 memset(sb, 0, sizeof(*sb));
1127
1128 sb->md_magic = MD_SB_MAGIC;
1129 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 sb->patch_version = mddev->patch_version;
1131 sb->gvalid_words = 0; /* ignored */
1132 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1133 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1134 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1135 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1136
1137 sb->ctime = mddev->ctime;
1138 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001139 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 sb->raid_disks = mddev->raid_disks;
1141 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001142 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 sb->utime = mddev->utime;
1144 sb->state = 0;
1145 sb->events_hi = (mddev->events>>32);
1146 sb->events_lo = (u32)mddev->events;
1147
NeilBrownf6705572006-03-27 01:18:11 -08001148 if (mddev->reshape_position == MaxSector)
1149 sb->minor_version = 90;
1150 else {
1151 sb->minor_version = 91;
1152 sb->reshape_position = mddev->reshape_position;
1153 sb->new_level = mddev->new_level;
1154 sb->delta_disks = mddev->delta_disks;
1155 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001156 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001157 }
1158 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (mddev->in_sync)
1160 {
1161 sb->recovery_cp = mddev->recovery_cp;
1162 sb->cp_events_hi = (mddev->events>>32);
1163 sb->cp_events_lo = (u32)mddev->events;
1164 if (mddev->recovery_cp == MaxSector)
1165 sb->state = (1<< MD_SB_CLEAN);
1166 } else
1167 sb->recovery_cp = 0;
1168
1169 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001170 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
NeilBrownc3d97142009-12-14 12:49:52 +11001172 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001173 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001176 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001178 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001179 int is_active = test_bit(In_sync, &rdev2->flags);
1180
1181 if (rdev2->raid_disk >= 0 &&
1182 sb->minor_version >= 91)
1183 /* we have nowhere to store the recovery_offset,
1184 * but if it is not below the reshape_position,
1185 * we can piggy-back on that.
1186 */
1187 is_active = 1;
1188 if (rdev2->raid_disk < 0 ||
1189 test_bit(Faulty, &rdev2->flags))
1190 is_active = 0;
1191 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001192 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001194 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001195 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 d = &sb->disks[rdev2->desc_nr];
1197 nr_disks++;
1198 d->number = rdev2->desc_nr;
1199 d->major = MAJOR(rdev2->bdev->bd_dev);
1200 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001201 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 d->raid_disk = rdev2->raid_disk;
1203 else
1204 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001205 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001207 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001209 if (test_bit(In_sync, &rdev2->flags))
1210 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 active++;
1212 working++;
1213 } else {
1214 d->state = 0;
1215 spare++;
1216 working++;
1217 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001218 if (test_bit(WriteMostly, &rdev2->flags))
1219 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 /* now set the "removed" and "faulty" bits on any missing devices */
1222 for (i=0 ; i < mddev->raid_disks ; i++) {
1223 mdp_disk_t *d = &sb->disks[i];
1224 if (d->state == 0 && d->number == 0) {
1225 d->number = i;
1226 d->raid_disk = i;
1227 d->state = (1<<MD_DISK_REMOVED);
1228 d->state |= (1<<MD_DISK_FAULTY);
1229 failed++;
1230 }
1231 }
1232 sb->nr_disks = nr_disks;
1233 sb->active_disks = active;
1234 sb->working_disks = working;
1235 sb->failed_disks = failed;
1236 sb->spare_disks = spare;
1237
1238 sb->this_disk = sb->disks[rdev->desc_nr];
1239 sb->sb_csum = calc_sb_csum(sb);
1240}
1241
1242/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001243 * rdev_size_change for 0.90.0
1244 */
1245static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001246super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001247{
Andre Noll58c0fed2009-03-31 14:33:13 +11001248 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001249 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001250 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001251 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001252 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001253 if (!num_sectors || num_sectors > rdev->sb_start)
1254 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001255 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001256 rdev->sb_page);
1257 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001258 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001259}
1260
1261
1262/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 * version 1 superblock
1264 */
1265
NeilBrown1c05b4b2006-10-21 10:24:08 -07001266static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001268 __le32 disk_csum;
1269 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 unsigned long long newcsum;
1271 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001272 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 int i;
1274
1275 disk_csum = sb->sb_csum;
1276 sb->sb_csum = 0;
1277 newcsum = 0;
1278 for (i=0; size>=4; size -= 4 )
1279 newcsum += le32_to_cpu(*isuper++);
1280
1281 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001282 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1285 sb->sb_csum = disk_csum;
1286 return cpu_to_le32(csum);
1287}
1288
1289static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1290{
1291 struct mdp_superblock_1 *sb;
1292 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001293 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001295 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 /*
Andre Noll0f420352008-07-11 22:02:23 +10001298 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 * It is always aligned to a 4K boundary and
1300 * depeding on minor_version, it can be:
1301 * 0: At least 8K, but less than 12K, from end of device
1302 * 1: At start of device
1303 * 2: 4K from start of device.
1304 */
1305 switch(minor_version) {
1306 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001307 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1308 sb_start -= 8*2;
1309 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 break;
1311 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001312 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 break;
1314 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001315 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 break;
1317 default:
1318 return -EINVAL;
1319 }
Andre Noll0f420352008-07-11 22:02:23 +10001320 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
NeilBrown0002b272005-09-09 16:23:53 -07001322 /* superblock is rarely larger than 1K, but it can be larger,
1323 * and it is safe to read 4k, so we do that
1324 */
1325 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if (ret) return ret;
1327
1328
1329 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1330
1331 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1332 sb->major_version != cpu_to_le32(1) ||
1333 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001334 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001335 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 return -EINVAL;
1337
1338 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1339 printk("md: invalid superblock checksum on %s\n",
1340 bdevname(rdev->bdev,b));
1341 return -EINVAL;
1342 }
1343 if (le64_to_cpu(sb->data_size) < 10) {
1344 printk("md: data_size too small on %s\n",
1345 bdevname(rdev->bdev,b));
1346 return -EINVAL;
1347 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 rdev->preferred_minor = 0xffff;
1350 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001351 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
NeilBrown0002b272005-09-09 16:23:53 -07001353 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001354 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001355 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001356 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1357
1358 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001359 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001360 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001361
NeilBrown31b65a02006-07-10 04:44:14 -07001362 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1363 rdev->desc_nr = -1;
1364 else
1365 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1366
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001367 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001368 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001369 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 __u64 ev1, ev2;
1371 struct mdp_superblock_1 *refsb =
1372 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1373
1374 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1375 sb->level != refsb->level ||
1376 sb->layout != refsb->layout ||
1377 sb->chunksize != refsb->chunksize) {
1378 printk(KERN_WARNING "md: %s has strangely different"
1379 " superblock to %s\n",
1380 bdevname(rdev->bdev,b),
1381 bdevname(refdev->bdev,b2));
1382 return -EINVAL;
1383 }
1384 ev1 = le64_to_cpu(sb->events);
1385 ev2 = le64_to_cpu(refsb->events);
1386
1387 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001388 ret = 1;
1389 else
1390 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
NeilBrowna1801f82008-03-04 14:29:31 -08001392 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001393 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1394 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001396 rdev->sectors = rdev->sb_start;
1397 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001399 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001400 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001401 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001402 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404
1405static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1406{
1407 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001408 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
NeilBrown41158c72005-06-21 17:17:25 -07001410 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001411 clear_bit(Faulty, &rdev->flags);
1412 clear_bit(In_sync, &rdev->flags);
1413 clear_bit(WriteMostly, &rdev->flags);
1414 clear_bit(BarriersNotsupp, &rdev->flags);
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (mddev->raid_disks == 0) {
1417 mddev->major_version = 1;
1418 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001419 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001420 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1422 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1423 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001424 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 mddev->layout = le32_to_cpu(sb->layout);
1426 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001427 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001428 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001429 mddev->bitmap_info.offset = 0;
1430 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1433 memcpy(mddev->uuid, sb->set_uuid, 16);
1434
1435 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001436
NeilBrown71c08052005-09-09 16:23:51 -07001437 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001438 mddev->bitmap_info.file == NULL )
1439 mddev->bitmap_info.offset =
1440 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001441
NeilBrownf6705572006-03-27 01:18:11 -08001442 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1443 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1444 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1445 mddev->new_level = le32_to_cpu(sb->new_level);
1446 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001447 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001448 } else {
1449 mddev->reshape_position = MaxSector;
1450 mddev->delta_disks = 0;
1451 mddev->new_level = mddev->level;
1452 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001453 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001454 }
1455
NeilBrown41158c72005-06-21 17:17:25 -07001456 } else if (mddev->pers == NULL) {
1457 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 ++ev1;
1459 if (ev1 < mddev->events)
1460 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001461 } else if (mddev->bitmap) {
1462 /* If adding to array with a bitmap, then we can accept an
1463 * older device, but not too old.
1464 */
NeilBrown41158c72005-06-21 17:17:25 -07001465 if (ev1 < mddev->bitmap->events_cleared)
1466 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001467 } else {
1468 if (ev1 < mddev->events)
1469 /* just a hot-add of a new device, leave raid_disk at -1 */
1470 return 0;
1471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (mddev->level != LEVEL_MULTIPATH) {
1473 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001474 if (rdev->desc_nr < 0 ||
1475 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1476 role = 0xffff;
1477 rdev->desc_nr = -1;
1478 } else
1479 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 switch(role) {
1481 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 break;
1483 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001484 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 break;
1486 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001487 if ((le32_to_cpu(sb->feature_map) &
1488 MD_FEATURE_RECOVERY_OFFSET))
1489 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1490 else
1491 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 rdev->raid_disk = role;
1493 break;
1494 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001495 if (sb->devflags & WriteMostly1)
1496 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001497 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001498 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return 0;
1501}
1502
1503static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1504{
1505 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 mdk_rdev_t *rdev2;
1507 int max_dev, i;
1508 /* make rdev->sb match mddev and rdev data. */
1509
1510 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1511
1512 sb->feature_map = 0;
1513 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001514 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 memset(sb->pad1, 0, sizeof(sb->pad1));
1516 memset(sb->pad2, 0, sizeof(sb->pad2));
1517 memset(sb->pad3, 0, sizeof(sb->pad3));
1518
1519 sb->utime = cpu_to_le64((__u64)mddev->utime);
1520 sb->events = cpu_to_le64(mddev->events);
1521 if (mddev->in_sync)
1522 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1523 else
1524 sb->resync_offset = cpu_to_le64(0);
1525
NeilBrown1c05b4b2006-10-21 10:24:08 -07001526 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001527
NeilBrownf0ca3402006-02-02 14:28:04 -08001528 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001529 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001530 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001531 sb->level = cpu_to_le32(mddev->level);
1532 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001533
NeilBrownc3d97142009-12-14 12:49:52 +11001534 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1535 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001536 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001537 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001538
1539 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001540 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001541 sb->feature_map |=
1542 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1543 sb->recovery_offset =
1544 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001545 }
1546
NeilBrownf6705572006-03-27 01:18:11 -08001547 if (mddev->reshape_position != MaxSector) {
1548 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1549 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1550 sb->new_layout = cpu_to_le32(mddev->new_layout);
1551 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1552 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001553 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001554 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001557 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if (rdev2->desc_nr+1 > max_dev)
1559 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001560
NeilBrown70471da2009-08-03 10:59:57 +10001561 if (max_dev > le32_to_cpu(sb->max_dev)) {
1562 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001563 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001564 rdev->sb_size = max_dev * 2 + 256;
1565 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1566 if (rdev->sb_size & bmask)
1567 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 for (i=0; i<max_dev;i++)
1570 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1571
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001572 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001574 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001576 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001578 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001579 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 else
1581 sb->dev_roles[i] = cpu_to_le16(0xffff);
1582 }
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 sb->sb_csum = calc_sb_1_csum(sb);
1585}
1586
Chris Webb0cd17fe2008-06-28 08:31:46 +10001587static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001588super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001589{
1590 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001591 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001592 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001593 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001594 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001595 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001596 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1597 max_sectors -= rdev->data_offset;
1598 if (!num_sectors || num_sectors > max_sectors)
1599 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001600 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001601 /* minor version 0 with bitmap we can't move */
1602 return 0;
1603 } else {
1604 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001605 sector_t sb_start;
1606 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1607 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001608 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001609 if (!num_sectors || num_sectors > max_sectors)
1610 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001611 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001612 }
1613 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001614 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001615 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001616 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001617 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001618 rdev->sb_page);
1619 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001620 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001621}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Adrian Bunk75c96f82005-05-05 16:16:09 -07001623static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 [0] = {
1625 .name = "0.90.0",
1626 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001627 .load_super = super_90_load,
1628 .validate_super = super_90_validate,
1629 .sync_super = super_90_sync,
1630 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 },
1632 [1] = {
1633 .name = "md-1",
1634 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001635 .load_super = super_1_load,
1636 .validate_super = super_1_validate,
1637 .sync_super = super_1_sync,
1638 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 },
1640};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1643{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001644 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
NeilBrown4b809912008-07-21 17:05:25 +10001646 rcu_read_lock();
1647 rdev_for_each_rcu(rdev, mddev1)
1648 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001649 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001650 rdev2->bdev->bd_contains) {
1651 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001652 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001653 }
1654 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 return 0;
1656}
1657
1658static LIST_HEAD(pending_raid_disks);
1659
Andre Nollac5e7112009-08-03 10:59:47 +10001660/*
1661 * Try to register data integrity profile for an mddev
1662 *
1663 * This is called when an array is started and after a disk has been kicked
1664 * from the array. It only succeeds if all working and active component devices
1665 * are integrity capable with matching profiles.
1666 */
1667int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001668{
Andre Nollac5e7112009-08-03 10:59:47 +10001669 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001670
Andre Nollac5e7112009-08-03 10:59:47 +10001671 if (list_empty(&mddev->disks))
1672 return 0; /* nothing to do */
1673 if (blk_get_integrity(mddev->gendisk))
1674 return 0; /* already registered */
1675 list_for_each_entry(rdev, &mddev->disks, same_set) {
1676 /* skip spares and non-functional disks */
1677 if (test_bit(Faulty, &rdev->flags))
1678 continue;
1679 if (rdev->raid_disk < 0)
1680 continue;
1681 /*
1682 * If at least one rdev is not integrity capable, we can not
1683 * enable data integrity for the md device.
1684 */
1685 if (!bdev_get_integrity(rdev->bdev))
1686 return -EINVAL;
1687 if (!reference) {
1688 /* Use the first rdev as the reference */
1689 reference = rdev;
1690 continue;
1691 }
1692 /* does this rdev's profile match the reference profile? */
1693 if (blk_integrity_compare(reference->bdev->bd_disk,
1694 rdev->bdev->bd_disk) < 0)
1695 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001696 }
Andre Nollac5e7112009-08-03 10:59:47 +10001697 /*
1698 * All component devices are integrity capable and have matching
1699 * profiles, register the common profile for the md device.
1700 */
1701 if (blk_integrity_register(mddev->gendisk,
1702 bdev_get_integrity(reference->bdev)) != 0) {
1703 printk(KERN_ERR "md: failed to register integrity for %s\n",
1704 mdname(mddev));
1705 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001706 }
Andre Nollac5e7112009-08-03 10:59:47 +10001707 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1708 mdname(mddev));
1709 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001710}
Andre Nollac5e7112009-08-03 10:59:47 +10001711EXPORT_SYMBOL(md_integrity_register);
1712
1713/* Disable data integrity if non-capable/non-matching disk is being added */
1714void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1715{
1716 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1717 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1718
1719 if (!bi_mddev) /* nothing to do */
1720 return;
1721 if (rdev->raid_disk < 0) /* skip spares */
1722 return;
1723 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1724 rdev->bdev->bd_disk) >= 0)
1725 return;
1726 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1727 blk_integrity_unregister(mddev->gendisk);
1728}
1729EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1732{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001733 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001734 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001735 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001736 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 if (rdev->mddev) {
1739 MD_BUG();
1740 return -EINVAL;
1741 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001742
1743 /* prevent duplicates */
1744 if (find_rdev(mddev, rdev->bdev->bd_dev))
1745 return -EEXIST;
1746
Andre Nolldd8ac332009-03-31 14:33:13 +11001747 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1748 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1749 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001750 if (mddev->pers) {
1751 /* Cannot change size, so fail
1752 * If mddev->level <= 0, then we don't care
1753 * about aligning sizes (e.g. linear)
1754 */
1755 if (mddev->level > 0)
1756 return -ENOSPC;
1757 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001758 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 /* Verify rdev->desc_nr is unique.
1762 * If it is -1, assign a free number, else
1763 * check number is not in use
1764 */
1765 if (rdev->desc_nr < 0) {
1766 int choice = 0;
1767 if (mddev->pers) choice = mddev->raid_disks;
1768 while (find_rdev_nr(mddev, choice))
1769 choice++;
1770 rdev->desc_nr = choice;
1771 } else {
1772 if (find_rdev_nr(mddev, rdev->desc_nr))
1773 return -EBUSY;
1774 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001775 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1776 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1777 mdname(mddev), mddev->max_disks);
1778 return -EBUSY;
1779 }
NeilBrown19133a42005-11-08 21:39:35 -08001780 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001781 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001782 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001785 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001786
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001787 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001788 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001789
Tejun Heo0762b8b2008-08-25 19:56:12 +09001790 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001791 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1792 kobject_del(&rdev->kobj);
1793 goto fail;
1794 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001795 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1796
NeilBrown4b809912008-07-21 17:05:25 +10001797 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001798 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001799
1800 /* May as well allow recovery to be retried once */
1801 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001804
1805 fail:
1806 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1807 b, mdname(mddev));
1808 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809}
1810
NeilBrown177a99b2008-02-06 01:39:56 -08001811static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001812{
1813 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1814 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001815 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001816}
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1819{
1820 char b[BDEVNAME_SIZE];
1821 if (!rdev->mddev) {
1822 MD_BUG();
1823 return;
1824 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001825 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001826 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1828 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001829 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001830 sysfs_put(rdev->sysfs_state);
1831 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001832 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001833 * writing to 'remove' to "dev/state". We also need
1834 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001835 */
NeilBrown4b809912008-07-21 17:05:25 +10001836 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001837 INIT_WORK(&rdev->del_work, md_delayed_delete);
1838 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001839 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840}
1841
1842/*
1843 * prevent the device from being mounted, repartitioned or
1844 * otherwise reused by a RAID array (or any other kernel
1845 * subsystem), by bd_claiming the device.
1846 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001847static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
1849 int err = 0;
1850 struct block_device *bdev;
1851 char b[BDEVNAME_SIZE];
1852
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001853 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (IS_ERR(bdev)) {
1855 printk(KERN_ERR "md: could not open %s.\n",
1856 __bdevname(dev, b));
1857 return PTR_ERR(bdev);
1858 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001859 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (err) {
1861 printk(KERN_ERR "md: could not bd_claim %s.\n",
1862 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001863 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 return err;
1865 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001866 if (!shared)
1867 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 rdev->bdev = bdev;
1869 return err;
1870}
1871
1872static void unlock_rdev(mdk_rdev_t *rdev)
1873{
1874 struct block_device *bdev = rdev->bdev;
1875 rdev->bdev = NULL;
1876 if (!bdev)
1877 MD_BUG();
1878 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001879 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
1882void md_autodetect_dev(dev_t dev);
1883
1884static void export_rdev(mdk_rdev_t * rdev)
1885{
1886 char b[BDEVNAME_SIZE];
1887 printk(KERN_INFO "md: export_rdev(%s)\n",
1888 bdevname(rdev->bdev,b));
1889 if (rdev->mddev)
1890 MD_BUG();
1891 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001893 if (test_bit(AutoDetected, &rdev->flags))
1894 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895#endif
1896 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001897 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898}
1899
1900static void kick_rdev_from_array(mdk_rdev_t * rdev)
1901{
1902 unbind_rdev_from_array(rdev);
1903 export_rdev(rdev);
1904}
1905
1906static void export_array(mddev_t *mddev)
1907{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001908 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
NeilBrownd089c6a2008-02-06 01:39:59 -08001910 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 if (!rdev->mddev) {
1912 MD_BUG();
1913 continue;
1914 }
1915 kick_rdev_from_array(rdev);
1916 }
1917 if (!list_empty(&mddev->disks))
1918 MD_BUG();
1919 mddev->raid_disks = 0;
1920 mddev->major_version = 0;
1921}
1922
1923static void print_desc(mdp_disk_t *desc)
1924{
1925 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1926 desc->major,desc->minor,desc->raid_disk,desc->state);
1927}
1928
Cheng Renquancd2ac932009-01-09 08:31:08 +11001929static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
1931 int i;
1932
1933 printk(KERN_INFO
1934 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1935 sb->major_version, sb->minor_version, sb->patch_version,
1936 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1937 sb->ctime);
1938 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1939 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1940 sb->md_minor, sb->layout, sb->chunk_size);
1941 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1942 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1943 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1944 sb->failed_disks, sb->spare_disks,
1945 sb->sb_csum, (unsigned long)sb->events_lo);
1946
1947 printk(KERN_INFO);
1948 for (i = 0; i < MD_SB_DISKS; i++) {
1949 mdp_disk_t *desc;
1950
1951 desc = sb->disks + i;
1952 if (desc->number || desc->major || desc->minor ||
1953 desc->raid_disk || (desc->state && (desc->state != 4))) {
1954 printk(" D %2d: ", i);
1955 print_desc(desc);
1956 }
1957 }
1958 printk(KERN_INFO "md: THIS: ");
1959 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
1961
Cheng Renquancd2ac932009-01-09 08:31:08 +11001962static void print_sb_1(struct mdp_superblock_1 *sb)
1963{
1964 __u8 *uuid;
1965
1966 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001967 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08001968 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001969 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001970 le32_to_cpu(sb->major_version),
1971 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08001972 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11001973 sb->set_name,
1974 (unsigned long long)le64_to_cpu(sb->ctime)
1975 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1976
1977 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001978 printk(KERN_INFO
1979 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11001980 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08001981 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001982 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1983 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001984 le32_to_cpu(sb->level),
1985 (unsigned long long)le64_to_cpu(sb->size),
1986 le32_to_cpu(sb->raid_disks),
1987 le32_to_cpu(sb->layout),
1988 le32_to_cpu(sb->chunksize),
1989 (unsigned long long)le64_to_cpu(sb->data_offset),
1990 (unsigned long long)le64_to_cpu(sb->data_size),
1991 (unsigned long long)le64_to_cpu(sb->super_offset),
1992 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1993 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08001994 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11001995 sb->devflags,
1996 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1997 (unsigned long long)le64_to_cpu(sb->events),
1998 (unsigned long long)le64_to_cpu(sb->resync_offset),
1999 le32_to_cpu(sb->sb_csum),
2000 le32_to_cpu(sb->max_dev)
2001 );
2002}
2003
2004static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
2006 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002007 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2008 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002009 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2010 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002012 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2013 switch (major_version) {
2014 case 0:
2015 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
2016 break;
2017 case 1:
2018 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
2019 break;
2020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 } else
2022 printk(KERN_INFO "md: no rdev superblock!\n");
2023}
2024
Adrian Bunk5e563412006-06-26 00:27:42 -07002025static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002027 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 mdk_rdev_t *rdev;
2029 mddev_t *mddev;
2030 char b[BDEVNAME_SIZE];
2031
2032 printk("\n");
2033 printk("md: **********************************\n");
2034 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2035 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002036 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
NeilBrown32a76272005-06-21 17:17:14 -07002038 if (mddev->bitmap)
2039 bitmap_print_sb(mddev->bitmap);
2040 else
2041 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002042 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 printk("<%s>", bdevname(rdev->bdev,b));
2044 printk("\n");
2045
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002046 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002047 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 }
2049 printk("md: **********************************\n");
2050 printk("\n");
2051}
2052
2053
NeilBrown42543762006-06-26 00:27:57 -07002054static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
NeilBrown42543762006-06-26 00:27:57 -07002056 /* Update each superblock (in-memory image), but
2057 * if we are allowed to, skip spares which already
2058 * have the right event counter, or have one earlier
2059 * (which would mean they aren't being marked as dirty
2060 * with the rest of the array)
2061 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
NeilBrown5e865102009-11-12 12:08:04 +11002064 /* First make sure individual recovery_offsets are correct */
2065 list_for_each_entry(rdev, &mddev->disks, same_set) {
2066 if (rdev->raid_disk >= 0 &&
2067 !test_bit(In_sync, &rdev->flags) &&
2068 mddev->curr_resync_completed > rdev->recovery_offset)
2069 rdev->recovery_offset = mddev->curr_resync_completed;
2070
2071 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002072 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002073 if (rdev->sb_events == mddev->events ||
2074 (nospares &&
2075 rdev->raid_disk < 0 &&
2076 (rdev->sb_events&1)==0 &&
2077 rdev->sb_events+1 == mddev->events)) {
2078 /* Don't update this superblock */
2079 rdev->sb_loaded = 2;
2080 } else {
2081 super_types[mddev->major_version].
2082 sync_super(mddev, rdev);
2083 rdev->sb_loaded = 1;
2084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 }
2086}
2087
NeilBrown850b2b422006-10-03 01:15:46 -07002088static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002091 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002092 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
NeilBrown1b57f132009-06-18 08:48:19 +10002094 mddev->utime = get_seconds();
NeilBrown8377bc82008-04-30 00:52:28 -07002095 if (mddev->external)
2096 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08002098 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002099
NeilBrown850b2b422006-10-03 01:15:46 -07002100 set_bit(MD_CHANGE_PENDING, &mddev->flags);
2101 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2102 force_change = 1;
2103 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2104 /* just a clean<-> dirty transition, possibly leave spares alone,
2105 * though if events isn't the right even/odd, we will have to do
2106 * spares after all
2107 */
2108 nospares = 1;
2109 if (force_change)
2110 nospares = 0;
2111 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002112 /* If the array is degraded, then skipping spares is both
2113 * dangerous and fairly pointless.
2114 * Dangerous because a device that was removed from the array
2115 * might have a event_count that still looks up-to-date,
2116 * so it can be re-added without a resync.
2117 * Pointless because if there are any spares to skip,
2118 * then a recovery will happen and soon that array won't
2119 * be degraded any more and the spare can go back to sleep then.
2120 */
NeilBrown850b2b422006-10-03 01:15:46 -07002121 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002122
NeilBrown06d91a52005-06-21 17:17:12 -07002123 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002124
2125 /* If this is just a dirty<->clean transition, and the array is clean
2126 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002127 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002128 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08002129 && (mddev->events & 1)
2130 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07002131 mddev->events--;
2132 else {
2133 /* otherwise we have to go forward and ... */
2134 mddev->events ++;
2135 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
NeilBrown51d56682009-08-13 09:54:02 +10002136 /* .. if the array isn't clean, an 'even' event must also go
2137 * to spares. */
2138 if ((mddev->events&1)==0)
NeilBrown42543762006-06-26 00:27:57 -07002139 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002140 } else {
NeilBrown51d56682009-08-13 09:54:02 +10002141 /* otherwise an 'odd' event must go to spares */
2142 if ((mddev->events&1))
NeilBrown42543762006-06-26 00:27:57 -07002143 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002144 }
2145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147 if (!mddev->events) {
2148 /*
2149 * oops, this 64-bit counter should never wrap.
2150 * Either we are in around ~1 trillion A.C., assuming
2151 * 1 reboot per second, or we have a bug:
2152 */
2153 MD_BUG();
2154 mddev->events --;
2155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157 /*
2158 * do not write anything to disk if using
2159 * nonpersistent superblocks
2160 */
NeilBrown06d91a52005-06-21 17:17:12 -07002161 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08002162 if (!mddev->external)
2163 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2164
NeilBrowna9701a32005-11-08 21:39:34 -08002165 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002166 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 return;
NeilBrown06d91a52005-06-21 17:17:12 -07002168 }
NeilBrowne6910632008-02-06 01:39:51 -08002169 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002170 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 dprintk(KERN_INFO
2173 "md: updating %s RAID superblock on device (in sync %d)\n",
2174 mdname(mddev),mddev->in_sync);
2175
NeilBrown4ad13662007-07-17 04:06:13 -07002176 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002177 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 char b[BDEVNAME_SIZE];
2179 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002180 if (rdev->sb_loaded != 1)
2181 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002182 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 dprintk("(skipping faulty ");
2184
2185 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002186 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002187 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002188 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002189 rdev->sb_page);
2190 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2191 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002192 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002193 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 } else
2196 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002197 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 /* only need to write one superblock... */
2199 break;
2200 }
NeilBrowna9701a32005-11-08 21:39:34 -08002201 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002202 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002203
NeilBrowna9701a32005-11-08 21:39:34 -08002204 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b422006-10-03 01:15:46 -07002205 if (mddev->in_sync != sync_req ||
2206 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002207 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002208 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002209 goto repeat;
2210 }
NeilBrown850b2b422006-10-03 01:15:46 -07002211 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002212 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002213 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002214 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2215 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217}
2218
Andre Noll7f6ce762008-03-23 18:34:54 +01002219/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002220 * We want to accept with case. For this we use cmd_match.
2221 */
2222static int cmd_match(const char *cmd, const char *str)
2223{
2224 /* See if cmd, written into a sysfs file, matches
2225 * str. They must either be the same, or cmd can
2226 * have a trailing newline
2227 */
2228 while (*cmd && *str && *cmd == *str) {
2229 cmd++;
2230 str++;
2231 }
2232 if (*cmd == '\n')
2233 cmd++;
2234 if (*str || *cmd)
2235 return 0;
2236 return 1;
2237}
2238
NeilBrown86e6ffd2005-11-08 21:39:24 -08002239struct rdev_sysfs_entry {
2240 struct attribute attr;
2241 ssize_t (*show)(mdk_rdev_t *, char *);
2242 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2243};
2244
2245static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002246state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002247{
2248 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002249 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002250
NeilBrownb2d444d2005-11-08 21:39:31 -08002251 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002252 len+= sprintf(page+len, "%sfaulty",sep);
2253 sep = ",";
2254 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002255 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002256 len += sprintf(page+len, "%sin_sync",sep);
2257 sep = ",";
2258 }
NeilBrownf6556752006-06-26 00:28:01 -07002259 if (test_bit(WriteMostly, &rdev->flags)) {
2260 len += sprintf(page+len, "%swrite_mostly",sep);
2261 sep = ",";
2262 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002263 if (test_bit(Blocked, &rdev->flags)) {
2264 len += sprintf(page+len, "%sblocked", sep);
2265 sep = ",";
2266 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002267 if (!test_bit(Faulty, &rdev->flags) &&
2268 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002269 len += sprintf(page+len, "%sspare", sep);
2270 sep = ",";
2271 }
2272 return len+sprintf(page+len, "\n");
2273}
2274
NeilBrown45dc2de2006-06-26 00:27:58 -07002275static ssize_t
2276state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2277{
2278 /* can write
2279 * faulty - simulates and error
2280 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002281 * writemostly - sets write_mostly
2282 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002283 * blocked - sets the Blocked flag
2284 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002285 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002286 */
2287 int err = -EINVAL;
2288 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2289 md_error(rdev->mddev, rdev);
2290 err = 0;
2291 } else if (cmd_match(buf, "remove")) {
2292 if (rdev->raid_disk >= 0)
2293 err = -EBUSY;
2294 else {
2295 mddev_t *mddev = rdev->mddev;
2296 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002297 if (mddev->pers)
2298 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002299 md_new_event(mddev);
2300 err = 0;
2301 }
NeilBrownf6556752006-06-26 00:28:01 -07002302 } else if (cmd_match(buf, "writemostly")) {
2303 set_bit(WriteMostly, &rdev->flags);
2304 err = 0;
2305 } else if (cmd_match(buf, "-writemostly")) {
2306 clear_bit(WriteMostly, &rdev->flags);
2307 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002308 } else if (cmd_match(buf, "blocked")) {
2309 set_bit(Blocked, &rdev->flags);
2310 err = 0;
2311 } else if (cmd_match(buf, "-blocked")) {
2312 clear_bit(Blocked, &rdev->flags);
2313 wake_up(&rdev->blocked_wait);
2314 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2315 md_wakeup_thread(rdev->mddev->thread);
2316
2317 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002318 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2319 set_bit(In_sync, &rdev->flags);
2320 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002321 }
NeilBrown3c0ee632008-10-21 13:25:28 +11002322 if (!err && rdev->sysfs_state)
2323 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002324 return err ? err : len;
2325}
NeilBrown80ca3a42006-07-10 04:44:18 -07002326static struct rdev_sysfs_entry rdev_state =
2327__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002328
2329static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002330errors_show(mdk_rdev_t *rdev, char *page)
2331{
2332 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2333}
2334
2335static ssize_t
2336errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2337{
2338 char *e;
2339 unsigned long n = simple_strtoul(buf, &e, 10);
2340 if (*buf && (*e == 0 || *e == '\n')) {
2341 atomic_set(&rdev->corrected_errors, n);
2342 return len;
2343 }
2344 return -EINVAL;
2345}
2346static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002347__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002348
NeilBrown014236d2006-01-06 00:20:55 -08002349static ssize_t
2350slot_show(mdk_rdev_t *rdev, char *page)
2351{
2352 if (rdev->raid_disk < 0)
2353 return sprintf(page, "none\n");
2354 else
2355 return sprintf(page, "%d\n", rdev->raid_disk);
2356}
2357
2358static ssize_t
2359slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2360{
2361 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002362 int err;
2363 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002364 int slot = simple_strtoul(buf, &e, 10);
2365 if (strncmp(buf, "none", 4)==0)
2366 slot = -1;
2367 else if (e==buf || (*e && *e!= '\n'))
2368 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002369 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002370 /* Setting 'slot' on an active array requires also
2371 * updating the 'rd%d' link, and communicating
2372 * with the personality with ->hot_*_disk.
2373 * For now we only support removing
2374 * failed/spare devices. This normally happens automatically,
2375 * but not when the metadata is externally managed.
2376 */
NeilBrownc303da62008-02-06 01:39:51 -08002377 if (rdev->raid_disk == -1)
2378 return -EEXIST;
2379 /* personality does all needed checks */
2380 if (rdev->mddev->pers->hot_add_disk == NULL)
2381 return -EINVAL;
2382 err = rdev->mddev->pers->
2383 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2384 if (err)
2385 return err;
2386 sprintf(nm, "rd%d", rdev->raid_disk);
2387 sysfs_remove_link(&rdev->mddev->kobj, nm);
2388 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2389 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002390 } else if (rdev->mddev->pers) {
2391 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002392 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002393 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002394 */
2395
2396 if (rdev->raid_disk != -1)
2397 return -EBUSY;
2398
2399 if (rdev->mddev->pers->hot_add_disk == NULL)
2400 return -EINVAL;
2401
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002402 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002403 if (rdev2->raid_disk == slot)
2404 return -EEXIST;
2405
2406 rdev->raid_disk = slot;
2407 if (test_bit(In_sync, &rdev->flags))
2408 rdev->saved_raid_disk = slot;
2409 else
2410 rdev->saved_raid_disk = -1;
2411 err = rdev->mddev->pers->
2412 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002413 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002414 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002415 return err;
Neil Brown52664732008-06-28 08:31:44 +10002416 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002417 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002418 sprintf(nm, "rd%d", rdev->raid_disk);
2419 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2420 printk(KERN_WARNING
2421 "md: cannot register "
2422 "%s for %s\n",
2423 nm, mdname(rdev->mddev));
2424
2425 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002426 } else {
2427 if (slot >= rdev->mddev->raid_disks)
2428 return -ENOSPC;
2429 rdev->raid_disk = slot;
2430 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002431 clear_bit(Faulty, &rdev->flags);
2432 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002433 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002434 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002435 }
NeilBrown014236d2006-01-06 00:20:55 -08002436 return len;
2437}
2438
2439
2440static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002441__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002442
NeilBrown93c8cad2006-01-06 00:20:56 -08002443static ssize_t
2444offset_show(mdk_rdev_t *rdev, char *page)
2445{
Andrew Morton6961ece2006-01-06 00:20:59 -08002446 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002447}
2448
2449static ssize_t
2450offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2451{
2452 char *e;
2453 unsigned long long offset = simple_strtoull(buf, &e, 10);
2454 if (e==buf || (*e && *e != '\n'))
2455 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002456 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002457 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002458 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002459 /* Must set offset before size, so overlap checks
2460 * can be sane */
2461 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002462 rdev->data_offset = offset;
2463 return len;
2464}
2465
2466static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002467__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002468
NeilBrown83303b62006-01-06 00:21:06 -08002469static ssize_t
2470rdev_size_show(mdk_rdev_t *rdev, char *page)
2471{
Andre Nolldd8ac332009-03-31 14:33:13 +11002472 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002473}
2474
NeilBrownc5d79ad2008-02-06 01:39:54 -08002475static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2476{
2477 /* check if two start/length pairs overlap */
2478 if (s1+l1 <= s2)
2479 return 0;
2480 if (s2+l2 <= s1)
2481 return 0;
2482 return 1;
2483}
2484
Dan Williamsb522adc2009-03-31 15:00:31 +11002485static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2486{
2487 unsigned long long blocks;
2488 sector_t new;
2489
2490 if (strict_strtoull(buf, 10, &blocks) < 0)
2491 return -EINVAL;
2492
2493 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2494 return -EINVAL; /* sector conversion overflow */
2495
2496 new = blocks * 2;
2497 if (new != blocks * 2)
2498 return -EINVAL; /* unsigned long long to sector_t overflow */
2499
2500 *sectors = new;
2501 return 0;
2502}
2503
NeilBrown83303b62006-01-06 00:21:06 -08002504static ssize_t
2505rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2506{
NeilBrown27c529b2008-03-04 14:29:33 -08002507 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002508 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002509 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002510
Dan Williamsb522adc2009-03-31 15:00:31 +11002511 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002512 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002513 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002514 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002515 sectors = super_types[my_mddev->major_version].
2516 rdev_size_change(rdev, sectors);
2517 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002518 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002519 } else if (!sectors)
2520 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2521 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002522 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002523 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002524 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002525
Andre Nolldd8ac332009-03-31 14:33:13 +11002526 rdev->sectors = sectors;
2527 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002528 /* need to check that all other rdevs with the same ->bdev
2529 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002530 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002531 * we have to change it back, we will have the lock again.
2532 */
2533 mddev_t *mddev;
2534 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002535 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002536
NeilBrown27c529b2008-03-04 14:29:33 -08002537 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002538 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002539 mdk_rdev_t *rdev2;
2540
2541 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002542 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002543 if (test_bit(AllReserved, &rdev2->flags) ||
2544 (rdev->bdev == rdev2->bdev &&
2545 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002546 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002547 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002548 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002549 overlap = 1;
2550 break;
2551 }
2552 mddev_unlock(mddev);
2553 if (overlap) {
2554 mddev_put(mddev);
2555 break;
2556 }
2557 }
NeilBrown27c529b2008-03-04 14:29:33 -08002558 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002559 if (overlap) {
2560 /* Someone else could have slipped in a size
2561 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002562 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002563 * safe, and trust userspace not to race with
2564 * itself
2565 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002566 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002567 return -EBUSY;
2568 }
2569 }
NeilBrown83303b62006-01-06 00:21:06 -08002570 return len;
2571}
2572
2573static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002574__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002575
Dan Williams06e3c812009-12-12 21:17:12 -07002576
2577static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2578{
2579 unsigned long long recovery_start = rdev->recovery_offset;
2580
2581 if (test_bit(In_sync, &rdev->flags) ||
2582 recovery_start == MaxSector)
2583 return sprintf(page, "none\n");
2584
2585 return sprintf(page, "%llu\n", recovery_start);
2586}
2587
2588static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2589{
2590 unsigned long long recovery_start;
2591
2592 if (cmd_match(buf, "none"))
2593 recovery_start = MaxSector;
2594 else if (strict_strtoull(buf, 10, &recovery_start))
2595 return -EINVAL;
2596
2597 if (rdev->mddev->pers &&
2598 rdev->raid_disk >= 0)
2599 return -EBUSY;
2600
2601 rdev->recovery_offset = recovery_start;
2602 if (recovery_start == MaxSector)
2603 set_bit(In_sync, &rdev->flags);
2604 else
2605 clear_bit(In_sync, &rdev->flags);
2606 return len;
2607}
2608
2609static struct rdev_sysfs_entry rdev_recovery_start =
2610__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2611
NeilBrown86e6ffd2005-11-08 21:39:24 -08002612static struct attribute *rdev_default_attrs[] = {
2613 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002614 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002615 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002616 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002617 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002618 &rdev_recovery_start.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002619 NULL,
2620};
2621static ssize_t
2622rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2623{
2624 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2625 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002626 mddev_t *mddev = rdev->mddev;
2627 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002628
2629 if (!entry->show)
2630 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002631
2632 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2633 if (!rv) {
2634 if (rdev->mddev == NULL)
2635 rv = -EBUSY;
2636 else
2637 rv = entry->show(rdev, page);
2638 mddev_unlock(mddev);
2639 }
2640 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002641}
2642
2643static ssize_t
2644rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2645 const char *page, size_t length)
2646{
2647 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2648 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002649 ssize_t rv;
2650 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002651
2652 if (!entry->store)
2653 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002654 if (!capable(CAP_SYS_ADMIN))
2655 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002656 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002657 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002658 if (rdev->mddev == NULL)
2659 rv = -EBUSY;
2660 else
2661 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002662 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002663 }
2664 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002665}
2666
2667static void rdev_free(struct kobject *ko)
2668{
2669 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2670 kfree(rdev);
2671}
2672static struct sysfs_ops rdev_sysfs_ops = {
2673 .show = rdev_attr_show,
2674 .store = rdev_attr_store,
2675};
2676static struct kobj_type rdev_ktype = {
2677 .release = rdev_free,
2678 .sysfs_ops = &rdev_sysfs_ops,
2679 .default_attrs = rdev_default_attrs,
2680};
2681
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682/*
2683 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2684 *
2685 * mark the device faulty if:
2686 *
2687 * - the device is nonexistent (zero size)
2688 * - the device has no valid superblock
2689 *
2690 * a faulty rdev _never_ has rdev->sb set.
2691 */
2692static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2693{
2694 char b[BDEVNAME_SIZE];
2695 int err;
2696 mdk_rdev_t *rdev;
2697 sector_t size;
2698
NeilBrown9ffae0c2006-01-06 00:20:32 -08002699 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 if (!rdev) {
2701 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2702 return ERR_PTR(-ENOMEM);
2703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
2705 if ((err = alloc_disk_sb(rdev)))
2706 goto abort_free;
2707
NeilBrownc5d79ad2008-02-06 01:39:54 -08002708 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 if (err)
2710 goto abort_free;
2711
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002712 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002715 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002716 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002717 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002719 rdev->sb_events = 0;
Robert Becker1e509152009-12-14 12:49:58 +11002720 rdev->last_read_error.tv_sec = 0;
2721 rdev->last_read_error.tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002723 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002724 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
2726 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2727 if (!size) {
2728 printk(KERN_WARNING
2729 "md: %s has zero or unknown size, marking faulty!\n",
2730 bdevname(rdev->bdev,b));
2731 err = -EINVAL;
2732 goto abort_free;
2733 }
2734
2735 if (super_format >= 0) {
2736 err = super_types[super_format].
2737 load_super(rdev, NULL, super_minor);
2738 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002739 printk(KERN_WARNING
2740 "md: %s does not have a valid v%d.%d "
2741 "superblock, not importing!\n",
2742 bdevname(rdev->bdev,b),
2743 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 goto abort_free;
2745 }
2746 if (err < 0) {
2747 printk(KERN_WARNING
2748 "md: could not read %s's sb, not importing!\n",
2749 bdevname(rdev->bdev,b));
2750 goto abort_free;
2751 }
2752 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002755 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
2757 return rdev;
2758
2759abort_free:
2760 if (rdev->sb_page) {
2761 if (rdev->bdev)
2762 unlock_rdev(rdev);
2763 free_disk_sb(rdev);
2764 }
2765 kfree(rdev);
2766 return ERR_PTR(err);
2767}
2768
2769/*
2770 * Check a full RAID array for plausibility
2771 */
2772
2773
NeilBrowna757e642005-04-16 15:26:42 -07002774static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775{
2776 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002777 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 char b[BDEVNAME_SIZE];
2779
2780 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002781 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 switch (super_types[mddev->major_version].
2783 load_super(rdev, freshest, mddev->minor_version)) {
2784 case 1:
2785 freshest = rdev;
2786 break;
2787 case 0:
2788 break;
2789 default:
2790 printk( KERN_ERR \
2791 "md: fatal superblock inconsistency in %s"
2792 " -- removing from array\n",
2793 bdevname(rdev->bdev,b));
2794 kick_rdev_from_array(rdev);
2795 }
2796
2797
2798 super_types[mddev->major_version].
2799 validate_super(mddev, freshest);
2800
2801 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002802 rdev_for_each(rdev, tmp, mddev) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002803 if (rdev->desc_nr >= mddev->max_disks ||
2804 i > mddev->max_disks) {
2805 printk(KERN_WARNING
2806 "md: %s: %s: only %d devices permitted\n",
2807 mdname(mddev), bdevname(rdev->bdev, b),
2808 mddev->max_disks);
2809 kick_rdev_from_array(rdev);
2810 continue;
2811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 if (rdev != freshest)
2813 if (super_types[mddev->major_version].
2814 validate_super(mddev, rdev)) {
2815 printk(KERN_WARNING "md: kicking non-fresh %s"
2816 " from array!\n",
2817 bdevname(rdev->bdev,b));
2818 kick_rdev_from_array(rdev);
2819 continue;
2820 }
2821 if (mddev->level == LEVEL_MULTIPATH) {
2822 rdev->desc_nr = i++;
2823 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002824 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002825 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002826 rdev->raid_disk = -1;
2827 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 }
2829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830}
2831
NeilBrown72e02072009-12-14 12:49:55 +11002832/* Read a fixed-point number.
2833 * Numbers in sysfs attributes should be in "standard" units where
2834 * possible, so time should be in seconds.
2835 * However we internally use a a much smaller unit such as
2836 * milliseconds or jiffies.
2837 * This function takes a decimal number with a possible fractional
2838 * component, and produces an integer which is the result of
2839 * multiplying that number by 10^'scale'.
2840 * all without any floating-point arithmetic.
2841 */
2842int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2843{
2844 unsigned long result = 0;
2845 long decimals = -1;
2846 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2847 if (*cp == '.')
2848 decimals = 0;
2849 else if (decimals < scale) {
2850 unsigned int value;
2851 value = *cp - '0';
2852 result = result * 10 + value;
2853 if (decimals >= 0)
2854 decimals++;
2855 }
2856 cp++;
2857 }
2858 if (*cp == '\n')
2859 cp++;
2860 if (*cp)
2861 return -EINVAL;
2862 if (decimals < 0)
2863 decimals = 0;
2864 while (decimals < scale) {
2865 result *= 10;
2866 decimals ++;
2867 }
2868 *res = result;
2869 return 0;
2870}
2871
2872
NeilBrown19052c02008-08-05 15:54:13 +10002873static void md_safemode_timeout(unsigned long data);
2874
NeilBrowneae17012005-11-08 21:39:23 -08002875static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002876safe_delay_show(mddev_t *mddev, char *page)
2877{
2878 int msec = (mddev->safemode_delay*1000)/HZ;
2879 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2880}
2881static ssize_t
2882safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2883{
NeilBrown16f17b32006-06-26 00:27:37 -07002884 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07002885
NeilBrown72e02072009-12-14 12:49:55 +11002886 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002887 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07002888 if (msec == 0)
2889 mddev->safemode_delay = 0;
2890 else {
NeilBrown19052c02008-08-05 15:54:13 +10002891 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002892 mddev->safemode_delay = (msec*HZ)/1000;
2893 if (mddev->safemode_delay == 0)
2894 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002895 if (mddev->safemode_delay < old_delay)
2896 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002897 }
2898 return len;
2899}
2900static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002901__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002902
2903static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002904level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002905{
NeilBrown2604b702006-01-06 00:20:36 -08002906 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002907 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002908 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002909 else if (mddev->clevel[0])
2910 return sprintf(page, "%s\n", mddev->clevel);
2911 else if (mddev->level != LEVEL_NONE)
2912 return sprintf(page, "%d\n", mddev->level);
2913 else
2914 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002915}
2916
NeilBrownd9d166c2006-01-06 00:20:51 -08002917static ssize_t
2918level_store(mddev_t *mddev, const char *buf, size_t len)
2919{
NeilBrown245f46c2009-03-31 14:39:39 +11002920 char level[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002921 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002922 struct mdk_personality *pers;
2923 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002924 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002925
2926 if (mddev->pers == NULL) {
2927 if (len == 0)
2928 return 0;
2929 if (len >= sizeof(mddev->clevel))
2930 return -ENOSPC;
2931 strncpy(mddev->clevel, buf, len);
2932 if (mddev->clevel[len-1] == '\n')
2933 len--;
2934 mddev->clevel[len] = 0;
2935 mddev->level = LEVEL_NONE;
2936 return rv;
2937 }
2938
2939 /* request to change the personality. Need to ensure:
2940 * - array is not engaged in resync/recovery/reshape
2941 * - old personality can be suspended
2942 * - new personality will access other array.
2943 */
2944
2945 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownd9d166c2006-01-06 00:20:51 -08002946 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002947
2948 if (!mddev->pers->quiesce) {
2949 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2950 mdname(mddev), mddev->pers->name);
2951 return -EINVAL;
2952 }
2953
2954 /* Now find the new personality */
2955 if (len == 0 || len >= sizeof(level))
2956 return -EINVAL;
2957 strncpy(level, buf, len);
2958 if (level[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002959 len--;
NeilBrown245f46c2009-03-31 14:39:39 +11002960 level[len] = 0;
2961
2962 request_module("md-%s", level);
2963 spin_lock(&pers_lock);
2964 pers = find_pers(LEVEL_NONE, level);
2965 if (!pers || !try_module_get(pers->owner)) {
2966 spin_unlock(&pers_lock);
2967 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2968 return -EINVAL;
2969 }
2970 spin_unlock(&pers_lock);
2971
2972 if (pers == mddev->pers) {
2973 /* Nothing to do! */
2974 module_put(pers->owner);
2975 return rv;
2976 }
2977 if (!pers->takeover) {
2978 module_put(pers->owner);
2979 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2980 mdname(mddev), level);
2981 return -EINVAL;
2982 }
2983
2984 /* ->takeover must set new_* and/or delta_disks
2985 * if it succeeds, and may set them when it fails.
2986 */
2987 priv = pers->takeover(mddev);
2988 if (IS_ERR(priv)) {
2989 mddev->new_level = mddev->level;
2990 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002991 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002992 mddev->raid_disks -= mddev->delta_disks;
2993 mddev->delta_disks = 0;
2994 module_put(pers->owner);
2995 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2996 mdname(mddev), level);
2997 return PTR_ERR(priv);
2998 }
2999
3000 /* Looks like we have a winner */
3001 mddev_suspend(mddev);
3002 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003003
3004 if (mddev->pers->sync_request == NULL &&
3005 pers->sync_request != NULL) {
3006 /* need to add the md_redundancy_group */
3007 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3008 printk(KERN_WARNING
3009 "md: cannot register extra attributes for %s\n",
3010 mdname(mddev));
3011 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3012 }
3013 if (mddev->pers->sync_request != NULL &&
3014 pers->sync_request == NULL) {
3015 /* need to remove the md_redundancy_group */
3016 if (mddev->to_remove == NULL)
3017 mddev->to_remove = &md_redundancy_group;
3018 }
3019
Trela Maciej54071b32010-03-08 16:02:42 +11003020 if (mddev->pers->sync_request == NULL &&
3021 mddev->external) {
3022 /* We are converting from a no-redundancy array
3023 * to a redundancy array and metadata is managed
3024 * externally so we need to be sure that writes
3025 * won't block due to a need to transition
3026 * clean->dirty
3027 * until external management is started.
3028 */
3029 mddev->in_sync = 0;
3030 mddev->safemode_delay = 0;
3031 mddev->safemode = 0;
3032 }
3033
NeilBrown245f46c2009-03-31 14:39:39 +11003034 module_put(mddev->pers->owner);
NeilBrown3a981b02009-08-03 10:59:55 +10003035 /* Invalidate devices that are now superfluous */
3036 list_for_each_entry(rdev, &mddev->disks, same_set)
3037 if (rdev->raid_disk >= mddev->raid_disks) {
3038 rdev->raid_disk = -1;
3039 clear_bit(In_sync, &rdev->flags);
3040 }
NeilBrown245f46c2009-03-31 14:39:39 +11003041 mddev->pers = pers;
3042 mddev->private = priv;
3043 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3044 mddev->level = mddev->new_level;
3045 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003046 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003047 mddev->delta_disks = 0;
3048 pers->run(mddev);
3049 mddev_resume(mddev);
3050 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3051 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3052 md_wakeup_thread(mddev->thread);
NeilBrownd9d166c2006-01-06 00:20:51 -08003053 return rv;
3054}
3055
3056static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003057__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003058
NeilBrownd4dbd022006-06-26 00:27:59 -07003059
3060static ssize_t
3061layout_show(mddev_t *mddev, char *page)
3062{
3063 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003064 if (mddev->reshape_position != MaxSector &&
3065 mddev->layout != mddev->new_layout)
3066 return sprintf(page, "%d (%d)\n",
3067 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003068 return sprintf(page, "%d\n", mddev->layout);
3069}
3070
3071static ssize_t
3072layout_store(mddev_t *mddev, const char *buf, size_t len)
3073{
3074 char *e;
3075 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003076
3077 if (!*buf || (*e && *e != '\n'))
3078 return -EINVAL;
3079
NeilBrownb3546032009-03-31 14:56:41 +11003080 if (mddev->pers) {
3081 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003082 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003083 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003084 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003085 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003086 if (err) {
3087 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003088 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003089 }
NeilBrownb3546032009-03-31 14:56:41 +11003090 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003091 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003092 if (mddev->reshape_position == MaxSector)
3093 mddev->layout = n;
3094 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003095 return len;
3096}
3097static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003098__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003099
3100
NeilBrowneae17012005-11-08 21:39:23 -08003101static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003102raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003103{
NeilBrownbb636542005-11-08 21:39:45 -08003104 if (mddev->raid_disks == 0)
3105 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003106 if (mddev->reshape_position != MaxSector &&
3107 mddev->delta_disks != 0)
3108 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3109 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003110 return sprintf(page, "%d\n", mddev->raid_disks);
3111}
3112
NeilBrownda943b992006-01-06 00:20:54 -08003113static int update_raid_disks(mddev_t *mddev, int raid_disks);
3114
3115static ssize_t
3116raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3117{
NeilBrownda943b992006-01-06 00:20:54 -08003118 char *e;
3119 int rv = 0;
3120 unsigned long n = simple_strtoul(buf, &e, 10);
3121
3122 if (!*buf || (*e && *e != '\n'))
3123 return -EINVAL;
3124
3125 if (mddev->pers)
3126 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003127 else if (mddev->reshape_position != MaxSector) {
3128 int olddisks = mddev->raid_disks - mddev->delta_disks;
3129 mddev->delta_disks = n - olddisks;
3130 mddev->raid_disks = n;
3131 } else
NeilBrownda943b992006-01-06 00:20:54 -08003132 mddev->raid_disks = n;
3133 return rv ? rv : len;
3134}
3135static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003136__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003137
NeilBrown24dd4692005-11-08 21:39:26 -08003138static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003139chunk_size_show(mddev_t *mddev, char *page)
3140{
NeilBrown08a02ec2007-05-09 02:35:38 -07003141 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003142 mddev->chunk_sectors != mddev->new_chunk_sectors)
3143 return sprintf(page, "%d (%d)\n",
3144 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003145 mddev->chunk_sectors << 9);
3146 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003147}
3148
3149static ssize_t
3150chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3151{
NeilBrown3b343802006-01-06 00:20:47 -08003152 char *e;
3153 unsigned long n = simple_strtoul(buf, &e, 10);
3154
NeilBrown3b343802006-01-06 00:20:47 -08003155 if (!*buf || (*e && *e != '\n'))
3156 return -EINVAL;
3157
NeilBrownb3546032009-03-31 14:56:41 +11003158 if (mddev->pers) {
3159 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003160 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003161 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003162 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003163 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003164 if (err) {
3165 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003166 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003167 }
NeilBrownb3546032009-03-31 14:56:41 +11003168 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003169 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003170 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003171 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003172 }
NeilBrown3b343802006-01-06 00:20:47 -08003173 return len;
3174}
3175static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003176__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003177
NeilBrowna94213b2006-06-26 00:28:00 -07003178static ssize_t
3179resync_start_show(mddev_t *mddev, char *page)
3180{
NeilBrownd1a7c502009-03-31 15:24:32 +11003181 if (mddev->recovery_cp == MaxSector)
3182 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003183 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3184}
3185
3186static ssize_t
3187resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3188{
NeilBrowna94213b2006-06-26 00:28:00 -07003189 char *e;
3190 unsigned long long n = simple_strtoull(buf, &e, 10);
3191
3192 if (mddev->pers)
3193 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003194 if (cmd_match(buf, "none"))
3195 n = MaxSector;
3196 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003197 return -EINVAL;
3198
3199 mddev->recovery_cp = n;
3200 return len;
3201}
3202static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003203__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003204
NeilBrown9e653b62006-06-26 00:27:58 -07003205/*
3206 * The array state can be:
3207 *
3208 * clear
3209 * No devices, no size, no level
3210 * Equivalent to STOP_ARRAY ioctl
3211 * inactive
3212 * May have some settings, but array is not active
3213 * all IO results in error
3214 * When written, doesn't tear down array, but just stops it
3215 * suspended (not supported yet)
3216 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003217 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003218 * readonly
3219 * no resync can happen. no superblocks get written.
3220 * write requests fail
3221 * read-auto
3222 * like readonly, but behaves like 'clean' on a write request.
3223 *
3224 * clean - no pending writes, but otherwise active.
3225 * When written to inactive array, starts without resync
3226 * If a write request arrives then
3227 * if metadata is known, mark 'dirty' and switch to 'active'.
3228 * if not known, block and switch to write-pending
3229 * If written to an active array that has pending writes, then fails.
3230 * active
3231 * fully active: IO and resync can be happening.
3232 * When written to inactive array, starts with resync
3233 *
3234 * write-pending
3235 * clean, but writes are blocked waiting for 'active' to be written.
3236 *
3237 * active-idle
3238 * like active, but no writes have been seen for a while (100msec).
3239 *
3240 */
3241enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3242 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003243static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003244 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3245 "write-pending", "active-idle", NULL };
3246
3247static int match_word(const char *word, char **list)
3248{
3249 int n;
3250 for (n=0; list[n]; n++)
3251 if (cmd_match(word, list[n]))
3252 break;
3253 return n;
3254}
3255
3256static ssize_t
3257array_state_show(mddev_t *mddev, char *page)
3258{
3259 enum array_state st = inactive;
3260
3261 if (mddev->pers)
3262 switch(mddev->ro) {
3263 case 1:
3264 st = readonly;
3265 break;
3266 case 2:
3267 st = read_auto;
3268 break;
3269 case 0:
3270 if (mddev->in_sync)
3271 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08003272 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3273 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003274 else if (mddev->safemode)
3275 st = active_idle;
3276 else
3277 st = active;
3278 }
3279 else {
3280 if (list_empty(&mddev->disks) &&
3281 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003282 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003283 st = clear;
3284 else
3285 st = inactive;
3286 }
3287 return sprintf(page, "%s\n", array_states[st]);
3288}
3289
Neil Browndf5b20c2008-07-11 22:02:22 +10003290static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003291static int do_md_run(mddev_t * mddev);
3292static int restart_array(mddev_t *mddev);
3293
3294static ssize_t
3295array_state_store(mddev_t *mddev, const char *buf, size_t len)
3296{
3297 int err = -EINVAL;
3298 enum array_state st = match_word(buf, array_states);
3299 switch(st) {
3300 case bad_word:
3301 break;
3302 case clear:
3303 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003304 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003305 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003306 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003307 break;
3308 case inactive:
3309 /* stopping an active array */
3310 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003311 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003312 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003313 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003314 } else
3315 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003316 break;
3317 case suspended:
3318 break; /* not supported yet */
3319 case readonly:
3320 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10003321 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003322 else {
3323 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003324 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003325 err = do_md_run(mddev);
3326 }
3327 break;
3328 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003329 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003330 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10003331 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003332 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003333 err = restart_array(mddev);
3334 if (err == 0) {
3335 mddev->ro = 2;
3336 set_disk_ro(mddev->gendisk, 0);
3337 }
NeilBrown9e653b62006-06-26 00:27:58 -07003338 } else {
3339 mddev->ro = 2;
3340 err = do_md_run(mddev);
3341 }
3342 break;
3343 case clean:
3344 if (mddev->pers) {
3345 restart_array(mddev);
3346 spin_lock_irq(&mddev->write_lock);
3347 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003348 if (mddev->in_sync == 0) {
3349 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003350 if (mddev->safemode == 1)
3351 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003352 if (mddev->persistent)
3353 set_bit(MD_CHANGE_CLEAN,
3354 &mddev->flags);
3355 }
3356 err = 0;
3357 } else
3358 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003359 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003360 } else
3361 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003362 break;
3363 case active:
3364 if (mddev->pers) {
3365 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003366 if (mddev->external)
3367 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003368 wake_up(&mddev->sb_wait);
3369 err = 0;
3370 } else {
3371 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003372 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003373 err = do_md_run(mddev);
3374 }
3375 break;
3376 case write_pending:
3377 case active_idle:
3378 /* these cannot be set */
3379 break;
3380 }
3381 if (err)
3382 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003383 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003384 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003385 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003386 }
NeilBrown9e653b62006-06-26 00:27:58 -07003387}
NeilBrown80ca3a42006-07-10 04:44:18 -07003388static struct md_sysfs_entry md_array_state =
3389__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003390
NeilBrown6d7ff7382006-01-06 00:21:16 -08003391static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003392max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3393 return sprintf(page, "%d\n",
3394 atomic_read(&mddev->max_corr_read_errors));
3395}
3396
3397static ssize_t
3398max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3399{
3400 char *e;
3401 unsigned long n = simple_strtoul(buf, &e, 10);
3402
3403 if (*buf && (*e == 0 || *e == '\n')) {
3404 atomic_set(&mddev->max_corr_read_errors, n);
3405 return len;
3406 }
3407 return -EINVAL;
3408}
3409
3410static struct md_sysfs_entry max_corr_read_errors =
3411__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3412 max_corrected_read_errors_store);
3413
3414static ssize_t
NeilBrown6d7ff7382006-01-06 00:21:16 -08003415null_show(mddev_t *mddev, char *page)
3416{
3417 return -EINVAL;
3418}
3419
3420static ssize_t
3421new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3422{
3423 /* buf must be %d:%d\n? giving major and minor numbers */
3424 /* The new device is added to the array.
3425 * If the array has a persistent superblock, we read the
3426 * superblock to initialise info and check validity.
3427 * Otherwise, only checking done is that in bind_rdev_to_array,
3428 * which mainly checks size.
3429 */
3430 char *e;
3431 int major = simple_strtoul(buf, &e, 10);
3432 int minor;
3433 dev_t dev;
3434 mdk_rdev_t *rdev;
3435 int err;
3436
3437 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3438 return -EINVAL;
3439 minor = simple_strtoul(e+1, &e, 10);
3440 if (*e && *e != '\n')
3441 return -EINVAL;
3442 dev = MKDEV(major, minor);
3443 if (major != MAJOR(dev) ||
3444 minor != MINOR(dev))
3445 return -EOVERFLOW;
3446
3447
3448 if (mddev->persistent) {
3449 rdev = md_import_device(dev, mddev->major_version,
3450 mddev->minor_version);
3451 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3452 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3453 mdk_rdev_t, same_set);
3454 err = super_types[mddev->major_version]
3455 .load_super(rdev, rdev0, mddev->minor_version);
3456 if (err < 0)
3457 goto out;
3458 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003459 } else if (mddev->external)
3460 rdev = md_import_device(dev, -2, -1);
3461 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003462 rdev = md_import_device(dev, -1, -1);
3463
3464 if (IS_ERR(rdev))
3465 return PTR_ERR(rdev);
3466 err = bind_rdev_to_array(rdev, mddev);
3467 out:
3468 if (err)
3469 export_rdev(rdev);
3470 return err ? err : len;
3471}
3472
3473static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003474__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003475
3476static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003477bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3478{
3479 char *end;
3480 unsigned long chunk, end_chunk;
3481
3482 if (!mddev->bitmap)
3483 goto out;
3484 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3485 while (*buf) {
3486 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3487 if (buf == end) break;
3488 if (*end == '-') { /* range */
3489 buf = end + 1;
3490 end_chunk = simple_strtoul(buf, &end, 0);
3491 if (buf == end) break;
3492 }
3493 if (*end && !isspace(*end)) break;
3494 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003495 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003496 }
3497 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3498out:
3499 return len;
3500}
3501
3502static struct md_sysfs_entry md_bitmap =
3503__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3504
3505static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003506size_show(mddev_t *mddev, char *page)
3507{
Andre Noll58c0fed2009-03-31 14:33:13 +11003508 return sprintf(page, "%llu\n",
3509 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003510}
3511
Andre Nolld71f9f82008-07-11 22:02:22 +10003512static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003513
3514static ssize_t
3515size_store(mddev_t *mddev, const char *buf, size_t len)
3516{
3517 /* If array is inactive, we can reduce the component size, but
3518 * not increase it (except from 0).
3519 * If array is active, we can try an on-line resize
3520 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003521 sector_t sectors;
3522 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003523
Andre Noll58c0fed2009-03-31 14:33:13 +11003524 if (err < 0)
3525 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003526 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003527 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07003528 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003529 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003530 if (mddev->dev_sectors == 0 ||
3531 mddev->dev_sectors > sectors)
3532 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003533 else
3534 err = -ENOSPC;
3535 }
3536 return err ? err : len;
3537}
3538
3539static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003540__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003541
NeilBrown8bb93aa2006-01-06 00:20:50 -08003542
3543/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003544 * This is one of
3545 * 'none' for arrays with no metadata (good luck...)
3546 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003547 * or N.M for internally known formats
3548 */
3549static ssize_t
3550metadata_show(mddev_t *mddev, char *page)
3551{
3552 if (mddev->persistent)
3553 return sprintf(page, "%d.%d\n",
3554 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003555 else if (mddev->external)
3556 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003557 else
3558 return sprintf(page, "none\n");
3559}
3560
3561static ssize_t
3562metadata_store(mddev_t *mddev, const char *buf, size_t len)
3563{
3564 int major, minor;
3565 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003566 /* Changing the details of 'external' metadata is
3567 * always permitted. Otherwise there must be
3568 * no devices attached to the array.
3569 */
3570 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3571 ;
3572 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003573 return -EBUSY;
3574
3575 if (cmd_match(buf, "none")) {
3576 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003577 mddev->external = 0;
3578 mddev->major_version = 0;
3579 mddev->minor_version = 90;
3580 return len;
3581 }
3582 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003583 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003584 if (namelen >= sizeof(mddev->metadata_type))
3585 namelen = sizeof(mddev->metadata_type)-1;
3586 strncpy(mddev->metadata_type, buf+9, namelen);
3587 mddev->metadata_type[namelen] = 0;
3588 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3589 mddev->metadata_type[--namelen] = 0;
3590 mddev->persistent = 0;
3591 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003592 mddev->major_version = 0;
3593 mddev->minor_version = 90;
3594 return len;
3595 }
3596 major = simple_strtoul(buf, &e, 10);
3597 if (e==buf || *e != '.')
3598 return -EINVAL;
3599 buf = e+1;
3600 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003601 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003602 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003603 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003604 return -ENOENT;
3605 mddev->major_version = major;
3606 mddev->minor_version = minor;
3607 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003608 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003609 return len;
3610}
3611
3612static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003613__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003614
NeilBrowna35b0d62006-01-06 00:20:49 -08003615static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003616action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003617{
NeilBrown7eec3142005-11-08 21:39:44 -08003618 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003619 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3620 type = "frozen";
3621 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003622 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003623 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3624 type = "reshape";
3625 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003626 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3627 type = "resync";
3628 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3629 type = "check";
3630 else
3631 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003632 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003633 type = "recover";
3634 }
3635 return sprintf(page, "%s\n", type);
3636}
3637
3638static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003639action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003640{
NeilBrown7eec3142005-11-08 21:39:44 -08003641 if (!mddev->pers || !mddev->pers->sync_request)
3642 return -EINVAL;
3643
NeilBrownb6a9ce62009-05-26 09:41:17 +10003644 if (cmd_match(page, "frozen"))
3645 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3646 else
3647 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3648
3649 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003650 if (mddev->sync_thread) {
3651 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3652 md_unregister_thread(mddev->sync_thread);
3653 mddev->sync_thread = NULL;
3654 mddev->recovery = 0;
3655 }
NeilBrown03c902e2006-01-06 00:20:46 -08003656 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3657 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003658 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003659 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003660 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003661 else if (cmd_match(page, "recover")) {
3662 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3663 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3664 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003665 int err;
3666 if (mddev->pers->start_reshape == NULL)
3667 return -EINVAL;
3668 err = mddev->pers->start_reshape(mddev);
3669 if (err)
3670 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003671 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003672 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003673 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003674 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003675 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003676 return -EINVAL;
3677 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3678 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003679 }
NeilBrown03c902e2006-01-06 00:20:46 -08003680 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003681 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003682 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003683 return len;
3684}
3685
NeilBrown9d888832005-11-08 21:39:26 -08003686static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003687mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003688{
3689 return sprintf(page, "%llu\n",
3690 (unsigned long long) mddev->resync_mismatches);
3691}
3692
NeilBrown80ca3a42006-07-10 04:44:18 -07003693static struct md_sysfs_entry md_scan_mode =
3694__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003695
NeilBrown96de1e62005-11-08 21:39:39 -08003696
NeilBrown80ca3a42006-07-10 04:44:18 -07003697static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003698
NeilBrown88202a02006-01-06 00:21:36 -08003699static ssize_t
3700sync_min_show(mddev_t *mddev, char *page)
3701{
3702 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3703 mddev->sync_speed_min ? "local": "system");
3704}
3705
3706static ssize_t
3707sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3708{
3709 int min;
3710 char *e;
3711 if (strncmp(buf, "system", 6)==0) {
3712 mddev->sync_speed_min = 0;
3713 return len;
3714 }
3715 min = simple_strtoul(buf, &e, 10);
3716 if (buf == e || (*e && *e != '\n') || min <= 0)
3717 return -EINVAL;
3718 mddev->sync_speed_min = min;
3719 return len;
3720}
3721
3722static struct md_sysfs_entry md_sync_min =
3723__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3724
3725static ssize_t
3726sync_max_show(mddev_t *mddev, char *page)
3727{
3728 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3729 mddev->sync_speed_max ? "local": "system");
3730}
3731
3732static ssize_t
3733sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3734{
3735 int max;
3736 char *e;
3737 if (strncmp(buf, "system", 6)==0) {
3738 mddev->sync_speed_max = 0;
3739 return len;
3740 }
3741 max = simple_strtoul(buf, &e, 10);
3742 if (buf == e || (*e && *e != '\n') || max <= 0)
3743 return -EINVAL;
3744 mddev->sync_speed_max = max;
3745 return len;
3746}
3747
3748static struct md_sysfs_entry md_sync_max =
3749__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3750
Iustin Popd7f3d292007-10-16 23:30:54 -07003751static ssize_t
3752degraded_show(mddev_t *mddev, char *page)
3753{
3754 return sprintf(page, "%d\n", mddev->degraded);
3755}
3756static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003757
3758static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003759sync_force_parallel_show(mddev_t *mddev, char *page)
3760{
3761 return sprintf(page, "%d\n", mddev->parallel_resync);
3762}
3763
3764static ssize_t
3765sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3766{
3767 long n;
3768
3769 if (strict_strtol(buf, 10, &n))
3770 return -EINVAL;
3771
3772 if (n != 0 && n != 1)
3773 return -EINVAL;
3774
3775 mddev->parallel_resync = n;
3776
3777 if (mddev->sync_thread)
3778 wake_up(&resync_wait);
3779
3780 return len;
3781}
3782
3783/* force parallel resync, even with shared block devices */
3784static struct md_sysfs_entry md_sync_force_parallel =
3785__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3786 sync_force_parallel_show, sync_force_parallel_store);
3787
3788static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003789sync_speed_show(mddev_t *mddev, char *page)
3790{
3791 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003792 if (mddev->curr_resync == 0)
3793 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003794 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3795 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003796 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003797 db = resync - mddev->resync_mark_cnt;
3798 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003799}
3800
NeilBrown80ca3a42006-07-10 04:44:18 -07003801static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003802
3803static ssize_t
3804sync_completed_show(mddev_t *mddev, char *page)
3805{
Andre Noll58c0fed2009-03-31 14:33:13 +11003806 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003807
NeilBrownacb180b2009-04-14 16:28:34 +10003808 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3809 return sprintf(page, "none\n");
3810
NeilBrown88202a02006-01-06 00:21:36 -08003811 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003812 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003813 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003814 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003815
NeilBrownacb180b2009-04-14 16:28:34 +10003816 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003817 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003818}
3819
NeilBrown80ca3a42006-07-10 04:44:18 -07003820static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003821
NeilBrowne464eaf2006-03-27 01:18:14 -08003822static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003823min_sync_show(mddev_t *mddev, char *page)
3824{
3825 return sprintf(page, "%llu\n",
3826 (unsigned long long)mddev->resync_min);
3827}
3828static ssize_t
3829min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3830{
3831 unsigned long long min;
3832 if (strict_strtoull(buf, 10, &min))
3833 return -EINVAL;
3834 if (min > mddev->resync_max)
3835 return -EINVAL;
3836 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3837 return -EBUSY;
3838
3839 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003840 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003841 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003842 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003843 return -EINVAL;
3844 }
3845 mddev->resync_min = min;
3846
3847 return len;
3848}
3849
3850static struct md_sysfs_entry md_min_sync =
3851__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3852
3853static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003854max_sync_show(mddev_t *mddev, char *page)
3855{
3856 if (mddev->resync_max == MaxSector)
3857 return sprintf(page, "max\n");
3858 else
3859 return sprintf(page, "%llu\n",
3860 (unsigned long long)mddev->resync_max);
3861}
3862static ssize_t
3863max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3864{
3865 if (strncmp(buf, "max", 3) == 0)
3866 mddev->resync_max = MaxSector;
3867 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003868 unsigned long long max;
3869 if (strict_strtoull(buf, 10, &max))
3870 return -EINVAL;
3871 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003872 return -EINVAL;
3873 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003874 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003875 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3876 return -EBUSY;
3877
3878 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003879 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003880 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003881 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003882 return -EINVAL;
3883 }
3884 mddev->resync_max = max;
3885 }
3886 wake_up(&mddev->recovery_wait);
3887 return len;
3888}
3889
3890static struct md_sysfs_entry md_max_sync =
3891__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3892
3893static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003894suspend_lo_show(mddev_t *mddev, char *page)
3895{
3896 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3897}
3898
3899static ssize_t
3900suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3901{
3902 char *e;
3903 unsigned long long new = simple_strtoull(buf, &e, 10);
3904
NeilBrownb8d966e2009-07-01 11:14:04 +10003905 if (mddev->pers == NULL ||
3906 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003907 return -EINVAL;
3908 if (buf == e || (*e && *e != '\n'))
3909 return -EINVAL;
3910 if (new >= mddev->suspend_hi ||
3911 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3912 mddev->suspend_lo = new;
3913 mddev->pers->quiesce(mddev, 2);
3914 return len;
3915 } else
3916 return -EINVAL;
3917}
3918static struct md_sysfs_entry md_suspend_lo =
3919__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3920
3921
3922static ssize_t
3923suspend_hi_show(mddev_t *mddev, char *page)
3924{
3925 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3926}
3927
3928static ssize_t
3929suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3930{
3931 char *e;
3932 unsigned long long new = simple_strtoull(buf, &e, 10);
3933
NeilBrownb8d966e2009-07-01 11:14:04 +10003934 if (mddev->pers == NULL ||
3935 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003936 return -EINVAL;
3937 if (buf == e || (*e && *e != '\n'))
3938 return -EINVAL;
3939 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3940 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3941 mddev->suspend_hi = new;
3942 mddev->pers->quiesce(mddev, 1);
3943 mddev->pers->quiesce(mddev, 0);
3944 return len;
3945 } else
3946 return -EINVAL;
3947}
3948static struct md_sysfs_entry md_suspend_hi =
3949__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3950
NeilBrown08a02ec2007-05-09 02:35:38 -07003951static ssize_t
3952reshape_position_show(mddev_t *mddev, char *page)
3953{
3954 if (mddev->reshape_position != MaxSector)
3955 return sprintf(page, "%llu\n",
3956 (unsigned long long)mddev->reshape_position);
3957 strcpy(page, "none\n");
3958 return 5;
3959}
3960
3961static ssize_t
3962reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3963{
3964 char *e;
3965 unsigned long long new = simple_strtoull(buf, &e, 10);
3966 if (mddev->pers)
3967 return -EBUSY;
3968 if (buf == e || (*e && *e != '\n'))
3969 return -EINVAL;
3970 mddev->reshape_position = new;
3971 mddev->delta_disks = 0;
3972 mddev->new_level = mddev->level;
3973 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003974 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07003975 return len;
3976}
3977
3978static struct md_sysfs_entry md_reshape_position =
3979__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3980 reshape_position_store);
3981
Dan Williamsb522adc2009-03-31 15:00:31 +11003982static ssize_t
3983array_size_show(mddev_t *mddev, char *page)
3984{
3985 if (mddev->external_size)
3986 return sprintf(page, "%llu\n",
3987 (unsigned long long)mddev->array_sectors/2);
3988 else
3989 return sprintf(page, "default\n");
3990}
3991
3992static ssize_t
3993array_size_store(mddev_t *mddev, const char *buf, size_t len)
3994{
3995 sector_t sectors;
3996
3997 if (strncmp(buf, "default", 7) == 0) {
3998 if (mddev->pers)
3999 sectors = mddev->pers->size(mddev, 0, 0);
4000 else
4001 sectors = mddev->array_sectors;
4002
4003 mddev->external_size = 0;
4004 } else {
4005 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4006 return -EINVAL;
4007 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004008 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004009
4010 mddev->external_size = 1;
4011 }
4012
4013 mddev->array_sectors = sectors;
4014 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004015 if (mddev->pers)
4016 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11004017
4018 return len;
4019}
4020
4021static struct md_sysfs_entry md_array_size =
4022__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4023 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004024
NeilBrowneae17012005-11-08 21:39:23 -08004025static struct attribute *md_default_attrs[] = {
4026 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004027 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004028 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004029 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004030 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004031 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004032 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004033 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004034 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004035 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004036 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004037 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004038 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004039 NULL,
4040};
4041
4042static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004043 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004044 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004045 &md_sync_min.attr,
4046 &md_sync_max.attr,
4047 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004048 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004049 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004050 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004051 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004052 &md_suspend_lo.attr,
4053 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004054 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004055 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004056 NULL,
4057};
NeilBrown411036f2005-11-08 21:39:40 -08004058static struct attribute_group md_redundancy_group = {
4059 .name = NULL,
4060 .attrs = md_redundancy_attrs,
4061};
4062
NeilBrowneae17012005-11-08 21:39:23 -08004063
4064static ssize_t
4065md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4066{
4067 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4068 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004069 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004070
4071 if (!entry->show)
4072 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004073 rv = mddev_lock(mddev);
4074 if (!rv) {
4075 rv = entry->show(mddev, page);
4076 mddev_unlock(mddev);
4077 }
NeilBrown96de1e62005-11-08 21:39:39 -08004078 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004079}
4080
4081static ssize_t
4082md_attr_store(struct kobject *kobj, struct attribute *attr,
4083 const char *page, size_t length)
4084{
4085 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4086 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004087 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004088
4089 if (!entry->store)
4090 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004091 if (!capable(CAP_SYS_ADMIN))
4092 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004093 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004094 if (mddev->hold_active == UNTIL_IOCTL)
4095 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004096 if (!rv) {
4097 rv = entry->store(mddev, page, length);
4098 mddev_unlock(mddev);
4099 }
NeilBrown96de1e62005-11-08 21:39:39 -08004100 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004101}
4102
4103static void md_free(struct kobject *ko)
4104{
4105 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004106
4107 if (mddev->sysfs_state)
4108 sysfs_put(mddev->sysfs_state);
4109
4110 if (mddev->gendisk) {
4111 del_gendisk(mddev->gendisk);
4112 put_disk(mddev->gendisk);
4113 }
4114 if (mddev->queue)
4115 blk_cleanup_queue(mddev->queue);
4116
NeilBrowneae17012005-11-08 21:39:23 -08004117 kfree(mddev);
4118}
4119
4120static struct sysfs_ops md_sysfs_ops = {
4121 .show = md_attr_show,
4122 .store = md_attr_store,
4123};
4124static struct kobj_type md_ktype = {
4125 .release = md_free,
4126 .sysfs_ops = &md_sysfs_ops,
4127 .default_attrs = md_default_attrs,
4128};
4129
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130int mdp_major = 0;
4131
Dan Williams5fd3a172009-03-04 00:57:25 -07004132static void mddev_delayed_delete(struct work_struct *ws)
4133{
4134 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4135
NeilBrown43a70502009-12-14 12:49:55 +11004136 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004137 kobject_del(&mddev->kobj);
4138 kobject_put(&mddev->kobj);
4139}
4140
NeilBrownefeb53c2009-01-09 08:31:10 +11004141static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004143 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 mddev_t *mddev = mddev_find(dev);
4145 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004146 int partitioned;
4147 int shift;
4148 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004149 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150
4151 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004152 return -ENODEV;
4153
4154 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4155 shift = partitioned ? MdpMinorShift : 0;
4156 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157
NeilBrownd3374822009-01-09 08:31:10 +11004158 /* wait for any previous instance if this device
4159 * to be completed removed (mddev_delayed_delete).
4160 */
4161 flush_scheduled_work();
4162
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004163 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004164 error = -EEXIST;
4165 if (mddev->gendisk)
4166 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004167
4168 if (name) {
4169 /* Need to ensure that 'name' is not a duplicate.
4170 */
4171 mddev_t *mddev2;
4172 spin_lock(&all_mddevs_lock);
4173
4174 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4175 if (mddev2->gendisk &&
4176 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4177 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004178 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004179 }
4180 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 }
NeilBrown8b765392009-01-09 08:31:08 +11004182
NeilBrown0909dc42009-07-01 12:27:21 +10004183 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004184 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004185 if (!mddev->queue)
4186 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004187 mddev->queue->queuedata = mddev;
4188
NeilBrown8b765392009-01-09 08:31:08 +11004189 /* Can be unlocked because the queue is new: no concurrency */
4190 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4191
NeilBrown409c57f2009-03-31 14:39:39 +11004192 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004193
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 disk = alloc_disk(1 << shift);
4195 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004196 blk_cleanup_queue(mddev->queue);
4197 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004198 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004200 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004202 if (name)
4203 strcpy(disk->disk_name, name);
4204 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004206 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 disk->fops = &md_fops;
4209 disk->private_data = mddev;
4210 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004211 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004212 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004213 * remove it now.
4214 */
4215 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 add_disk(disk);
4217 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004218 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4219 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004220 if (error) {
4221 /* This isn't possible, but as kobject_init_and_add is marked
4222 * __must_check, we must do something with the result
4223 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004224 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4225 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004226 error = 0;
4227 }
NeilBrown43a70502009-12-14 12:49:55 +11004228 if (sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4229 printk(KERN_DEBUG "pointless warning\n");
NeilBrown0909dc42009-07-01 12:27:21 +10004230 abort:
4231 mutex_unlock(&disks_mutex);
4232 if (!error) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004233 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11004234 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
4235 }
NeilBrownd3374822009-01-09 08:31:10 +11004236 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004237 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004238}
4239
4240static struct kobject *md_probe(dev_t dev, int *part, void *data)
4241{
4242 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 return NULL;
4244}
4245
NeilBrownefeb53c2009-01-09 08:31:10 +11004246static int add_named_array(const char *val, struct kernel_param *kp)
4247{
4248 /* val must be "md_*" where * is not all digits.
4249 * We allocate an array with a large free minor number, and
4250 * set the name to val. val must not already be an active name.
4251 */
4252 int len = strlen(val);
4253 char buf[DISK_NAME_LEN];
4254
4255 while (len && val[len-1] == '\n')
4256 len--;
4257 if (len >= DISK_NAME_LEN)
4258 return -E2BIG;
4259 strlcpy(buf, val, len+1);
4260 if (strncmp(buf, "md_", 3) != 0)
4261 return -EINVAL;
4262 return md_alloc(0, buf);
4263}
4264
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265static void md_safemode_timeout(unsigned long data)
4266{
4267 mddev_t *mddev = (mddev_t *) data;
4268
Neil Brown0fd62b82008-06-28 08:31:36 +10004269 if (!atomic_read(&mddev->writes_pending)) {
4270 mddev->safemode = 1;
4271 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11004272 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 md_wakeup_thread(mddev->thread);
4275}
4276
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004277static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278
4279static int do_md_run(mddev_t * mddev)
4280{
NeilBrown2604b702006-01-06 00:20:36 -08004281 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 mdk_rdev_t *rdev;
4283 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08004284 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285
NeilBrowna757e642005-04-16 15:26:42 -07004286 if (list_empty(&mddev->disks))
4287 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289
4290 if (mddev->pers)
4291 return -EBUSY;
4292
NeilBrownb6eb1272010-04-15 10:13:47 +10004293 /* These two calls synchronise us with the
4294 * sysfs_remove_group calls in mddev_unlock,
4295 * so they must have completed.
4296 */
4297 mutex_lock(&mddev->open_mutex);
4298 mutex_unlock(&mddev->open_mutex);
4299
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 /*
4301 * Analyze all RAID superblock(s)
4302 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004303 if (!mddev->raid_disks) {
4304 if (!mddev->persistent)
4305 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004306 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308
NeilBrownd9d166c2006-01-06 00:20:51 -08004309 if (mddev->level != LEVEL_NONE)
4310 request_module("md-level-%d", mddev->level);
4311 else if (mddev->clevel[0])
4312 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313
4314 /*
4315 * Drop all container device buffers, from now on
4316 * the only valid external interface is through the md
4317 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004319 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004320 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 continue;
4322 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004323 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004324
4325 /* perform some consistency tests on the device.
4326 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004327 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004328 */
Andre Noll0f420352008-07-11 22:02:23 +10004329 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004330 if (mddev->dev_sectors &&
4331 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004332 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004333 printk("md: %s: data overlaps metadata\n",
4334 mdname(mddev));
4335 return -EINVAL;
4336 }
4337 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004338 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004339 > rdev->data_offset) {
4340 printk("md: %s: metadata overlaps data\n",
4341 mdname(mddev));
4342 return -EINVAL;
4343 }
4344 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004345 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 }
4347
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
4350 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004351 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004352 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004354 if (mddev->level != LEVEL_NONE)
4355 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4356 mddev->level);
4357 else
4358 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4359 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 return -EINVAL;
4361 }
NeilBrown2604b702006-01-06 00:20:36 -08004362 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004364 if (mddev->level != pers->level) {
4365 mddev->level = pers->level;
4366 mddev->new_level = pers->level;
4367 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004368 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369
NeilBrownf6705572006-03-27 01:18:11 -08004370 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004371 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004372 /* This personality cannot handle reshaping... */
4373 mddev->pers = NULL;
4374 module_put(pers->owner);
4375 return -EINVAL;
4376 }
4377
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004378 if (pers->sync_request) {
4379 /* Warn if this is a potentially silly
4380 * configuration.
4381 */
4382 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4383 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004384 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004385
4386 list_for_each_entry(rdev, &mddev->disks, same_set)
4387 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004388 if (rdev < rdev2 &&
4389 rdev->bdev->bd_contains ==
4390 rdev2->bdev->bd_contains) {
4391 printk(KERN_WARNING
4392 "%s: WARNING: %s appears to be"
4393 " on the same physical disk as"
4394 " %s.\n",
4395 mdname(mddev),
4396 bdevname(rdev->bdev,b),
4397 bdevname(rdev2->bdev,b2));
4398 warned = 1;
4399 }
4400 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004401
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004402 if (warned)
4403 printk(KERN_WARNING
4404 "True protection against single-disk"
4405 " failure might be compromised.\n");
4406 }
4407
NeilBrown657390d2005-08-26 18:34:16 -07004408 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004409 /* may be over-ridden by personality */
4410 mddev->resync_max_sectors = mddev->dev_sectors;
4411
NeilBrowna9701a32005-11-08 21:39:34 -08004412 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004413 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414
NeilBrown0f9552b52009-12-30 12:08:50 +11004415 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004416 mddev->ro = 2; /* read-only, but switch on first write */
4417
NeilBrownb15c2e52006-01-06 00:20:16 -08004418 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004419 if (err)
4420 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004421 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4422 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4423 " but 'external_size' not in effect?\n", __func__);
4424 printk(KERN_ERR
4425 "md: invalid array_size %llu > default size %llu\n",
4426 (unsigned long long)mddev->array_sectors / 2,
4427 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4428 err = -EINVAL;
4429 mddev->pers->stop(mddev);
4430 }
4431 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004432 err = bitmap_create(mddev);
4433 if (err) {
4434 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4435 mdname(mddev), err);
4436 mddev->pers->stop(mddev);
4437 }
4438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 module_put(mddev->pers->owner);
4441 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004442 bitmap_destroy(mddev);
4443 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004445 if (mddev->pers->sync_request) {
4446 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4447 printk(KERN_WARNING
4448 "md: cannot register extra attributes for %s\n",
4449 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004450 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004451 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004452 mddev->ro = 0;
4453
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004455 atomic_set(&mddev->max_corr_read_errors,
4456 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 mddev->safemode = 0;
4458 mddev->safemode_timer.function = md_safemode_timeout;
4459 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004460 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004462
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004463 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004464 if (rdev->raid_disk >= 0) {
4465 char nm[20];
4466 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004467 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4468 printk("md: cannot register %s for %s\n",
4469 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471
4472 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4473
NeilBrown850b2b422006-10-03 01:15:46 -07004474 if (mddev->flags)
4475 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476
Andre Nollf233ea52008-07-21 17:05:22 +10004477 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
NeilBrown0b8c9de2006-07-10 04:44:15 -07004479 md_wakeup_thread(mddev->thread);
4480 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004481
NeilBrown449aad32009-08-03 10:59:58 +10004482 revalidate_disk(mddev->gendisk);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004483 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08004484 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004485 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004486 if (mddev->sysfs_action)
4487 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004488 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09004489 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 return 0;
4491}
4492
4493static int restart_array(mddev_t *mddev)
4494{
4495 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496
Andre Noll80fab1d2008-07-11 22:02:21 +10004497 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004499 return -ENXIO;
4500 if (!mddev->pers)
4501 return -EINVAL;
4502 if (!mddev->ro)
4503 return -EBUSY;
4504 mddev->safemode = 0;
4505 mddev->ro = 0;
4506 set_disk_ro(disk, 0);
4507 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4508 mdname(mddev));
4509 /* Kick recovery or resync if necessary */
4510 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4511 md_wakeup_thread(mddev->thread);
4512 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004513 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004514 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515}
4516
NeilBrownacc55e22006-06-26 00:27:47 -07004517/* similar to deny_write_access, but accounts for our holding a reference
4518 * to the file ourselves */
4519static int deny_bitmap_write_access(struct file * file)
4520{
4521 struct inode *inode = file->f_mapping->host;
4522
4523 spin_lock(&inode->i_lock);
4524 if (atomic_read(&inode->i_writecount) > 1) {
4525 spin_unlock(&inode->i_lock);
4526 return -ETXTBSY;
4527 }
4528 atomic_set(&inode->i_writecount, -1);
4529 spin_unlock(&inode->i_lock);
4530
4531 return 0;
4532}
4533
NeilBrown43a70502009-12-14 12:49:55 +11004534void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004535{
4536 struct inode *inode = file->f_mapping->host;
4537
4538 spin_lock(&inode->i_lock);
4539 atomic_set(&inode->i_writecount, 1);
4540 spin_unlock(&inode->i_lock);
4541}
4542
NeilBrown9e653b62006-06-26 00:27:58 -07004543/* mode:
4544 * 0 - completely stop and dis-assemble array
4545 * 1 - switch to readonly
4546 * 2 - stop but do not disassemble array
4547 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004548static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549{
4550 int err = 0;
4551 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004552 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553
NeilBrownc8c00a62009-08-10 12:50:52 +10004554 mutex_lock(&mddev->open_mutex);
NeilBrownf2ea68c2008-07-21 17:05:25 +10004555 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004556 printk("md: %s still in use.\n",mdname(mddev));
NeilBrownc8c00a62009-08-10 12:50:52 +10004557 err = -EBUSY;
4558 } else if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
4560 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004561 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4563 md_unregister_thread(mddev->sync_thread);
4564 mddev->sync_thread = NULL;
4565 }
4566
4567 del_timer_sync(&mddev->safemode_timer);
4568
NeilBrown9e653b62006-06-26 00:27:58 -07004569 switch(mode) {
4570 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004572 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 goto out;
4574 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004575 break;
4576 case 0: /* disassemble */
4577 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004578 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004579 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 if (mddev->ro)
4581 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004582
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004584 mddev->queue->merge_bvec_fn = NULL;
4585 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004586 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 module_put(mddev->pers->owner);
NeilBrowna64c8762010-04-14 17:15:37 +10004588 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4589 mddev->to_remove = &md_redundancy_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004591 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004592 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004593
NeilBrownc4647292009-05-07 12:51:06 +10004594 list_for_each_entry(rdev, &mddev->disks, same_set)
4595 if (rdev->raid_disk >= 0) {
4596 char nm[20];
4597 sprintf(nm, "rd%d", rdev->raid_disk);
4598 sysfs_remove_link(&mddev->kobj, nm);
4599 }
4600
NeilBrown0d4ca602006-12-10 02:20:44 -08004601 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004602 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08004603
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 if (mddev->ro)
4605 mddev->ro = 0;
4606 }
NeilBrown850b2b422006-10-03 01:15:46 -07004607 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 /* mark array as shutdown cleanly */
4609 mddev->in_sync = 1;
NeilBrown850b2b422006-10-03 01:15:46 -07004610 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 }
NeilBrown9e653b62006-06-26 00:27:58 -07004612 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004614 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown80ffb3c2009-08-18 10:35:26 +10004615 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 }
NeilBrownc8c00a62009-08-10 12:50:52 +10004617out:
4618 mutex_unlock(&mddev->open_mutex);
4619 if (err)
4620 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 /*
4622 * Free resources if final stop
4623 */
NeilBrown9e653b62006-06-26 00:27:58 -07004624 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004625
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4627
NeilBrown978f9462006-02-02 14:28:05 -08004628 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004629 if (mddev->bitmap_info.file) {
4630 restore_bitmap_write_access(mddev->bitmap_info.file);
4631 fput(mddev->bitmap_info.file);
4632 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004633 }
NeilBrownc3d97142009-12-14 12:49:52 +11004634 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004635
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 export_array(mddev);
4637
Andre Nollf233ea52008-07-21 17:05:22 +10004638 mddev->array_sectors = 0;
Dan Williamsb522adc2009-03-31 15:00:31 +11004639 mddev->external_size = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004640 mddev->dev_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004641 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004642 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004643 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004644 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004645 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004646 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004647 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004648 mddev->level = LEVEL_NONE;
4649 mddev->clevel[0] = 0;
4650 mddev->flags = 0;
4651 mddev->ro = 0;
4652 mddev->metadata_type[0] = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10004653 mddev->chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004654 mddev->ctime = mddev->utime = 0;
4655 mddev->layout = 0;
4656 mddev->max_disks = 0;
4657 mddev->events = 0;
4658 mddev->delta_disks = 0;
4659 mddev->new_level = LEVEL_NONE;
4660 mddev->new_layout = 0;
Andre Noll664e7c42009-06-18 08:45:27 +10004661 mddev->new_chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004662 mddev->curr_resync = 0;
4663 mddev->resync_mismatches = 0;
4664 mddev->suspend_lo = mddev->suspend_hi = 0;
4665 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4666 mddev->recovery = 0;
4667 mddev->in_sync = 0;
4668 mddev->changed = 0;
4669 mddev->degraded = 0;
4670 mddev->barriers_work = 0;
4671 mddev->safemode = 0;
NeilBrownc3d97142009-12-14 12:49:52 +11004672 mddev->bitmap_info.offset = 0;
4673 mddev->bitmap_info.default_offset = 0;
NeilBrown42a04b52009-12-14 12:49:53 +11004674 mddev->bitmap_info.chunksize = 0;
4675 mddev->bitmap_info.daemon_sleep = 0;
4676 mddev->bitmap_info.max_write_behind = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004677 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004678 if (mddev->hold_active == UNTIL_STOP)
4679 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004680
NeilBrowna8a55c32006-06-26 00:27:39 -07004681 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4683 mdname(mddev));
4684 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004685 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004686 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004687 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 return err;
4689}
4690
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004691#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692static void autorun_array(mddev_t *mddev)
4693{
4694 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 int err;
4696
NeilBrowna757e642005-04-16 15:26:42 -07004697 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699
4700 printk(KERN_INFO "md: running: ");
4701
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004702 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 char b[BDEVNAME_SIZE];
4704 printk("<%s>", bdevname(rdev->bdev,b));
4705 }
4706 printk("\n");
4707
NeilBrownd710e132008-10-13 11:55:12 +11004708 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 if (err) {
4710 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004711 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 }
4713}
4714
4715/*
4716 * lets try to run arrays based on all disks that have arrived
4717 * until now. (those are in pending_raid_disks)
4718 *
4719 * the method: pick the first pending disk, collect all disks with
4720 * the same UUID, remove all from the pending list and put them into
4721 * the 'same_array' list. Then order this list based on superblock
4722 * update time (freshest comes first), kick out 'old' disks and
4723 * compare superblocks. If everything's fine then run it.
4724 *
4725 * If "unit" is allocated, then bump its reference count
4726 */
4727static void autorun_devices(int part)
4728{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004729 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 mddev_t *mddev;
4731 char b[BDEVNAME_SIZE];
4732
4733 printk(KERN_INFO "md: autorun ...\n");
4734 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004735 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004737 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 rdev0 = list_entry(pending_raid_disks.next,
4739 mdk_rdev_t, same_set);
4740
4741 printk(KERN_INFO "md: considering %s ...\n",
4742 bdevname(rdev0->bdev,b));
4743 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004744 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 if (super_90_load(rdev, rdev0, 0) >= 0) {
4746 printk(KERN_INFO "md: adding %s ...\n",
4747 bdevname(rdev->bdev,b));
4748 list_move(&rdev->same_set, &candidates);
4749 }
4750 /*
4751 * now we have a set of devices, with all of them having
4752 * mostly sane superblocks. It's time to allocate the
4753 * mddev.
4754 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004755 if (part) {
4756 dev = MKDEV(mdp_major,
4757 rdev0->preferred_minor << MdpMinorShift);
4758 unit = MINOR(dev) >> MdpMinorShift;
4759 } else {
4760 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4761 unit = MINOR(dev);
4762 }
4763 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4765 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4766 break;
4767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768
4769 md_probe(dev, NULL, NULL);
4770 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004771 if (!mddev || !mddev->gendisk) {
4772 if (mddev)
4773 mddev_put(mddev);
4774 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 "md: cannot allocate memory for md drive.\n");
4776 break;
4777 }
4778 if (mddev_lock(mddev))
4779 printk(KERN_WARNING "md: %s locked, cannot run\n",
4780 mdname(mddev));
4781 else if (mddev->raid_disks || mddev->major_version
4782 || !list_empty(&mddev->disks)) {
4783 printk(KERN_WARNING
4784 "md: %s already running, cannot run %s\n",
4785 mdname(mddev), bdevname(rdev0->bdev,b));
4786 mddev_unlock(mddev);
4787 } else {
4788 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004789 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004790 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 list_del_init(&rdev->same_set);
4792 if (bind_rdev_to_array(rdev, mddev))
4793 export_rdev(rdev);
4794 }
4795 autorun_array(mddev);
4796 mddev_unlock(mddev);
4797 }
4798 /* on success, candidates will be empty, on error
4799 * it won't...
4800 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004801 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004802 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 mddev_put(mddev);
4806 }
4807 printk(KERN_INFO "md: ... autorun DONE.\n");
4808}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004809#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811static int get_version(void __user * arg)
4812{
4813 mdu_version_t ver;
4814
4815 ver.major = MD_MAJOR_VERSION;
4816 ver.minor = MD_MINOR_VERSION;
4817 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4818
4819 if (copy_to_user(arg, &ver, sizeof(ver)))
4820 return -EFAULT;
4821
4822 return 0;
4823}
4824
4825static int get_array_info(mddev_t * mddev, void __user * arg)
4826{
4827 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004828 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830
NeilBrowna9f326e2009-09-23 18:06:41 +10004831 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004832 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004834 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 failed++;
4836 else {
4837 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004838 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004839 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 else
4841 spare++;
4842 }
4843 }
4844
4845 info.major_version = mddev->major_version;
4846 info.minor_version = mddev->minor_version;
4847 info.patch_version = MD_PATCHLEVEL_VERSION;
4848 info.ctime = mddev->ctime;
4849 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004850 info.size = mddev->dev_sectors / 2;
4851 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004852 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 info.nr_disks = nr;
4854 info.raid_disks = mddev->raid_disks;
4855 info.md_minor = mddev->md_minor;
4856 info.not_persistent= !mddev->persistent;
4857
4858 info.utime = mddev->utime;
4859 info.state = 0;
4860 if (mddev->in_sync)
4861 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11004862 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07004863 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004864 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 info.working_disks = working;
4866 info.failed_disks = failed;
4867 info.spare_disks = spare;
4868
4869 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004870 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871
4872 if (copy_to_user(arg, &info, sizeof(info)))
4873 return -EFAULT;
4874
4875 return 0;
4876}
4877
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004878static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004879{
4880 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4881 char *ptr, *buf = NULL;
4882 int err = -ENOMEM;
4883
Dan Williamsb5470dc2008-06-27 21:44:04 -07004884 if (md_allow_write(mddev))
4885 file = kmalloc(sizeof(*file), GFP_NOIO);
4886 else
4887 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004888
NeilBrown32a76272005-06-21 17:17:14 -07004889 if (!file)
4890 goto out;
4891
4892 /* bitmap disabled, zero the first byte and copy out */
4893 if (!mddev->bitmap || !mddev->bitmap->file) {
4894 file->pathname[0] = '\0';
4895 goto copy_out;
4896 }
4897
4898 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4899 if (!buf)
4900 goto out;
4901
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004902 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4903 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004904 goto out;
4905
4906 strcpy(file->pathname, ptr);
4907
4908copy_out:
4909 err = 0;
4910 if (copy_to_user(arg, file, sizeof(*file)))
4911 err = -EFAULT;
4912out:
4913 kfree(buf);
4914 kfree(file);
4915 return err;
4916}
4917
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918static int get_disk_info(mddev_t * mddev, void __user * arg)
4919{
4920 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 mdk_rdev_t *rdev;
4922
4923 if (copy_from_user(&info, arg, sizeof(info)))
4924 return -EFAULT;
4925
Andre Noll26ef3792008-07-11 22:02:21 +10004926 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 if (rdev) {
4928 info.major = MAJOR(rdev->bdev->bd_dev);
4929 info.minor = MINOR(rdev->bdev->bd_dev);
4930 info.raid_disk = rdev->raid_disk;
4931 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004932 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004934 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 info.state |= (1<<MD_DISK_ACTIVE);
4936 info.state |= (1<<MD_DISK_SYNC);
4937 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004938 if (test_bit(WriteMostly, &rdev->flags))
4939 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 } else {
4941 info.major = info.minor = 0;
4942 info.raid_disk = -1;
4943 info.state = (1<<MD_DISK_REMOVED);
4944 }
4945
4946 if (copy_to_user(arg, &info, sizeof(info)))
4947 return -EFAULT;
4948
4949 return 0;
4950}
4951
4952static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4953{
4954 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4955 mdk_rdev_t *rdev;
4956 dev_t dev = MKDEV(info->major,info->minor);
4957
4958 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4959 return -EOVERFLOW;
4960
4961 if (!mddev->raid_disks) {
4962 int err;
4963 /* expecting a device which has a superblock */
4964 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4965 if (IS_ERR(rdev)) {
4966 printk(KERN_WARNING
4967 "md: md_import_device returned %ld\n",
4968 PTR_ERR(rdev));
4969 return PTR_ERR(rdev);
4970 }
4971 if (!list_empty(&mddev->disks)) {
4972 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4973 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10004974 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 .load_super(rdev, rdev0, mddev->minor_version);
4976 if (err < 0) {
4977 printk(KERN_WARNING
4978 "md: %s has different UUID to %s\n",
4979 bdevname(rdev->bdev,b),
4980 bdevname(rdev0->bdev,b2));
4981 export_rdev(rdev);
4982 return -EINVAL;
4983 }
4984 }
4985 err = bind_rdev_to_array(rdev, mddev);
4986 if (err)
4987 export_rdev(rdev);
4988 return err;
4989 }
4990
4991 /*
4992 * add_new_disk can be used once the array is assembled
4993 * to add "hot spares". They must already have a superblock
4994 * written
4995 */
4996 if (mddev->pers) {
4997 int err;
4998 if (!mddev->pers->hot_add_disk) {
4999 printk(KERN_WARNING
5000 "%s: personality does not support diskops!\n",
5001 mdname(mddev));
5002 return -EINVAL;
5003 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005004 if (mddev->persistent)
5005 rdev = md_import_device(dev, mddev->major_version,
5006 mddev->minor_version);
5007 else
5008 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 if (IS_ERR(rdev)) {
5010 printk(KERN_WARNING
5011 "md: md_import_device returned %ld\n",
5012 PTR_ERR(rdev));
5013 return PTR_ERR(rdev);
5014 }
NeilBrown41158c72005-06-21 17:17:25 -07005015 /* set save_raid_disk if appropriate */
5016 if (!mddev->persistent) {
5017 if (info->state & (1<<MD_DISK_SYNC) &&
5018 info->raid_disk < mddev->raid_disks)
5019 rdev->raid_disk = info->raid_disk;
5020 else
5021 rdev->raid_disk = -1;
5022 } else
5023 super_types[mddev->major_version].
5024 validate_super(mddev, rdev);
5025 rdev->saved_raid_disk = rdev->raid_disk;
5026
NeilBrownb2d444d2005-11-08 21:39:31 -08005027 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005028 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5029 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005030 else
5031 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005032
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 rdev->raid_disk = -1;
5034 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005035 if (!err && !mddev->pers->hot_remove_disk) {
5036 /* If there is hot_add_disk but no hot_remove_disk
5037 * then added disks for geometry changes,
5038 * and should be added immediately.
5039 */
5040 super_types[mddev->major_version].
5041 validate_super(mddev, rdev);
5042 err = mddev->pers->hot_add_disk(mddev, rdev);
5043 if (err)
5044 unbind_rdev_from_array(rdev);
5045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 if (err)
5047 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005048 else
NeilBrown3c0ee632008-10-21 13:25:28 +11005049 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005050
NeilBrown17571282006-12-10 02:20:52 -08005051 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005052 if (mddev->degraded)
5053 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005054 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07005055 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 return err;
5057 }
5058
5059 /* otherwise, add_new_disk is only allowed
5060 * for major_version==0 superblocks
5061 */
5062 if (mddev->major_version != 0) {
5063 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5064 mdname(mddev));
5065 return -EINVAL;
5066 }
5067
5068 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5069 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005070 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 if (IS_ERR(rdev)) {
5072 printk(KERN_WARNING
5073 "md: error, md_import_device() returned %ld\n",
5074 PTR_ERR(rdev));
5075 return PTR_ERR(rdev);
5076 }
5077 rdev->desc_nr = info->number;
5078 if (info->raid_disk < mddev->raid_disks)
5079 rdev->raid_disk = info->raid_disk;
5080 else
5081 rdev->raid_disk = -1;
5082
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005084 if (info->state & (1<<MD_DISK_SYNC))
5085 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005087 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5088 set_bit(WriteMostly, &rdev->flags);
5089
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 if (!mddev->persistent) {
5091 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10005092 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 } else
Andre Noll0f420352008-07-11 22:02:23 +10005094 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10005095 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096
NeilBrown2bf071b2006-01-06 00:20:55 -08005097 err = bind_rdev_to_array(rdev, mddev);
5098 if (err) {
5099 export_rdev(rdev);
5100 return err;
5101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 }
5103
5104 return 0;
5105}
5106
5107static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5108{
5109 char b[BDEVNAME_SIZE];
5110 mdk_rdev_t *rdev;
5111
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 rdev = find_rdev(mddev, dev);
5113 if (!rdev)
5114 return -ENXIO;
5115
5116 if (rdev->raid_disk >= 0)
5117 goto busy;
5118
5119 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07005120 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005121 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122
5123 return 0;
5124busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005125 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 bdevname(rdev->bdev,b), mdname(mddev));
5127 return -EBUSY;
5128}
5129
5130static int hot_add_disk(mddev_t * mddev, dev_t dev)
5131{
5132 char b[BDEVNAME_SIZE];
5133 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 mdk_rdev_t *rdev;
5135
5136 if (!mddev->pers)
5137 return -ENODEV;
5138
5139 if (mddev->major_version != 0) {
5140 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5141 " version-0 superblocks.\n",
5142 mdname(mddev));
5143 return -EINVAL;
5144 }
5145 if (!mddev->pers->hot_add_disk) {
5146 printk(KERN_WARNING
5147 "%s: personality does not support diskops!\n",
5148 mdname(mddev));
5149 return -EINVAL;
5150 }
5151
NeilBrownd710e132008-10-13 11:55:12 +11005152 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 if (IS_ERR(rdev)) {
5154 printk(KERN_WARNING
5155 "md: error, md_import_device() returned %ld\n",
5156 PTR_ERR(rdev));
5157 return -EINVAL;
5158 }
5159
5160 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10005161 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 else
Andre Noll0f420352008-07-11 22:02:23 +10005163 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164
NeilBrown8190e752009-06-18 08:48:58 +10005165 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166
NeilBrownb2d444d2005-11-08 21:39:31 -08005167 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 printk(KERN_WARNING
5169 "md: can not hot-add faulty %s disk to %s!\n",
5170 bdevname(rdev->bdev,b), mdname(mddev));
5171 err = -EINVAL;
5172 goto abort_export;
5173 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005174 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005176 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005177 err = bind_rdev_to_array(rdev, mddev);
5178 if (err)
5179 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180
5181 /*
5182 * The rest should better be atomic, we can have disk failures
5183 * noticed in interrupt contexts ...
5184 */
5185
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 rdev->raid_disk = -1;
5187
NeilBrown850b2b422006-10-03 01:15:46 -07005188 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189
5190 /*
5191 * Kick recovery, maybe this spare has to be added to the
5192 * array immediately.
5193 */
5194 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5195 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005196 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 return 0;
5198
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199abort_export:
5200 export_rdev(rdev);
5201 return err;
5202}
5203
NeilBrown32a76272005-06-21 17:17:14 -07005204static int set_bitmap_file(mddev_t *mddev, int fd)
5205{
5206 int err;
5207
NeilBrown36fa3062005-09-09 16:23:45 -07005208 if (mddev->pers) {
5209 if (!mddev->pers->quiesce)
5210 return -EBUSY;
5211 if (mddev->recovery || mddev->sync_thread)
5212 return -EBUSY;
5213 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005214 }
5215
NeilBrown36fa3062005-09-09 16:23:45 -07005216
5217 if (fd >= 0) {
5218 if (mddev->bitmap)
5219 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005220 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005221
NeilBrownc3d97142009-12-14 12:49:52 +11005222 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005223 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5224 mdname(mddev));
5225 return -EBADF;
5226 }
5227
NeilBrownc3d97142009-12-14 12:49:52 +11005228 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005229 if (err) {
5230 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5231 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005232 fput(mddev->bitmap_info.file);
5233 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005234 return err;
5235 }
NeilBrownc3d97142009-12-14 12:49:52 +11005236 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005237 } else if (mddev->bitmap == NULL)
5238 return -ENOENT; /* cannot remove what isn't there */
5239 err = 0;
5240 if (mddev->pers) {
5241 mddev->pers->quiesce(mddev, 1);
5242 if (fd >= 0)
5243 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005244 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005245 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005246 fd = -1; /* make sure to put the file */
5247 }
NeilBrown36fa3062005-09-09 16:23:45 -07005248 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005249 }
5250 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005251 if (mddev->bitmap_info.file) {
5252 restore_bitmap_write_access(mddev->bitmap_info.file);
5253 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005254 }
NeilBrownc3d97142009-12-14 12:49:52 +11005255 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005256 }
5257
NeilBrown32a76272005-06-21 17:17:14 -07005258 return err;
5259}
5260
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261/*
5262 * set_array_info is used two different ways
5263 * The original usage is when creating a new array.
5264 * In this usage, raid_disks is > 0 and it together with
5265 * level, size, not_persistent,layout,chunksize determine the
5266 * shape of the array.
5267 * This will always create an array with a type-0.90.0 superblock.
5268 * The newer usage is when assembling an array.
5269 * In this case raid_disks will be 0, and the major_version field is
5270 * use to determine which style super-blocks are to be found on the devices.
5271 * The minor and patch _version numbers are also kept incase the
5272 * super_block handler wishes to interpret them.
5273 */
5274static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5275{
5276
5277 if (info->raid_disks == 0) {
5278 /* just setting version number for superblock loading */
5279 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005280 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 super_types[info->major_version].name == NULL) {
5282 /* maybe try to auto-load a module? */
5283 printk(KERN_INFO
5284 "md: superblock version %d not known\n",
5285 info->major_version);
5286 return -EINVAL;
5287 }
5288 mddev->major_version = info->major_version;
5289 mddev->minor_version = info->minor_version;
5290 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005291 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005292 /* ensure mddev_put doesn't delete this now that there
5293 * is some minimal configuration.
5294 */
5295 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 return 0;
5297 }
5298 mddev->major_version = MD_MAJOR_VERSION;
5299 mddev->minor_version = MD_MINOR_VERSION;
5300 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5301 mddev->ctime = get_seconds();
5302
5303 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005304 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005305 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 mddev->raid_disks = info->raid_disks;
5307 /* don't set md_minor, it is determined by which /dev/md* was
5308 * openned
5309 */
5310 if (info->state & (1<<MD_SB_CLEAN))
5311 mddev->recovery_cp = MaxSector;
5312 else
5313 mddev->recovery_cp = 0;
5314 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005315 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316
5317 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005318 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319
5320 mddev->max_disks = MD_SB_DISKS;
5321
NeilBrowne6910632008-02-06 01:39:51 -08005322 if (mddev->persistent)
5323 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07005324 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325
NeilBrownc3d97142009-12-14 12:49:52 +11005326 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5327 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005328
NeilBrownf6705572006-03-27 01:18:11 -08005329 mddev->reshape_position = MaxSector;
5330
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 /*
5332 * Generate a 128 bit UUID
5333 */
5334 get_random_bytes(mddev->uuid, 16);
5335
NeilBrownf6705572006-03-27 01:18:11 -08005336 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005337 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005338 mddev->new_layout = mddev->layout;
5339 mddev->delta_disks = 0;
5340
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 return 0;
5342}
5343
Dan Williams1f403622009-03-31 14:59:03 +11005344void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5345{
Dan Williamsb522adc2009-03-31 15:00:31 +11005346 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5347
5348 if (mddev->external_size)
5349 return;
5350
Dan Williams1f403622009-03-31 14:59:03 +11005351 mddev->array_sectors = array_sectors;
5352}
5353EXPORT_SYMBOL(md_set_array_sectors);
5354
Andre Nolld71f9f82008-07-11 22:02:22 +10005355static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005356{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005357 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005358 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005359 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005360
5361 if (mddev->pers->resize == NULL)
5362 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005363 /* The "num_sectors" is the number of sectors of each device that
5364 * is used. This can only make sense for arrays with redundancy.
5365 * linear and raid0 always use whatever space is available. We can only
5366 * consider changing this number if no resync or reconstruction is
5367 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005368 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005369 * of each device. If num_sectors is zero, we find the largest size
5370 * that fits.
5371
NeilBrowna35b0d62006-01-06 00:20:49 -08005372 */
5373 if (mddev->sync_thread)
5374 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005375 if (mddev->bitmap)
5376 /* Sorry, cannot grow a bitmap yet, just remove it,
5377 * grow, and re-add.
5378 */
5379 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005380 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005381 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005382
Andre Nolld71f9f82008-07-11 22:02:22 +10005383 if (fit && (num_sectors == 0 || num_sectors > avail))
5384 num_sectors = avail;
5385 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005386 return -ENOSPC;
5387 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005388 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005389 if (!rv)
5390 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005391 return rv;
5392}
5393
NeilBrownda943b992006-01-06 00:20:54 -08005394static int update_raid_disks(mddev_t *mddev, int raid_disks)
5395{
5396 int rv;
5397 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005398 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005399 return -EINVAL;
5400 if (raid_disks <= 0 ||
5401 raid_disks >= mddev->max_disks)
5402 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005403 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005404 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005405 mddev->delta_disks = raid_disks - mddev->raid_disks;
5406
5407 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005408 return rv;
5409}
5410
5411
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412/*
5413 * update_array_info is used to change the configuration of an
5414 * on-line array.
5415 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5416 * fields in the info are checked against the array.
5417 * Any differences that cannot be handled will cause an error.
5418 * Normally, only one change can be managed at a time.
5419 */
5420static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5421{
5422 int rv = 0;
5423 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005424 int state = 0;
5425
5426 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005427 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005428 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429
5430 if (mddev->major_version != info->major_version ||
5431 mddev->minor_version != info->minor_version ||
5432/* mddev->patch_version != info->patch_version || */
5433 mddev->ctime != info->ctime ||
5434 mddev->level != info->level ||
5435/* mddev->layout != info->layout || */
5436 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005437 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005438 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5439 ((state^info->state) & 0xfffffe00)
5440 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 return -EINVAL;
5442 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005443 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5444 cnt++;
5445 if (mddev->raid_disks != info->raid_disks)
5446 cnt++;
5447 if (mddev->layout != info->layout)
5448 cnt++;
5449 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5450 cnt++;
5451 if (cnt == 0)
5452 return 0;
5453 if (cnt > 1)
5454 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455
5456 if (mddev->layout != info->layout) {
5457 /* Change layout
5458 * we don't need to do anything at the md level, the
5459 * personality will take care of it all.
5460 */
NeilBrown50ac1682009-06-18 08:47:55 +10005461 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005463 else {
5464 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005465 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005466 if (rv)
5467 mddev->new_layout = mddev->layout;
5468 return rv;
5469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005471 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005472 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473
NeilBrownda943b992006-01-06 00:20:54 -08005474 if (mddev->raid_disks != info->raid_disks)
5475 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476
NeilBrown36fa3062005-09-09 16:23:45 -07005477 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5478 if (mddev->pers->quiesce == NULL)
5479 return -EINVAL;
5480 if (mddev->recovery || mddev->sync_thread)
5481 return -EBUSY;
5482 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5483 /* add the bitmap */
5484 if (mddev->bitmap)
5485 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005486 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005487 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005488 mddev->bitmap_info.offset =
5489 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005490 mddev->pers->quiesce(mddev, 1);
5491 rv = bitmap_create(mddev);
5492 if (rv)
5493 bitmap_destroy(mddev);
5494 mddev->pers->quiesce(mddev, 0);
5495 } else {
5496 /* remove the bitmap */
5497 if (!mddev->bitmap)
5498 return -ENOENT;
5499 if (mddev->bitmap->file)
5500 return -EINVAL;
5501 mddev->pers->quiesce(mddev, 1);
5502 bitmap_destroy(mddev);
5503 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005504 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005505 }
5506 }
NeilBrown850b2b422006-10-03 01:15:46 -07005507 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 return rv;
5509}
5510
5511static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5512{
5513 mdk_rdev_t *rdev;
5514
5515 if (mddev->pers == NULL)
5516 return -ENODEV;
5517
5518 rdev = find_rdev(mddev, dev);
5519 if (!rdev)
5520 return -ENODEV;
5521
5522 md_error(mddev, rdev);
5523 return 0;
5524}
5525
Andre Noll2f9618c2008-04-25 18:57:58 +02005526/*
5527 * We have a problem here : there is no easy way to give a CHS
5528 * virtual geometry. We currently pretend that we have a 2 heads
5529 * 4 sectors (with a BIG number of cylinders...). This drives
5530 * dosfs just mad... ;-)
5531 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005532static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5533{
5534 mddev_t *mddev = bdev->bd_disk->private_data;
5535
5536 geo->heads = 2;
5537 geo->sectors = 4;
5538 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5539 return 0;
5540}
5541
Al Viroa39907f2008-03-02 10:31:15 -05005542static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 unsigned int cmd, unsigned long arg)
5544{
5545 int err = 0;
5546 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10005548 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549
5550 if (!capable(CAP_SYS_ADMIN))
5551 return -EACCES;
5552
5553 /*
5554 * Commands dealing with the RAID driver but not any
5555 * particular array:
5556 */
5557 switch (cmd)
5558 {
5559 case RAID_VERSION:
5560 err = get_version(argp);
5561 goto done;
5562
5563 case PRINT_RAID_DEBUG:
5564 err = 0;
5565 md_print_devices();
5566 goto done;
5567
5568#ifndef MODULE
5569 case RAID_AUTORUN:
5570 err = 0;
5571 autostart_arrays(arg);
5572 goto done;
5573#endif
5574 default:;
5575 }
5576
5577 /*
5578 * Commands creating/starting a new array:
5579 */
5580
Al Viroa39907f2008-03-02 10:31:15 -05005581 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582
5583 if (!mddev) {
5584 BUG();
5585 goto abort;
5586 }
5587
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 err = mddev_lock(mddev);
5589 if (err) {
5590 printk(KERN_INFO
5591 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5592 err, cmd);
5593 goto abort;
5594 }
5595
5596 switch (cmd)
5597 {
5598 case SET_ARRAY_INFO:
5599 {
5600 mdu_array_info_t info;
5601 if (!arg)
5602 memset(&info, 0, sizeof(info));
5603 else if (copy_from_user(&info, argp, sizeof(info))) {
5604 err = -EFAULT;
5605 goto abort_unlock;
5606 }
5607 if (mddev->pers) {
5608 err = update_array_info(mddev, &info);
5609 if (err) {
5610 printk(KERN_WARNING "md: couldn't update"
5611 " array info. %d\n", err);
5612 goto abort_unlock;
5613 }
5614 goto done_unlock;
5615 }
5616 if (!list_empty(&mddev->disks)) {
5617 printk(KERN_WARNING
5618 "md: array %s already has disks!\n",
5619 mdname(mddev));
5620 err = -EBUSY;
5621 goto abort_unlock;
5622 }
5623 if (mddev->raid_disks) {
5624 printk(KERN_WARNING
5625 "md: array %s already initialised!\n",
5626 mdname(mddev));
5627 err = -EBUSY;
5628 goto abort_unlock;
5629 }
5630 err = set_array_info(mddev, &info);
5631 if (err) {
5632 printk(KERN_WARNING "md: couldn't set"
5633 " array info. %d\n", err);
5634 goto abort_unlock;
5635 }
5636 }
5637 goto done_unlock;
5638
5639 default:;
5640 }
5641
5642 /*
5643 * Commands querying/configuring an existing array:
5644 */
NeilBrown32a76272005-06-21 17:17:14 -07005645 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005646 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005647 if ((!mddev->raid_disks && !mddev->external)
5648 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5649 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5650 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 err = -ENODEV;
5652 goto abort_unlock;
5653 }
5654
5655 /*
5656 * Commands even a read-only array can execute:
5657 */
5658 switch (cmd)
5659 {
5660 case GET_ARRAY_INFO:
5661 err = get_array_info(mddev, argp);
5662 goto done_unlock;
5663
NeilBrown32a76272005-06-21 17:17:14 -07005664 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005665 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005666 goto done_unlock;
5667
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 case GET_DISK_INFO:
5669 err = get_disk_info(mddev, argp);
5670 goto done_unlock;
5671
5672 case RESTART_ARRAY_RW:
5673 err = restart_array(mddev);
5674 goto done_unlock;
5675
5676 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005677 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 goto done_unlock;
5679
5680 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005681 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 goto done_unlock;
5683
Dan Williamse2218352010-05-12 08:25:37 +10005684 case BLKROSET:
5685 if (get_user(ro, (int __user *)(arg))) {
5686 err = -EFAULT;
5687 goto done_unlock;
5688 }
5689 err = -EINVAL;
5690
5691 /* if the bdev is going readonly the value of mddev->ro
5692 * does not matter, no writes are coming
5693 */
5694 if (ro)
5695 goto done_unlock;
5696
5697 /* are we are already prepared for writes? */
5698 if (mddev->ro != 1)
5699 goto done_unlock;
5700
5701 /* transitioning to readauto need only happen for
5702 * arrays that call md_write_start
5703 */
5704 if (mddev->pers) {
5705 err = restart_array(mddev);
5706 if (err == 0) {
5707 mddev->ro = 2;
5708 set_disk_ro(mddev->gendisk, 0);
5709 }
5710 }
5711 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 }
5713
5714 /*
5715 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005716 * superblock, so we do not allow them on read-only arrays.
5717 * However non-MD ioctls (e.g. get-size) will still come through
5718 * here and hit the 'default' below, so only disallow
5719 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005721 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005722 if (mddev->ro == 2) {
5723 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005724 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005725 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5726 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005727 } else {
5728 err = -EROFS;
5729 goto abort_unlock;
5730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 }
5732
5733 switch (cmd)
5734 {
5735 case ADD_NEW_DISK:
5736 {
5737 mdu_disk_info_t info;
5738 if (copy_from_user(&info, argp, sizeof(info)))
5739 err = -EFAULT;
5740 else
5741 err = add_new_disk(mddev, &info);
5742 goto done_unlock;
5743 }
5744
5745 case HOT_REMOVE_DISK:
5746 err = hot_remove_disk(mddev, new_decode_dev(arg));
5747 goto done_unlock;
5748
5749 case HOT_ADD_DISK:
5750 err = hot_add_disk(mddev, new_decode_dev(arg));
5751 goto done_unlock;
5752
5753 case SET_DISK_FAULTY:
5754 err = set_disk_faulty(mddev, new_decode_dev(arg));
5755 goto done_unlock;
5756
5757 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005758 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 goto done_unlock;
5760
NeilBrown32a76272005-06-21 17:17:14 -07005761 case SET_BITMAP_FILE:
5762 err = set_bitmap_file(mddev, (int)arg);
5763 goto done_unlock;
5764
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 err = -EINVAL;
5767 goto abort_unlock;
5768 }
5769
5770done_unlock:
5771abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005772 if (mddev->hold_active == UNTIL_IOCTL &&
5773 err != -EINVAL)
5774 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 mddev_unlock(mddev);
5776
5777 return err;
5778done:
5779 if (err)
5780 MD_BUG();
5781abort:
5782 return err;
5783}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005784#ifdef CONFIG_COMPAT
5785static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5786 unsigned int cmd, unsigned long arg)
5787{
5788 switch (cmd) {
5789 case HOT_REMOVE_DISK:
5790 case HOT_ADD_DISK:
5791 case SET_DISK_FAULTY:
5792 case SET_BITMAP_FILE:
5793 /* These take in integer arg, do not convert */
5794 break;
5795 default:
5796 arg = (unsigned long)compat_ptr(arg);
5797 break;
5798 }
5799
5800 return md_ioctl(bdev, mode, cmd, arg);
5801}
5802#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803
Al Viroa39907f2008-03-02 10:31:15 -05005804static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805{
5806 /*
5807 * Succeed if we can lock the mddev, which confirms that
5808 * it isn't being stopped right now.
5809 */
NeilBrownd3374822009-01-09 08:31:10 +11005810 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 int err;
5812
NeilBrownd3374822009-01-09 08:31:10 +11005813 if (mddev->gendisk != bdev->bd_disk) {
5814 /* we are racing with mddev_put which is discarding this
5815 * bd_disk.
5816 */
5817 mddev_put(mddev);
5818 /* Wait until bdev->bd_disk is definitely gone */
5819 flush_scheduled_work();
5820 /* Then retry the open from the top */
5821 return -ERESTARTSYS;
5822 }
5823 BUG_ON(mddev != bdev->bd_disk->private_data);
5824
NeilBrownc8c00a62009-08-10 12:50:52 +10005825 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 goto out;
5827
5828 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005829 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005830 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831
Al Viroa39907f2008-03-02 10:31:15 -05005832 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 out:
5834 return err;
5835}
5836
Al Viroa39907f2008-03-02 10:31:15 -05005837static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838{
Al Viroa39907f2008-03-02 10:31:15 -05005839 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005841 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005842 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 mddev_put(mddev);
5844
5845 return 0;
5846}
5847
Linus Torvalds44ce62942007-05-09 18:51:36 -07005848static int md_media_changed(struct gendisk *disk)
5849{
5850 mddev_t *mddev = disk->private_data;
5851
5852 return mddev->changed;
5853}
5854
5855static int md_revalidate(struct gendisk *disk)
5856{
5857 mddev_t *mddev = disk->private_data;
5858
5859 mddev->changed = 0;
5860 return 0;
5861}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005862static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863{
5864 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005865 .open = md_open,
5866 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005867 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005868#ifdef CONFIG_COMPAT
5869 .compat_ioctl = md_compat_ioctl,
5870#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005871 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07005872 .media_changed = md_media_changed,
5873 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874};
5875
Adrian Bunk75c96f82005-05-05 16:16:09 -07005876static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877{
5878 mdk_thread_t *thread = arg;
5879
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 /*
5881 * md_thread is a 'system-thread', it's priority should be very
5882 * high. We avoid resource deadlocks individually in each
5883 * raid personality. (RAID5 does preallocation) We also use RR and
5884 * the very same RT priority as kswapd, thus we will never get
5885 * into a priority inversion deadlock.
5886 *
5887 * we definitely have to have equal or higher priority than
5888 * bdflush, otherwise bdflush will deadlock if there are too
5889 * many dirty RAID5 blocks.
5890 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891
NeilBrown6985c432005-10-19 21:23:47 -07005892 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005893 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894
NeilBrown93588e22005-11-15 00:09:12 -08005895 /* We need to wait INTERRUPTIBLE so that
5896 * we don't add to the load-average.
5897 * That means we need to be sure no signals are
5898 * pending
5899 */
5900 if (signal_pending(current))
5901 flush_signals(current);
5902
5903 wait_event_interruptible_timeout
5904 (thread->wqueue,
5905 test_bit(THREAD_WAKEUP, &thread->flags)
5906 || kthread_should_stop(),
5907 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908
5909 clear_bit(THREAD_WAKEUP, &thread->flags);
5910
NeilBrown787453c2005-11-08 21:39:43 -08005911 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005913
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 return 0;
5915}
5916
5917void md_wakeup_thread(mdk_thread_t *thread)
5918{
5919 if (thread) {
5920 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5921 set_bit(THREAD_WAKEUP, &thread->flags);
5922 wake_up(&thread->wqueue);
5923 }
5924}
5925
5926mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5927 const char *name)
5928{
5929 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930
NeilBrown9ffae0c2006-01-06 00:20:32 -08005931 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932 if (!thread)
5933 return NULL;
5934
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 init_waitqueue_head(&thread->wqueue);
5936
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 thread->run = run;
5938 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005939 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10005940 thread->tsk = kthread_run(md_thread, thread,
5941 "%s_%s",
5942 mdname(thread->mddev),
5943 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07005944 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 kfree(thread);
5946 return NULL;
5947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 return thread;
5949}
5950
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951void md_unregister_thread(mdk_thread_t *thread)
5952{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005953 if (!thread)
5954 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005955 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005956
5957 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958 kfree(thread);
5959}
5960
5961void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5962{
5963 if (!mddev) {
5964 MD_BUG();
5965 return;
5966 }
5967
NeilBrownb2d444d2005-11-08 21:39:31 -08005968 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005970
5971 if (mddev->external)
5972 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005973/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5975 mdname(mddev),
5976 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5977 __builtin_return_address(0),__builtin_return_address(1),
5978 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005979*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005980 if (!mddev->pers)
5981 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 if (!mddev->pers->error_handler)
5983 return;
5984 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005985 if (mddev->degraded)
5986 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc0cc75f2010-03-22 10:28:51 +11005987 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5989 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5990 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005991 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992}
5993
5994/* seq_file implementation /proc/mdstat */
5995
5996static void status_unused(struct seq_file *seq)
5997{
5998 int i = 0;
5999 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000
6001 seq_printf(seq, "unused devices: ");
6002
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006003 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 char b[BDEVNAME_SIZE];
6005 i++;
6006 seq_printf(seq, "%s ",
6007 bdevname(rdev->bdev,b));
6008 }
6009 if (!i)
6010 seq_printf(seq, "<none>");
6011
6012 seq_printf(seq, "\n");
6013}
6014
6015
6016static void status_resync(struct seq_file *seq, mddev_t * mddev)
6017{
NeilBrowndd71cf62009-05-07 12:49:35 +10006018 sector_t max_sectors, resync, res;
6019 unsigned long dt, db;
6020 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006021 int scale;
6022 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023
NeilBrowndd71cf62009-05-07 12:49:35 +10006024 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025
6026 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006027 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006029 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030
6031 /*
6032 * Should not happen.
6033 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006034 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035 MD_BUG();
6036 return;
6037 }
NeilBrown4588b422006-03-27 01:18:04 -08006038 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006039 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006040 * u32, as those are the requirements for sector_div.
6041 * Thus 'scale' must be at least 10
6042 */
6043 scale = 10;
6044 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006045 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006046 scale++;
6047 }
6048 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006049 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006050
6051 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 {
NeilBrown4588b422006-03-27 01:18:04 -08006053 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 seq_printf(seq, "[");
6055 for (i = 0; i < x; i++)
6056 seq_printf(seq, "=");
6057 seq_printf(seq, ">");
6058 for (i = 0; i < y; i++)
6059 seq_printf(seq, ".");
6060 seq_printf(seq, "] ");
6061 }
NeilBrown4588b422006-03-27 01:18:04 -08006062 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006063 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6064 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006065 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6066 "check" :
6067 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6068 "resync" : "recovery"))),
6069 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006070 (unsigned long long) resync/2,
6071 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072
6073 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 * dt: time from mark until now
6075 * db: blocks written from mark until now
6076 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006077 *
6078 * rt is a sector_t, so could be 32bit or 64bit.
6079 * So we divide before multiply in case it is 32bit and close
6080 * to the limit.
6081 * We scale the divisor (db) by 32 to avoid loosing precision
6082 * near the end of resync when the number of remaining sectors
6083 * is close to 'db'.
6084 * We then divide rt by 32 after multiplying by db to compensate.
6085 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 */
6087 dt = ((jiffies - mddev->resync_mark) / HZ);
6088 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006089 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6090 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091
NeilBrowndd71cf62009-05-07 12:49:35 +10006092 rt = max_sectors - resync; /* number of remaining sectors */
6093 sector_div(rt, db/32+1);
6094 rt *= dt;
6095 rt >>= 5;
6096
6097 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6098 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099
NeilBrownff4e8d92006-07-10 04:44:16 -07006100 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101}
6102
6103static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6104{
6105 struct list_head *tmp;
6106 loff_t l = *pos;
6107 mddev_t *mddev;
6108
6109 if (l >= 0x10000)
6110 return NULL;
6111 if (!l--)
6112 /* header */
6113 return (void*)1;
6114
6115 spin_lock(&all_mddevs_lock);
6116 list_for_each(tmp,&all_mddevs)
6117 if (!l--) {
6118 mddev = list_entry(tmp, mddev_t, all_mddevs);
6119 mddev_get(mddev);
6120 spin_unlock(&all_mddevs_lock);
6121 return mddev;
6122 }
6123 spin_unlock(&all_mddevs_lock);
6124 if (!l--)
6125 return (void*)2;/* tail */
6126 return NULL;
6127}
6128
6129static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6130{
6131 struct list_head *tmp;
6132 mddev_t *next_mddev, *mddev = v;
6133
6134 ++*pos;
6135 if (v == (void*)2)
6136 return NULL;
6137
6138 spin_lock(&all_mddevs_lock);
6139 if (v == (void*)1)
6140 tmp = all_mddevs.next;
6141 else
6142 tmp = mddev->all_mddevs.next;
6143 if (tmp != &all_mddevs)
6144 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6145 else {
6146 next_mddev = (void*)2;
6147 *pos = 0x10000;
6148 }
6149 spin_unlock(&all_mddevs_lock);
6150
6151 if (v != (void*)1)
6152 mddev_put(mddev);
6153 return next_mddev;
6154
6155}
6156
6157static void md_seq_stop(struct seq_file *seq, void *v)
6158{
6159 mddev_t *mddev = v;
6160
6161 if (mddev && v != (void*)1 && v != (void*)2)
6162 mddev_put(mddev);
6163}
6164
NeilBrownd7603b72006-01-06 00:20:30 -08006165struct mdstat_info {
6166 int event;
6167};
6168
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169static int md_seq_show(struct seq_file *seq, void *v)
6170{
6171 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006172 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006174 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006175 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176
6177 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006178 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 seq_printf(seq, "Personalities : ");
6180 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006181 list_for_each_entry(pers, &pers_list, list)
6182 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183
6184 spin_unlock(&pers_lock);
6185 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006186 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 return 0;
6188 }
6189 if (v == (void*)2) {
6190 status_unused(seq);
6191 return 0;
6192 }
6193
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006194 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006196
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6198 seq_printf(seq, "%s : %sactive", mdname(mddev),
6199 mddev->pers ? "" : "in");
6200 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006201 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006203 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006204 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205 seq_printf(seq, " %s", mddev->pers->name);
6206 }
6207
Andre Nolldd8ac332009-03-31 14:33:13 +11006208 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006209 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 char b[BDEVNAME_SIZE];
6211 seq_printf(seq, " %s[%d]",
6212 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006213 if (test_bit(WriteMostly, &rdev->flags))
6214 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006215 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216 seq_printf(seq, "(F)");
6217 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006218 } else if (rdev->raid_disk < 0)
6219 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006220 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221 }
6222
6223 if (!list_empty(&mddev->disks)) {
6224 if (mddev->pers)
6225 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006226 (unsigned long long)
6227 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228 else
6229 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006230 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006232 if (mddev->persistent) {
6233 if (mddev->major_version != 0 ||
6234 mddev->minor_version != 90) {
6235 seq_printf(seq," super %d.%d",
6236 mddev->major_version,
6237 mddev->minor_version);
6238 }
NeilBrowne6910632008-02-06 01:39:51 -08006239 } else if (mddev->external)
6240 seq_printf(seq, " super external:%s",
6241 mddev->metadata_type);
6242 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006243 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244
6245 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006246 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006248 if (mddev->pers->sync_request) {
6249 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006250 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006251 seq_printf(seq, "\n ");
6252 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6253 seq_printf(seq, "\tresync=DELAYED\n ");
6254 else if (mddev->recovery_cp < MaxSector)
6255 seq_printf(seq, "\tresync=PENDING\n ");
6256 }
NeilBrown32a76272005-06-21 17:17:14 -07006257 } else
6258 seq_printf(seq, "\n ");
6259
6260 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006261 unsigned long chunk_kb;
6262 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006263 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006264 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006265 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6266 "%lu%s chunk",
6267 bitmap->pages - bitmap->missing_pages,
6268 bitmap->pages,
6269 (bitmap->pages - bitmap->missing_pages)
6270 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006271 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006272 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006273 if (bitmap->file) {
6274 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006275 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006276 }
NeilBrown78d742d2005-06-21 17:17:15 -07006277
NeilBrown32a76272005-06-21 17:17:14 -07006278 seq_printf(seq, "\n");
6279 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 }
6281
6282 seq_printf(seq, "\n");
6283 }
6284 mddev_unlock(mddev);
6285
6286 return 0;
6287}
6288
Jan Engelhardt110518b2009-05-07 12:49:37 +10006289static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290 .start = md_seq_start,
6291 .next = md_seq_next,
6292 .stop = md_seq_stop,
6293 .show = md_seq_show,
6294};
6295
6296static int md_seq_open(struct inode *inode, struct file *file)
6297{
6298 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006299 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6300 if (mi == NULL)
6301 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302
6303 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006304 if (error)
6305 kfree(mi);
6306 else {
6307 struct seq_file *p = file->private_data;
6308 p->private = mi;
6309 mi->event = atomic_read(&md_event_count);
6310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311 return error;
6312}
6313
NeilBrownd7603b72006-01-06 00:20:30 -08006314static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6315{
6316 struct seq_file *m = filp->private_data;
6317 struct mdstat_info *mi = m->private;
6318 int mask;
6319
6320 poll_wait(filp, &md_event_waiters, wait);
6321
6322 /* always allow read */
6323 mask = POLLIN | POLLRDNORM;
6324
6325 if (mi->event != atomic_read(&md_event_count))
6326 mask |= POLLERR | POLLPRI;
6327 return mask;
6328}
6329
Arjan van de Venfa027c22007-02-12 00:55:33 -08006330static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006331 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332 .open = md_seq_open,
6333 .read = seq_read,
6334 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006335 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006336 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337};
6338
NeilBrown2604b702006-01-06 00:20:36 -08006339int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006342 list_add_tail(&p->list, &pers_list);
6343 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344 spin_unlock(&pers_lock);
6345 return 0;
6346}
6347
NeilBrown2604b702006-01-06 00:20:36 -08006348int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349{
NeilBrown2604b702006-01-06 00:20:36 -08006350 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006352 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 spin_unlock(&pers_lock);
6354 return 0;
6355}
6356
NeilBrowneea1bf32009-03-31 14:27:02 +11006357static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358{
6359 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006361 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362
6363 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006364 rcu_read_lock();
6365 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006367 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6368 (int)part_stat_read(&disk->part0, sectors[1]) -
6369 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006370 /* sync IO will cause sync_io to increase before the disk_stats
6371 * as sync_io is counted when a request starts, and
6372 * disk_stats is counted when it completes.
6373 * So resync activity will cause curr_events to be smaller than
6374 * when there was no such activity.
6375 * non-sync IO will cause disk_stat to increase without
6376 * increasing sync_io so curr_events will (eventually)
6377 * be larger than it was before. Once it becomes
6378 * substantially larger, the test below will cause
6379 * the array to appear non-idle, and resync will slow
6380 * down.
6381 * If there is a lot of outstanding resync activity when
6382 * we set last_event to curr_events, then all that activity
6383 * completing might cause the array to appear non-idle
6384 * and resync will be slowed down even though there might
6385 * not have been non-resync activity. This will only
6386 * happen once though. 'last_events' will soon reflect
6387 * the state where there is little or no outstanding
6388 * resync requests, and further resync activity will
6389 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006390 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006392 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393 rdev->last_events = curr_events;
6394 idle = 0;
6395 }
6396 }
NeilBrown4b809912008-07-21 17:05:25 +10006397 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 return idle;
6399}
6400
6401void md_done_sync(mddev_t *mddev, int blocks, int ok)
6402{
6403 /* another "blocks" (512byte) blocks have been synced */
6404 atomic_sub(blocks, &mddev->recovery_active);
6405 wake_up(&mddev->recovery_wait);
6406 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006407 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408 md_wakeup_thread(mddev->thread);
6409 // stop recovery, signal do_sync ....
6410 }
6411}
6412
6413
NeilBrown06d91a52005-06-21 17:17:12 -07006414/* md_write_start(mddev, bi)
6415 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006416 * in superblock) before writing, schedule a superblock update
6417 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006418 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006419void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420{
Neil Brown0fd62b82008-06-28 08:31:36 +10006421 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006422 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006423 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006424
NeilBrownf91de922005-11-08 21:39:36 -08006425 BUG_ON(mddev->ro == 1);
6426 if (mddev->ro == 2) {
6427 /* need to switch to read/write */
6428 mddev->ro = 0;
6429 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6430 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006431 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006432 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006433 }
NeilBrown06d91a52005-06-21 17:17:12 -07006434 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006435 if (mddev->safemode == 1)
6436 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006437 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006438 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006439 if (mddev->in_sync) {
6440 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006441 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006442 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006443 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006444 }
NeilBrowna9701a32005-11-08 21:39:34 -08006445 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006446 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006447 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006448 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006449 wait_event(mddev->sb_wait,
6450 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6451 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452}
6453
6454void md_write_end(mddev_t *mddev)
6455{
6456 if (atomic_dec_and_test(&mddev->writes_pending)) {
6457 if (mddev->safemode == 2)
6458 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006459 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6461 }
6462}
6463
NeilBrown2a2275d2007-01-26 00:57:11 -08006464/* md_allow_write(mddev)
6465 * Calling this ensures that the array is marked 'active' so that writes
6466 * may proceed without blocking. It is important to call this before
6467 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6468 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006469 *
6470 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6471 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006472 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006473int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006474{
6475 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006476 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006477 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006478 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006479 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006480 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006481
6482 spin_lock_irq(&mddev->write_lock);
6483 if (mddev->in_sync) {
6484 mddev->in_sync = 0;
6485 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6486 if (mddev->safemode_delay &&
6487 mddev->safemode == 0)
6488 mddev->safemode = 1;
6489 spin_unlock_irq(&mddev->write_lock);
6490 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006491 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006492 } else
6493 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006494
6495 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6496 return -EAGAIN;
6497 else
6498 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006499}
6500EXPORT_SYMBOL_GPL(md_allow_write);
6501
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502#define SYNC_MARKS 10
6503#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006504void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505{
6506 mddev_t *mddev2;
6507 unsigned int currspeed = 0,
6508 window;
NeilBrown57afd892005-06-21 17:17:13 -07006509 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510 unsigned long mark[SYNC_MARKS];
6511 sector_t mark_cnt[SYNC_MARKS];
6512 int last_mark,m;
6513 struct list_head *tmp;
6514 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006515 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006516 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006517 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518
6519 /* just incase thread restarts... */
6520 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6521 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006522 if (mddev->ro) /* never try to sync a read-only array */
6523 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524
NeilBrown61df9d92006-10-03 01:15:57 -07006525 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6526 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6527 desc = "data-check";
6528 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6529 desc = "requested-resync";
6530 else
6531 desc = "resync";
6532 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6533 desc = "reshape";
6534 else
6535 desc = "recovery";
6536
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537 /* we overload curr_resync somewhat here.
6538 * 0 == not engaged in resync at all
6539 * 2 == checking that there is no conflict with another sync
6540 * 1 == like 2, but have yielded to allow conflicting resync to
6541 * commense
6542 * other == active in resync - this many blocks
6543 *
6544 * Before starting a resync we must have set curr_resync to
6545 * 2, and then checked that every "conflicting" array has curr_resync
6546 * less than ours. When we find one that is the same or higher
6547 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6548 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6549 * This will mean we have to start checking from the beginning again.
6550 *
6551 */
6552
6553 do {
6554 mddev->curr_resync = 2;
6555
6556 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006557 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006558 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006559
6560 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006562 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563 if (mddev2 == mddev)
6564 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006565 if (!mddev->parallel_resync
6566 && mddev2->curr_resync
6567 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568 DEFINE_WAIT(wq);
6569 if (mddev < mddev2 && mddev->curr_resync == 2) {
6570 /* arbitrarily yield */
6571 mddev->curr_resync = 1;
6572 wake_up(&resync_wait);
6573 }
6574 if (mddev > mddev2 && mddev->curr_resync == 1)
6575 /* no need to wait here, we can wait the next
6576 * time 'round when curr_resync == 2
6577 */
6578 continue;
NeilBrown97441972008-09-19 11:49:54 +10006579 /* We need to wait 'interruptible' so as not to
6580 * contribute to the load average, and not to
6581 * be caught by 'softlockup'
6582 */
6583 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006584 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006585 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006586 printk(KERN_INFO "md: delaying %s of %s"
6587 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006589 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006591 if (signal_pending(current))
6592 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593 schedule();
6594 finish_wait(&resync_wait, &wq);
6595 goto try_again;
6596 }
6597 finish_wait(&resync_wait, &wq);
6598 }
6599 }
6600 } while (mddev->curr_resync < 2);
6601
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006602 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006603 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006605 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606 */
6607 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006608 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006609 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006610 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6611 j = mddev->resync_min;
6612 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006613 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006614
NeilBrownccfcc3c2006-03-27 01:18:09 -08006615 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006616 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006617 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006619 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006620 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006621 rcu_read_lock();
6622 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006623 if (rdev->raid_disk >= 0 &&
6624 !test_bit(Faulty, &rdev->flags) &&
6625 !test_bit(In_sync, &rdev->flags) &&
6626 rdev->recovery_offset < j)
6627 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006628 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630
NeilBrown61df9d92006-10-03 01:15:57 -07006631 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6632 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6633 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006634 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006635 "(but not more than %d KB/sec) for %s.\n",
6636 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637
NeilBrowneea1bf32009-03-31 14:27:02 +11006638 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006639
NeilBrown57afd892005-06-21 17:17:13 -07006640 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641 for (m = 0; m < SYNC_MARKS; m++) {
6642 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006643 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644 }
6645 last_mark = 0;
6646 mddev->resync_mark = mark[last_mark];
6647 mddev->resync_mark_cnt = mark_cnt[last_mark];
6648
6649 /*
6650 * Tune reconstruction:
6651 */
6652 window = 32*(PAGE_SIZE/512);
6653 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6654 window/2,(unsigned long long) max_sectors/2);
6655
6656 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657 last_check = 0;
6658
6659 if (j>2) {
6660 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006661 "md: resuming %s of %s from checkpoint.\n",
6662 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663 mddev->curr_resync = j;
6664 }
NeilBrownefa59332009-12-14 12:49:49 +11006665 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666
6667 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006668 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669
NeilBrown57afd892005-06-21 17:17:13 -07006670 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006671
NeilBrown7a91ee12009-05-26 12:57:21 +10006672 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6673 ((mddev->curr_resync > mddev->curr_resync_completed &&
6674 (mddev->curr_resync - mddev->curr_resync_completed)
6675 > (max_sectors >> 4)) ||
6676 (j - mddev->curr_resync_completed)*2
6677 >= mddev->resync_max - mddev->curr_resync_completed
6678 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006679 /* time to update curr_resync_completed */
6680 blk_unplug(mddev->queue);
6681 wait_event(mddev->recovery_wait,
6682 atomic_read(&mddev->recovery_active) == 0);
6683 mddev->curr_resync_completed =
6684 mddev->curr_resync;
6685 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006686 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006687 }
NeilBrownacb180b2009-04-14 16:28:34 +10006688
NeilBrowne62e58a2009-07-01 13:15:35 +10006689 while (j >= mddev->resync_max && !kthread_should_stop()) {
6690 /* As this condition is controlled by user-space,
6691 * we can block indefinitely, so use '_interruptible'
6692 * to avoid triggering warnings.
6693 */
6694 flush_signals(current); /* just in case */
6695 wait_event_interruptible(mddev->recovery_wait,
6696 mddev->resync_max > j
6697 || kthread_should_stop());
6698 }
NeilBrownacb180b2009-04-14 16:28:34 +10006699
6700 if (kthread_should_stop())
6701 goto interrupted;
6702
NeilBrown57afd892005-06-21 17:17:13 -07006703 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006704 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006705 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006706 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707 goto out;
6708 }
NeilBrown57afd892005-06-21 17:17:13 -07006709
6710 if (!skipped) { /* actual IO requested */
6711 io_sectors += sectors;
6712 atomic_add(sectors, &mddev->recovery_active);
6713 }
6714
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 j += sectors;
6716 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006717 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006718 if (last_check == 0)
6719 /* this is the earliers that rebuilt will be
6720 * visible in /proc/mdstat
6721 */
6722 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006723
6724 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725 continue;
6726
NeilBrown57afd892005-06-21 17:17:13 -07006727 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728
NeilBrowndfc70642008-05-23 13:04:39 -07006729 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730 break;
6731
6732 repeat:
6733 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6734 /* step marks */
6735 int next = (last_mark+1) % SYNC_MARKS;
6736
6737 mddev->resync_mark = mark[next];
6738 mddev->resync_mark_cnt = mark_cnt[next];
6739 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006740 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741 last_mark = next;
6742 }
6743
6744
NeilBrownc6207272008-02-06 01:39:52 -08006745 if (kthread_should_stop())
6746 goto interrupted;
6747
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748
6749 /*
6750 * this loop exits only if either when we are slower than
6751 * the 'hard' speed limit, or the system was IO-idle for
6752 * a jiffy.
6753 * the system might be non-idle CPU-wise, but we only care
6754 * about not overloading the IO subsystem. (things like an
6755 * e2fsck being done on the RAID array should execute fast)
6756 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006757 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758 cond_resched();
6759
NeilBrown57afd892005-06-21 17:17:13 -07006760 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6761 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762
NeilBrown88202a02006-01-06 00:21:36 -08006763 if (currspeed > speed_min(mddev)) {
6764 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006765 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006766 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767 goto repeat;
6768 }
6769 }
6770 }
NeilBrown61df9d92006-10-03 01:15:57 -07006771 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772 /*
6773 * this also signals 'finished resyncing' to md_stop
6774 */
6775 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006776 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777
6778 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6779
6780 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006781 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782
NeilBrowndfc70642008-05-23 13:04:39 -07006783 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006784 mddev->curr_resync > 2) {
6785 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6786 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6787 if (mddev->curr_resync >= mddev->recovery_cp) {
6788 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006789 "md: checkpointing %s of %s.\n",
6790 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006791 mddev->recovery_cp = mddev->curr_resync;
6792 }
6793 } else
6794 mddev->recovery_cp = MaxSector;
6795 } else {
6796 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6797 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006798 rcu_read_lock();
6799 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006800 if (rdev->raid_disk >= 0 &&
6801 !test_bit(Faulty, &rdev->flags) &&
6802 !test_bit(In_sync, &rdev->flags) &&
6803 rdev->recovery_offset < mddev->curr_resync)
6804 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07006805 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807 }
NeilBrown17571282006-12-10 02:20:52 -08006808 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006811 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6812 /* We completed so min/max setting can be forgotten if used. */
6813 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6814 mddev->resync_min = 0;
6815 mddev->resync_max = MaxSector;
6816 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6817 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006819 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6820 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006821 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822 wake_up(&resync_wait);
6823 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6824 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006825 return;
6826
6827 interrupted:
6828 /*
6829 * got a signal, exit.
6830 */
6831 printk(KERN_INFO
6832 "md: md_do_sync() got signal ... exiting\n");
6833 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6834 goto out;
6835
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836}
NeilBrown29269552006-03-27 01:18:10 -08006837EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838
6839
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006840static int remove_and_add_spares(mddev_t *mddev)
6841{
6842 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006843 int spares = 0;
6844
NeilBrown97e4f422009-03-31 14:33:13 +11006845 mddev->curr_resync_completed = 0;
6846
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006847 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006848 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006849 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006850 (test_bit(Faulty, &rdev->flags) ||
6851 ! test_bit(In_sync, &rdev->flags)) &&
6852 atomic_read(&rdev->nr_pending)==0) {
6853 if (mddev->pers->hot_remove_disk(
6854 mddev, rdev->raid_disk)==0) {
6855 char nm[20];
6856 sprintf(nm,"rd%d", rdev->raid_disk);
6857 sysfs_remove_link(&mddev->kobj, nm);
6858 rdev->raid_disk = -1;
6859 }
6860 }
6861
NeilBrown4044ba52009-01-09 08:31:11 +11006862 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006863 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006864 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006865 !test_bit(In_sync, &rdev->flags) &&
6866 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006867 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006868 if (rdev->raid_disk < 0
6869 && !test_bit(Faulty, &rdev->flags)) {
6870 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006871 if (mddev->pers->
6872 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006873 char nm[20];
6874 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006875 if (sysfs_create_link(&mddev->kobj,
6876 &rdev->kobj, nm))
6877 printk(KERN_WARNING
6878 "md: cannot register "
6879 "%s for %s\n",
6880 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006881 spares++;
6882 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11006883 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006884 } else
6885 break;
6886 }
NeilBrowndfc70642008-05-23 13:04:39 -07006887 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006888 }
6889 return spares;
6890}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006891/*
6892 * This routine is regularly called by all per-raid-array threads to
6893 * deal with generic issues like resync and super-block update.
6894 * Raid personalities that don't have a thread (linear/raid0) do not
6895 * need this as they never do any recovery or update the superblock.
6896 *
6897 * It does not do any resync itself, but rather "forks" off other threads
6898 * to do that as needed.
6899 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6900 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006901 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902 * and wakeups up this thread which will reap the thread and finish up.
6903 * This thread also removes any faulty devices (with nr_pending == 0).
6904 *
6905 * The overall approach is:
6906 * 1/ if the superblock needs updating, update it.
6907 * 2/ If a recovery thread is running, don't do anything else.
6908 * 3/ If recovery has finished, clean up, possibly marking spares active.
6909 * 4/ If there are any faulty devices, remove them.
6910 * 5/ If array is degraded, try to add spares devices
6911 * 6/ If array has spares or is not in-sync, start a resync thread.
6912 */
6913void md_check_recovery(mddev_t *mddev)
6914{
6915 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916
6917
NeilBrown5f404022005-06-21 17:17:16 -07006918 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11006919 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920
6921 if (mddev->ro)
6922 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006923
6924 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006925 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006926 printk(KERN_INFO "md: %s in immediate safe mode\n",
6927 mdname(mddev));
6928 mddev->safemode = 2;
6929 }
6930 flush_signals(current);
6931 }
6932
NeilBrownc89a8ee2008-08-05 15:54:13 +10006933 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6934 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006936 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006938 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006939 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006940 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6941 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942 ))
6943 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006944
NeilBrowndf5b89b2006-03-27 01:18:20 -08006945 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006946 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006947
NeilBrownc89a8ee2008-08-05 15:54:13 +10006948 if (mddev->ro) {
6949 /* Only thing we do on a ro array is remove
6950 * failed devices.
6951 */
6952 remove_and_add_spares(mddev);
6953 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6954 goto unlock;
6955 }
6956
NeilBrown31a59e32008-04-30 00:52:30 -07006957 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006958 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006959 spin_lock_irq(&mddev->write_lock);
6960 if (mddev->safemode &&
6961 !atomic_read(&mddev->writes_pending) &&
6962 !mddev->in_sync &&
6963 mddev->recovery_cp == MaxSector) {
6964 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006965 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006966 if (mddev->persistent)
6967 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6968 }
6969 if (mddev->safemode == 1)
6970 mddev->safemode = 0;
6971 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006972 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006973 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006974 }
NeilBrownfca4d842005-06-21 17:17:11 -07006975
NeilBrown850b2b422006-10-03 01:15:46 -07006976 if (mddev->flags)
6977 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006978
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6980 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6981 /* resync/recovery still happening */
6982 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6983 goto unlock;
6984 }
6985 if (mddev->sync_thread) {
6986 /* resync has finished, collect result */
6987 md_unregister_thread(mddev->sync_thread);
6988 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006989 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6990 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991 /* success...*/
6992 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006993 if (mddev->pers->spare_active(mddev))
6994 sysfs_notify(&mddev->kobj, NULL,
6995 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996 }
NeilBrowncea9c222009-03-31 15:15:05 +11006997 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6998 mddev->pers->finish_reshape)
6999 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07007000 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07007001
7002 /* if array is no-longer degraded, then any saved_raid_disk
7003 * information must be scrapped
7004 */
7005 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007006 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07007007 rdev->saved_raid_disk = -1;
7008
Linus Torvalds1da177e2005-04-16 15:20:36 -07007009 mddev->recovery = 0;
7010 /* flag recovery needed just to double check */
7011 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11007012 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007013 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007014 goto unlock;
7015 }
Neil Brown72a23c22008-06-28 08:31:41 +10007016 /* Set RUNNING before clearing NEEDED to avoid
7017 * any transients in the value of "sync_action".
7018 */
7019 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7020 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007021 /* Clear some bits that don't mean anything, but
7022 * might be left set
7023 */
NeilBrown24dd4692005-11-08 21:39:26 -08007024 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7025 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007027 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7028 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029 /* no recovery is running.
7030 * remove any failed drives, then
7031 * add spares if possible.
7032 * Spare are also removed and re-added, to allow
7033 * the personality to fail the re-add.
7034 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007036 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007037 if (mddev->pers->check_reshape == NULL ||
7038 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007039 /* Cannot proceed */
7040 goto unlock;
7041 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007042 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007043 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007044 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7045 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007046 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007047 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007048 } else if (mddev->recovery_cp < MaxSector) {
7049 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007050 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007051 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7052 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007054
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007056 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7057 /* We are adding a device or devices to an array
7058 * which has the bitmap stored on all devices.
7059 * So make sure all bitmap pages get written
7060 */
7061 bitmap_write_all(mddev->bitmap);
7062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063 mddev->sync_thread = md_register_thread(md_do_sync,
7064 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007065 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066 if (!mddev->sync_thread) {
7067 printk(KERN_ERR "%s: could not start resync"
7068 " thread...\n",
7069 mdname(mddev));
7070 /* leave the spares where they are, it shouldn't hurt */
7071 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08007072 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11007074 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007075 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076 }
7077 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007078 if (!mddev->sync_thread) {
7079 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7080 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7081 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007082 if (mddev->sysfs_action)
7083 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085 mddev_unlock(mddev);
7086 }
7087}
7088
Dan Williams6bfe0b42008-04-30 00:52:32 -07007089void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7090{
NeilBrown3c0ee632008-10-21 13:25:28 +11007091 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007092 wait_event_timeout(rdev->blocked_wait,
7093 !test_bit(Blocked, &rdev->flags),
7094 msecs_to_jiffies(5000));
7095 rdev_dec_pending(rdev, mddev);
7096}
7097EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7098
Adrian Bunk75c96f82005-05-05 16:16:09 -07007099static int md_notify_reboot(struct notifier_block *this,
7100 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101{
7102 struct list_head *tmp;
7103 mddev_t *mddev;
7104
7105 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7106
7107 printk(KERN_INFO "md: stopping all md devices.\n");
7108
NeilBrown29ac4aa2008-02-06 01:39:58 -08007109 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007110 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007111 /* Force a switch to readonly even array
7112 * appears to still be in use. Hence
7113 * the '100'.
7114 */
NeilBrownd710e132008-10-13 11:55:12 +11007115 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007116 mddev_unlock(mddev);
7117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118 /*
7119 * certain more exotic SCSI devices are known to be
7120 * volatile wrt too early system reboots. While the
7121 * right place to handle this issue is the given
7122 * driver, we do want to have a safe RAID driver ...
7123 */
7124 mdelay(1000*1);
7125 }
7126 return NOTIFY_DONE;
7127}
7128
Adrian Bunk75c96f82005-05-05 16:16:09 -07007129static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130 .notifier_call = md_notify_reboot,
7131 .next = NULL,
7132 .priority = INT_MAX, /* before any real devices */
7133};
7134
7135static void md_geninit(void)
7136{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7138
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07007139 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140}
7141
Adrian Bunk75c96f82005-05-05 16:16:09 -07007142static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007144 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145 return -1;
7146 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007147 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148 return -1;
7149 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007150 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007151 md_probe, NULL, NULL);
7152 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153 md_probe, NULL, NULL);
7154
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007156 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157
7158 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160}
7161
7162
7163#ifndef MODULE
7164
7165/*
7166 * Searches all registered partitions for autorun RAID arrays
7167 * at boot time.
7168 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007169
7170static LIST_HEAD(all_detected_devices);
7171struct detected_devices_node {
7172 struct list_head list;
7173 dev_t dev;
7174};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007175
7176void md_autodetect_dev(dev_t dev)
7177{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007178 struct detected_devices_node *node_detected_dev;
7179
7180 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7181 if (node_detected_dev) {
7182 node_detected_dev->dev = dev;
7183 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7184 } else {
7185 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7186 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188}
7189
7190
7191static void autostart_arrays(int part)
7192{
7193 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007194 struct detected_devices_node *node_detected_dev;
7195 dev_t dev;
7196 int i_scanned, i_passed;
7197
7198 i_scanned = 0;
7199 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200
7201 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7202
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007203 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7204 i_scanned++;
7205 node_detected_dev = list_entry(all_detected_devices.next,
7206 struct detected_devices_node, list);
7207 list_del(&node_detected_dev->list);
7208 dev = node_detected_dev->dev;
7209 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007210 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211 if (IS_ERR(rdev))
7212 continue;
7213
NeilBrownb2d444d2005-11-08 21:39:31 -08007214 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007215 MD_BUG();
7216 continue;
7217 }
NeilBrownd0fae182008-03-04 14:29:31 -08007218 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007220 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007222
7223 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7224 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225
7226 autorun_devices(part);
7227}
7228
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007229#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230
7231static __exit void md_exit(void)
7232{
7233 mddev_t *mddev;
7234 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007235
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007236 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007237 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007239 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240 unregister_blkdev(mdp_major, "mdp");
7241 unregister_reboot_notifier(&md_notifier);
7242 unregister_sysctl_table(raid_table_header);
7243 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007244 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007246 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007247 }
7248}
7249
Dan Williams685784a2007-07-09 11:56:42 -07007250subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251module_exit(md_exit)
7252
NeilBrownf91de922005-11-08 21:39:36 -08007253static int get_ro(char *buffer, struct kernel_param *kp)
7254{
7255 return sprintf(buffer, "%d", start_readonly);
7256}
7257static int set_ro(const char *val, struct kernel_param *kp)
7258{
7259 char *e;
7260 int num = simple_strtoul(val, &e, 10);
7261 if (*val && (*e == '\0' || *e == '\n')) {
7262 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007263 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007264 }
7265 return -EINVAL;
7266}
7267
NeilBrown80ca3a42006-07-10 04:44:18 -07007268module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7269module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007270
NeilBrownefeb53c2009-01-09 08:31:10 +11007271module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007272
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273EXPORT_SYMBOL(register_md_personality);
7274EXPORT_SYMBOL(unregister_md_personality);
7275EXPORT_SYMBOL(md_error);
7276EXPORT_SYMBOL(md_done_sync);
7277EXPORT_SYMBOL(md_write_start);
7278EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279EXPORT_SYMBOL(md_register_thread);
7280EXPORT_SYMBOL(md_unregister_thread);
7281EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282EXPORT_SYMBOL(md_check_recovery);
7283MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007284MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007285MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007286MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);