blob: f4f5f82f9f533109fff1898e14d7e30824c62d53 [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) &&
389 !mddev->hold_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 list_del(&mddev->all_mddevs);
NeilBrownd3374822009-01-09 08:31:10 +1100391 if (mddev->gendisk) {
392 /* we did a probe so need to clean up.
393 * Call schedule_work inside the spinlock
394 * so that flush_scheduled_work() after
395 * mddev_find will succeed in waiting for the
396 * work to be done.
397 */
398 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
399 schedule_work(&mddev->del_work);
400 } else
401 kfree(mddev);
402 }
403 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406static mddev_t * mddev_find(dev_t unit)
407{
408 mddev_t *mddev, *new = NULL;
409
410 retry:
411 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
NeilBrownefeb53c2009-01-09 08:31:10 +1100413 if (unit) {
414 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
415 if (mddev->unit == unit) {
416 mddev_get(mddev);
417 spin_unlock(&all_mddevs_lock);
418 kfree(new);
419 return mddev;
420 }
421
422 if (new) {
423 list_add(&new->all_mddevs, &all_mddevs);
424 spin_unlock(&all_mddevs_lock);
425 new->hold_active = UNTIL_IOCTL;
426 return new;
427 }
428 } else if (new) {
429 /* find an unused unit number */
430 static int next_minor = 512;
431 int start = next_minor;
432 int is_free = 0;
433 int dev = 0;
434 while (!is_free) {
435 dev = MKDEV(MD_MAJOR, next_minor);
436 next_minor++;
437 if (next_minor > MINORMASK)
438 next_minor = 0;
439 if (next_minor == start) {
440 /* Oh dear, all in use. */
441 spin_unlock(&all_mddevs_lock);
442 kfree(new);
443 return NULL;
444 }
445
446 is_free = 1;
447 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
448 if (mddev->unit == dev) {
449 is_free = 0;
450 break;
451 }
452 }
453 new->unit = dev;
454 new->md_minor = MINOR(dev);
455 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 list_add(&new->all_mddevs, &all_mddevs);
457 spin_unlock(&all_mddevs_lock);
458 return new;
459 }
460 spin_unlock(&all_mddevs_lock);
461
NeilBrown9ffae0c2006-01-06 00:20:32 -0800462 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (!new)
464 return NULL;
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 new->unit = unit;
467 if (MAJOR(unit) == MD_MAJOR)
468 new->md_minor = MINOR(unit);
469 else
470 new->md_minor = MINOR(unit) >> MdpMinorShift;
471
NeilBrownc8c00a62009-08-10 12:50:52 +1000472 mutex_init(&new->open_mutex);
NeilBrowndf5b89b2006-03-27 01:18:20 -0800473 mutex_init(&new->reconfig_mutex);
NeilBrownc3d97142009-12-14 12:49:52 +1100474 mutex_init(&new->bitmap_info.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 INIT_LIST_HEAD(&new->disks);
476 INIT_LIST_HEAD(&new->all_mddevs);
477 init_timer(&new->safemode_timer);
478 atomic_set(&new->active, 1);
NeilBrownf2ea68c2008-07-21 17:05:25 +1000479 atomic_set(&new->openers, 0);
NeilBrown409c57f2009-03-31 14:39:39 +1100480 atomic_set(&new->active_io, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700481 spin_lock_init(&new->write_lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100482 atomic_set(&new->flush_pending, 0);
NeilBrown3d310eb2005-06-21 17:17:26 -0700483 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700484 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700485 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000486 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800487 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700488 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 goto retry;
491}
492
493static inline int mddev_lock(mddev_t * mddev)
494{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800495 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
Dan Williamsb522adc2009-03-31 15:00:31 +1100498static inline int mddev_is_locked(mddev_t *mddev)
499{
500 return mutex_is_locked(&mddev->reconfig_mutex);
501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503static inline int mddev_trylock(mddev_t * mddev)
504{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800505 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
508static inline void mddev_unlock(mddev_t * mddev)
509{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800510 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
NeilBrown005eca52005-08-22 13:11:08 -0700512 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
NeilBrown2989ddb2006-01-06 00:20:43 -0800515static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100517 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100519 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (rdev->desc_nr == nr)
521 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return NULL;
524}
525
526static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
527{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 mdk_rdev_t *rdev;
529
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100530 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (rdev->bdev->bd_dev == dev)
532 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return NULL;
535}
536
NeilBrownd9d166c2006-01-06 00:20:51 -0800537static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800538{
539 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800540 list_for_each_entry(pers, &pers_list, list) {
541 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800542 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800543 if (strcmp(pers->name, clevel)==0)
544 return pers;
545 }
NeilBrown2604b702006-01-06 00:20:36 -0800546 return NULL;
547}
548
Andre Nollb73df2d2008-07-11 22:02:23 +1000549/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700550static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Andre Nollb73df2d2008-07-11 22:02:23 +1000552 sector_t num_sectors = bdev->bd_inode->i_size / 512;
553 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556static int alloc_disk_sb(mdk_rdev_t * rdev)
557{
558 if (rdev->sb_page)
559 MD_BUG();
560
561 rdev->sb_page = alloc_page(GFP_KERNEL);
562 if (!rdev->sb_page) {
563 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000564 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566
567 return 0;
568}
569
570static void free_disk_sb(mdk_rdev_t * rdev)
571{
572 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800573 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 rdev->sb_loaded = 0;
575 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000576 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100577 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579}
580
581
NeilBrown6712ecf2007-09-27 12:47:43 +0200582static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700583{
584 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800585 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700586
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700587 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
588 printk("md: super_written gets error=%d, uptodate=%d\n",
589 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
590 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800591 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700592 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700593
NeilBrowna9701a32005-11-08 21:39:34 -0800594 if (atomic_dec_and_test(&mddev->pending_writes))
595 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700596 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700597}
598
NeilBrown6712ecf2007-09-27 12:47:43 +0200599static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800600{
601 struct bio *bio2 = bio->bi_private;
602 mdk_rdev_t *rdev = bio2->bi_private;
603 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800604
605 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
606 error == -EOPNOTSUPP) {
607 unsigned long flags;
608 /* barriers don't appear to be supported :-( */
609 set_bit(BarriersNotsupp, &rdev->flags);
610 mddev->barriers_work = 0;
611 spin_lock_irqsave(&mddev->write_lock, flags);
612 bio2->bi_next = mddev->biolist;
613 mddev->biolist = bio2;
614 spin_unlock_irqrestore(&mddev->write_lock, flags);
615 wake_up(&mddev->sb_wait);
616 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200617 } else {
618 bio_put(bio2);
619 bio->bi_private = rdev;
620 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800621 }
NeilBrowna9701a32005-11-08 21:39:34 -0800622}
623
NeilBrown7bfa19f2005-06-21 17:17:28 -0700624void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
625 sector_t sector, int size, struct page *page)
626{
627 /* write first size bytes of page to sector of rdev
628 * Increment mddev->pending_writes before returning
629 * and decrement it on completion, waking up sb_wait
630 * if zero is reached.
631 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800632 *
633 * As we might need to resubmit the request if BIO_RW_BARRIER
634 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700635 */
636 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Jens Axboe93dbb392009-02-16 10:25:40 +0100637 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700638
639 bio->bi_bdev = rdev->bdev;
640 bio->bi_sector = sector;
641 bio_add_page(bio, page, size, 0);
642 bio->bi_private = rdev;
643 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800644 bio->bi_rw = rw;
645
NeilBrown7bfa19f2005-06-21 17:17:28 -0700646 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800647 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
648 struct bio *rbio;
649 rw |= (1<<BIO_RW_BARRIER);
650 rbio = bio_clone(bio, GFP_NOIO);
651 rbio->bi_private = bio;
652 rbio->bi_end_io = super_written_barrier;
653 submit_bio(rw, rbio);
654 } else
655 submit_bio(rw, bio);
656}
657
658void md_super_wait(mddev_t *mddev)
659{
660 /* wait for all superblock writes that were scheduled to complete.
661 * if any had to be retried (due to BARRIER problems), retry them
662 */
663 DEFINE_WAIT(wq);
664 for(;;) {
665 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
666 if (atomic_read(&mddev->pending_writes)==0)
667 break;
668 while (mddev->biolist) {
669 struct bio *bio;
670 spin_lock_irq(&mddev->write_lock);
671 bio = mddev->biolist;
672 mddev->biolist = bio->bi_next ;
673 bio->bi_next = NULL;
674 spin_unlock_irq(&mddev->write_lock);
675 submit_bio(bio->bi_rw, bio);
676 }
677 schedule();
678 }
679 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700680}
681
NeilBrown6712ecf2007-09-27 12:47:43 +0200682static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
NeilBrowna654b9d82005-06-21 17:17:27 -0700687int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 struct page *page, int rw)
689{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700690 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 struct completion event;
692 int ret;
693
Jens Axboe93dbb392009-02-16 10:25:40 +0100694 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 bio->bi_bdev = bdev;
697 bio->bi_sector = sector;
698 bio_add_page(bio, page, size, 0);
699 init_completion(&event);
700 bio->bi_private = &event;
701 bio->bi_end_io = bi_complete;
702 submit_bio(rw, bio);
703 wait_for_completion(&event);
704
705 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
706 bio_put(bio);
707 return ret;
708}
NeilBrowna8745db2006-01-06 00:20:34 -0800709EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
NeilBrown0002b272005-09-09 16:23:53 -0700711static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 char b[BDEVNAME_SIZE];
714 if (!rdev->sb_page) {
715 MD_BUG();
716 return -EINVAL;
717 }
718 if (rdev->sb_loaded)
719 return 0;
720
721
Andre Noll0f420352008-07-11 22:02:23 +1000722 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 goto fail;
724 rdev->sb_loaded = 1;
725 return 0;
726
727fail:
728 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
729 bdevname(rdev->bdev,b));
730 return -EINVAL;
731}
732
733static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
734{
Andre Noll05710462008-07-11 22:02:20 +1000735 return sb1->set_uuid0 == sb2->set_uuid0 &&
736 sb1->set_uuid1 == sb2->set_uuid1 &&
737 sb1->set_uuid2 == sb2->set_uuid2 &&
738 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
742{
743 int ret;
744 mdp_super_t *tmp1, *tmp2;
745
746 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
747 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
748
749 if (!tmp1 || !tmp2) {
750 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100751 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 goto abort;
753 }
754
755 *tmp1 = *sb1;
756 *tmp2 = *sb2;
757
758 /*
759 * nr_disks is not constant
760 */
761 tmp1->nr_disks = 0;
762 tmp2->nr_disks = 0;
763
Andre Nollce0c8e02008-07-11 22:02:20 +1000764 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700766 kfree(tmp1);
767 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return ret;
769}
770
NeilBrown4d167f02007-05-09 02:35:37 -0700771
772static u32 md_csum_fold(u32 csum)
773{
774 csum = (csum & 0xffff) + (csum >> 16);
775 return (csum & 0xffff) + (csum >> 16);
776}
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778static unsigned int calc_sb_csum(mdp_super_t * sb)
779{
NeilBrown4d167f02007-05-09 02:35:37 -0700780 u64 newcsum = 0;
781 u32 *sb32 = (u32*)sb;
782 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 unsigned int disk_csum, csum;
784
785 disk_csum = sb->sb_csum;
786 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700787
788 for (i = 0; i < MD_SB_BYTES/4 ; i++)
789 newcsum += sb32[i];
790 csum = (newcsum & 0xffffffff) + (newcsum>>32);
791
792
793#ifdef CONFIG_ALPHA
794 /* This used to use csum_partial, which was wrong for several
795 * reasons including that different results are returned on
796 * different architectures. It isn't critical that we get exactly
797 * the same return value as before (we always csum_fold before
798 * testing, and that removes any differences). However as we
799 * know that csum_partial always returned a 16bit value on
800 * alphas, do a fold to maximise conformity to previous behaviour.
801 */
802 sb->sb_csum = md_csum_fold(disk_csum);
803#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700805#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return csum;
807}
808
809
810/*
811 * Handle superblock details.
812 * We want to be able to handle multiple superblock formats
813 * so we have a common interface to them all, and an array of
814 * different handlers.
815 * We rely on user-space to write the initial superblock, and support
816 * reading and updating of superblocks.
817 * Interface methods are:
818 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
819 * loads and validates a superblock on dev.
820 * if refdev != NULL, compare superblocks on both devices
821 * Return:
822 * 0 - dev has a superblock that is compatible with refdev
823 * 1 - dev has a superblock that is compatible and newer than refdev
824 * so dev should be used as the refdev in future
825 * -EINVAL superblock incompatible or invalid
826 * -othererror e.g. -EIO
827 *
828 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
829 * Verify that dev is acceptable into mddev.
830 * The first time, mddev->raid_disks will be 0, and data from
831 * dev should be merged in. Subsequent calls check that dev
832 * is new enough. Return 0 or -EINVAL
833 *
834 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
835 * Update the superblock for rdev with data in mddev
836 * This does not write to disc.
837 *
838 */
839
840struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000841 char *name;
842 struct module *owner;
843 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
844 int minor_version);
845 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
846 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
847 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000848 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849};
850
851/*
Andre Noll0894cc32009-06-18 08:49:23 +1000852 * Check that the given mddev has no bitmap.
853 *
854 * This function is called from the run method of all personalities that do not
855 * support bitmaps. It prints an error message and returns non-zero if mddev
856 * has a bitmap. Otherwise, it returns 0.
857 *
858 */
859int md_check_no_bitmap(mddev_t *mddev)
860{
NeilBrownc3d97142009-12-14 12:49:52 +1100861 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000862 return 0;
863 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
864 mdname(mddev), mddev->pers->name);
865 return 1;
866}
867EXPORT_SYMBOL(md_check_no_bitmap);
868
869/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 * load_super for 0.90.0
871 */
872static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
873{
874 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
875 mdp_super_t *sb;
876 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 /*
Andre Noll0f420352008-07-11 22:02:23 +1000879 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 * it's at the end of the disk.
881 *
882 * It also happens to be a multiple of 4Kb.
883 */
Andre Noll0f420352008-07-11 22:02:23 +1000884 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
NeilBrown0002b272005-09-09 16:23:53 -0700886 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (ret) return ret;
888
889 ret = -EINVAL;
890
891 bdevname(rdev->bdev, b);
892 sb = (mdp_super_t*)page_address(rdev->sb_page);
893
894 if (sb->md_magic != MD_SB_MAGIC) {
895 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
896 b);
897 goto abort;
898 }
899
900 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800901 sb->minor_version < 90 ||
902 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
904 sb->major_version, sb->minor_version,
905 b);
906 goto abort;
907 }
908
909 if (sb->raid_disks <= 0)
910 goto abort;
911
NeilBrown4d167f02007-05-09 02:35:37 -0700912 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
914 b);
915 goto abort;
916 }
917
918 rdev->preferred_minor = sb->md_minor;
919 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700920 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 if (sb->level == LEVEL_MULTIPATH)
923 rdev->desc_nr = -1;
924 else
925 rdev->desc_nr = sb->this_disk.number;
926
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700927 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700929 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 __u64 ev1, ev2;
931 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
932 if (!uuid_equal(refsb, sb)) {
933 printk(KERN_WARNING "md: %s has different UUID to %s\n",
934 b, bdevname(refdev->bdev,b2));
935 goto abort;
936 }
937 if (!sb_equal(refsb, sb)) {
938 printk(KERN_WARNING "md: %s has same UUID"
939 " but different superblock to %s\n",
940 b, bdevname(refdev->bdev, b2));
941 goto abort;
942 }
943 ev1 = md_event(sb);
944 ev2 = md_event(refsb);
945 if (ev1 > ev2)
946 ret = 1;
947 else
948 ret = 0;
949 }
NeilBrown8190e752009-06-18 08:48:58 +1000950 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Andre Nolldd8ac332009-03-31 14:33:13 +1100952 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -0800953 /* "this cannot possibly happen" ... */
954 ret = -EINVAL;
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 abort:
957 return ret;
958}
959
960/*
961 * validate_super for 0.90.0
962 */
963static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
964{
965 mdp_disk_t *desc;
966 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700967 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
NeilBrown41158c72005-06-21 17:17:25 -0700969 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800970 clear_bit(Faulty, &rdev->flags);
971 clear_bit(In_sync, &rdev->flags);
972 clear_bit(WriteMostly, &rdev->flags);
973 clear_bit(BarriersNotsupp, &rdev->flags);
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (mddev->raid_disks == 0) {
976 mddev->major_version = 0;
977 mddev->minor_version = sb->minor_version;
978 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800979 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +1000980 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 mddev->ctime = sb->ctime;
982 mddev->utime = sb->utime;
983 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800984 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 mddev->layout = sb->layout;
986 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +1100987 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -0700988 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +1100989 mddev->bitmap_info.offset = 0;
990 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
NeilBrownf6705572006-03-27 01:18:11 -0800992 if (mddev->minor_version >= 91) {
993 mddev->reshape_position = sb->reshape_position;
994 mddev->delta_disks = sb->delta_disks;
995 mddev->new_level = sb->new_level;
996 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +1000997 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -0800998 } else {
999 mddev->reshape_position = MaxSector;
1000 mddev->delta_disks = 0;
1001 mddev->new_level = mddev->level;
1002 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001003 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001004 }
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (sb->state & (1<<MD_SB_CLEAN))
1007 mddev->recovery_cp = MaxSector;
1008 else {
1009 if (sb->events_hi == sb->cp_events_hi &&
1010 sb->events_lo == sb->cp_events_lo) {
1011 mddev->recovery_cp = sb->recovery_cp;
1012 } else
1013 mddev->recovery_cp = 0;
1014 }
1015
1016 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1017 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1018 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1019 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1020
1021 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001022
1023 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001024 mddev->bitmap_info.file == NULL)
1025 mddev->bitmap_info.offset =
1026 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001027
NeilBrown41158c72005-06-21 17:17:25 -07001028 } else if (mddev->pers == NULL) {
1029 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 ++ev1;
1031 if (ev1 < mddev->events)
1032 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001033 } else if (mddev->bitmap) {
1034 /* if adding to array with a bitmap, then we can accept an
1035 * older device ... but not too old.
1036 */
NeilBrown41158c72005-06-21 17:17:25 -07001037 if (ev1 < mddev->bitmap->events_cleared)
1038 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001039 } else {
1040 if (ev1 < mddev->events)
1041 /* just a hot-add of a new device, leave raid_disk at -1 */
1042 return 0;
1043 }
NeilBrown41158c72005-06-21 17:17:25 -07001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 desc = sb->disks + rdev->desc_nr;
1047
1048 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001049 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001050 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1051 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001052 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd92009-11-13 17:40:48 +11001054 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1055 /* active but not in sync implies recovery up to
1056 * reshape position. We don't know exactly where
1057 * that is, so set to zero for now */
1058 if (mddev->minor_version >= 91) {
1059 rdev->recovery_offset = 0;
1060 rdev->raid_disk = desc->raid_disk;
1061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001063 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1064 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001065 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001066 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return 0;
1068}
1069
1070/*
1071 * sync_super for 0.90.0
1072 */
1073static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1074{
1075 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 mdk_rdev_t *rdev2;
1077 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
1080 /* make rdev->sb match mddev data..
1081 *
1082 * 1/ zero out disks
1083 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1084 * 3/ any empty disks < next_spare become removed
1085 *
1086 * disks[0] gets initialised to REMOVED because
1087 * we cannot be sure from other fields if it has
1088 * been initialised or not.
1089 */
1090 int i;
1091 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1092
NeilBrown61181562005-09-09 16:24:02 -07001093 rdev->sb_size = MD_SB_BYTES;
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 sb = (mdp_super_t*)page_address(rdev->sb_page);
1096
1097 memset(sb, 0, sizeof(*sb));
1098
1099 sb->md_magic = MD_SB_MAGIC;
1100 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 sb->patch_version = mddev->patch_version;
1102 sb->gvalid_words = 0; /* ignored */
1103 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1104 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1105 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1106 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1107
1108 sb->ctime = mddev->ctime;
1109 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001110 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 sb->raid_disks = mddev->raid_disks;
1112 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001113 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 sb->utime = mddev->utime;
1115 sb->state = 0;
1116 sb->events_hi = (mddev->events>>32);
1117 sb->events_lo = (u32)mddev->events;
1118
NeilBrownf6705572006-03-27 01:18:11 -08001119 if (mddev->reshape_position == MaxSector)
1120 sb->minor_version = 90;
1121 else {
1122 sb->minor_version = 91;
1123 sb->reshape_position = mddev->reshape_position;
1124 sb->new_level = mddev->new_level;
1125 sb->delta_disks = mddev->delta_disks;
1126 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001127 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001128 }
1129 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (mddev->in_sync)
1131 {
1132 sb->recovery_cp = mddev->recovery_cp;
1133 sb->cp_events_hi = (mddev->events>>32);
1134 sb->cp_events_lo = (u32)mddev->events;
1135 if (mddev->recovery_cp == MaxSector)
1136 sb->state = (1<< MD_SB_CLEAN);
1137 } else
1138 sb->recovery_cp = 0;
1139
1140 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001141 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
NeilBrownc3d97142009-12-14 12:49:52 +11001143 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001144 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001147 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001149 int desc_nr;
NeilBrown0261cd92009-11-13 17:40:48 +11001150 int is_active = test_bit(In_sync, &rdev2->flags);
1151
1152 if (rdev2->raid_disk >= 0 &&
1153 sb->minor_version >= 91)
1154 /* we have nowhere to store the recovery_offset,
1155 * but if it is not below the reshape_position,
1156 * we can piggy-back on that.
1157 */
1158 is_active = 1;
1159 if (rdev2->raid_disk < 0 ||
1160 test_bit(Faulty, &rdev2->flags))
1161 is_active = 0;
1162 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001163 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001165 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001166 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 d = &sb->disks[rdev2->desc_nr];
1168 nr_disks++;
1169 d->number = rdev2->desc_nr;
1170 d->major = MAJOR(rdev2->bdev->bd_dev);
1171 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd92009-11-13 17:40:48 +11001172 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 d->raid_disk = rdev2->raid_disk;
1174 else
1175 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001176 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd92009-11-13 17:40:48 +11001178 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd92009-11-13 17:40:48 +11001180 if (test_bit(In_sync, &rdev2->flags))
1181 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 active++;
1183 working++;
1184 } else {
1185 d->state = 0;
1186 spare++;
1187 working++;
1188 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001189 if (test_bit(WriteMostly, &rdev2->flags))
1190 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 /* now set the "removed" and "faulty" bits on any missing devices */
1193 for (i=0 ; i < mddev->raid_disks ; i++) {
1194 mdp_disk_t *d = &sb->disks[i];
1195 if (d->state == 0 && d->number == 0) {
1196 d->number = i;
1197 d->raid_disk = i;
1198 d->state = (1<<MD_DISK_REMOVED);
1199 d->state |= (1<<MD_DISK_FAULTY);
1200 failed++;
1201 }
1202 }
1203 sb->nr_disks = nr_disks;
1204 sb->active_disks = active;
1205 sb->working_disks = working;
1206 sb->failed_disks = failed;
1207 sb->spare_disks = spare;
1208
1209 sb->this_disk = sb->disks[rdev->desc_nr];
1210 sb->sb_csum = calc_sb_csum(sb);
1211}
1212
1213/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001214 * rdev_size_change for 0.90.0
1215 */
1216static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001217super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001218{
Andre Noll58c0fed2009-03-31 14:33:13 +11001219 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001220 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001221 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001222 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001223 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001224 if (!num_sectors || num_sectors > rdev->sb_start)
1225 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001226 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001227 rdev->sb_page);
1228 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001229 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001230}
1231
1232
1233/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 * version 1 superblock
1235 */
1236
NeilBrown1c05b4b2006-10-21 10:24:08 -07001237static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001239 __le32 disk_csum;
1240 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 unsigned long long newcsum;
1242 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001243 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 int i;
1245
1246 disk_csum = sb->sb_csum;
1247 sb->sb_csum = 0;
1248 newcsum = 0;
1249 for (i=0; size>=4; size -= 4 )
1250 newcsum += le32_to_cpu(*isuper++);
1251
1252 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001253 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1256 sb->sb_csum = disk_csum;
1257 return cpu_to_le32(csum);
1258}
1259
1260static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1261{
1262 struct mdp_superblock_1 *sb;
1263 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001264 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001266 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 /*
Andre Noll0f420352008-07-11 22:02:23 +10001269 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 * It is always aligned to a 4K boundary and
1271 * depeding on minor_version, it can be:
1272 * 0: At least 8K, but less than 12K, from end of device
1273 * 1: At start of device
1274 * 2: 4K from start of device.
1275 */
1276 switch(minor_version) {
1277 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001278 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1279 sb_start -= 8*2;
1280 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 break;
1282 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001283 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 break;
1285 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001286 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 break;
1288 default:
1289 return -EINVAL;
1290 }
Andre Noll0f420352008-07-11 22:02:23 +10001291 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
NeilBrown0002b272005-09-09 16:23:53 -07001293 /* superblock is rarely larger than 1K, but it can be larger,
1294 * and it is safe to read 4k, so we do that
1295 */
1296 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 if (ret) return ret;
1298
1299
1300 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1301
1302 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1303 sb->major_version != cpu_to_le32(1) ||
1304 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001305 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001306 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return -EINVAL;
1308
1309 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1310 printk("md: invalid superblock checksum on %s\n",
1311 bdevname(rdev->bdev,b));
1312 return -EINVAL;
1313 }
1314 if (le64_to_cpu(sb->data_size) < 10) {
1315 printk("md: data_size too small on %s\n",
1316 bdevname(rdev->bdev,b));
1317 return -EINVAL;
1318 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 rdev->preferred_minor = 0xffff;
1321 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001322 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
NeilBrown0002b272005-09-09 16:23:53 -07001324 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001325 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001326 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001327 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1328
1329 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001330 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001331 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001332
NeilBrown31b65a02006-07-10 04:44:14 -07001333 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1334 rdev->desc_nr = -1;
1335 else
1336 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1337
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001338 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001339 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001340 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 __u64 ev1, ev2;
1342 struct mdp_superblock_1 *refsb =
1343 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1344
1345 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1346 sb->level != refsb->level ||
1347 sb->layout != refsb->layout ||
1348 sb->chunksize != refsb->chunksize) {
1349 printk(KERN_WARNING "md: %s has strangely different"
1350 " superblock to %s\n",
1351 bdevname(rdev->bdev,b),
1352 bdevname(refdev->bdev,b2));
1353 return -EINVAL;
1354 }
1355 ev1 = le64_to_cpu(sb->events);
1356 ev2 = le64_to_cpu(refsb->events);
1357
1358 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001359 ret = 1;
1360 else
1361 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
NeilBrowna1801f82008-03-04 14:29:31 -08001363 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001364 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1365 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001367 rdev->sectors = rdev->sb_start;
1368 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001370 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001371 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001372 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001373 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
1376static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1377{
1378 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001379 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
NeilBrown41158c72005-06-21 17:17:25 -07001381 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001382 clear_bit(Faulty, &rdev->flags);
1383 clear_bit(In_sync, &rdev->flags);
1384 clear_bit(WriteMostly, &rdev->flags);
1385 clear_bit(BarriersNotsupp, &rdev->flags);
1386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 if (mddev->raid_disks == 0) {
1388 mddev->major_version = 1;
1389 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001390 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001391 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1393 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1394 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001395 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 mddev->layout = le32_to_cpu(sb->layout);
1397 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001398 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001399 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001400 mddev->bitmap_info.offset = 0;
1401 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1404 memcpy(mddev->uuid, sb->set_uuid, 16);
1405
1406 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001407
NeilBrown71c08052005-09-09 16:23:51 -07001408 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001409 mddev->bitmap_info.file == NULL )
1410 mddev->bitmap_info.offset =
1411 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001412
NeilBrownf6705572006-03-27 01:18:11 -08001413 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1414 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1415 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1416 mddev->new_level = le32_to_cpu(sb->new_level);
1417 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001418 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001419 } else {
1420 mddev->reshape_position = MaxSector;
1421 mddev->delta_disks = 0;
1422 mddev->new_level = mddev->level;
1423 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001424 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001425 }
1426
NeilBrown41158c72005-06-21 17:17:25 -07001427 } else if (mddev->pers == NULL) {
1428 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 ++ev1;
1430 if (ev1 < mddev->events)
1431 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001432 } else if (mddev->bitmap) {
1433 /* If adding to array with a bitmap, then we can accept an
1434 * older device, but not too old.
1435 */
NeilBrown41158c72005-06-21 17:17:25 -07001436 if (ev1 < mddev->bitmap->events_cleared)
1437 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001438 } else {
1439 if (ev1 < mddev->events)
1440 /* just a hot-add of a new device, leave raid_disk at -1 */
1441 return 0;
1442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 if (mddev->level != LEVEL_MULTIPATH) {
1444 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001445 if (rdev->desc_nr < 0 ||
1446 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1447 role = 0xffff;
1448 rdev->desc_nr = -1;
1449 } else
1450 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 switch(role) {
1452 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 break;
1454 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001455 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 break;
1457 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001458 if ((le32_to_cpu(sb->feature_map) &
1459 MD_FEATURE_RECOVERY_OFFSET))
1460 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1461 else
1462 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 rdev->raid_disk = role;
1464 break;
1465 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001466 if (sb->devflags & WriteMostly1)
1467 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001468 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001469 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 return 0;
1472}
1473
1474static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1475{
1476 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 mdk_rdev_t *rdev2;
1478 int max_dev, i;
1479 /* make rdev->sb match mddev and rdev data. */
1480
1481 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1482
1483 sb->feature_map = 0;
1484 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001485 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 memset(sb->pad1, 0, sizeof(sb->pad1));
1487 memset(sb->pad2, 0, sizeof(sb->pad2));
1488 memset(sb->pad3, 0, sizeof(sb->pad3));
1489
1490 sb->utime = cpu_to_le64((__u64)mddev->utime);
1491 sb->events = cpu_to_le64(mddev->events);
1492 if (mddev->in_sync)
1493 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1494 else
1495 sb->resync_offset = cpu_to_le64(0);
1496
NeilBrown1c05b4b2006-10-21 10:24:08 -07001497 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001498
NeilBrownf0ca3402006-02-02 14:28:04 -08001499 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001500 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001501 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001502 sb->level = cpu_to_le32(mddev->level);
1503 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001504
NeilBrownc3d97142009-12-14 12:49:52 +11001505 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1506 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001507 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001508 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001509
1510 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001511 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001512 sb->feature_map |=
1513 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1514 sb->recovery_offset =
1515 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001516 }
1517
NeilBrownf6705572006-03-27 01:18:11 -08001518 if (mddev->reshape_position != MaxSector) {
1519 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1520 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1521 sb->new_layout = cpu_to_le32(mddev->new_layout);
1522 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1523 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001524 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001525 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001528 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if (rdev2->desc_nr+1 > max_dev)
1530 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001531
NeilBrown70471da2009-08-03 10:59:57 +10001532 if (max_dev > le32_to_cpu(sb->max_dev)) {
1533 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001534 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001535 rdev->sb_size = max_dev * 2 + 256;
1536 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1537 if (rdev->sb_size & bmask)
1538 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 for (i=0; i<max_dev;i++)
1541 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1542
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001543 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001545 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001547 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001549 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001550 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 else
1552 sb->dev_roles[i] = cpu_to_le16(0xffff);
1553 }
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 sb->sb_csum = calc_sb_1_csum(sb);
1556}
1557
Chris Webb0cd17fe2008-06-28 08:31:46 +10001558static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001559super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001560{
1561 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001562 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001563 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001564 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001565 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001566 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001567 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1568 max_sectors -= rdev->data_offset;
1569 if (!num_sectors || num_sectors > max_sectors)
1570 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001571 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001572 /* minor version 0 with bitmap we can't move */
1573 return 0;
1574 } else {
1575 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001576 sector_t sb_start;
1577 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1578 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001579 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001580 if (!num_sectors || num_sectors > max_sectors)
1581 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001582 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001583 }
1584 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001585 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001586 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001587 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001588 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001589 rdev->sb_page);
1590 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001591 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001592}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Adrian Bunk75c96f82005-05-05 16:16:09 -07001594static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 [0] = {
1596 .name = "0.90.0",
1597 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001598 .load_super = super_90_load,
1599 .validate_super = super_90_validate,
1600 .sync_super = super_90_sync,
1601 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 },
1603 [1] = {
1604 .name = "md-1",
1605 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001606 .load_super = super_1_load,
1607 .validate_super = super_1_validate,
1608 .sync_super = super_1_sync,
1609 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 },
1611};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1614{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001615 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
NeilBrown4b809912008-07-21 17:05:25 +10001617 rcu_read_lock();
1618 rdev_for_each_rcu(rdev, mddev1)
1619 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001620 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001621 rdev2->bdev->bd_contains) {
1622 rcu_read_unlock();
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001623 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001624 }
1625 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return 0;
1627}
1628
1629static LIST_HEAD(pending_raid_disks);
1630
Andre Nollac5e7112009-08-03 10:59:47 +10001631/*
1632 * Try to register data integrity profile for an mddev
1633 *
1634 * This is called when an array is started and after a disk has been kicked
1635 * from the array. It only succeeds if all working and active component devices
1636 * are integrity capable with matching profiles.
1637 */
1638int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001639{
Andre Nollac5e7112009-08-03 10:59:47 +10001640 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001641
Andre Nollac5e7112009-08-03 10:59:47 +10001642 if (list_empty(&mddev->disks))
1643 return 0; /* nothing to do */
1644 if (blk_get_integrity(mddev->gendisk))
1645 return 0; /* already registered */
1646 list_for_each_entry(rdev, &mddev->disks, same_set) {
1647 /* skip spares and non-functional disks */
1648 if (test_bit(Faulty, &rdev->flags))
1649 continue;
1650 if (rdev->raid_disk < 0)
1651 continue;
1652 /*
1653 * If at least one rdev is not integrity capable, we can not
1654 * enable data integrity for the md device.
1655 */
1656 if (!bdev_get_integrity(rdev->bdev))
1657 return -EINVAL;
1658 if (!reference) {
1659 /* Use the first rdev as the reference */
1660 reference = rdev;
1661 continue;
1662 }
1663 /* does this rdev's profile match the reference profile? */
1664 if (blk_integrity_compare(reference->bdev->bd_disk,
1665 rdev->bdev->bd_disk) < 0)
1666 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001667 }
Andre Nollac5e7112009-08-03 10:59:47 +10001668 /*
1669 * All component devices are integrity capable and have matching
1670 * profiles, register the common profile for the md device.
1671 */
1672 if (blk_integrity_register(mddev->gendisk,
1673 bdev_get_integrity(reference->bdev)) != 0) {
1674 printk(KERN_ERR "md: failed to register integrity for %s\n",
1675 mdname(mddev));
1676 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001677 }
Andre Nollac5e7112009-08-03 10:59:47 +10001678 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1679 mdname(mddev));
1680 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001681}
Andre Nollac5e7112009-08-03 10:59:47 +10001682EXPORT_SYMBOL(md_integrity_register);
1683
1684/* Disable data integrity if non-capable/non-matching disk is being added */
1685void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1686{
1687 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1688 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1689
1690 if (!bi_mddev) /* nothing to do */
1691 return;
1692 if (rdev->raid_disk < 0) /* skip spares */
1693 return;
1694 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1695 rdev->bdev->bd_disk) >= 0)
1696 return;
1697 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1698 blk_integrity_unregister(mddev->gendisk);
1699}
1700EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1703{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001704 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001705 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001706 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001707 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709 if (rdev->mddev) {
1710 MD_BUG();
1711 return -EINVAL;
1712 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001713
1714 /* prevent duplicates */
1715 if (find_rdev(mddev, rdev->bdev->bd_dev))
1716 return -EEXIST;
1717
Andre Nolldd8ac332009-03-31 14:33:13 +11001718 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1719 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1720 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001721 if (mddev->pers) {
1722 /* Cannot change size, so fail
1723 * If mddev->level <= 0, then we don't care
1724 * about aligning sizes (e.g. linear)
1725 */
1726 if (mddev->level > 0)
1727 return -ENOSPC;
1728 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001729 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 /* Verify rdev->desc_nr is unique.
1733 * If it is -1, assign a free number, else
1734 * check number is not in use
1735 */
1736 if (rdev->desc_nr < 0) {
1737 int choice = 0;
1738 if (mddev->pers) choice = mddev->raid_disks;
1739 while (find_rdev_nr(mddev, choice))
1740 choice++;
1741 rdev->desc_nr = choice;
1742 } else {
1743 if (find_rdev_nr(mddev, rdev->desc_nr))
1744 return -EBUSY;
1745 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001746 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1747 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1748 mdname(mddev), mddev->max_disks);
1749 return -EBUSY;
1750 }
NeilBrown19133a42005-11-08 21:39:35 -08001751 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001752 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001753 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001756 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001757
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001758 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001759 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001760
Tejun Heo0762b8b2008-08-25 19:56:12 +09001761 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001762 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1763 kobject_del(&rdev->kobj);
1764 goto fail;
1765 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001766 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1767
NeilBrown4b809912008-07-21 17:05:25 +10001768 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001769 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001770
1771 /* May as well allow recovery to be retried once */
1772 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001775
1776 fail:
1777 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1778 b, mdname(mddev));
1779 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
NeilBrown177a99b2008-02-06 01:39:56 -08001782static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001783{
1784 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1785 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001786 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001787}
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1790{
1791 char b[BDEVNAME_SIZE];
1792 if (!rdev->mddev) {
1793 MD_BUG();
1794 return;
1795 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001796 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001797 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1799 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001800 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001801 sysfs_put(rdev->sysfs_state);
1802 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001803 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001804 * writing to 'remove' to "dev/state". We also need
1805 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001806 */
NeilBrown4b809912008-07-21 17:05:25 +10001807 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001808 INIT_WORK(&rdev->del_work, md_delayed_delete);
1809 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001810 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
1813/*
1814 * prevent the device from being mounted, repartitioned or
1815 * otherwise reused by a RAID array (or any other kernel
1816 * subsystem), by bd_claiming the device.
1817 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001818static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
1820 int err = 0;
1821 struct block_device *bdev;
1822 char b[BDEVNAME_SIZE];
1823
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001824 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (IS_ERR(bdev)) {
1826 printk(KERN_ERR "md: could not open %s.\n",
1827 __bdevname(dev, b));
1828 return PTR_ERR(bdev);
1829 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001830 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (err) {
1832 printk(KERN_ERR "md: could not bd_claim %s.\n",
1833 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001834 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return err;
1836 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001837 if (!shared)
1838 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 rdev->bdev = bdev;
1840 return err;
1841}
1842
1843static void unlock_rdev(mdk_rdev_t *rdev)
1844{
1845 struct block_device *bdev = rdev->bdev;
1846 rdev->bdev = NULL;
1847 if (!bdev)
1848 MD_BUG();
1849 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001850 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
1853void md_autodetect_dev(dev_t dev);
1854
1855static void export_rdev(mdk_rdev_t * rdev)
1856{
1857 char b[BDEVNAME_SIZE];
1858 printk(KERN_INFO "md: export_rdev(%s)\n",
1859 bdevname(rdev->bdev,b));
1860 if (rdev->mddev)
1861 MD_BUG();
1862 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001864 if (test_bit(AutoDetected, &rdev->flags))
1865 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866#endif
1867 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001868 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869}
1870
1871static void kick_rdev_from_array(mdk_rdev_t * rdev)
1872{
1873 unbind_rdev_from_array(rdev);
1874 export_rdev(rdev);
1875}
1876
1877static void export_array(mddev_t *mddev)
1878{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001879 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
NeilBrownd089c6a2008-02-06 01:39:59 -08001881 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (!rdev->mddev) {
1883 MD_BUG();
1884 continue;
1885 }
1886 kick_rdev_from_array(rdev);
1887 }
1888 if (!list_empty(&mddev->disks))
1889 MD_BUG();
1890 mddev->raid_disks = 0;
1891 mddev->major_version = 0;
1892}
1893
1894static void print_desc(mdp_disk_t *desc)
1895{
1896 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1897 desc->major,desc->minor,desc->raid_disk,desc->state);
1898}
1899
Cheng Renquancd2ac932009-01-09 08:31:08 +11001900static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
1902 int i;
1903
1904 printk(KERN_INFO
1905 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1906 sb->major_version, sb->minor_version, sb->patch_version,
1907 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1908 sb->ctime);
1909 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1910 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1911 sb->md_minor, sb->layout, sb->chunk_size);
1912 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1913 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1914 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1915 sb->failed_disks, sb->spare_disks,
1916 sb->sb_csum, (unsigned long)sb->events_lo);
1917
1918 printk(KERN_INFO);
1919 for (i = 0; i < MD_SB_DISKS; i++) {
1920 mdp_disk_t *desc;
1921
1922 desc = sb->disks + i;
1923 if (desc->number || desc->major || desc->minor ||
1924 desc->raid_disk || (desc->state && (desc->state != 4))) {
1925 printk(" D %2d: ", i);
1926 print_desc(desc);
1927 }
1928 }
1929 printk(KERN_INFO "md: THIS: ");
1930 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
Cheng Renquancd2ac932009-01-09 08:31:08 +11001933static void print_sb_1(struct mdp_superblock_1 *sb)
1934{
1935 __u8 *uuid;
1936
1937 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001938 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08001939 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001940 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001941 le32_to_cpu(sb->major_version),
1942 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08001943 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11001944 sb->set_name,
1945 (unsigned long long)le64_to_cpu(sb->ctime)
1946 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1947
1948 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001949 printk(KERN_INFO
1950 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11001951 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08001952 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001953 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1954 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001955 le32_to_cpu(sb->level),
1956 (unsigned long long)le64_to_cpu(sb->size),
1957 le32_to_cpu(sb->raid_disks),
1958 le32_to_cpu(sb->layout),
1959 le32_to_cpu(sb->chunksize),
1960 (unsigned long long)le64_to_cpu(sb->data_offset),
1961 (unsigned long long)le64_to_cpu(sb->data_size),
1962 (unsigned long long)le64_to_cpu(sb->super_offset),
1963 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1964 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08001965 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11001966 sb->devflags,
1967 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1968 (unsigned long long)le64_to_cpu(sb->events),
1969 (unsigned long long)le64_to_cpu(sb->resync_offset),
1970 le32_to_cpu(sb->sb_csum),
1971 le32_to_cpu(sb->max_dev)
1972 );
1973}
1974
1975static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11001978 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1979 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08001980 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1981 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11001983 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1984 switch (major_version) {
1985 case 0:
1986 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1987 break;
1988 case 1:
1989 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1990 break;
1991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 } else
1993 printk(KERN_INFO "md: no rdev superblock!\n");
1994}
1995
Adrian Bunk5e563412006-06-26 00:27:42 -07001996static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001998 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 mdk_rdev_t *rdev;
2000 mddev_t *mddev;
2001 char b[BDEVNAME_SIZE];
2002
2003 printk("\n");
2004 printk("md: **********************************\n");
2005 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2006 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002007 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
NeilBrown32a76272005-06-21 17:17:14 -07002009 if (mddev->bitmap)
2010 bitmap_print_sb(mddev->bitmap);
2011 else
2012 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002013 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 printk("<%s>", bdevname(rdev->bdev,b));
2015 printk("\n");
2016
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002017 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002018 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
2020 printk("md: **********************************\n");
2021 printk("\n");
2022}
2023
2024
NeilBrown42543762006-06-26 00:27:57 -07002025static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
NeilBrown42543762006-06-26 00:27:57 -07002027 /* Update each superblock (in-memory image), but
2028 * if we are allowed to, skip spares which already
2029 * have the right event counter, or have one earlier
2030 * (which would mean they aren't being marked as dirty
2031 * with the rest of the array)
2032 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
NeilBrown5e865102009-11-12 12:08:04 +11002035 /* First make sure individual recovery_offsets are correct */
2036 list_for_each_entry(rdev, &mddev->disks, same_set) {
2037 if (rdev->raid_disk >= 0 &&
2038 !test_bit(In_sync, &rdev->flags) &&
2039 mddev->curr_resync_completed > rdev->recovery_offset)
2040 rdev->recovery_offset = mddev->curr_resync_completed;
2041
2042 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002043 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002044 if (rdev->sb_events == mddev->events ||
2045 (nospares &&
2046 rdev->raid_disk < 0 &&
2047 (rdev->sb_events&1)==0 &&
2048 rdev->sb_events+1 == mddev->events)) {
2049 /* Don't update this superblock */
2050 rdev->sb_loaded = 2;
2051 } else {
2052 super_types[mddev->major_version].
2053 sync_super(mddev, rdev);
2054 rdev->sb_loaded = 1;
2055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 }
2057}
2058
NeilBrown850b2b42006-10-03 01:15:46 -07002059static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002062 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002063 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
NeilBrown1b57f132009-06-18 08:48:19 +10002065 mddev->utime = get_seconds();
NeilBrown8377bc82008-04-30 00:52:28 -07002066 if (mddev->external)
2067 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08002069 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002070
NeilBrown850b2b42006-10-03 01:15:46 -07002071 set_bit(MD_CHANGE_PENDING, &mddev->flags);
2072 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2073 force_change = 1;
2074 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2075 /* just a clean<-> dirty transition, possibly leave spares alone,
2076 * though if events isn't the right even/odd, we will have to do
2077 * spares after all
2078 */
2079 nospares = 1;
2080 if (force_change)
2081 nospares = 0;
2082 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002083 /* If the array is degraded, then skipping spares is both
2084 * dangerous and fairly pointless.
2085 * Dangerous because a device that was removed from the array
2086 * might have a event_count that still looks up-to-date,
2087 * so it can be re-added without a resync.
2088 * Pointless because if there are any spares to skip,
2089 * then a recovery will happen and soon that array won't
2090 * be degraded any more and the spare can go back to sleep then.
2091 */
NeilBrown850b2b42006-10-03 01:15:46 -07002092 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002093
NeilBrown06d91a52005-06-21 17:17:12 -07002094 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002095
2096 /* If this is just a dirty<->clean transition, and the array is clean
2097 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002098 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002099 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08002100 && (mddev->events & 1)
2101 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07002102 mddev->events--;
2103 else {
2104 /* otherwise we have to go forward and ... */
2105 mddev->events ++;
2106 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
NeilBrown51d56682009-08-13 09:54:02 +10002107 /* .. if the array isn't clean, an 'even' event must also go
2108 * to spares. */
2109 if ((mddev->events&1)==0)
NeilBrown42543762006-06-26 00:27:57 -07002110 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002111 } else {
NeilBrown51d56682009-08-13 09:54:02 +10002112 /* otherwise an 'odd' event must go to spares */
2113 if ((mddev->events&1))
NeilBrown42543762006-06-26 00:27:57 -07002114 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002115 }
2116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
2118 if (!mddev->events) {
2119 /*
2120 * oops, this 64-bit counter should never wrap.
2121 * Either we are in around ~1 trillion A.C., assuming
2122 * 1 reboot per second, or we have a bug:
2123 */
2124 MD_BUG();
2125 mddev->events --;
2126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
2128 /*
2129 * do not write anything to disk if using
2130 * nonpersistent superblocks
2131 */
NeilBrown06d91a52005-06-21 17:17:12 -07002132 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08002133 if (!mddev->external)
2134 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2135
NeilBrowna9701a32005-11-08 21:39:34 -08002136 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002137 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 return;
NeilBrown06d91a52005-06-21 17:17:12 -07002139 }
NeilBrowne6910632008-02-06 01:39:51 -08002140 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002141 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143 dprintk(KERN_INFO
2144 "md: updating %s RAID superblock on device (in sync %d)\n",
2145 mdname(mddev),mddev->in_sync);
2146
NeilBrown4ad13662007-07-17 04:06:13 -07002147 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002148 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 char b[BDEVNAME_SIZE];
2150 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002151 if (rdev->sb_loaded != 1)
2152 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002153 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 dprintk("(skipping faulty ");
2155
2156 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002157 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002158 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002159 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002160 rdev->sb_page);
2161 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2162 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002163 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002164 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 } else
2167 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002168 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 /* only need to write one superblock... */
2170 break;
2171 }
NeilBrowna9701a32005-11-08 21:39:34 -08002172 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002173 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002174
NeilBrowna9701a32005-11-08 21:39:34 -08002175 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002176 if (mddev->in_sync != sync_req ||
2177 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002178 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002179 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002180 goto repeat;
2181 }
NeilBrown850b2b42006-10-03 01:15:46 -07002182 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002183 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002184 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002185 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2186 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188}
2189
Andre Noll7f6ce762008-03-23 18:34:54 +01002190/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002191 * We want to accept with case. For this we use cmd_match.
2192 */
2193static int cmd_match(const char *cmd, const char *str)
2194{
2195 /* See if cmd, written into a sysfs file, matches
2196 * str. They must either be the same, or cmd can
2197 * have a trailing newline
2198 */
2199 while (*cmd && *str && *cmd == *str) {
2200 cmd++;
2201 str++;
2202 }
2203 if (*cmd == '\n')
2204 cmd++;
2205 if (*str || *cmd)
2206 return 0;
2207 return 1;
2208}
2209
NeilBrown86e6ffd2005-11-08 21:39:24 -08002210struct rdev_sysfs_entry {
2211 struct attribute attr;
2212 ssize_t (*show)(mdk_rdev_t *, char *);
2213 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2214};
2215
2216static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002217state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002218{
2219 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002220 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002221
NeilBrownb2d444d2005-11-08 21:39:31 -08002222 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002223 len+= sprintf(page+len, "%sfaulty",sep);
2224 sep = ",";
2225 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002226 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002227 len += sprintf(page+len, "%sin_sync",sep);
2228 sep = ",";
2229 }
NeilBrownf6556752006-06-26 00:28:01 -07002230 if (test_bit(WriteMostly, &rdev->flags)) {
2231 len += sprintf(page+len, "%swrite_mostly",sep);
2232 sep = ",";
2233 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002234 if (test_bit(Blocked, &rdev->flags)) {
2235 len += sprintf(page+len, "%sblocked", sep);
2236 sep = ",";
2237 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002238 if (!test_bit(Faulty, &rdev->flags) &&
2239 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002240 len += sprintf(page+len, "%sspare", sep);
2241 sep = ",";
2242 }
2243 return len+sprintf(page+len, "\n");
2244}
2245
NeilBrown45dc2de2006-06-26 00:27:58 -07002246static ssize_t
2247state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2248{
2249 /* can write
2250 * faulty - simulates and error
2251 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002252 * writemostly - sets write_mostly
2253 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002254 * blocked - sets the Blocked flag
2255 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002256 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002257 */
2258 int err = -EINVAL;
2259 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2260 md_error(rdev->mddev, rdev);
2261 err = 0;
2262 } else if (cmd_match(buf, "remove")) {
2263 if (rdev->raid_disk >= 0)
2264 err = -EBUSY;
2265 else {
2266 mddev_t *mddev = rdev->mddev;
2267 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002268 if (mddev->pers)
2269 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002270 md_new_event(mddev);
2271 err = 0;
2272 }
NeilBrownf6556752006-06-26 00:28:01 -07002273 } else if (cmd_match(buf, "writemostly")) {
2274 set_bit(WriteMostly, &rdev->flags);
2275 err = 0;
2276 } else if (cmd_match(buf, "-writemostly")) {
2277 clear_bit(WriteMostly, &rdev->flags);
2278 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002279 } else if (cmd_match(buf, "blocked")) {
2280 set_bit(Blocked, &rdev->flags);
2281 err = 0;
2282 } else if (cmd_match(buf, "-blocked")) {
2283 clear_bit(Blocked, &rdev->flags);
2284 wake_up(&rdev->blocked_wait);
2285 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2286 md_wakeup_thread(rdev->mddev->thread);
2287
2288 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002289 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2290 set_bit(In_sync, &rdev->flags);
2291 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002292 }
NeilBrown3c0ee632008-10-21 13:25:28 +11002293 if (!err && rdev->sysfs_state)
2294 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002295 return err ? err : len;
2296}
NeilBrown80ca3a42006-07-10 04:44:18 -07002297static struct rdev_sysfs_entry rdev_state =
2298__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002299
2300static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002301errors_show(mdk_rdev_t *rdev, char *page)
2302{
2303 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2304}
2305
2306static ssize_t
2307errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2308{
2309 char *e;
2310 unsigned long n = simple_strtoul(buf, &e, 10);
2311 if (*buf && (*e == 0 || *e == '\n')) {
2312 atomic_set(&rdev->corrected_errors, n);
2313 return len;
2314 }
2315 return -EINVAL;
2316}
2317static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002318__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002319
NeilBrown014236d2006-01-06 00:20:55 -08002320static ssize_t
2321slot_show(mdk_rdev_t *rdev, char *page)
2322{
2323 if (rdev->raid_disk < 0)
2324 return sprintf(page, "none\n");
2325 else
2326 return sprintf(page, "%d\n", rdev->raid_disk);
2327}
2328
2329static ssize_t
2330slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2331{
2332 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002333 int err;
2334 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002335 int slot = simple_strtoul(buf, &e, 10);
2336 if (strncmp(buf, "none", 4)==0)
2337 slot = -1;
2338 else if (e==buf || (*e && *e!= '\n'))
2339 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002340 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002341 /* Setting 'slot' on an active array requires also
2342 * updating the 'rd%d' link, and communicating
2343 * with the personality with ->hot_*_disk.
2344 * For now we only support removing
2345 * failed/spare devices. This normally happens automatically,
2346 * but not when the metadata is externally managed.
2347 */
NeilBrownc303da62008-02-06 01:39:51 -08002348 if (rdev->raid_disk == -1)
2349 return -EEXIST;
2350 /* personality does all needed checks */
2351 if (rdev->mddev->pers->hot_add_disk == NULL)
2352 return -EINVAL;
2353 err = rdev->mddev->pers->
2354 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2355 if (err)
2356 return err;
2357 sprintf(nm, "rd%d", rdev->raid_disk);
2358 sysfs_remove_link(&rdev->mddev->kobj, nm);
2359 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2360 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002361 } else if (rdev->mddev->pers) {
2362 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002363 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002364 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002365 */
2366
2367 if (rdev->raid_disk != -1)
2368 return -EBUSY;
2369
2370 if (rdev->mddev->pers->hot_add_disk == NULL)
2371 return -EINVAL;
2372
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002373 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002374 if (rdev2->raid_disk == slot)
2375 return -EEXIST;
2376
2377 rdev->raid_disk = slot;
2378 if (test_bit(In_sync, &rdev->flags))
2379 rdev->saved_raid_disk = slot;
2380 else
2381 rdev->saved_raid_disk = -1;
2382 err = rdev->mddev->pers->
2383 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002384 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002385 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002386 return err;
Neil Brown52664732008-06-28 08:31:44 +10002387 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002388 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002389 sprintf(nm, "rd%d", rdev->raid_disk);
2390 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2391 printk(KERN_WARNING
2392 "md: cannot register "
2393 "%s for %s\n",
2394 nm, mdname(rdev->mddev));
2395
2396 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002397 } else {
2398 if (slot >= rdev->mddev->raid_disks)
2399 return -ENOSPC;
2400 rdev->raid_disk = slot;
2401 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002402 clear_bit(Faulty, &rdev->flags);
2403 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002404 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002405 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002406 }
NeilBrown014236d2006-01-06 00:20:55 -08002407 return len;
2408}
2409
2410
2411static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002412__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002413
NeilBrown93c8cad2006-01-06 00:20:56 -08002414static ssize_t
2415offset_show(mdk_rdev_t *rdev, char *page)
2416{
Andrew Morton6961ece2006-01-06 00:20:59 -08002417 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002418}
2419
2420static ssize_t
2421offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2422{
2423 char *e;
2424 unsigned long long offset = simple_strtoull(buf, &e, 10);
2425 if (e==buf || (*e && *e != '\n'))
2426 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002427 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002428 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002429 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002430 /* Must set offset before size, so overlap checks
2431 * can be sane */
2432 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002433 rdev->data_offset = offset;
2434 return len;
2435}
2436
2437static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002438__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002439
NeilBrown83303b62006-01-06 00:21:06 -08002440static ssize_t
2441rdev_size_show(mdk_rdev_t *rdev, char *page)
2442{
Andre Nolldd8ac332009-03-31 14:33:13 +11002443 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002444}
2445
NeilBrownc5d79ad2008-02-06 01:39:54 -08002446static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2447{
2448 /* check if two start/length pairs overlap */
2449 if (s1+l1 <= s2)
2450 return 0;
2451 if (s2+l2 <= s1)
2452 return 0;
2453 return 1;
2454}
2455
Dan Williamsb522adc2009-03-31 15:00:31 +11002456static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2457{
2458 unsigned long long blocks;
2459 sector_t new;
2460
2461 if (strict_strtoull(buf, 10, &blocks) < 0)
2462 return -EINVAL;
2463
2464 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2465 return -EINVAL; /* sector conversion overflow */
2466
2467 new = blocks * 2;
2468 if (new != blocks * 2)
2469 return -EINVAL; /* unsigned long long to sector_t overflow */
2470
2471 *sectors = new;
2472 return 0;
2473}
2474
NeilBrown83303b62006-01-06 00:21:06 -08002475static ssize_t
2476rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2477{
NeilBrown27c529b2008-03-04 14:29:33 -08002478 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002479 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002480 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002481
Dan Williamsb522adc2009-03-31 15:00:31 +11002482 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002483 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002484 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002485 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002486 sectors = super_types[my_mddev->major_version].
2487 rdev_size_change(rdev, sectors);
2488 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002489 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002490 } else if (!sectors)
2491 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2492 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002493 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002494 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002495 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002496
Andre Nolldd8ac332009-03-31 14:33:13 +11002497 rdev->sectors = sectors;
2498 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002499 /* need to check that all other rdevs with the same ->bdev
2500 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002501 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002502 * we have to change it back, we will have the lock again.
2503 */
2504 mddev_t *mddev;
2505 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002506 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002507
NeilBrown27c529b2008-03-04 14:29:33 -08002508 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002509 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002510 mdk_rdev_t *rdev2;
2511
2512 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002513 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002514 if (test_bit(AllReserved, &rdev2->flags) ||
2515 (rdev->bdev == rdev2->bdev &&
2516 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002517 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002518 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002519 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002520 overlap = 1;
2521 break;
2522 }
2523 mddev_unlock(mddev);
2524 if (overlap) {
2525 mddev_put(mddev);
2526 break;
2527 }
2528 }
NeilBrown27c529b2008-03-04 14:29:33 -08002529 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002530 if (overlap) {
2531 /* Someone else could have slipped in a size
2532 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002533 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002534 * safe, and trust userspace not to race with
2535 * itself
2536 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002537 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002538 return -EBUSY;
2539 }
2540 }
NeilBrown83303b62006-01-06 00:21:06 -08002541 return len;
2542}
2543
2544static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002545__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002546
Dan Williams06e3c812009-12-12 21:17:12 -07002547
2548static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2549{
2550 unsigned long long recovery_start = rdev->recovery_offset;
2551
2552 if (test_bit(In_sync, &rdev->flags) ||
2553 recovery_start == MaxSector)
2554 return sprintf(page, "none\n");
2555
2556 return sprintf(page, "%llu\n", recovery_start);
2557}
2558
2559static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2560{
2561 unsigned long long recovery_start;
2562
2563 if (cmd_match(buf, "none"))
2564 recovery_start = MaxSector;
2565 else if (strict_strtoull(buf, 10, &recovery_start))
2566 return -EINVAL;
2567
2568 if (rdev->mddev->pers &&
2569 rdev->raid_disk >= 0)
2570 return -EBUSY;
2571
2572 rdev->recovery_offset = recovery_start;
2573 if (recovery_start == MaxSector)
2574 set_bit(In_sync, &rdev->flags);
2575 else
2576 clear_bit(In_sync, &rdev->flags);
2577 return len;
2578}
2579
2580static struct rdev_sysfs_entry rdev_recovery_start =
2581__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2582
NeilBrown86e6ffd2005-11-08 21:39:24 -08002583static struct attribute *rdev_default_attrs[] = {
2584 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002585 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002586 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002587 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002588 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002589 &rdev_recovery_start.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002590 NULL,
2591};
2592static ssize_t
2593rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2594{
2595 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2596 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002597 mddev_t *mddev = rdev->mddev;
2598 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002599
2600 if (!entry->show)
2601 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002602
2603 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2604 if (!rv) {
2605 if (rdev->mddev == NULL)
2606 rv = -EBUSY;
2607 else
2608 rv = entry->show(rdev, page);
2609 mddev_unlock(mddev);
2610 }
2611 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002612}
2613
2614static ssize_t
2615rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2616 const char *page, size_t length)
2617{
2618 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2619 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002620 ssize_t rv;
2621 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002622
2623 if (!entry->store)
2624 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002625 if (!capable(CAP_SYS_ADMIN))
2626 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002627 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002628 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002629 if (rdev->mddev == NULL)
2630 rv = -EBUSY;
2631 else
2632 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002633 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002634 }
2635 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002636}
2637
2638static void rdev_free(struct kobject *ko)
2639{
2640 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2641 kfree(rdev);
2642}
2643static struct sysfs_ops rdev_sysfs_ops = {
2644 .show = rdev_attr_show,
2645 .store = rdev_attr_store,
2646};
2647static struct kobj_type rdev_ktype = {
2648 .release = rdev_free,
2649 .sysfs_ops = &rdev_sysfs_ops,
2650 .default_attrs = rdev_default_attrs,
2651};
2652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653/*
2654 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2655 *
2656 * mark the device faulty if:
2657 *
2658 * - the device is nonexistent (zero size)
2659 * - the device has no valid superblock
2660 *
2661 * a faulty rdev _never_ has rdev->sb set.
2662 */
2663static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2664{
2665 char b[BDEVNAME_SIZE];
2666 int err;
2667 mdk_rdev_t *rdev;
2668 sector_t size;
2669
NeilBrown9ffae0c2006-01-06 00:20:32 -08002670 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 if (!rdev) {
2672 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2673 return ERR_PTR(-ENOMEM);
2674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
2676 if ((err = alloc_disk_sb(rdev)))
2677 goto abort_free;
2678
NeilBrownc5d79ad2008-02-06 01:39:54 -08002679 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 if (err)
2681 goto abort_free;
2682
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002683 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002684
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002686 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002687 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002688 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002690 rdev->sb_events = 0;
Robert Becker1e509152009-12-14 12:49:58 +11002691 rdev->last_read_error.tv_sec = 0;
2692 rdev->last_read_error.tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002694 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002695 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
2697 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2698 if (!size) {
2699 printk(KERN_WARNING
2700 "md: %s has zero or unknown size, marking faulty!\n",
2701 bdevname(rdev->bdev,b));
2702 err = -EINVAL;
2703 goto abort_free;
2704 }
2705
2706 if (super_format >= 0) {
2707 err = super_types[super_format].
2708 load_super(rdev, NULL, super_minor);
2709 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002710 printk(KERN_WARNING
2711 "md: %s does not have a valid v%d.%d "
2712 "superblock, not importing!\n",
2713 bdevname(rdev->bdev,b),
2714 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 goto abort_free;
2716 }
2717 if (err < 0) {
2718 printk(KERN_WARNING
2719 "md: could not read %s's sb, not importing!\n",
2720 bdevname(rdev->bdev,b));
2721 goto abort_free;
2722 }
2723 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002724
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002726 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
2728 return rdev;
2729
2730abort_free:
2731 if (rdev->sb_page) {
2732 if (rdev->bdev)
2733 unlock_rdev(rdev);
2734 free_disk_sb(rdev);
2735 }
2736 kfree(rdev);
2737 return ERR_PTR(err);
2738}
2739
2740/*
2741 * Check a full RAID array for plausibility
2742 */
2743
2744
NeilBrowna757e642005-04-16 15:26:42 -07002745static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746{
2747 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002748 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 char b[BDEVNAME_SIZE];
2750
2751 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002752 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 switch (super_types[mddev->major_version].
2754 load_super(rdev, freshest, mddev->minor_version)) {
2755 case 1:
2756 freshest = rdev;
2757 break;
2758 case 0:
2759 break;
2760 default:
2761 printk( KERN_ERR \
2762 "md: fatal superblock inconsistency in %s"
2763 " -- removing from array\n",
2764 bdevname(rdev->bdev,b));
2765 kick_rdev_from_array(rdev);
2766 }
2767
2768
2769 super_types[mddev->major_version].
2770 validate_super(mddev, freshest);
2771
2772 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002773 rdev_for_each(rdev, tmp, mddev) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002774 if (rdev->desc_nr >= mddev->max_disks ||
2775 i > mddev->max_disks) {
2776 printk(KERN_WARNING
2777 "md: %s: %s: only %d devices permitted\n",
2778 mdname(mddev), bdevname(rdev->bdev, b),
2779 mddev->max_disks);
2780 kick_rdev_from_array(rdev);
2781 continue;
2782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 if (rdev != freshest)
2784 if (super_types[mddev->major_version].
2785 validate_super(mddev, rdev)) {
2786 printk(KERN_WARNING "md: kicking non-fresh %s"
2787 " from array!\n",
2788 bdevname(rdev->bdev,b));
2789 kick_rdev_from_array(rdev);
2790 continue;
2791 }
2792 if (mddev->level == LEVEL_MULTIPATH) {
2793 rdev->desc_nr = i++;
2794 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002795 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002796 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002797 rdev->raid_disk = -1;
2798 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 }
2800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801}
2802
NeilBrown72e02072009-12-14 12:49:55 +11002803/* Read a fixed-point number.
2804 * Numbers in sysfs attributes should be in "standard" units where
2805 * possible, so time should be in seconds.
2806 * However we internally use a a much smaller unit such as
2807 * milliseconds or jiffies.
2808 * This function takes a decimal number with a possible fractional
2809 * component, and produces an integer which is the result of
2810 * multiplying that number by 10^'scale'.
2811 * all without any floating-point arithmetic.
2812 */
2813int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2814{
2815 unsigned long result = 0;
2816 long decimals = -1;
2817 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2818 if (*cp == '.')
2819 decimals = 0;
2820 else if (decimals < scale) {
2821 unsigned int value;
2822 value = *cp - '0';
2823 result = result * 10 + value;
2824 if (decimals >= 0)
2825 decimals++;
2826 }
2827 cp++;
2828 }
2829 if (*cp == '\n')
2830 cp++;
2831 if (*cp)
2832 return -EINVAL;
2833 if (decimals < 0)
2834 decimals = 0;
2835 while (decimals < scale) {
2836 result *= 10;
2837 decimals ++;
2838 }
2839 *res = result;
2840 return 0;
2841}
2842
2843
NeilBrown19052c02008-08-05 15:54:13 +10002844static void md_safemode_timeout(unsigned long data);
2845
NeilBrowneae17012005-11-08 21:39:23 -08002846static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002847safe_delay_show(mddev_t *mddev, char *page)
2848{
2849 int msec = (mddev->safemode_delay*1000)/HZ;
2850 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2851}
2852static ssize_t
2853safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2854{
NeilBrown16f17b32006-06-26 00:27:37 -07002855 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07002856
NeilBrown72e02072009-12-14 12:49:55 +11002857 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002858 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07002859 if (msec == 0)
2860 mddev->safemode_delay = 0;
2861 else {
NeilBrown19052c02008-08-05 15:54:13 +10002862 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002863 mddev->safemode_delay = (msec*HZ)/1000;
2864 if (mddev->safemode_delay == 0)
2865 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002866 if (mddev->safemode_delay < old_delay)
2867 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002868 }
2869 return len;
2870}
2871static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002872__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002873
2874static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002875level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002876{
NeilBrown2604b702006-01-06 00:20:36 -08002877 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002878 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002879 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002880 else if (mddev->clevel[0])
2881 return sprintf(page, "%s\n", mddev->clevel);
2882 else if (mddev->level != LEVEL_NONE)
2883 return sprintf(page, "%d\n", mddev->level);
2884 else
2885 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002886}
2887
NeilBrownd9d166c2006-01-06 00:20:51 -08002888static ssize_t
2889level_store(mddev_t *mddev, const char *buf, size_t len)
2890{
NeilBrown245f46c2009-03-31 14:39:39 +11002891 char level[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002892 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002893 struct mdk_personality *pers;
2894 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002895 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002896
2897 if (mddev->pers == NULL) {
2898 if (len == 0)
2899 return 0;
2900 if (len >= sizeof(mddev->clevel))
2901 return -ENOSPC;
2902 strncpy(mddev->clevel, buf, len);
2903 if (mddev->clevel[len-1] == '\n')
2904 len--;
2905 mddev->clevel[len] = 0;
2906 mddev->level = LEVEL_NONE;
2907 return rv;
2908 }
2909
2910 /* request to change the personality. Need to ensure:
2911 * - array is not engaged in resync/recovery/reshape
2912 * - old personality can be suspended
2913 * - new personality will access other array.
2914 */
2915
2916 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownd9d166c2006-01-06 00:20:51 -08002917 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002918
2919 if (!mddev->pers->quiesce) {
2920 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2921 mdname(mddev), mddev->pers->name);
2922 return -EINVAL;
2923 }
2924
2925 /* Now find the new personality */
2926 if (len == 0 || len >= sizeof(level))
2927 return -EINVAL;
2928 strncpy(level, buf, len);
2929 if (level[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002930 len--;
NeilBrown245f46c2009-03-31 14:39:39 +11002931 level[len] = 0;
2932
2933 request_module("md-%s", level);
2934 spin_lock(&pers_lock);
2935 pers = find_pers(LEVEL_NONE, level);
2936 if (!pers || !try_module_get(pers->owner)) {
2937 spin_unlock(&pers_lock);
2938 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2939 return -EINVAL;
2940 }
2941 spin_unlock(&pers_lock);
2942
2943 if (pers == mddev->pers) {
2944 /* Nothing to do! */
2945 module_put(pers->owner);
2946 return rv;
2947 }
2948 if (!pers->takeover) {
2949 module_put(pers->owner);
2950 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2951 mdname(mddev), level);
2952 return -EINVAL;
2953 }
2954
2955 /* ->takeover must set new_* and/or delta_disks
2956 * if it succeeds, and may set them when it fails.
2957 */
2958 priv = pers->takeover(mddev);
2959 if (IS_ERR(priv)) {
2960 mddev->new_level = mddev->level;
2961 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002962 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002963 mddev->raid_disks -= mddev->delta_disks;
2964 mddev->delta_disks = 0;
2965 module_put(pers->owner);
2966 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2967 mdname(mddev), level);
2968 return PTR_ERR(priv);
2969 }
2970
2971 /* Looks like we have a winner */
2972 mddev_suspend(mddev);
2973 mddev->pers->stop(mddev);
2974 module_put(mddev->pers->owner);
NeilBrown3a981b02009-08-03 10:59:55 +10002975 /* Invalidate devices that are now superfluous */
2976 list_for_each_entry(rdev, &mddev->disks, same_set)
2977 if (rdev->raid_disk >= mddev->raid_disks) {
2978 rdev->raid_disk = -1;
2979 clear_bit(In_sync, &rdev->flags);
2980 }
NeilBrown245f46c2009-03-31 14:39:39 +11002981 mddev->pers = pers;
2982 mddev->private = priv;
2983 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2984 mddev->level = mddev->new_level;
2985 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002986 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002987 mddev->delta_disks = 0;
2988 pers->run(mddev);
2989 mddev_resume(mddev);
2990 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2991 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2992 md_wakeup_thread(mddev->thread);
NeilBrownd9d166c2006-01-06 00:20:51 -08002993 return rv;
2994}
2995
2996static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002997__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002998
NeilBrownd4dbd022006-06-26 00:27:59 -07002999
3000static ssize_t
3001layout_show(mddev_t *mddev, char *page)
3002{
3003 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003004 if (mddev->reshape_position != MaxSector &&
3005 mddev->layout != mddev->new_layout)
3006 return sprintf(page, "%d (%d)\n",
3007 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003008 return sprintf(page, "%d\n", mddev->layout);
3009}
3010
3011static ssize_t
3012layout_store(mddev_t *mddev, const char *buf, size_t len)
3013{
3014 char *e;
3015 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003016
3017 if (!*buf || (*e && *e != '\n'))
3018 return -EINVAL;
3019
NeilBrownb3546032009-03-31 14:56:41 +11003020 if (mddev->pers) {
3021 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003022 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003023 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003024 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003025 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003026 if (err) {
3027 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003028 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003029 }
NeilBrownb3546032009-03-31 14:56:41 +11003030 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003031 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003032 if (mddev->reshape_position == MaxSector)
3033 mddev->layout = n;
3034 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003035 return len;
3036}
3037static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003038__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003039
3040
NeilBrowneae17012005-11-08 21:39:23 -08003041static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003042raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003043{
NeilBrownbb636542005-11-08 21:39:45 -08003044 if (mddev->raid_disks == 0)
3045 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003046 if (mddev->reshape_position != MaxSector &&
3047 mddev->delta_disks != 0)
3048 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3049 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003050 return sprintf(page, "%d\n", mddev->raid_disks);
3051}
3052
NeilBrownda943b92006-01-06 00:20:54 -08003053static int update_raid_disks(mddev_t *mddev, int raid_disks);
3054
3055static ssize_t
3056raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3057{
NeilBrownda943b92006-01-06 00:20:54 -08003058 char *e;
3059 int rv = 0;
3060 unsigned long n = simple_strtoul(buf, &e, 10);
3061
3062 if (!*buf || (*e && *e != '\n'))
3063 return -EINVAL;
3064
3065 if (mddev->pers)
3066 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003067 else if (mddev->reshape_position != MaxSector) {
3068 int olddisks = mddev->raid_disks - mddev->delta_disks;
3069 mddev->delta_disks = n - olddisks;
3070 mddev->raid_disks = n;
3071 } else
NeilBrownda943b92006-01-06 00:20:54 -08003072 mddev->raid_disks = n;
3073 return rv ? rv : len;
3074}
3075static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003076__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003077
NeilBrown24dd4692005-11-08 21:39:26 -08003078static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003079chunk_size_show(mddev_t *mddev, char *page)
3080{
NeilBrown08a02ec2007-05-09 02:35:38 -07003081 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003082 mddev->chunk_sectors != mddev->new_chunk_sectors)
3083 return sprintf(page, "%d (%d)\n",
3084 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003085 mddev->chunk_sectors << 9);
3086 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003087}
3088
3089static ssize_t
3090chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3091{
NeilBrown3b343802006-01-06 00:20:47 -08003092 char *e;
3093 unsigned long n = simple_strtoul(buf, &e, 10);
3094
NeilBrown3b343802006-01-06 00:20:47 -08003095 if (!*buf || (*e && *e != '\n'))
3096 return -EINVAL;
3097
NeilBrownb3546032009-03-31 14:56:41 +11003098 if (mddev->pers) {
3099 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003100 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003101 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003102 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003103 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003104 if (err) {
3105 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003106 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003107 }
NeilBrownb3546032009-03-31 14:56:41 +11003108 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003109 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003110 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003111 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003112 }
NeilBrown3b343802006-01-06 00:20:47 -08003113 return len;
3114}
3115static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003116__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003117
NeilBrowna94213b2006-06-26 00:28:00 -07003118static ssize_t
3119resync_start_show(mddev_t *mddev, char *page)
3120{
NeilBrownd1a7c502009-03-31 15:24:32 +11003121 if (mddev->recovery_cp == MaxSector)
3122 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003123 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3124}
3125
3126static ssize_t
3127resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3128{
NeilBrowna94213b2006-06-26 00:28:00 -07003129 char *e;
3130 unsigned long long n = simple_strtoull(buf, &e, 10);
3131
3132 if (mddev->pers)
3133 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003134 if (cmd_match(buf, "none"))
3135 n = MaxSector;
3136 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003137 return -EINVAL;
3138
3139 mddev->recovery_cp = n;
3140 return len;
3141}
3142static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003143__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003144
NeilBrown9e653b62006-06-26 00:27:58 -07003145/*
3146 * The array state can be:
3147 *
3148 * clear
3149 * No devices, no size, no level
3150 * Equivalent to STOP_ARRAY ioctl
3151 * inactive
3152 * May have some settings, but array is not active
3153 * all IO results in error
3154 * When written, doesn't tear down array, but just stops it
3155 * suspended (not supported yet)
3156 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003157 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003158 * readonly
3159 * no resync can happen. no superblocks get written.
3160 * write requests fail
3161 * read-auto
3162 * like readonly, but behaves like 'clean' on a write request.
3163 *
3164 * clean - no pending writes, but otherwise active.
3165 * When written to inactive array, starts without resync
3166 * If a write request arrives then
3167 * if metadata is known, mark 'dirty' and switch to 'active'.
3168 * if not known, block and switch to write-pending
3169 * If written to an active array that has pending writes, then fails.
3170 * active
3171 * fully active: IO and resync can be happening.
3172 * When written to inactive array, starts with resync
3173 *
3174 * write-pending
3175 * clean, but writes are blocked waiting for 'active' to be written.
3176 *
3177 * active-idle
3178 * like active, but no writes have been seen for a while (100msec).
3179 *
3180 */
3181enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3182 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003183static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003184 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3185 "write-pending", "active-idle", NULL };
3186
3187static int match_word(const char *word, char **list)
3188{
3189 int n;
3190 for (n=0; list[n]; n++)
3191 if (cmd_match(word, list[n]))
3192 break;
3193 return n;
3194}
3195
3196static ssize_t
3197array_state_show(mddev_t *mddev, char *page)
3198{
3199 enum array_state st = inactive;
3200
3201 if (mddev->pers)
3202 switch(mddev->ro) {
3203 case 1:
3204 st = readonly;
3205 break;
3206 case 2:
3207 st = read_auto;
3208 break;
3209 case 0:
3210 if (mddev->in_sync)
3211 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08003212 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3213 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003214 else if (mddev->safemode)
3215 st = active_idle;
3216 else
3217 st = active;
3218 }
3219 else {
3220 if (list_empty(&mddev->disks) &&
3221 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003222 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003223 st = clear;
3224 else
3225 st = inactive;
3226 }
3227 return sprintf(page, "%s\n", array_states[st]);
3228}
3229
Neil Browndf5b20c2008-07-11 22:02:22 +10003230static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003231static int do_md_run(mddev_t * mddev);
3232static int restart_array(mddev_t *mddev);
3233
3234static ssize_t
3235array_state_store(mddev_t *mddev, const char *buf, size_t len)
3236{
3237 int err = -EINVAL;
3238 enum array_state st = match_word(buf, array_states);
3239 switch(st) {
3240 case bad_word:
3241 break;
3242 case clear:
3243 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003244 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003245 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003246 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003247 break;
3248 case inactive:
3249 /* stopping an active array */
3250 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003251 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003252 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003253 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003254 } else
3255 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003256 break;
3257 case suspended:
3258 break; /* not supported yet */
3259 case readonly:
3260 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10003261 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003262 else {
3263 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003264 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003265 err = do_md_run(mddev);
3266 }
3267 break;
3268 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003269 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003270 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10003271 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003272 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003273 err = restart_array(mddev);
3274 if (err == 0) {
3275 mddev->ro = 2;
3276 set_disk_ro(mddev->gendisk, 0);
3277 }
NeilBrown9e653b62006-06-26 00:27:58 -07003278 } else {
3279 mddev->ro = 2;
3280 err = do_md_run(mddev);
3281 }
3282 break;
3283 case clean:
3284 if (mddev->pers) {
3285 restart_array(mddev);
3286 spin_lock_irq(&mddev->write_lock);
3287 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003288 if (mddev->in_sync == 0) {
3289 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003290 if (mddev->safemode == 1)
3291 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003292 if (mddev->persistent)
3293 set_bit(MD_CHANGE_CLEAN,
3294 &mddev->flags);
3295 }
3296 err = 0;
3297 } else
3298 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003299 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003300 } else
3301 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003302 break;
3303 case active:
3304 if (mddev->pers) {
3305 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003306 if (mddev->external)
3307 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003308 wake_up(&mddev->sb_wait);
3309 err = 0;
3310 } else {
3311 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003312 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003313 err = do_md_run(mddev);
3314 }
3315 break;
3316 case write_pending:
3317 case active_idle:
3318 /* these cannot be set */
3319 break;
3320 }
3321 if (err)
3322 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003323 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003324 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003325 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003326 }
NeilBrown9e653b62006-06-26 00:27:58 -07003327}
NeilBrown80ca3a42006-07-10 04:44:18 -07003328static struct md_sysfs_entry md_array_state =
3329__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003330
NeilBrown6d7ff732006-01-06 00:21:16 -08003331static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003332max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3333 return sprintf(page, "%d\n",
3334 atomic_read(&mddev->max_corr_read_errors));
3335}
3336
3337static ssize_t
3338max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3339{
3340 char *e;
3341 unsigned long n = simple_strtoul(buf, &e, 10);
3342
3343 if (*buf && (*e == 0 || *e == '\n')) {
3344 atomic_set(&mddev->max_corr_read_errors, n);
3345 return len;
3346 }
3347 return -EINVAL;
3348}
3349
3350static struct md_sysfs_entry max_corr_read_errors =
3351__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3352 max_corrected_read_errors_store);
3353
3354static ssize_t
NeilBrown6d7ff732006-01-06 00:21:16 -08003355null_show(mddev_t *mddev, char *page)
3356{
3357 return -EINVAL;
3358}
3359
3360static ssize_t
3361new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3362{
3363 /* buf must be %d:%d\n? giving major and minor numbers */
3364 /* The new device is added to the array.
3365 * If the array has a persistent superblock, we read the
3366 * superblock to initialise info and check validity.
3367 * Otherwise, only checking done is that in bind_rdev_to_array,
3368 * which mainly checks size.
3369 */
3370 char *e;
3371 int major = simple_strtoul(buf, &e, 10);
3372 int minor;
3373 dev_t dev;
3374 mdk_rdev_t *rdev;
3375 int err;
3376
3377 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3378 return -EINVAL;
3379 minor = simple_strtoul(e+1, &e, 10);
3380 if (*e && *e != '\n')
3381 return -EINVAL;
3382 dev = MKDEV(major, minor);
3383 if (major != MAJOR(dev) ||
3384 minor != MINOR(dev))
3385 return -EOVERFLOW;
3386
3387
3388 if (mddev->persistent) {
3389 rdev = md_import_device(dev, mddev->major_version,
3390 mddev->minor_version);
3391 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3392 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3393 mdk_rdev_t, same_set);
3394 err = super_types[mddev->major_version]
3395 .load_super(rdev, rdev0, mddev->minor_version);
3396 if (err < 0)
3397 goto out;
3398 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003399 } else if (mddev->external)
3400 rdev = md_import_device(dev, -2, -1);
3401 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003402 rdev = md_import_device(dev, -1, -1);
3403
3404 if (IS_ERR(rdev))
3405 return PTR_ERR(rdev);
3406 err = bind_rdev_to_array(rdev, mddev);
3407 out:
3408 if (err)
3409 export_rdev(rdev);
3410 return err ? err : len;
3411}
3412
3413static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003414__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003415
3416static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003417bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3418{
3419 char *end;
3420 unsigned long chunk, end_chunk;
3421
3422 if (!mddev->bitmap)
3423 goto out;
3424 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3425 while (*buf) {
3426 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3427 if (buf == end) break;
3428 if (*end == '-') { /* range */
3429 buf = end + 1;
3430 end_chunk = simple_strtoul(buf, &end, 0);
3431 if (buf == end) break;
3432 }
3433 if (*end && !isspace(*end)) break;
3434 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003435 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003436 }
3437 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3438out:
3439 return len;
3440}
3441
3442static struct md_sysfs_entry md_bitmap =
3443__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3444
3445static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003446size_show(mddev_t *mddev, char *page)
3447{
Andre Noll58c0fed2009-03-31 14:33:13 +11003448 return sprintf(page, "%llu\n",
3449 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003450}
3451
Andre Nolld71f9f82008-07-11 22:02:22 +10003452static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003453
3454static ssize_t
3455size_store(mddev_t *mddev, const char *buf, size_t len)
3456{
3457 /* If array is inactive, we can reduce the component size, but
3458 * not increase it (except from 0).
3459 * If array is active, we can try an on-line resize
3460 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003461 sector_t sectors;
3462 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003463
Andre Noll58c0fed2009-03-31 14:33:13 +11003464 if (err < 0)
3465 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003466 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003467 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003468 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003469 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003470 if (mddev->dev_sectors == 0 ||
3471 mddev->dev_sectors > sectors)
3472 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003473 else
3474 err = -ENOSPC;
3475 }
3476 return err ? err : len;
3477}
3478
3479static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003480__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003481
NeilBrown8bb93aa2006-01-06 00:20:50 -08003482
3483/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003484 * This is one of
3485 * 'none' for arrays with no metadata (good luck...)
3486 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003487 * or N.M for internally known formats
3488 */
3489static ssize_t
3490metadata_show(mddev_t *mddev, char *page)
3491{
3492 if (mddev->persistent)
3493 return sprintf(page, "%d.%d\n",
3494 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003495 else if (mddev->external)
3496 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003497 else
3498 return sprintf(page, "none\n");
3499}
3500
3501static ssize_t
3502metadata_store(mddev_t *mddev, const char *buf, size_t len)
3503{
3504 int major, minor;
3505 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003506 /* Changing the details of 'external' metadata is
3507 * always permitted. Otherwise there must be
3508 * no devices attached to the array.
3509 */
3510 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3511 ;
3512 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003513 return -EBUSY;
3514
3515 if (cmd_match(buf, "none")) {
3516 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003517 mddev->external = 0;
3518 mddev->major_version = 0;
3519 mddev->minor_version = 90;
3520 return len;
3521 }
3522 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003523 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003524 if (namelen >= sizeof(mddev->metadata_type))
3525 namelen = sizeof(mddev->metadata_type)-1;
3526 strncpy(mddev->metadata_type, buf+9, namelen);
3527 mddev->metadata_type[namelen] = 0;
3528 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3529 mddev->metadata_type[--namelen] = 0;
3530 mddev->persistent = 0;
3531 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003532 mddev->major_version = 0;
3533 mddev->minor_version = 90;
3534 return len;
3535 }
3536 major = simple_strtoul(buf, &e, 10);
3537 if (e==buf || *e != '.')
3538 return -EINVAL;
3539 buf = e+1;
3540 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003541 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003542 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003543 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003544 return -ENOENT;
3545 mddev->major_version = major;
3546 mddev->minor_version = minor;
3547 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003548 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003549 return len;
3550}
3551
3552static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003553__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003554
NeilBrowna35b0d62006-01-06 00:20:49 -08003555static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003556action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003557{
NeilBrown7eec3142005-11-08 21:39:44 -08003558 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003559 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3560 type = "frozen";
3561 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003562 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003563 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3564 type = "reshape";
3565 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003566 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3567 type = "resync";
3568 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3569 type = "check";
3570 else
3571 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003572 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003573 type = "recover";
3574 }
3575 return sprintf(page, "%s\n", type);
3576}
3577
3578static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003579action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003580{
NeilBrown7eec3142005-11-08 21:39:44 -08003581 if (!mddev->pers || !mddev->pers->sync_request)
3582 return -EINVAL;
3583
NeilBrownb6a9ce62009-05-26 09:41:17 +10003584 if (cmd_match(page, "frozen"))
3585 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3586 else
3587 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3588
3589 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003590 if (mddev->sync_thread) {
3591 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3592 md_unregister_thread(mddev->sync_thread);
3593 mddev->sync_thread = NULL;
3594 mddev->recovery = 0;
3595 }
NeilBrown03c902e2006-01-06 00:20:46 -08003596 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3597 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003598 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003599 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003600 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003601 else if (cmd_match(page, "recover")) {
3602 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3603 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3604 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003605 int err;
3606 if (mddev->pers->start_reshape == NULL)
3607 return -EINVAL;
3608 err = mddev->pers->start_reshape(mddev);
3609 if (err)
3610 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003611 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003612 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003613 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003614 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003615 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003616 return -EINVAL;
3617 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3618 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003619 }
NeilBrown03c902e2006-01-06 00:20:46 -08003620 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003621 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003622 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003623 return len;
3624}
3625
NeilBrown9d888832005-11-08 21:39:26 -08003626static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003627mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003628{
3629 return sprintf(page, "%llu\n",
3630 (unsigned long long) mddev->resync_mismatches);
3631}
3632
NeilBrown80ca3a42006-07-10 04:44:18 -07003633static struct md_sysfs_entry md_scan_mode =
3634__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003635
NeilBrown96de1e62005-11-08 21:39:39 -08003636
NeilBrown80ca3a42006-07-10 04:44:18 -07003637static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003638
NeilBrown88202a02006-01-06 00:21:36 -08003639static ssize_t
3640sync_min_show(mddev_t *mddev, char *page)
3641{
3642 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3643 mddev->sync_speed_min ? "local": "system");
3644}
3645
3646static ssize_t
3647sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3648{
3649 int min;
3650 char *e;
3651 if (strncmp(buf, "system", 6)==0) {
3652 mddev->sync_speed_min = 0;
3653 return len;
3654 }
3655 min = simple_strtoul(buf, &e, 10);
3656 if (buf == e || (*e && *e != '\n') || min <= 0)
3657 return -EINVAL;
3658 mddev->sync_speed_min = min;
3659 return len;
3660}
3661
3662static struct md_sysfs_entry md_sync_min =
3663__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3664
3665static ssize_t
3666sync_max_show(mddev_t *mddev, char *page)
3667{
3668 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3669 mddev->sync_speed_max ? "local": "system");
3670}
3671
3672static ssize_t
3673sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3674{
3675 int max;
3676 char *e;
3677 if (strncmp(buf, "system", 6)==0) {
3678 mddev->sync_speed_max = 0;
3679 return len;
3680 }
3681 max = simple_strtoul(buf, &e, 10);
3682 if (buf == e || (*e && *e != '\n') || max <= 0)
3683 return -EINVAL;
3684 mddev->sync_speed_max = max;
3685 return len;
3686}
3687
3688static struct md_sysfs_entry md_sync_max =
3689__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3690
Iustin Popd7f3d292007-10-16 23:30:54 -07003691static ssize_t
3692degraded_show(mddev_t *mddev, char *page)
3693{
3694 return sprintf(page, "%d\n", mddev->degraded);
3695}
3696static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003697
3698static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003699sync_force_parallel_show(mddev_t *mddev, char *page)
3700{
3701 return sprintf(page, "%d\n", mddev->parallel_resync);
3702}
3703
3704static ssize_t
3705sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3706{
3707 long n;
3708
3709 if (strict_strtol(buf, 10, &n))
3710 return -EINVAL;
3711
3712 if (n != 0 && n != 1)
3713 return -EINVAL;
3714
3715 mddev->parallel_resync = n;
3716
3717 if (mddev->sync_thread)
3718 wake_up(&resync_wait);
3719
3720 return len;
3721}
3722
3723/* force parallel resync, even with shared block devices */
3724static struct md_sysfs_entry md_sync_force_parallel =
3725__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3726 sync_force_parallel_show, sync_force_parallel_store);
3727
3728static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003729sync_speed_show(mddev_t *mddev, char *page)
3730{
3731 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003732 if (mddev->curr_resync == 0)
3733 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003734 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3735 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003736 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003737 db = resync - mddev->resync_mark_cnt;
3738 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003739}
3740
NeilBrown80ca3a42006-07-10 04:44:18 -07003741static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003742
3743static ssize_t
3744sync_completed_show(mddev_t *mddev, char *page)
3745{
Andre Noll58c0fed2009-03-31 14:33:13 +11003746 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003747
NeilBrownacb180b2009-04-14 16:28:34 +10003748 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3749 return sprintf(page, "none\n");
3750
NeilBrown88202a02006-01-06 00:21:36 -08003751 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003752 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003753 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003754 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003755
NeilBrownacb180b2009-04-14 16:28:34 +10003756 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003757 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003758}
3759
NeilBrown80ca3a42006-07-10 04:44:18 -07003760static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003761
NeilBrowne464eaf2006-03-27 01:18:14 -08003762static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003763min_sync_show(mddev_t *mddev, char *page)
3764{
3765 return sprintf(page, "%llu\n",
3766 (unsigned long long)mddev->resync_min);
3767}
3768static ssize_t
3769min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3770{
3771 unsigned long long min;
3772 if (strict_strtoull(buf, 10, &min))
3773 return -EINVAL;
3774 if (min > mddev->resync_max)
3775 return -EINVAL;
3776 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3777 return -EBUSY;
3778
3779 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003780 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003781 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003782 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003783 return -EINVAL;
3784 }
3785 mddev->resync_min = min;
3786
3787 return len;
3788}
3789
3790static struct md_sysfs_entry md_min_sync =
3791__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3792
3793static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003794max_sync_show(mddev_t *mddev, char *page)
3795{
3796 if (mddev->resync_max == MaxSector)
3797 return sprintf(page, "max\n");
3798 else
3799 return sprintf(page, "%llu\n",
3800 (unsigned long long)mddev->resync_max);
3801}
3802static ssize_t
3803max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3804{
3805 if (strncmp(buf, "max", 3) == 0)
3806 mddev->resync_max = MaxSector;
3807 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003808 unsigned long long max;
3809 if (strict_strtoull(buf, 10, &max))
3810 return -EINVAL;
3811 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003812 return -EINVAL;
3813 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003814 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003815 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3816 return -EBUSY;
3817
3818 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003819 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003820 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003821 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003822 return -EINVAL;
3823 }
3824 mddev->resync_max = max;
3825 }
3826 wake_up(&mddev->recovery_wait);
3827 return len;
3828}
3829
3830static struct md_sysfs_entry md_max_sync =
3831__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3832
3833static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003834suspend_lo_show(mddev_t *mddev, char *page)
3835{
3836 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3837}
3838
3839static ssize_t
3840suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3841{
3842 char *e;
3843 unsigned long long new = simple_strtoull(buf, &e, 10);
3844
NeilBrownb8d966e2009-07-01 11:14:04 +10003845 if (mddev->pers == NULL ||
3846 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003847 return -EINVAL;
3848 if (buf == e || (*e && *e != '\n'))
3849 return -EINVAL;
3850 if (new >= mddev->suspend_hi ||
3851 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3852 mddev->suspend_lo = new;
3853 mddev->pers->quiesce(mddev, 2);
3854 return len;
3855 } else
3856 return -EINVAL;
3857}
3858static struct md_sysfs_entry md_suspend_lo =
3859__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3860
3861
3862static ssize_t
3863suspend_hi_show(mddev_t *mddev, char *page)
3864{
3865 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3866}
3867
3868static ssize_t
3869suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3870{
3871 char *e;
3872 unsigned long long new = simple_strtoull(buf, &e, 10);
3873
NeilBrownb8d966e2009-07-01 11:14:04 +10003874 if (mddev->pers == NULL ||
3875 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003876 return -EINVAL;
3877 if (buf == e || (*e && *e != '\n'))
3878 return -EINVAL;
3879 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3880 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3881 mddev->suspend_hi = new;
3882 mddev->pers->quiesce(mddev, 1);
3883 mddev->pers->quiesce(mddev, 0);
3884 return len;
3885 } else
3886 return -EINVAL;
3887}
3888static struct md_sysfs_entry md_suspend_hi =
3889__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3890
NeilBrown08a02ec2007-05-09 02:35:38 -07003891static ssize_t
3892reshape_position_show(mddev_t *mddev, char *page)
3893{
3894 if (mddev->reshape_position != MaxSector)
3895 return sprintf(page, "%llu\n",
3896 (unsigned long long)mddev->reshape_position);
3897 strcpy(page, "none\n");
3898 return 5;
3899}
3900
3901static ssize_t
3902reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3903{
3904 char *e;
3905 unsigned long long new = simple_strtoull(buf, &e, 10);
3906 if (mddev->pers)
3907 return -EBUSY;
3908 if (buf == e || (*e && *e != '\n'))
3909 return -EINVAL;
3910 mddev->reshape_position = new;
3911 mddev->delta_disks = 0;
3912 mddev->new_level = mddev->level;
3913 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003914 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07003915 return len;
3916}
3917
3918static struct md_sysfs_entry md_reshape_position =
3919__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3920 reshape_position_store);
3921
Dan Williamsb522adc2009-03-31 15:00:31 +11003922static ssize_t
3923array_size_show(mddev_t *mddev, char *page)
3924{
3925 if (mddev->external_size)
3926 return sprintf(page, "%llu\n",
3927 (unsigned long long)mddev->array_sectors/2);
3928 else
3929 return sprintf(page, "default\n");
3930}
3931
3932static ssize_t
3933array_size_store(mddev_t *mddev, const char *buf, size_t len)
3934{
3935 sector_t sectors;
3936
3937 if (strncmp(buf, "default", 7) == 0) {
3938 if (mddev->pers)
3939 sectors = mddev->pers->size(mddev, 0, 0);
3940 else
3941 sectors = mddev->array_sectors;
3942
3943 mddev->external_size = 0;
3944 } else {
3945 if (strict_blocks_to_sectors(buf, &sectors) < 0)
3946 return -EINVAL;
3947 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10003948 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11003949
3950 mddev->external_size = 1;
3951 }
3952
3953 mddev->array_sectors = sectors;
3954 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10003955 if (mddev->pers)
3956 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11003957
3958 return len;
3959}
3960
3961static struct md_sysfs_entry md_array_size =
3962__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
3963 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08003964
NeilBrowneae17012005-11-08 21:39:23 -08003965static struct attribute *md_default_attrs[] = {
3966 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003967 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003968 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003969 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003970 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003971 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003972 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08003973 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003974 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003975 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003976 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11003977 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11003978 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003979 NULL,
3980};
3981
3982static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003983 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003984 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003985 &md_sync_min.attr,
3986 &md_sync_max.attr,
3987 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003988 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003989 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003990 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003991 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003992 &md_suspend_lo.attr,
3993 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003994 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003995 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003996 NULL,
3997};
NeilBrown411036f2005-11-08 21:39:40 -08003998static struct attribute_group md_redundancy_group = {
3999 .name = NULL,
4000 .attrs = md_redundancy_attrs,
4001};
4002
NeilBrowneae17012005-11-08 21:39:23 -08004003
4004static ssize_t
4005md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4006{
4007 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4008 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004009 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004010
4011 if (!entry->show)
4012 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004013 rv = mddev_lock(mddev);
4014 if (!rv) {
4015 rv = entry->show(mddev, page);
4016 mddev_unlock(mddev);
4017 }
NeilBrown96de1e62005-11-08 21:39:39 -08004018 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004019}
4020
4021static ssize_t
4022md_attr_store(struct kobject *kobj, struct attribute *attr,
4023 const char *page, size_t length)
4024{
4025 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4026 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004027 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004028
4029 if (!entry->store)
4030 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004031 if (!capable(CAP_SYS_ADMIN))
4032 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004033 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004034 if (mddev->hold_active == UNTIL_IOCTL)
4035 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004036 if (!rv) {
4037 rv = entry->store(mddev, page, length);
4038 mddev_unlock(mddev);
4039 }
NeilBrown96de1e62005-11-08 21:39:39 -08004040 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004041}
4042
4043static void md_free(struct kobject *ko)
4044{
4045 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004046
4047 if (mddev->sysfs_state)
4048 sysfs_put(mddev->sysfs_state);
4049
4050 if (mddev->gendisk) {
4051 del_gendisk(mddev->gendisk);
4052 put_disk(mddev->gendisk);
4053 }
4054 if (mddev->queue)
4055 blk_cleanup_queue(mddev->queue);
4056
NeilBrowneae17012005-11-08 21:39:23 -08004057 kfree(mddev);
4058}
4059
4060static struct sysfs_ops md_sysfs_ops = {
4061 .show = md_attr_show,
4062 .store = md_attr_store,
4063};
4064static struct kobj_type md_ktype = {
4065 .release = md_free,
4066 .sysfs_ops = &md_sysfs_ops,
4067 .default_attrs = md_default_attrs,
4068};
4069
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070int mdp_major = 0;
4071
Dan Williams5fd3a172009-03-04 00:57:25 -07004072static void mddev_delayed_delete(struct work_struct *ws)
4073{
4074 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4075
4076 if (mddev->private == &md_redundancy_group) {
4077 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
4078 if (mddev->sysfs_action)
4079 sysfs_put(mddev->sysfs_action);
4080 mddev->sysfs_action = NULL;
4081 mddev->private = NULL;
4082 }
NeilBrown43a70502009-12-14 12:49:55 +11004083 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004084 kobject_del(&mddev->kobj);
4085 kobject_put(&mddev->kobj);
4086}
4087
NeilBrownefeb53c2009-01-09 08:31:10 +11004088static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004090 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 mddev_t *mddev = mddev_find(dev);
4092 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004093 int partitioned;
4094 int shift;
4095 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004096 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097
4098 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004099 return -ENODEV;
4100
4101 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4102 shift = partitioned ? MdpMinorShift : 0;
4103 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104
NeilBrownd3374822009-01-09 08:31:10 +11004105 /* wait for any previous instance if this device
4106 * to be completed removed (mddev_delayed_delete).
4107 */
4108 flush_scheduled_work();
4109
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004110 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004111 error = -EEXIST;
4112 if (mddev->gendisk)
4113 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004114
4115 if (name) {
4116 /* Need to ensure that 'name' is not a duplicate.
4117 */
4118 mddev_t *mddev2;
4119 spin_lock(&all_mddevs_lock);
4120
4121 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4122 if (mddev2->gendisk &&
4123 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4124 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004125 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004126 }
4127 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 }
NeilBrown8b765392009-01-09 08:31:08 +11004129
NeilBrown0909dc42009-07-01 12:27:21 +10004130 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004131 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004132 if (!mddev->queue)
4133 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004134 mddev->queue->queuedata = mddev;
4135
NeilBrown8b765392009-01-09 08:31:08 +11004136 /* Can be unlocked because the queue is new: no concurrency */
4137 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4138
NeilBrown409c57f2009-03-31 14:39:39 +11004139 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004140
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 disk = alloc_disk(1 << shift);
4142 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004143 blk_cleanup_queue(mddev->queue);
4144 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004145 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004147 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004149 if (name)
4150 strcpy(disk->disk_name, name);
4151 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004153 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 disk->fops = &md_fops;
4156 disk->private_data = mddev;
4157 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004158 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004159 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004160 * remove it now.
4161 */
4162 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 add_disk(disk);
4164 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004165 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4166 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004167 if (error) {
4168 /* This isn't possible, but as kobject_init_and_add is marked
4169 * __must_check, we must do something with the result
4170 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004171 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4172 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004173 error = 0;
4174 }
NeilBrown43a70502009-12-14 12:49:55 +11004175 if (sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4176 printk(KERN_DEBUG "pointless warning\n");
NeilBrown0909dc42009-07-01 12:27:21 +10004177 abort:
4178 mutex_unlock(&disks_mutex);
4179 if (!error) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004180 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11004181 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
4182 }
NeilBrownd3374822009-01-09 08:31:10 +11004183 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004184 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004185}
4186
4187static struct kobject *md_probe(dev_t dev, int *part, void *data)
4188{
4189 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 return NULL;
4191}
4192
NeilBrownefeb53c2009-01-09 08:31:10 +11004193static int add_named_array(const char *val, struct kernel_param *kp)
4194{
4195 /* val must be "md_*" where * is not all digits.
4196 * We allocate an array with a large free minor number, and
4197 * set the name to val. val must not already be an active name.
4198 */
4199 int len = strlen(val);
4200 char buf[DISK_NAME_LEN];
4201
4202 while (len && val[len-1] == '\n')
4203 len--;
4204 if (len >= DISK_NAME_LEN)
4205 return -E2BIG;
4206 strlcpy(buf, val, len+1);
4207 if (strncmp(buf, "md_", 3) != 0)
4208 return -EINVAL;
4209 return md_alloc(0, buf);
4210}
4211
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212static void md_safemode_timeout(unsigned long data)
4213{
4214 mddev_t *mddev = (mddev_t *) data;
4215
Neil Brown0fd62b82008-06-28 08:31:36 +10004216 if (!atomic_read(&mddev->writes_pending)) {
4217 mddev->safemode = 1;
4218 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11004219 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 md_wakeup_thread(mddev->thread);
4222}
4223
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004224static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225
4226static int do_md_run(mddev_t * mddev)
4227{
NeilBrown2604b702006-01-06 00:20:36 -08004228 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 mdk_rdev_t *rdev;
4230 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08004231 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232
NeilBrowna757e642005-04-16 15:26:42 -07004233 if (list_empty(&mddev->disks))
4234 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
4237 if (mddev->pers)
4238 return -EBUSY;
4239
4240 /*
4241 * Analyze all RAID superblock(s)
4242 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004243 if (!mddev->raid_disks) {
4244 if (!mddev->persistent)
4245 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004246 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
NeilBrownd9d166c2006-01-06 00:20:51 -08004249 if (mddev->level != LEVEL_NONE)
4250 request_module("md-level-%d", mddev->level);
4251 else if (mddev->clevel[0])
4252 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253
4254 /*
4255 * Drop all container device buffers, from now on
4256 * the only valid external interface is through the md
4257 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004259 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004260 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 continue;
4262 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004263 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004264
4265 /* perform some consistency tests on the device.
4266 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004267 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004268 */
Andre Noll0f420352008-07-11 22:02:23 +10004269 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004270 if (mddev->dev_sectors &&
4271 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004272 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004273 printk("md: %s: data overlaps metadata\n",
4274 mdname(mddev));
4275 return -EINVAL;
4276 }
4277 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004278 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004279 > rdev->data_offset) {
4280 printk("md: %s: metadata overlaps data\n",
4281 mdname(mddev));
4282 return -EINVAL;
4283 }
4284 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004285 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 }
4287
4288 md_probe(mddev->unit, NULL, NULL);
4289 disk = mddev->gendisk;
4290 if (!disk)
4291 return -ENOMEM;
4292
4293 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004294 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004295 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004297 if (mddev->level != LEVEL_NONE)
4298 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4299 mddev->level);
4300 else
4301 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4302 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 return -EINVAL;
4304 }
NeilBrown2604b702006-01-06 00:20:36 -08004305 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004307 if (mddev->level != pers->level) {
4308 mddev->level = pers->level;
4309 mddev->new_level = pers->level;
4310 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004311 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312
NeilBrownf6705572006-03-27 01:18:11 -08004313 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004314 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004315 /* This personality cannot handle reshaping... */
4316 mddev->pers = NULL;
4317 module_put(pers->owner);
4318 return -EINVAL;
4319 }
4320
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004321 if (pers->sync_request) {
4322 /* Warn if this is a potentially silly
4323 * configuration.
4324 */
4325 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4326 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004327 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004328
4329 list_for_each_entry(rdev, &mddev->disks, same_set)
4330 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004331 if (rdev < rdev2 &&
4332 rdev->bdev->bd_contains ==
4333 rdev2->bdev->bd_contains) {
4334 printk(KERN_WARNING
4335 "%s: WARNING: %s appears to be"
4336 " on the same physical disk as"
4337 " %s.\n",
4338 mdname(mddev),
4339 bdevname(rdev->bdev,b),
4340 bdevname(rdev2->bdev,b2));
4341 warned = 1;
4342 }
4343 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004344
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004345 if (warned)
4346 printk(KERN_WARNING
4347 "True protection against single-disk"
4348 " failure might be compromised.\n");
4349 }
4350
NeilBrown657390d2005-08-26 18:34:16 -07004351 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004352 /* may be over-ridden by personality */
4353 mddev->resync_max_sectors = mddev->dev_sectors;
4354
NeilBrowna9701a32005-11-08 21:39:34 -08004355 mddev->barriers_work = 1;
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004356 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
NeilBrownf91de922005-11-08 21:39:36 -08004358 if (start_readonly)
4359 mddev->ro = 2; /* read-only, but switch on first write */
4360
NeilBrownb15c2e52006-01-06 00:20:16 -08004361 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004362 if (err)
4363 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004364 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4365 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4366 " but 'external_size' not in effect?\n", __func__);
4367 printk(KERN_ERR
4368 "md: invalid array_size %llu > default size %llu\n",
4369 (unsigned long long)mddev->array_sectors / 2,
4370 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4371 err = -EINVAL;
4372 mddev->pers->stop(mddev);
4373 }
4374 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004375 err = bitmap_create(mddev);
4376 if (err) {
4377 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4378 mdname(mddev), err);
4379 mddev->pers->stop(mddev);
4380 }
4381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 module_put(mddev->pers->owner);
4384 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004385 bitmap_destroy(mddev);
4386 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004388 if (mddev->pers->sync_request) {
4389 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4390 printk(KERN_WARNING
4391 "md: cannot register extra attributes for %s\n",
4392 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004393 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004394 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004395 mddev->ro = 0;
4396
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004398 atomic_set(&mddev->max_corr_read_errors,
4399 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 mddev->safemode = 0;
4401 mddev->safemode_timer.function = md_safemode_timeout;
4402 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004403 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004405
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004406 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004407 if (rdev->raid_disk >= 0) {
4408 char nm[20];
4409 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004410 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4411 printk("md: cannot register %s for %s\n",
4412 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414
4415 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4416
NeilBrown850b2b42006-10-03 01:15:46 -07004417 if (mddev->flags)
4418 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419
Andre Nollf233ea52008-07-21 17:05:22 +10004420 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004422 /* If there is a partially-recovered drive we need to
4423 * start recovery here. If we leave it to md_check_recovery,
4424 * it will remove the drives and not do the right thing
4425 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07004426 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004427 int spares = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004428 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004429 if (rdev->raid_disk >= 0 &&
4430 !test_bit(In_sync, &rdev->flags) &&
4431 !test_bit(Faulty, &rdev->flags))
4432 /* complete an interrupted recovery */
4433 spares++;
4434 if (spares && mddev->pers->sync_request) {
4435 mddev->recovery = 0;
4436 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4437 mddev->sync_thread = md_register_thread(md_do_sync,
4438 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10004439 "resync");
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004440 if (!mddev->sync_thread) {
4441 printk(KERN_ERR "%s: could not start resync"
4442 " thread...\n",
4443 mdname(mddev));
4444 /* leave the spares where they are, it shouldn't hurt */
4445 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07004446 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004447 }
4448 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07004449 md_wakeup_thread(mddev->thread);
4450 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004451
NeilBrown449aad32009-08-03 10:59:58 +10004452 revalidate_disk(mddev->gendisk);
Linus Torvalds44ce6292007-05-09 18:51:36 -07004453 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08004454 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004455 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004456 if (mddev->sysfs_action)
4457 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004458 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09004459 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 return 0;
4461}
4462
4463static int restart_array(mddev_t *mddev)
4464{
4465 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466
Andre Noll80fab1d2008-07-11 22:02:21 +10004467 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004469 return -ENXIO;
4470 if (!mddev->pers)
4471 return -EINVAL;
4472 if (!mddev->ro)
4473 return -EBUSY;
4474 mddev->safemode = 0;
4475 mddev->ro = 0;
4476 set_disk_ro(disk, 0);
4477 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4478 mdname(mddev));
4479 /* Kick recovery or resync if necessary */
4480 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4481 md_wakeup_thread(mddev->thread);
4482 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004483 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004484 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485}
4486
NeilBrownacc55e22006-06-26 00:27:47 -07004487/* similar to deny_write_access, but accounts for our holding a reference
4488 * to the file ourselves */
4489static int deny_bitmap_write_access(struct file * file)
4490{
4491 struct inode *inode = file->f_mapping->host;
4492
4493 spin_lock(&inode->i_lock);
4494 if (atomic_read(&inode->i_writecount) > 1) {
4495 spin_unlock(&inode->i_lock);
4496 return -ETXTBSY;
4497 }
4498 atomic_set(&inode->i_writecount, -1);
4499 spin_unlock(&inode->i_lock);
4500
4501 return 0;
4502}
4503
NeilBrown43a70502009-12-14 12:49:55 +11004504void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004505{
4506 struct inode *inode = file->f_mapping->host;
4507
4508 spin_lock(&inode->i_lock);
4509 atomic_set(&inode->i_writecount, 1);
4510 spin_unlock(&inode->i_lock);
4511}
4512
NeilBrown9e653b62006-06-26 00:27:58 -07004513/* mode:
4514 * 0 - completely stop and dis-assemble array
4515 * 1 - switch to readonly
4516 * 2 - stop but do not disassemble array
4517 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004518static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519{
4520 int err = 0;
4521 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004522 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523
NeilBrownc8c00a62009-08-10 12:50:52 +10004524 mutex_lock(&mddev->open_mutex);
NeilBrownf2ea68c2008-07-21 17:05:25 +10004525 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004526 printk("md: %s still in use.\n",mdname(mddev));
NeilBrownc8c00a62009-08-10 12:50:52 +10004527 err = -EBUSY;
4528 } else if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
4530 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004531 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4533 md_unregister_thread(mddev->sync_thread);
4534 mddev->sync_thread = NULL;
4535 }
4536
4537 del_timer_sync(&mddev->safemode_timer);
4538
NeilBrown9e653b62006-06-26 00:27:58 -07004539 switch(mode) {
4540 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004542 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 goto out;
4544 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004545 break;
4546 case 0: /* disassemble */
4547 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004548 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004549 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 if (mddev->ro)
4551 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004552
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004554 mddev->queue->merge_bvec_fn = NULL;
4555 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004556 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 module_put(mddev->pers->owner);
Dan Williams5fd3a172009-03-04 00:57:25 -07004558 if (mddev->pers->sync_request)
4559 mddev->private = &md_redundancy_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004561 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004562 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004563
NeilBrownc4647292009-05-07 12:51:06 +10004564 list_for_each_entry(rdev, &mddev->disks, same_set)
4565 if (rdev->raid_disk >= 0) {
4566 char nm[20];
4567 sprintf(nm, "rd%d", rdev->raid_disk);
4568 sysfs_remove_link(&mddev->kobj, nm);
4569 }
4570
NeilBrown0d4ca602006-12-10 02:20:44 -08004571 set_capacity(disk, 0);
Linus Torvalds44ce6292007-05-09 18:51:36 -07004572 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08004573
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 if (mddev->ro)
4575 mddev->ro = 0;
4576 }
NeilBrown850b2b42006-10-03 01:15:46 -07004577 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 /* mark array as shutdown cleanly */
4579 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07004580 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 }
NeilBrown9e653b62006-06-26 00:27:58 -07004582 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004584 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown80ffb3c2009-08-18 10:35:26 +10004585 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 }
NeilBrownc8c00a62009-08-10 12:50:52 +10004587out:
4588 mutex_unlock(&mddev->open_mutex);
4589 if (err)
4590 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 /*
4592 * Free resources if final stop
4593 */
NeilBrown9e653b62006-06-26 00:27:58 -07004594 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004595
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4597
NeilBrown978f9462006-02-02 14:28:05 -08004598 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004599 if (mddev->bitmap_info.file) {
4600 restore_bitmap_write_access(mddev->bitmap_info.file);
4601 fput(mddev->bitmap_info.file);
4602 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004603 }
NeilBrownc3d97142009-12-14 12:49:52 +11004604 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004605
NeilBrown177a99b2008-02-06 01:39:56 -08004606 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07004607 flush_scheduled_work();
4608
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 export_array(mddev);
4610
Andre Nollf233ea52008-07-21 17:05:22 +10004611 mddev->array_sectors = 0;
Dan Williamsb522adc2009-03-31 15:00:31 +11004612 mddev->external_size = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004613 mddev->dev_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004614 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004615 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004616 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004617 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004618 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004619 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004620 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004621 mddev->level = LEVEL_NONE;
4622 mddev->clevel[0] = 0;
4623 mddev->flags = 0;
4624 mddev->ro = 0;
4625 mddev->metadata_type[0] = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10004626 mddev->chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004627 mddev->ctime = mddev->utime = 0;
4628 mddev->layout = 0;
4629 mddev->max_disks = 0;
4630 mddev->events = 0;
4631 mddev->delta_disks = 0;
4632 mddev->new_level = LEVEL_NONE;
4633 mddev->new_layout = 0;
Andre Noll664e7c42009-06-18 08:45:27 +10004634 mddev->new_chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004635 mddev->curr_resync = 0;
4636 mddev->resync_mismatches = 0;
4637 mddev->suspend_lo = mddev->suspend_hi = 0;
4638 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4639 mddev->recovery = 0;
4640 mddev->in_sync = 0;
4641 mddev->changed = 0;
4642 mddev->degraded = 0;
4643 mddev->barriers_work = 0;
4644 mddev->safemode = 0;
NeilBrownc3d97142009-12-14 12:49:52 +11004645 mddev->bitmap_info.offset = 0;
4646 mddev->bitmap_info.default_offset = 0;
NeilBrown42a04b52009-12-14 12:49:53 +11004647 mddev->bitmap_info.chunksize = 0;
4648 mddev->bitmap_info.daemon_sleep = 0;
4649 mddev->bitmap_info.max_write_behind = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004650 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004651 if (mddev->hold_active == UNTIL_STOP)
4652 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004653
NeilBrowna8a55c32006-06-26 00:27:39 -07004654 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4656 mdname(mddev));
4657 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004658 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004659 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004660 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 return err;
4662}
4663
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004664#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665static void autorun_array(mddev_t *mddev)
4666{
4667 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 int err;
4669
NeilBrowna757e642005-04-16 15:26:42 -07004670 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672
4673 printk(KERN_INFO "md: running: ");
4674
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004675 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 char b[BDEVNAME_SIZE];
4677 printk("<%s>", bdevname(rdev->bdev,b));
4678 }
4679 printk("\n");
4680
NeilBrownd710e132008-10-13 11:55:12 +11004681 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 if (err) {
4683 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004684 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 }
4686}
4687
4688/*
4689 * lets try to run arrays based on all disks that have arrived
4690 * until now. (those are in pending_raid_disks)
4691 *
4692 * the method: pick the first pending disk, collect all disks with
4693 * the same UUID, remove all from the pending list and put them into
4694 * the 'same_array' list. Then order this list based on superblock
4695 * update time (freshest comes first), kick out 'old' disks and
4696 * compare superblocks. If everything's fine then run it.
4697 *
4698 * If "unit" is allocated, then bump its reference count
4699 */
4700static void autorun_devices(int part)
4701{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004702 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 mddev_t *mddev;
4704 char b[BDEVNAME_SIZE];
4705
4706 printk(KERN_INFO "md: autorun ...\n");
4707 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004708 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004710 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 rdev0 = list_entry(pending_raid_disks.next,
4712 mdk_rdev_t, same_set);
4713
4714 printk(KERN_INFO "md: considering %s ...\n",
4715 bdevname(rdev0->bdev,b));
4716 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004717 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 if (super_90_load(rdev, rdev0, 0) >= 0) {
4719 printk(KERN_INFO "md: adding %s ...\n",
4720 bdevname(rdev->bdev,b));
4721 list_move(&rdev->same_set, &candidates);
4722 }
4723 /*
4724 * now we have a set of devices, with all of them having
4725 * mostly sane superblocks. It's time to allocate the
4726 * mddev.
4727 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004728 if (part) {
4729 dev = MKDEV(mdp_major,
4730 rdev0->preferred_minor << MdpMinorShift);
4731 unit = MINOR(dev) >> MdpMinorShift;
4732 } else {
4733 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4734 unit = MINOR(dev);
4735 }
4736 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4738 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4739 break;
4740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741
4742 md_probe(dev, NULL, NULL);
4743 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004744 if (!mddev || !mddev->gendisk) {
4745 if (mddev)
4746 mddev_put(mddev);
4747 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 "md: cannot allocate memory for md drive.\n");
4749 break;
4750 }
4751 if (mddev_lock(mddev))
4752 printk(KERN_WARNING "md: %s locked, cannot run\n",
4753 mdname(mddev));
4754 else if (mddev->raid_disks || mddev->major_version
4755 || !list_empty(&mddev->disks)) {
4756 printk(KERN_WARNING
4757 "md: %s already running, cannot run %s\n",
4758 mdname(mddev), bdevname(rdev0->bdev,b));
4759 mddev_unlock(mddev);
4760 } else {
4761 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004762 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004763 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 list_del_init(&rdev->same_set);
4765 if (bind_rdev_to_array(rdev, mddev))
4766 export_rdev(rdev);
4767 }
4768 autorun_array(mddev);
4769 mddev_unlock(mddev);
4770 }
4771 /* on success, candidates will be empty, on error
4772 * it won't...
4773 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004774 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004775 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 mddev_put(mddev);
4779 }
4780 printk(KERN_INFO "md: ... autorun DONE.\n");
4781}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004782#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784static int get_version(void __user * arg)
4785{
4786 mdu_version_t ver;
4787
4788 ver.major = MD_MAJOR_VERSION;
4789 ver.minor = MD_MINOR_VERSION;
4790 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4791
4792 if (copy_to_user(arg, &ver, sizeof(ver)))
4793 return -EFAULT;
4794
4795 return 0;
4796}
4797
4798static int get_array_info(mddev_t * mddev, void __user * arg)
4799{
4800 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004801 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803
NeilBrowna9f326e2009-09-23 18:06:41 +10004804 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004805 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004807 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 failed++;
4809 else {
4810 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004811 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004812 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 else
4814 spare++;
4815 }
4816 }
4817
4818 info.major_version = mddev->major_version;
4819 info.minor_version = mddev->minor_version;
4820 info.patch_version = MD_PATCHLEVEL_VERSION;
4821 info.ctime = mddev->ctime;
4822 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004823 info.size = mddev->dev_sectors / 2;
4824 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004825 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 info.nr_disks = nr;
4827 info.raid_disks = mddev->raid_disks;
4828 info.md_minor = mddev->md_minor;
4829 info.not_persistent= !mddev->persistent;
4830
4831 info.utime = mddev->utime;
4832 info.state = 0;
4833 if (mddev->in_sync)
4834 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11004835 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07004836 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004837 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 info.working_disks = working;
4839 info.failed_disks = failed;
4840 info.spare_disks = spare;
4841
4842 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004843 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844
4845 if (copy_to_user(arg, &info, sizeof(info)))
4846 return -EFAULT;
4847
4848 return 0;
4849}
4850
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004851static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004852{
4853 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4854 char *ptr, *buf = NULL;
4855 int err = -ENOMEM;
4856
Dan Williamsb5470dc2008-06-27 21:44:04 -07004857 if (md_allow_write(mddev))
4858 file = kmalloc(sizeof(*file), GFP_NOIO);
4859 else
4860 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004861
NeilBrown32a76272005-06-21 17:17:14 -07004862 if (!file)
4863 goto out;
4864
4865 /* bitmap disabled, zero the first byte and copy out */
4866 if (!mddev->bitmap || !mddev->bitmap->file) {
4867 file->pathname[0] = '\0';
4868 goto copy_out;
4869 }
4870
4871 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4872 if (!buf)
4873 goto out;
4874
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004875 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4876 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004877 goto out;
4878
4879 strcpy(file->pathname, ptr);
4880
4881copy_out:
4882 err = 0;
4883 if (copy_to_user(arg, file, sizeof(*file)))
4884 err = -EFAULT;
4885out:
4886 kfree(buf);
4887 kfree(file);
4888 return err;
4889}
4890
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891static int get_disk_info(mddev_t * mddev, void __user * arg)
4892{
4893 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 mdk_rdev_t *rdev;
4895
4896 if (copy_from_user(&info, arg, sizeof(info)))
4897 return -EFAULT;
4898
Andre Noll26ef3792008-07-11 22:02:21 +10004899 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 if (rdev) {
4901 info.major = MAJOR(rdev->bdev->bd_dev);
4902 info.minor = MINOR(rdev->bdev->bd_dev);
4903 info.raid_disk = rdev->raid_disk;
4904 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004905 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004907 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 info.state |= (1<<MD_DISK_ACTIVE);
4909 info.state |= (1<<MD_DISK_SYNC);
4910 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004911 if (test_bit(WriteMostly, &rdev->flags))
4912 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 } else {
4914 info.major = info.minor = 0;
4915 info.raid_disk = -1;
4916 info.state = (1<<MD_DISK_REMOVED);
4917 }
4918
4919 if (copy_to_user(arg, &info, sizeof(info)))
4920 return -EFAULT;
4921
4922 return 0;
4923}
4924
4925static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4926{
4927 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4928 mdk_rdev_t *rdev;
4929 dev_t dev = MKDEV(info->major,info->minor);
4930
4931 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4932 return -EOVERFLOW;
4933
4934 if (!mddev->raid_disks) {
4935 int err;
4936 /* expecting a device which has a superblock */
4937 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4938 if (IS_ERR(rdev)) {
4939 printk(KERN_WARNING
4940 "md: md_import_device returned %ld\n",
4941 PTR_ERR(rdev));
4942 return PTR_ERR(rdev);
4943 }
4944 if (!list_empty(&mddev->disks)) {
4945 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4946 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10004947 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 .load_super(rdev, rdev0, mddev->minor_version);
4949 if (err < 0) {
4950 printk(KERN_WARNING
4951 "md: %s has different UUID to %s\n",
4952 bdevname(rdev->bdev,b),
4953 bdevname(rdev0->bdev,b2));
4954 export_rdev(rdev);
4955 return -EINVAL;
4956 }
4957 }
4958 err = bind_rdev_to_array(rdev, mddev);
4959 if (err)
4960 export_rdev(rdev);
4961 return err;
4962 }
4963
4964 /*
4965 * add_new_disk can be used once the array is assembled
4966 * to add "hot spares". They must already have a superblock
4967 * written
4968 */
4969 if (mddev->pers) {
4970 int err;
4971 if (!mddev->pers->hot_add_disk) {
4972 printk(KERN_WARNING
4973 "%s: personality does not support diskops!\n",
4974 mdname(mddev));
4975 return -EINVAL;
4976 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004977 if (mddev->persistent)
4978 rdev = md_import_device(dev, mddev->major_version,
4979 mddev->minor_version);
4980 else
4981 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 if (IS_ERR(rdev)) {
4983 printk(KERN_WARNING
4984 "md: md_import_device returned %ld\n",
4985 PTR_ERR(rdev));
4986 return PTR_ERR(rdev);
4987 }
NeilBrown41158c72005-06-21 17:17:25 -07004988 /* set save_raid_disk if appropriate */
4989 if (!mddev->persistent) {
4990 if (info->state & (1<<MD_DISK_SYNC) &&
4991 info->raid_disk < mddev->raid_disks)
4992 rdev->raid_disk = info->raid_disk;
4993 else
4994 rdev->raid_disk = -1;
4995 } else
4996 super_types[mddev->major_version].
4997 validate_super(mddev, rdev);
4998 rdev->saved_raid_disk = rdev->raid_disk;
4999
NeilBrownb2d444d2005-11-08 21:39:31 -08005000 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005001 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5002 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005003 else
5004 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005005
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 rdev->raid_disk = -1;
5007 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005008 if (!err && !mddev->pers->hot_remove_disk) {
5009 /* If there is hot_add_disk but no hot_remove_disk
5010 * then added disks for geometry changes,
5011 * and should be added immediately.
5012 */
5013 super_types[mddev->major_version].
5014 validate_super(mddev, rdev);
5015 err = mddev->pers->hot_add_disk(mddev, rdev);
5016 if (err)
5017 unbind_rdev_from_array(rdev);
5018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 if (err)
5020 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005021 else
NeilBrown3c0ee632008-10-21 13:25:28 +11005022 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005023
NeilBrown17571282006-12-10 02:20:52 -08005024 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005025 if (mddev->degraded)
5026 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005027 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07005028 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 return err;
5030 }
5031
5032 /* otherwise, add_new_disk is only allowed
5033 * for major_version==0 superblocks
5034 */
5035 if (mddev->major_version != 0) {
5036 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5037 mdname(mddev));
5038 return -EINVAL;
5039 }
5040
5041 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5042 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005043 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 if (IS_ERR(rdev)) {
5045 printk(KERN_WARNING
5046 "md: error, md_import_device() returned %ld\n",
5047 PTR_ERR(rdev));
5048 return PTR_ERR(rdev);
5049 }
5050 rdev->desc_nr = info->number;
5051 if (info->raid_disk < mddev->raid_disks)
5052 rdev->raid_disk = info->raid_disk;
5053 else
5054 rdev->raid_disk = -1;
5055
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005057 if (info->state & (1<<MD_DISK_SYNC))
5058 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005060 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5061 set_bit(WriteMostly, &rdev->flags);
5062
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 if (!mddev->persistent) {
5064 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10005065 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 } else
Andre Noll0f420352008-07-11 22:02:23 +10005067 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10005068 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069
NeilBrown2bf071b2006-01-06 00:20:55 -08005070 err = bind_rdev_to_array(rdev, mddev);
5071 if (err) {
5072 export_rdev(rdev);
5073 return err;
5074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 }
5076
5077 return 0;
5078}
5079
5080static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5081{
5082 char b[BDEVNAME_SIZE];
5083 mdk_rdev_t *rdev;
5084
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 rdev = find_rdev(mddev, dev);
5086 if (!rdev)
5087 return -ENXIO;
5088
5089 if (rdev->raid_disk >= 0)
5090 goto busy;
5091
5092 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005093 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005094 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
5096 return 0;
5097busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005098 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 bdevname(rdev->bdev,b), mdname(mddev));
5100 return -EBUSY;
5101}
5102
5103static int hot_add_disk(mddev_t * mddev, dev_t dev)
5104{
5105 char b[BDEVNAME_SIZE];
5106 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 mdk_rdev_t *rdev;
5108
5109 if (!mddev->pers)
5110 return -ENODEV;
5111
5112 if (mddev->major_version != 0) {
5113 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5114 " version-0 superblocks.\n",
5115 mdname(mddev));
5116 return -EINVAL;
5117 }
5118 if (!mddev->pers->hot_add_disk) {
5119 printk(KERN_WARNING
5120 "%s: personality does not support diskops!\n",
5121 mdname(mddev));
5122 return -EINVAL;
5123 }
5124
NeilBrownd710e132008-10-13 11:55:12 +11005125 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 if (IS_ERR(rdev)) {
5127 printk(KERN_WARNING
5128 "md: error, md_import_device() returned %ld\n",
5129 PTR_ERR(rdev));
5130 return -EINVAL;
5131 }
5132
5133 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10005134 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 else
Andre Noll0f420352008-07-11 22:02:23 +10005136 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137
NeilBrown8190e752009-06-18 08:48:58 +10005138 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139
NeilBrownb2d444d2005-11-08 21:39:31 -08005140 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 printk(KERN_WARNING
5142 "md: can not hot-add faulty %s disk to %s!\n",
5143 bdevname(rdev->bdev,b), mdname(mddev));
5144 err = -EINVAL;
5145 goto abort_export;
5146 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005147 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005149 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005150 err = bind_rdev_to_array(rdev, mddev);
5151 if (err)
5152 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153
5154 /*
5155 * The rest should better be atomic, we can have disk failures
5156 * noticed in interrupt contexts ...
5157 */
5158
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 rdev->raid_disk = -1;
5160
NeilBrown850b2b42006-10-03 01:15:46 -07005161 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162
5163 /*
5164 * Kick recovery, maybe this spare has to be added to the
5165 * array immediately.
5166 */
5167 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5168 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005169 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 return 0;
5171
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172abort_export:
5173 export_rdev(rdev);
5174 return err;
5175}
5176
NeilBrown32a76272005-06-21 17:17:14 -07005177static int set_bitmap_file(mddev_t *mddev, int fd)
5178{
5179 int err;
5180
NeilBrown36fa3062005-09-09 16:23:45 -07005181 if (mddev->pers) {
5182 if (!mddev->pers->quiesce)
5183 return -EBUSY;
5184 if (mddev->recovery || mddev->sync_thread)
5185 return -EBUSY;
5186 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005187 }
5188
NeilBrown36fa3062005-09-09 16:23:45 -07005189
5190 if (fd >= 0) {
5191 if (mddev->bitmap)
5192 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005193 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005194
NeilBrownc3d97142009-12-14 12:49:52 +11005195 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005196 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5197 mdname(mddev));
5198 return -EBADF;
5199 }
5200
NeilBrownc3d97142009-12-14 12:49:52 +11005201 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005202 if (err) {
5203 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5204 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005205 fput(mddev->bitmap_info.file);
5206 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005207 return err;
5208 }
NeilBrownc3d97142009-12-14 12:49:52 +11005209 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005210 } else if (mddev->bitmap == NULL)
5211 return -ENOENT; /* cannot remove what isn't there */
5212 err = 0;
5213 if (mddev->pers) {
5214 mddev->pers->quiesce(mddev, 1);
5215 if (fd >= 0)
5216 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005217 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005218 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005219 fd = -1; /* make sure to put the file */
5220 }
NeilBrown36fa3062005-09-09 16:23:45 -07005221 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005222 }
5223 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005224 if (mddev->bitmap_info.file) {
5225 restore_bitmap_write_access(mddev->bitmap_info.file);
5226 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005227 }
NeilBrownc3d97142009-12-14 12:49:52 +11005228 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005229 }
5230
NeilBrown32a76272005-06-21 17:17:14 -07005231 return err;
5232}
5233
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234/*
5235 * set_array_info is used two different ways
5236 * The original usage is when creating a new array.
5237 * In this usage, raid_disks is > 0 and it together with
5238 * level, size, not_persistent,layout,chunksize determine the
5239 * shape of the array.
5240 * This will always create an array with a type-0.90.0 superblock.
5241 * The newer usage is when assembling an array.
5242 * In this case raid_disks will be 0, and the major_version field is
5243 * use to determine which style super-blocks are to be found on the devices.
5244 * The minor and patch _version numbers are also kept incase the
5245 * super_block handler wishes to interpret them.
5246 */
5247static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5248{
5249
5250 if (info->raid_disks == 0) {
5251 /* just setting version number for superblock loading */
5252 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005253 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 super_types[info->major_version].name == NULL) {
5255 /* maybe try to auto-load a module? */
5256 printk(KERN_INFO
5257 "md: superblock version %d not known\n",
5258 info->major_version);
5259 return -EINVAL;
5260 }
5261 mddev->major_version = info->major_version;
5262 mddev->minor_version = info->minor_version;
5263 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005264 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 return 0;
5266 }
5267 mddev->major_version = MD_MAJOR_VERSION;
5268 mddev->minor_version = MD_MINOR_VERSION;
5269 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5270 mddev->ctime = get_seconds();
5271
5272 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005273 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005274 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 mddev->raid_disks = info->raid_disks;
5276 /* don't set md_minor, it is determined by which /dev/md* was
5277 * openned
5278 */
5279 if (info->state & (1<<MD_SB_CLEAN))
5280 mddev->recovery_cp = MaxSector;
5281 else
5282 mddev->recovery_cp = 0;
5283 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005284 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285
5286 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005287 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288
5289 mddev->max_disks = MD_SB_DISKS;
5290
NeilBrowne6910632008-02-06 01:39:51 -08005291 if (mddev->persistent)
5292 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005293 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294
NeilBrownc3d97142009-12-14 12:49:52 +11005295 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5296 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005297
NeilBrownf6705572006-03-27 01:18:11 -08005298 mddev->reshape_position = MaxSector;
5299
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 /*
5301 * Generate a 128 bit UUID
5302 */
5303 get_random_bytes(mddev->uuid, 16);
5304
NeilBrownf6705572006-03-27 01:18:11 -08005305 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005306 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005307 mddev->new_layout = mddev->layout;
5308 mddev->delta_disks = 0;
5309
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 return 0;
5311}
5312
Dan Williams1f403622009-03-31 14:59:03 +11005313void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5314{
Dan Williamsb522adc2009-03-31 15:00:31 +11005315 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5316
5317 if (mddev->external_size)
5318 return;
5319
Dan Williams1f403622009-03-31 14:59:03 +11005320 mddev->array_sectors = array_sectors;
5321}
5322EXPORT_SYMBOL(md_set_array_sectors);
5323
Andre Nolld71f9f82008-07-11 22:02:22 +10005324static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005325{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005326 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005327 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005328 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005329
5330 if (mddev->pers->resize == NULL)
5331 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005332 /* The "num_sectors" is the number of sectors of each device that
5333 * is used. This can only make sense for arrays with redundancy.
5334 * linear and raid0 always use whatever space is available. We can only
5335 * consider changing this number if no resync or reconstruction is
5336 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005337 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005338 * of each device. If num_sectors is zero, we find the largest size
5339 * that fits.
5340
NeilBrowna35b0d62006-01-06 00:20:49 -08005341 */
5342 if (mddev->sync_thread)
5343 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005344 if (mddev->bitmap)
5345 /* Sorry, cannot grow a bitmap yet, just remove it,
5346 * grow, and re-add.
5347 */
5348 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005349 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005350 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005351
Andre Nolld71f9f82008-07-11 22:02:22 +10005352 if (fit && (num_sectors == 0 || num_sectors > avail))
5353 num_sectors = avail;
5354 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005355 return -ENOSPC;
5356 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005357 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005358 if (!rv)
5359 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005360 return rv;
5361}
5362
NeilBrownda943b92006-01-06 00:20:54 -08005363static int update_raid_disks(mddev_t *mddev, int raid_disks)
5364{
5365 int rv;
5366 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005367 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b92006-01-06 00:20:54 -08005368 return -EINVAL;
5369 if (raid_disks <= 0 ||
5370 raid_disks >= mddev->max_disks)
5371 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005372 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b92006-01-06 00:20:54 -08005373 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005374 mddev->delta_disks = raid_disks - mddev->raid_disks;
5375
5376 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b92006-01-06 00:20:54 -08005377 return rv;
5378}
5379
5380
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381/*
5382 * update_array_info is used to change the configuration of an
5383 * on-line array.
5384 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5385 * fields in the info are checked against the array.
5386 * Any differences that cannot be handled will cause an error.
5387 * Normally, only one change can be managed at a time.
5388 */
5389static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5390{
5391 int rv = 0;
5392 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005393 int state = 0;
5394
5395 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005396 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005397 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398
5399 if (mddev->major_version != info->major_version ||
5400 mddev->minor_version != info->minor_version ||
5401/* mddev->patch_version != info->patch_version || */
5402 mddev->ctime != info->ctime ||
5403 mddev->level != info->level ||
5404/* mddev->layout != info->layout || */
5405 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005406 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005407 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5408 ((state^info->state) & 0xfffffe00)
5409 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 return -EINVAL;
5411 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005412 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5413 cnt++;
5414 if (mddev->raid_disks != info->raid_disks)
5415 cnt++;
5416 if (mddev->layout != info->layout)
5417 cnt++;
5418 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5419 cnt++;
5420 if (cnt == 0)
5421 return 0;
5422 if (cnt > 1)
5423 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424
5425 if (mddev->layout != info->layout) {
5426 /* Change layout
5427 * we don't need to do anything at the md level, the
5428 * personality will take care of it all.
5429 */
NeilBrown50ac1682009-06-18 08:47:55 +10005430 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005432 else {
5433 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005434 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005435 if (rv)
5436 mddev->new_layout = mddev->layout;
5437 return rv;
5438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005440 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005441 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442
NeilBrownda943b92006-01-06 00:20:54 -08005443 if (mddev->raid_disks != info->raid_disks)
5444 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445
NeilBrown36fa3062005-09-09 16:23:45 -07005446 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5447 if (mddev->pers->quiesce == NULL)
5448 return -EINVAL;
5449 if (mddev->recovery || mddev->sync_thread)
5450 return -EBUSY;
5451 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5452 /* add the bitmap */
5453 if (mddev->bitmap)
5454 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005455 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005456 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005457 mddev->bitmap_info.offset =
5458 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005459 mddev->pers->quiesce(mddev, 1);
5460 rv = bitmap_create(mddev);
5461 if (rv)
5462 bitmap_destroy(mddev);
5463 mddev->pers->quiesce(mddev, 0);
5464 } else {
5465 /* remove the bitmap */
5466 if (!mddev->bitmap)
5467 return -ENOENT;
5468 if (mddev->bitmap->file)
5469 return -EINVAL;
5470 mddev->pers->quiesce(mddev, 1);
5471 bitmap_destroy(mddev);
5472 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005473 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005474 }
5475 }
NeilBrown850b2b42006-10-03 01:15:46 -07005476 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 return rv;
5478}
5479
5480static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5481{
5482 mdk_rdev_t *rdev;
5483
5484 if (mddev->pers == NULL)
5485 return -ENODEV;
5486
5487 rdev = find_rdev(mddev, dev);
5488 if (!rdev)
5489 return -ENODEV;
5490
5491 md_error(mddev, rdev);
5492 return 0;
5493}
5494
Andre Noll2f9618c2008-04-25 18:57:58 +02005495/*
5496 * We have a problem here : there is no easy way to give a CHS
5497 * virtual geometry. We currently pretend that we have a 2 heads
5498 * 4 sectors (with a BIG number of cylinders...). This drives
5499 * dosfs just mad... ;-)
5500 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005501static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5502{
5503 mddev_t *mddev = bdev->bd_disk->private_data;
5504
5505 geo->heads = 2;
5506 geo->sectors = 4;
5507 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5508 return 0;
5509}
5510
Al Viroa39907f2008-03-02 10:31:15 -05005511static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 unsigned int cmd, unsigned long arg)
5513{
5514 int err = 0;
5515 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 mddev_t *mddev = NULL;
5517
5518 if (!capable(CAP_SYS_ADMIN))
5519 return -EACCES;
5520
5521 /*
5522 * Commands dealing with the RAID driver but not any
5523 * particular array:
5524 */
5525 switch (cmd)
5526 {
5527 case RAID_VERSION:
5528 err = get_version(argp);
5529 goto done;
5530
5531 case PRINT_RAID_DEBUG:
5532 err = 0;
5533 md_print_devices();
5534 goto done;
5535
5536#ifndef MODULE
5537 case RAID_AUTORUN:
5538 err = 0;
5539 autostart_arrays(arg);
5540 goto done;
5541#endif
5542 default:;
5543 }
5544
5545 /*
5546 * Commands creating/starting a new array:
5547 */
5548
Al Viroa39907f2008-03-02 10:31:15 -05005549 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550
5551 if (!mddev) {
5552 BUG();
5553 goto abort;
5554 }
5555
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 err = mddev_lock(mddev);
5557 if (err) {
5558 printk(KERN_INFO
5559 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5560 err, cmd);
5561 goto abort;
5562 }
5563
5564 switch (cmd)
5565 {
5566 case SET_ARRAY_INFO:
5567 {
5568 mdu_array_info_t info;
5569 if (!arg)
5570 memset(&info, 0, sizeof(info));
5571 else if (copy_from_user(&info, argp, sizeof(info))) {
5572 err = -EFAULT;
5573 goto abort_unlock;
5574 }
5575 if (mddev->pers) {
5576 err = update_array_info(mddev, &info);
5577 if (err) {
5578 printk(KERN_WARNING "md: couldn't update"
5579 " array info. %d\n", err);
5580 goto abort_unlock;
5581 }
5582 goto done_unlock;
5583 }
5584 if (!list_empty(&mddev->disks)) {
5585 printk(KERN_WARNING
5586 "md: array %s already has disks!\n",
5587 mdname(mddev));
5588 err = -EBUSY;
5589 goto abort_unlock;
5590 }
5591 if (mddev->raid_disks) {
5592 printk(KERN_WARNING
5593 "md: array %s already initialised!\n",
5594 mdname(mddev));
5595 err = -EBUSY;
5596 goto abort_unlock;
5597 }
5598 err = set_array_info(mddev, &info);
5599 if (err) {
5600 printk(KERN_WARNING "md: couldn't set"
5601 " array info. %d\n", err);
5602 goto abort_unlock;
5603 }
5604 }
5605 goto done_unlock;
5606
5607 default:;
5608 }
5609
5610 /*
5611 * Commands querying/configuring an existing array:
5612 */
NeilBrown32a76272005-06-21 17:17:14 -07005613 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005614 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005615 if ((!mddev->raid_disks && !mddev->external)
5616 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5617 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5618 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 err = -ENODEV;
5620 goto abort_unlock;
5621 }
5622
5623 /*
5624 * Commands even a read-only array can execute:
5625 */
5626 switch (cmd)
5627 {
5628 case GET_ARRAY_INFO:
5629 err = get_array_info(mddev, argp);
5630 goto done_unlock;
5631
NeilBrown32a76272005-06-21 17:17:14 -07005632 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005633 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005634 goto done_unlock;
5635
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 case GET_DISK_INFO:
5637 err = get_disk_info(mddev, argp);
5638 goto done_unlock;
5639
5640 case RESTART_ARRAY_RW:
5641 err = restart_array(mddev);
5642 goto done_unlock;
5643
5644 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005645 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 goto done_unlock;
5647
5648 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005649 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 goto done_unlock;
5651
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 }
5653
5654 /*
5655 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005656 * superblock, so we do not allow them on read-only arrays.
5657 * However non-MD ioctls (e.g. get-size) will still come through
5658 * here and hit the 'default' below, so only disallow
5659 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005661 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005662 if (mddev->ro == 2) {
5663 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005664 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005665 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5666 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005667 } else {
5668 err = -EROFS;
5669 goto abort_unlock;
5670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 }
5672
5673 switch (cmd)
5674 {
5675 case ADD_NEW_DISK:
5676 {
5677 mdu_disk_info_t info;
5678 if (copy_from_user(&info, argp, sizeof(info)))
5679 err = -EFAULT;
5680 else
5681 err = add_new_disk(mddev, &info);
5682 goto done_unlock;
5683 }
5684
5685 case HOT_REMOVE_DISK:
5686 err = hot_remove_disk(mddev, new_decode_dev(arg));
5687 goto done_unlock;
5688
5689 case HOT_ADD_DISK:
5690 err = hot_add_disk(mddev, new_decode_dev(arg));
5691 goto done_unlock;
5692
5693 case SET_DISK_FAULTY:
5694 err = set_disk_faulty(mddev, new_decode_dev(arg));
5695 goto done_unlock;
5696
5697 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005698 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 goto done_unlock;
5700
NeilBrown32a76272005-06-21 17:17:14 -07005701 case SET_BITMAP_FILE:
5702 err = set_bitmap_file(mddev, (int)arg);
5703 goto done_unlock;
5704
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 err = -EINVAL;
5707 goto abort_unlock;
5708 }
5709
5710done_unlock:
5711abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005712 if (mddev->hold_active == UNTIL_IOCTL &&
5713 err != -EINVAL)
5714 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 mddev_unlock(mddev);
5716
5717 return err;
5718done:
5719 if (err)
5720 MD_BUG();
5721abort:
5722 return err;
5723}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005724#ifdef CONFIG_COMPAT
5725static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5726 unsigned int cmd, unsigned long arg)
5727{
5728 switch (cmd) {
5729 case HOT_REMOVE_DISK:
5730 case HOT_ADD_DISK:
5731 case SET_DISK_FAULTY:
5732 case SET_BITMAP_FILE:
5733 /* These take in integer arg, do not convert */
5734 break;
5735 default:
5736 arg = (unsigned long)compat_ptr(arg);
5737 break;
5738 }
5739
5740 return md_ioctl(bdev, mode, cmd, arg);
5741}
5742#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743
Al Viroa39907f2008-03-02 10:31:15 -05005744static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745{
5746 /*
5747 * Succeed if we can lock the mddev, which confirms that
5748 * it isn't being stopped right now.
5749 */
NeilBrownd3374822009-01-09 08:31:10 +11005750 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 int err;
5752
NeilBrownd3374822009-01-09 08:31:10 +11005753 if (mddev->gendisk != bdev->bd_disk) {
5754 /* we are racing with mddev_put which is discarding this
5755 * bd_disk.
5756 */
5757 mddev_put(mddev);
5758 /* Wait until bdev->bd_disk is definitely gone */
5759 flush_scheduled_work();
5760 /* Then retry the open from the top */
5761 return -ERESTARTSYS;
5762 }
5763 BUG_ON(mddev != bdev->bd_disk->private_data);
5764
NeilBrownc8c00a62009-08-10 12:50:52 +10005765 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 goto out;
5767
5768 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005769 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005770 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771
Al Viroa39907f2008-03-02 10:31:15 -05005772 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 out:
5774 return err;
5775}
5776
Al Viroa39907f2008-03-02 10:31:15 -05005777static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778{
Al Viroa39907f2008-03-02 10:31:15 -05005779 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005781 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005782 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783 mddev_put(mddev);
5784
5785 return 0;
5786}
5787
Linus Torvalds44ce6292007-05-09 18:51:36 -07005788static int md_media_changed(struct gendisk *disk)
5789{
5790 mddev_t *mddev = disk->private_data;
5791
5792 return mddev->changed;
5793}
5794
5795static int md_revalidate(struct gendisk *disk)
5796{
5797 mddev_t *mddev = disk->private_data;
5798
5799 mddev->changed = 0;
5800 return 0;
5801}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005802static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803{
5804 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005805 .open = md_open,
5806 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005807 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005808#ifdef CONFIG_COMPAT
5809 .compat_ioctl = md_compat_ioctl,
5810#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005811 .getgeo = md_getgeo,
Linus Torvalds44ce6292007-05-09 18:51:36 -07005812 .media_changed = md_media_changed,
5813 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814};
5815
Adrian Bunk75c96f82005-05-05 16:16:09 -07005816static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817{
5818 mdk_thread_t *thread = arg;
5819
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 /*
5821 * md_thread is a 'system-thread', it's priority should be very
5822 * high. We avoid resource deadlocks individually in each
5823 * raid personality. (RAID5 does preallocation) We also use RR and
5824 * the very same RT priority as kswapd, thus we will never get
5825 * into a priority inversion deadlock.
5826 *
5827 * we definitely have to have equal or higher priority than
5828 * bdflush, otherwise bdflush will deadlock if there are too
5829 * many dirty RAID5 blocks.
5830 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831
NeilBrown6985c432005-10-19 21:23:47 -07005832 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005833 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834
NeilBrown93588e22005-11-15 00:09:12 -08005835 /* We need to wait INTERRUPTIBLE so that
5836 * we don't add to the load-average.
5837 * That means we need to be sure no signals are
5838 * pending
5839 */
5840 if (signal_pending(current))
5841 flush_signals(current);
5842
5843 wait_event_interruptible_timeout
5844 (thread->wqueue,
5845 test_bit(THREAD_WAKEUP, &thread->flags)
5846 || kthread_should_stop(),
5847 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848
5849 clear_bit(THREAD_WAKEUP, &thread->flags);
5850
NeilBrown787453c2005-11-08 21:39:43 -08005851 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005853
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 return 0;
5855}
5856
5857void md_wakeup_thread(mdk_thread_t *thread)
5858{
5859 if (thread) {
5860 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5861 set_bit(THREAD_WAKEUP, &thread->flags);
5862 wake_up(&thread->wqueue);
5863 }
5864}
5865
5866mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5867 const char *name)
5868{
5869 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870
NeilBrown9ffae0c2006-01-06 00:20:32 -08005871 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 if (!thread)
5873 return NULL;
5874
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 init_waitqueue_head(&thread->wqueue);
5876
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 thread->run = run;
5878 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005879 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10005880 thread->tsk = kthread_run(md_thread, thread,
5881 "%s_%s",
5882 mdname(thread->mddev),
5883 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07005884 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 kfree(thread);
5886 return NULL;
5887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 return thread;
5889}
5890
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891void md_unregister_thread(mdk_thread_t *thread)
5892{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005893 if (!thread)
5894 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005895 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005896
5897 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 kfree(thread);
5899}
5900
5901void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5902{
5903 if (!mddev) {
5904 MD_BUG();
5905 return;
5906 }
5907
NeilBrownb2d444d2005-11-08 21:39:31 -08005908 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005910
5911 if (mddev->external)
5912 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005913/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5915 mdname(mddev),
5916 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5917 __builtin_return_address(0),__builtin_return_address(1),
5918 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005919*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005920 if (!mddev->pers)
5921 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 if (!mddev->pers->error_handler)
5923 return;
5924 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005925 if (mddev->degraded)
5926 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005927 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5929 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5930 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005931 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932}
5933
5934/* seq_file implementation /proc/mdstat */
5935
5936static void status_unused(struct seq_file *seq)
5937{
5938 int i = 0;
5939 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940
5941 seq_printf(seq, "unused devices: ");
5942
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005943 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 char b[BDEVNAME_SIZE];
5945 i++;
5946 seq_printf(seq, "%s ",
5947 bdevname(rdev->bdev,b));
5948 }
5949 if (!i)
5950 seq_printf(seq, "<none>");
5951
5952 seq_printf(seq, "\n");
5953}
5954
5955
5956static void status_resync(struct seq_file *seq, mddev_t * mddev)
5957{
NeilBrowndd71cf62009-05-07 12:49:35 +10005958 sector_t max_sectors, resync, res;
5959 unsigned long dt, db;
5960 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08005961 int scale;
5962 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963
NeilBrowndd71cf62009-05-07 12:49:35 +10005964 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965
5966 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10005967 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 else
NeilBrowndd71cf62009-05-07 12:49:35 +10005969 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970
5971 /*
5972 * Should not happen.
5973 */
NeilBrowndd71cf62009-05-07 12:49:35 +10005974 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 MD_BUG();
5976 return;
5977 }
NeilBrown4588b422006-03-27 01:18:04 -08005978 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10005979 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08005980 * u32, as those are the requirements for sector_div.
5981 * Thus 'scale' must be at least 10
5982 */
5983 scale = 10;
5984 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10005985 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08005986 scale++;
5987 }
5988 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10005989 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08005990
5991 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 {
NeilBrown4588b422006-03-27 01:18:04 -08005993 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994 seq_printf(seq, "[");
5995 for (i = 0; i < x; i++)
5996 seq_printf(seq, "=");
5997 seq_printf(seq, ">");
5998 for (i = 0; i < y; i++)
5999 seq_printf(seq, ".");
6000 seq_printf(seq, "] ");
6001 }
NeilBrown4588b422006-03-27 01:18:04 -08006002 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006003 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6004 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006005 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6006 "check" :
6007 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6008 "resync" : "recovery"))),
6009 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006010 (unsigned long long) resync/2,
6011 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012
6013 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 * dt: time from mark until now
6015 * db: blocks written from mark until now
6016 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006017 *
6018 * rt is a sector_t, so could be 32bit or 64bit.
6019 * So we divide before multiply in case it is 32bit and close
6020 * to the limit.
6021 * We scale the divisor (db) by 32 to avoid loosing precision
6022 * near the end of resync when the number of remaining sectors
6023 * is close to 'db'.
6024 * We then divide rt by 32 after multiplying by db to compensate.
6025 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026 */
6027 dt = ((jiffies - mddev->resync_mark) / HZ);
6028 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006029 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6030 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031
NeilBrowndd71cf62009-05-07 12:49:35 +10006032 rt = max_sectors - resync; /* number of remaining sectors */
6033 sector_div(rt, db/32+1);
6034 rt *= dt;
6035 rt >>= 5;
6036
6037 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6038 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039
NeilBrownff4e8d92006-07-10 04:44:16 -07006040 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041}
6042
6043static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6044{
6045 struct list_head *tmp;
6046 loff_t l = *pos;
6047 mddev_t *mddev;
6048
6049 if (l >= 0x10000)
6050 return NULL;
6051 if (!l--)
6052 /* header */
6053 return (void*)1;
6054
6055 spin_lock(&all_mddevs_lock);
6056 list_for_each(tmp,&all_mddevs)
6057 if (!l--) {
6058 mddev = list_entry(tmp, mddev_t, all_mddevs);
6059 mddev_get(mddev);
6060 spin_unlock(&all_mddevs_lock);
6061 return mddev;
6062 }
6063 spin_unlock(&all_mddevs_lock);
6064 if (!l--)
6065 return (void*)2;/* tail */
6066 return NULL;
6067}
6068
6069static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6070{
6071 struct list_head *tmp;
6072 mddev_t *next_mddev, *mddev = v;
6073
6074 ++*pos;
6075 if (v == (void*)2)
6076 return NULL;
6077
6078 spin_lock(&all_mddevs_lock);
6079 if (v == (void*)1)
6080 tmp = all_mddevs.next;
6081 else
6082 tmp = mddev->all_mddevs.next;
6083 if (tmp != &all_mddevs)
6084 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6085 else {
6086 next_mddev = (void*)2;
6087 *pos = 0x10000;
6088 }
6089 spin_unlock(&all_mddevs_lock);
6090
6091 if (v != (void*)1)
6092 mddev_put(mddev);
6093 return next_mddev;
6094
6095}
6096
6097static void md_seq_stop(struct seq_file *seq, void *v)
6098{
6099 mddev_t *mddev = v;
6100
6101 if (mddev && v != (void*)1 && v != (void*)2)
6102 mddev_put(mddev);
6103}
6104
NeilBrownd7603b72006-01-06 00:20:30 -08006105struct mdstat_info {
6106 int event;
6107};
6108
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109static int md_seq_show(struct seq_file *seq, void *v)
6110{
6111 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006112 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006114 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006115 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116
6117 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006118 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 seq_printf(seq, "Personalities : ");
6120 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006121 list_for_each_entry(pers, &pers_list, list)
6122 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123
6124 spin_unlock(&pers_lock);
6125 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006126 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 return 0;
6128 }
6129 if (v == (void*)2) {
6130 status_unused(seq);
6131 return 0;
6132 }
6133
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006134 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006136
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6138 seq_printf(seq, "%s : %sactive", mdname(mddev),
6139 mddev->pers ? "" : "in");
6140 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006141 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006143 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006144 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145 seq_printf(seq, " %s", mddev->pers->name);
6146 }
6147
Andre Nolldd8ac332009-03-31 14:33:13 +11006148 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006149 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 char b[BDEVNAME_SIZE];
6151 seq_printf(seq, " %s[%d]",
6152 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006153 if (test_bit(WriteMostly, &rdev->flags))
6154 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006155 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 seq_printf(seq, "(F)");
6157 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006158 } else if (rdev->raid_disk < 0)
6159 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006160 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 }
6162
6163 if (!list_empty(&mddev->disks)) {
6164 if (mddev->pers)
6165 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006166 (unsigned long long)
6167 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 else
6169 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006170 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006172 if (mddev->persistent) {
6173 if (mddev->major_version != 0 ||
6174 mddev->minor_version != 90) {
6175 seq_printf(seq," super %d.%d",
6176 mddev->major_version,
6177 mddev->minor_version);
6178 }
NeilBrowne6910632008-02-06 01:39:51 -08006179 } else if (mddev->external)
6180 seq_printf(seq, " super external:%s",
6181 mddev->metadata_type);
6182 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006183 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184
6185 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006186 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006188 if (mddev->pers->sync_request) {
6189 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006190 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006191 seq_printf(seq, "\n ");
6192 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6193 seq_printf(seq, "\tresync=DELAYED\n ");
6194 else if (mddev->recovery_cp < MaxSector)
6195 seq_printf(seq, "\tresync=PENDING\n ");
6196 }
NeilBrown32a76272005-06-21 17:17:14 -07006197 } else
6198 seq_printf(seq, "\n ");
6199
6200 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006201 unsigned long chunk_kb;
6202 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006203 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006204 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006205 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6206 "%lu%s chunk",
6207 bitmap->pages - bitmap->missing_pages,
6208 bitmap->pages,
6209 (bitmap->pages - bitmap->missing_pages)
6210 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006211 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006212 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006213 if (bitmap->file) {
6214 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006215 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006216 }
NeilBrown78d742d2005-06-21 17:17:15 -07006217
NeilBrown32a76272005-06-21 17:17:14 -07006218 seq_printf(seq, "\n");
6219 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 }
6221
6222 seq_printf(seq, "\n");
6223 }
6224 mddev_unlock(mddev);
6225
6226 return 0;
6227}
6228
Jan Engelhardt110518b2009-05-07 12:49:37 +10006229static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230 .start = md_seq_start,
6231 .next = md_seq_next,
6232 .stop = md_seq_stop,
6233 .show = md_seq_show,
6234};
6235
6236static int md_seq_open(struct inode *inode, struct file *file)
6237{
6238 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006239 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6240 if (mi == NULL)
6241 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242
6243 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006244 if (error)
6245 kfree(mi);
6246 else {
6247 struct seq_file *p = file->private_data;
6248 p->private = mi;
6249 mi->event = atomic_read(&md_event_count);
6250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251 return error;
6252}
6253
NeilBrownd7603b72006-01-06 00:20:30 -08006254static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6255{
6256 struct seq_file *m = filp->private_data;
6257 struct mdstat_info *mi = m->private;
6258 int mask;
6259
6260 poll_wait(filp, &md_event_waiters, wait);
6261
6262 /* always allow read */
6263 mask = POLLIN | POLLRDNORM;
6264
6265 if (mi->event != atomic_read(&md_event_count))
6266 mask |= POLLERR | POLLPRI;
6267 return mask;
6268}
6269
Arjan van de Venfa027c22007-02-12 00:55:33 -08006270static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006271 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272 .open = md_seq_open,
6273 .read = seq_read,
6274 .llseek = seq_lseek,
Martin Peschkec3f94b42007-05-09 02:35:35 -07006275 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006276 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277};
6278
NeilBrown2604b702006-01-06 00:20:36 -08006279int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006282 list_add_tail(&p->list, &pers_list);
6283 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284 spin_unlock(&pers_lock);
6285 return 0;
6286}
6287
NeilBrown2604b702006-01-06 00:20:36 -08006288int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289{
NeilBrown2604b702006-01-06 00:20:36 -08006290 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006292 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 spin_unlock(&pers_lock);
6294 return 0;
6295}
6296
NeilBrowneea1bf32009-03-31 14:27:02 +11006297static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298{
6299 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006301 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302
6303 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006304 rcu_read_lock();
6305 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006307 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6308 (int)part_stat_read(&disk->part0, sectors[1]) -
6309 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006310 /* sync IO will cause sync_io to increase before the disk_stats
6311 * as sync_io is counted when a request starts, and
6312 * disk_stats is counted when it completes.
6313 * So resync activity will cause curr_events to be smaller than
6314 * when there was no such activity.
6315 * non-sync IO will cause disk_stat to increase without
6316 * increasing sync_io so curr_events will (eventually)
6317 * be larger than it was before. Once it becomes
6318 * substantially larger, the test below will cause
6319 * the array to appear non-idle, and resync will slow
6320 * down.
6321 * If there is a lot of outstanding resync activity when
6322 * we set last_event to curr_events, then all that activity
6323 * completing might cause the array to appear non-idle
6324 * and resync will be slowed down even though there might
6325 * not have been non-resync activity. This will only
6326 * happen once though. 'last_events' will soon reflect
6327 * the state where there is little or no outstanding
6328 * resync requests, and further resync activity will
6329 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006330 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006332 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333 rdev->last_events = curr_events;
6334 idle = 0;
6335 }
6336 }
NeilBrown4b809912008-07-21 17:05:25 +10006337 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338 return idle;
6339}
6340
6341void md_done_sync(mddev_t *mddev, int blocks, int ok)
6342{
6343 /* another "blocks" (512byte) blocks have been synced */
6344 atomic_sub(blocks, &mddev->recovery_active);
6345 wake_up(&mddev->recovery_wait);
6346 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006347 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 md_wakeup_thread(mddev->thread);
6349 // stop recovery, signal do_sync ....
6350 }
6351}
6352
6353
NeilBrown06d91a52005-06-21 17:17:12 -07006354/* md_write_start(mddev, bi)
6355 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006356 * in superblock) before writing, schedule a superblock update
6357 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006358 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006359void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360{
Neil Brown0fd62b82008-06-28 08:31:36 +10006361 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006362 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006363 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006364
NeilBrownf91de922005-11-08 21:39:36 -08006365 BUG_ON(mddev->ro == 1);
6366 if (mddev->ro == 2) {
6367 /* need to switch to read/write */
6368 mddev->ro = 0;
6369 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6370 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006371 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006372 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006373 }
NeilBrown06d91a52005-06-21 17:17:12 -07006374 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006375 if (mddev->safemode == 1)
6376 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006377 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006378 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006379 if (mddev->in_sync) {
6380 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006381 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006382 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006383 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006384 }
NeilBrowna9701a32005-11-08 21:39:34 -08006385 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006386 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006387 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006388 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006389 wait_event(mddev->sb_wait,
6390 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6391 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392}
6393
6394void md_write_end(mddev_t *mddev)
6395{
6396 if (atomic_dec_and_test(&mddev->writes_pending)) {
6397 if (mddev->safemode == 2)
6398 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006399 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6401 }
6402}
6403
NeilBrown2a2275d2007-01-26 00:57:11 -08006404/* md_allow_write(mddev)
6405 * Calling this ensures that the array is marked 'active' so that writes
6406 * may proceed without blocking. It is important to call this before
6407 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6408 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006409 *
6410 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6411 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006412 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006413int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006414{
6415 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006416 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006417 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006418 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006419 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006420 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006421
6422 spin_lock_irq(&mddev->write_lock);
6423 if (mddev->in_sync) {
6424 mddev->in_sync = 0;
6425 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6426 if (mddev->safemode_delay &&
6427 mddev->safemode == 0)
6428 mddev->safemode = 1;
6429 spin_unlock_irq(&mddev->write_lock);
6430 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006431 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006432 } else
6433 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006434
6435 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6436 return -EAGAIN;
6437 else
6438 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006439}
6440EXPORT_SYMBOL_GPL(md_allow_write);
6441
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442#define SYNC_MARKS 10
6443#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006444void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445{
6446 mddev_t *mddev2;
6447 unsigned int currspeed = 0,
6448 window;
NeilBrown57afd892005-06-21 17:17:13 -07006449 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 unsigned long mark[SYNC_MARKS];
6451 sector_t mark_cnt[SYNC_MARKS];
6452 int last_mark,m;
6453 struct list_head *tmp;
6454 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006455 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006456 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006457 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458
6459 /* just incase thread restarts... */
6460 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6461 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006462 if (mddev->ro) /* never try to sync a read-only array */
6463 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464
NeilBrown61df9d92006-10-03 01:15:57 -07006465 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6466 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6467 desc = "data-check";
6468 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6469 desc = "requested-resync";
6470 else
6471 desc = "resync";
6472 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6473 desc = "reshape";
6474 else
6475 desc = "recovery";
6476
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477 /* we overload curr_resync somewhat here.
6478 * 0 == not engaged in resync at all
6479 * 2 == checking that there is no conflict with another sync
6480 * 1 == like 2, but have yielded to allow conflicting resync to
6481 * commense
6482 * other == active in resync - this many blocks
6483 *
6484 * Before starting a resync we must have set curr_resync to
6485 * 2, and then checked that every "conflicting" array has curr_resync
6486 * less than ours. When we find one that is the same or higher
6487 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6488 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6489 * This will mean we have to start checking from the beginning again.
6490 *
6491 */
6492
6493 do {
6494 mddev->curr_resync = 2;
6495
6496 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08006497 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07006498 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499 goto skip;
6500 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08006501 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502 if (mddev2 == mddev)
6503 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006504 if (!mddev->parallel_resync
6505 && mddev2->curr_resync
6506 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507 DEFINE_WAIT(wq);
6508 if (mddev < mddev2 && mddev->curr_resync == 2) {
6509 /* arbitrarily yield */
6510 mddev->curr_resync = 1;
6511 wake_up(&resync_wait);
6512 }
6513 if (mddev > mddev2 && mddev->curr_resync == 1)
6514 /* no need to wait here, we can wait the next
6515 * time 'round when curr_resync == 2
6516 */
6517 continue;
NeilBrown97441972008-09-19 11:49:54 +10006518 /* We need to wait 'interruptible' so as not to
6519 * contribute to the load average, and not to
6520 * be caught by 'softlockup'
6521 */
6522 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006523 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006524 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006525 printk(KERN_INFO "md: delaying %s of %s"
6526 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006528 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006530 if (signal_pending(current))
6531 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006532 schedule();
6533 finish_wait(&resync_wait, &wq);
6534 goto try_again;
6535 }
6536 finish_wait(&resync_wait, &wq);
6537 }
6538 }
6539 } while (mddev->curr_resync < 2);
6540
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006541 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006542 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006544 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545 */
6546 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006547 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006548 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006549 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6550 j = mddev->resync_min;
6551 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006552 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006553
NeilBrownccfcc3c2006-03-27 01:18:09 -08006554 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006555 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006556 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006558 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006559 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006560 rcu_read_lock();
6561 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006562 if (rdev->raid_disk >= 0 &&
6563 !test_bit(Faulty, &rdev->flags) &&
6564 !test_bit(In_sync, &rdev->flags) &&
6565 rdev->recovery_offset < j)
6566 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006567 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006569
NeilBrown61df9d92006-10-03 01:15:57 -07006570 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6571 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6572 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006573 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006574 "(but not more than %d KB/sec) for %s.\n",
6575 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576
NeilBrowneea1bf32009-03-31 14:27:02 +11006577 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006578
NeilBrown57afd892005-06-21 17:17:13 -07006579 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 for (m = 0; m < SYNC_MARKS; m++) {
6581 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006582 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583 }
6584 last_mark = 0;
6585 mddev->resync_mark = mark[last_mark];
6586 mddev->resync_mark_cnt = mark_cnt[last_mark];
6587
6588 /*
6589 * Tune reconstruction:
6590 */
6591 window = 32*(PAGE_SIZE/512);
6592 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6593 window/2,(unsigned long long) max_sectors/2);
6594
6595 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006596 last_check = 0;
6597
6598 if (j>2) {
6599 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006600 "md: resuming %s of %s from checkpoint.\n",
6601 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602 mddev->curr_resync = j;
6603 }
NeilBrownefa59332009-12-14 12:49:49 +11006604 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605
6606 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006607 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608
NeilBrown57afd892005-06-21 17:17:13 -07006609 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006610
NeilBrown7a91ee12009-05-26 12:57:21 +10006611 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6612 ((mddev->curr_resync > mddev->curr_resync_completed &&
6613 (mddev->curr_resync - mddev->curr_resync_completed)
6614 > (max_sectors >> 4)) ||
6615 (j - mddev->curr_resync_completed)*2
6616 >= mddev->resync_max - mddev->curr_resync_completed
6617 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006618 /* time to update curr_resync_completed */
6619 blk_unplug(mddev->queue);
6620 wait_event(mddev->recovery_wait,
6621 atomic_read(&mddev->recovery_active) == 0);
6622 mddev->curr_resync_completed =
6623 mddev->curr_resync;
6624 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006625 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006626 }
NeilBrownacb180b2009-04-14 16:28:34 +10006627
NeilBrowne62e58a2009-07-01 13:15:35 +10006628 while (j >= mddev->resync_max && !kthread_should_stop()) {
6629 /* As this condition is controlled by user-space,
6630 * we can block indefinitely, so use '_interruptible'
6631 * to avoid triggering warnings.
6632 */
6633 flush_signals(current); /* just in case */
6634 wait_event_interruptible(mddev->recovery_wait,
6635 mddev->resync_max > j
6636 || kthread_should_stop());
6637 }
NeilBrownacb180b2009-04-14 16:28:34 +10006638
6639 if (kthread_should_stop())
6640 goto interrupted;
6641
NeilBrown57afd892005-06-21 17:17:13 -07006642 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006643 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006644 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006645 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646 goto out;
6647 }
NeilBrown57afd892005-06-21 17:17:13 -07006648
6649 if (!skipped) { /* actual IO requested */
6650 io_sectors += sectors;
6651 atomic_add(sectors, &mddev->recovery_active);
6652 }
6653
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654 j += sectors;
6655 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006656 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006657 if (last_check == 0)
6658 /* this is the earliers that rebuilt will be
6659 * visible in /proc/mdstat
6660 */
6661 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006662
6663 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664 continue;
6665
NeilBrown57afd892005-06-21 17:17:13 -07006666 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667
NeilBrowndfc70642008-05-23 13:04:39 -07006668 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669 break;
6670
6671 repeat:
6672 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6673 /* step marks */
6674 int next = (last_mark+1) % SYNC_MARKS;
6675
6676 mddev->resync_mark = mark[next];
6677 mddev->resync_mark_cnt = mark_cnt[next];
6678 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006679 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680 last_mark = next;
6681 }
6682
6683
NeilBrownc6207272008-02-06 01:39:52 -08006684 if (kthread_should_stop())
6685 goto interrupted;
6686
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687
6688 /*
6689 * this loop exits only if either when we are slower than
6690 * the 'hard' speed limit, or the system was IO-idle for
6691 * a jiffy.
6692 * the system might be non-idle CPU-wise, but we only care
6693 * about not overloading the IO subsystem. (things like an
6694 * e2fsck being done on the RAID array should execute fast)
6695 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006696 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697 cond_resched();
6698
NeilBrown57afd892005-06-21 17:17:13 -07006699 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6700 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701
NeilBrown88202a02006-01-06 00:21:36 -08006702 if (currspeed > speed_min(mddev)) {
6703 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006704 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006705 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706 goto repeat;
6707 }
6708 }
6709 }
NeilBrown61df9d92006-10-03 01:15:57 -07006710 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711 /*
6712 * this also signals 'finished resyncing' to md_stop
6713 */
6714 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006715 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716
6717 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6718
6719 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006720 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721
NeilBrowndfc70642008-05-23 13:04:39 -07006722 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006723 mddev->curr_resync > 2) {
6724 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6725 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6726 if (mddev->curr_resync >= mddev->recovery_cp) {
6727 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006728 "md: checkpointing %s of %s.\n",
6729 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006730 mddev->recovery_cp = mddev->curr_resync;
6731 }
6732 } else
6733 mddev->recovery_cp = MaxSector;
6734 } else {
6735 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6736 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006737 rcu_read_lock();
6738 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006739 if (rdev->raid_disk >= 0 &&
6740 !test_bit(Faulty, &rdev->flags) &&
6741 !test_bit(In_sync, &rdev->flags) &&
6742 rdev->recovery_offset < mddev->curr_resync)
6743 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07006744 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746 }
NeilBrown17571282006-12-10 02:20:52 -08006747 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006750 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6751 /* We completed so min/max setting can be forgotten if used. */
6752 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6753 mddev->resync_min = 0;
6754 mddev->resync_max = MaxSector;
6755 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6756 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006758 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6759 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006760 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761 wake_up(&resync_wait);
6762 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6763 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006764 return;
6765
6766 interrupted:
6767 /*
6768 * got a signal, exit.
6769 */
6770 printk(KERN_INFO
6771 "md: md_do_sync() got signal ... exiting\n");
6772 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6773 goto out;
6774
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775}
NeilBrown29269552006-03-27 01:18:10 -08006776EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777
6778
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006779static int remove_and_add_spares(mddev_t *mddev)
6780{
6781 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006782 int spares = 0;
6783
NeilBrown97e4f422009-03-31 14:33:13 +11006784 mddev->curr_resync_completed = 0;
6785
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006786 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006787 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006788 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006789 (test_bit(Faulty, &rdev->flags) ||
6790 ! test_bit(In_sync, &rdev->flags)) &&
6791 atomic_read(&rdev->nr_pending)==0) {
6792 if (mddev->pers->hot_remove_disk(
6793 mddev, rdev->raid_disk)==0) {
6794 char nm[20];
6795 sprintf(nm,"rd%d", rdev->raid_disk);
6796 sysfs_remove_link(&mddev->kobj, nm);
6797 rdev->raid_disk = -1;
6798 }
6799 }
6800
NeilBrown4044ba52009-01-09 08:31:11 +11006801 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006802 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006803 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006804 !test_bit(In_sync, &rdev->flags) &&
6805 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006806 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006807 if (rdev->raid_disk < 0
6808 && !test_bit(Faulty, &rdev->flags)) {
6809 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006810 if (mddev->pers->
6811 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006812 char nm[20];
6813 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006814 if (sysfs_create_link(&mddev->kobj,
6815 &rdev->kobj, nm))
6816 printk(KERN_WARNING
6817 "md: cannot register "
6818 "%s for %s\n",
6819 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006820 spares++;
6821 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11006822 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006823 } else
6824 break;
6825 }
NeilBrowndfc70642008-05-23 13:04:39 -07006826 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006827 }
6828 return spares;
6829}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830/*
6831 * This routine is regularly called by all per-raid-array threads to
6832 * deal with generic issues like resync and super-block update.
6833 * Raid personalities that don't have a thread (linear/raid0) do not
6834 * need this as they never do any recovery or update the superblock.
6835 *
6836 * It does not do any resync itself, but rather "forks" off other threads
6837 * to do that as needed.
6838 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6839 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006840 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841 * and wakeups up this thread which will reap the thread and finish up.
6842 * This thread also removes any faulty devices (with nr_pending == 0).
6843 *
6844 * The overall approach is:
6845 * 1/ if the superblock needs updating, update it.
6846 * 2/ If a recovery thread is running, don't do anything else.
6847 * 3/ If recovery has finished, clean up, possibly marking spares active.
6848 * 4/ If there are any faulty devices, remove them.
6849 * 5/ If array is degraded, try to add spares devices
6850 * 6/ If array has spares or is not in-sync, start a resync thread.
6851 */
6852void md_check_recovery(mddev_t *mddev)
6853{
6854 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855
6856
NeilBrown5f404022005-06-21 17:17:16 -07006857 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11006858 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859
6860 if (mddev->ro)
6861 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006862
6863 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006864 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006865 printk(KERN_INFO "md: %s in immediate safe mode\n",
6866 mdname(mddev));
6867 mddev->safemode = 2;
6868 }
6869 flush_signals(current);
6870 }
6871
NeilBrownc89a8ee2008-08-05 15:54:13 +10006872 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6873 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006875 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006876 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006877 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006878 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006879 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6880 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881 ))
6882 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006883
NeilBrowndf5b89b2006-03-27 01:18:20 -08006884 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006885 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006886
NeilBrownc89a8ee2008-08-05 15:54:13 +10006887 if (mddev->ro) {
6888 /* Only thing we do on a ro array is remove
6889 * failed devices.
6890 */
6891 remove_and_add_spares(mddev);
6892 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6893 goto unlock;
6894 }
6895
NeilBrown31a59e32008-04-30 00:52:30 -07006896 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006897 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006898 spin_lock_irq(&mddev->write_lock);
6899 if (mddev->safemode &&
6900 !atomic_read(&mddev->writes_pending) &&
6901 !mddev->in_sync &&
6902 mddev->recovery_cp == MaxSector) {
6903 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006904 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006905 if (mddev->persistent)
6906 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6907 }
6908 if (mddev->safemode == 1)
6909 mddev->safemode = 0;
6910 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006911 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006912 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006913 }
NeilBrownfca4d842005-06-21 17:17:11 -07006914
NeilBrown850b2b42006-10-03 01:15:46 -07006915 if (mddev->flags)
6916 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006917
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006918 list_for_each_entry(rdev, &mddev->disks, same_set)
Neil Brown52664732008-06-28 08:31:44 +10006919 if (test_and_clear_bit(StateChanged, &rdev->flags))
NeilBrown3c0ee632008-10-21 13:25:28 +11006920 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown52664732008-06-28 08:31:44 +10006921
NeilBrown06d91a52005-06-21 17:17:12 -07006922
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6924 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6925 /* resync/recovery still happening */
6926 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6927 goto unlock;
6928 }
6929 if (mddev->sync_thread) {
6930 /* resync has finished, collect result */
6931 md_unregister_thread(mddev->sync_thread);
6932 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006933 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6934 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935 /* success...*/
6936 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006937 if (mddev->pers->spare_active(mddev))
6938 sysfs_notify(&mddev->kobj, NULL,
6939 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940 }
NeilBrowncea9c222009-03-31 15:15:05 +11006941 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6942 mddev->pers->finish_reshape)
6943 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07006944 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006945
6946 /* if array is no-longer degraded, then any saved_raid_disk
6947 * information must be scrapped
6948 */
6949 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006950 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07006951 rdev->saved_raid_disk = -1;
6952
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953 mddev->recovery = 0;
6954 /* flag recovery needed just to double check */
6955 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11006956 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006957 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958 goto unlock;
6959 }
Neil Brown72a23c22008-06-28 08:31:41 +10006960 /* Set RUNNING before clearing NEEDED to avoid
6961 * any transients in the value of "sync_action".
6962 */
6963 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6964 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006965 /* Clear some bits that don't mean anything, but
6966 * might be left set
6967 */
NeilBrown24dd4692005-11-08 21:39:26 -08006968 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6969 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006971 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6972 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973 /* no recovery is running.
6974 * remove any failed drives, then
6975 * add spares if possible.
6976 * Spare are also removed and re-added, to allow
6977 * the personality to fail the re-add.
6978 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006980 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10006981 if (mddev->pers->check_reshape == NULL ||
6982 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006983 /* Cannot proceed */
6984 goto unlock;
6985 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006986 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006987 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006988 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6989 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07006990 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006991 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006992 } else if (mddev->recovery_cp < MaxSector) {
6993 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006994 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006995 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6996 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006998
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007000 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7001 /* We are adding a device or devices to an array
7002 * which has the bitmap stored on all devices.
7003 * So make sure all bitmap pages get written
7004 */
7005 bitmap_write_all(mddev->bitmap);
7006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007 mddev->sync_thread = md_register_thread(md_do_sync,
7008 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007009 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010 if (!mddev->sync_thread) {
7011 printk(KERN_ERR "%s: could not start resync"
7012 " thread...\n",
7013 mdname(mddev));
7014 /* leave the spares where they are, it shouldn't hurt */
7015 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08007016 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11007018 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007019 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007020 }
7021 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007022 if (!mddev->sync_thread) {
7023 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7024 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7025 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007026 if (mddev->sysfs_action)
7027 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029 mddev_unlock(mddev);
7030 }
7031}
7032
Dan Williams6bfe0b42008-04-30 00:52:32 -07007033void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7034{
NeilBrown3c0ee632008-10-21 13:25:28 +11007035 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007036 wait_event_timeout(rdev->blocked_wait,
7037 !test_bit(Blocked, &rdev->flags),
7038 msecs_to_jiffies(5000));
7039 rdev_dec_pending(rdev, mddev);
7040}
7041EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7042
Adrian Bunk75c96f82005-05-05 16:16:09 -07007043static int md_notify_reboot(struct notifier_block *this,
7044 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045{
7046 struct list_head *tmp;
7047 mddev_t *mddev;
7048
7049 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7050
7051 printk(KERN_INFO "md: stopping all md devices.\n");
7052
NeilBrown29ac4aa2008-02-06 01:39:58 -08007053 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007054 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007055 /* Force a switch to readonly even array
7056 * appears to still be in use. Hence
7057 * the '100'.
7058 */
NeilBrownd710e132008-10-13 11:55:12 +11007059 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007060 mddev_unlock(mddev);
7061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007062 /*
7063 * certain more exotic SCSI devices are known to be
7064 * volatile wrt too early system reboots. While the
7065 * right place to handle this issue is the given
7066 * driver, we do want to have a safe RAID driver ...
7067 */
7068 mdelay(1000*1);
7069 }
7070 return NOTIFY_DONE;
7071}
7072
Adrian Bunk75c96f82005-05-05 16:16:09 -07007073static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074 .notifier_call = md_notify_reboot,
7075 .next = NULL,
7076 .priority = INT_MAX, /* before any real devices */
7077};
7078
7079static void md_geninit(void)
7080{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7082
Denis V. Lunevc7705f32008-04-29 01:02:35 -07007083 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084}
7085
Adrian Bunk75c96f82005-05-05 16:16:09 -07007086static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007088 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007089 return -1;
7090 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007091 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092 return -1;
7093 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007094 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007095 md_probe, NULL, NULL);
7096 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097 md_probe, NULL, NULL);
7098
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007100 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101
7102 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007103 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104}
7105
7106
7107#ifndef MODULE
7108
7109/*
7110 * Searches all registered partitions for autorun RAID arrays
7111 * at boot time.
7112 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007113
7114static LIST_HEAD(all_detected_devices);
7115struct detected_devices_node {
7116 struct list_head list;
7117 dev_t dev;
7118};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119
7120void md_autodetect_dev(dev_t dev)
7121{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007122 struct detected_devices_node *node_detected_dev;
7123
7124 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7125 if (node_detected_dev) {
7126 node_detected_dev->dev = dev;
7127 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7128 } else {
7129 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7130 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132}
7133
7134
7135static void autostart_arrays(int part)
7136{
7137 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007138 struct detected_devices_node *node_detected_dev;
7139 dev_t dev;
7140 int i_scanned, i_passed;
7141
7142 i_scanned = 0;
7143 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144
7145 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7146
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007147 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7148 i_scanned++;
7149 node_detected_dev = list_entry(all_detected_devices.next,
7150 struct detected_devices_node, list);
7151 list_del(&node_detected_dev->list);
7152 dev = node_detected_dev->dev;
7153 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007154 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155 if (IS_ERR(rdev))
7156 continue;
7157
NeilBrownb2d444d2005-11-08 21:39:31 -08007158 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159 MD_BUG();
7160 continue;
7161 }
NeilBrownd0fae182008-03-04 14:29:31 -08007162 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007164 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007166
7167 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7168 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169
7170 autorun_devices(part);
7171}
7172
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007173#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174
7175static __exit void md_exit(void)
7176{
7177 mddev_t *mddev;
7178 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007179
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007180 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007181 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007182
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007183 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184 unregister_blkdev(mdp_major, "mdp");
7185 unregister_reboot_notifier(&md_notifier);
7186 unregister_sysctl_table(raid_table_header);
7187 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007188 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007190 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191 }
7192}
7193
Dan Williams685784a2007-07-09 11:56:42 -07007194subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195module_exit(md_exit)
7196
NeilBrownf91de922005-11-08 21:39:36 -08007197static int get_ro(char *buffer, struct kernel_param *kp)
7198{
7199 return sprintf(buffer, "%d", start_readonly);
7200}
7201static int set_ro(const char *val, struct kernel_param *kp)
7202{
7203 char *e;
7204 int num = simple_strtoul(val, &e, 10);
7205 if (*val && (*e == '\0' || *e == '\n')) {
7206 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007207 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007208 }
7209 return -EINVAL;
7210}
7211
NeilBrown80ca3a42006-07-10 04:44:18 -07007212module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7213module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8e2006-01-06 00:20:15 -08007214
NeilBrownefeb53c2009-01-09 08:31:10 +11007215module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007216
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217EXPORT_SYMBOL(register_md_personality);
7218EXPORT_SYMBOL(unregister_md_personality);
7219EXPORT_SYMBOL(md_error);
7220EXPORT_SYMBOL(md_done_sync);
7221EXPORT_SYMBOL(md_write_start);
7222EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007223EXPORT_SYMBOL(md_register_thread);
7224EXPORT_SYMBOL(md_unregister_thread);
7225EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226EXPORT_SYMBOL(md_check_recovery);
7227MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007228MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007229MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007230MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);