blob: dd3dfe42d5a9d081c590018f1ac6325cfecdaf12 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/buffer_head.h> /* for invalidate_bdev */
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080042#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110043#include <linux/hdreg.h>
44#include <linux/proc_fs.h>
45#include <linux/random.h>
46#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070047#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110048#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110049#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110050#include <linux/raid/md_p.h>
51#include <linux/raid/md_u.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110052#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110053#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#define DEBUG 0
56#define dprintk(x...) ((void)(DEBUG && printk(x)))
57
58
59#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110060static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#endif
62
NeilBrown2604b702006-01-06 00:20:36 -080063static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static DEFINE_SPINLOCK(pers_lock);
65
Adrian Bunk5e563412006-06-26 00:27:42 -070066static void md_print_devices(void);
67
Bernd Schubert90b08712008-05-23 13:04:38 -070068static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
69
Adrian Bunk5e563412006-06-26 00:27:42 -070070#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
Robert Becker1e509152009-12-14 12:49:58 +110073 * Default number of read corrections we'll attempt on an rdev
74 * before ejecting it from the array. We divide the read error
75 * count by 2 for every hour elapsed between read errors.
76 */
77#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
78/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
80 * is 1000 KB/sec, so the extra system load does not show up that much.
81 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070082 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 * subsystem is idle. There is also an 'absolute maximum' reconstruction
84 * speed limit - in case reconstruction slows down your system despite
85 * idle IO detection.
86 *
87 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080088 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 */
90
91static int sysctl_speed_limit_min = 1000;
92static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080093static inline int speed_min(mddev_t *mddev)
94{
95 return mddev->sync_speed_min ?
96 mddev->sync_speed_min : sysctl_speed_limit_min;
97}
98
99static inline int speed_max(mddev_t *mddev)
100{
101 return mddev->sync_speed_max ?
102 mddev->sync_speed_max : sysctl_speed_limit_max;
103}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105static struct ctl_table_header *raid_table_header;
106
107static ctl_table raid_table[] = {
108 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 .procname = "speed_limit_min",
110 .data = &sysctl_speed_limit_min,
111 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700112 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800113 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 },
115 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 .procname = "speed_limit_max",
117 .data = &sysctl_speed_limit_max,
118 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700119 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800120 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800122 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123};
124
125static ctl_table raid_dir_table[] = {
126 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 .procname = "raid",
128 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700129 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 .child = raid_table,
131 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800132 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133};
134
135static ctl_table raid_root_table[] = {
136 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .procname = "dev",
138 .maxlen = 0,
139 .mode = 0555,
140 .child = raid_dir_table,
141 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800142 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700145static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
NeilBrownf91de922005-11-08 21:39:36 -0800147static int start_readonly;
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/*
NeilBrownd7603b72006-01-06 00:20:30 -0800150 * We have a system wide 'event count' that is incremented
151 * on any 'interesting' event, and readers of /proc/mdstat
152 * can use 'poll' or 'select' to find out when the event
153 * count increases.
154 *
155 * Events are:
156 * start array, stop array, error, add device, remove device,
157 * start build, activate spare
158 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800159static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800160static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800161void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800162{
163 atomic_inc(&md_event_count);
164 wake_up(&md_event_waiters);
165}
NeilBrown29269552006-03-27 01:18:10 -0800166EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800167
NeilBrownc331eb02006-05-30 21:27:13 -0700168/* Alternate version that can be called from interrupts
169 * when calling sysfs_notify isn't needed.
170 */
Adrian Bunk05381952006-06-26 00:28:01 -0700171static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700172{
173 atomic_inc(&md_event_count);
174 wake_up(&md_event_waiters);
175}
176
NeilBrownd7603b72006-01-06 00:20:30 -0800177/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * Enables to iterate over all existing md arrays
179 * all_mddevs_lock protects this list.
180 */
181static LIST_HEAD(all_mddevs);
182static DEFINE_SPINLOCK(all_mddevs_lock);
183
184
185/*
186 * iterates through all used mddevs in the system.
187 * We take care to grab the all_mddevs_lock whenever navigating
188 * the list, and to always hold a refcount when unlocked.
189 * Any code which breaks out of this loop while own
190 * a reference to the current mddev and must mddev_put it.
191 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800192#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 \
194 for (({ spin_lock(&all_mddevs_lock); \
195 tmp = all_mddevs.next; \
196 mddev = NULL;}); \
197 ({ if (tmp != &all_mddevs) \
198 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
199 spin_unlock(&all_mddevs_lock); \
200 if (mddev) mddev_put(mddev); \
201 mddev = list_entry(tmp, mddev_t, all_mddevs); \
202 tmp != &all_mddevs;}); \
203 ({ spin_lock(&all_mddevs_lock); \
204 tmp = tmp->next;}) \
205 )
206
207
NeilBrown409c57f2009-03-31 14:39:39 +1100208/* Rather than calling directly into the personality make_request function,
209 * IO requests come here first so that we can check if the device is
210 * being suspended pending a reconfiguration.
211 * We hold a refcount over the call to ->make_request. By the time that
212 * call has finished, the bio has been linked into some internal structure
213 * and so is visible to ->quiesce(), so we don't need the refcount any more.
214 */
215static int md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
NeilBrown409c57f2009-03-31 14:39:39 +1100217 mddev_t *mddev = q->queuedata;
218 int rv;
219 if (mddev == NULL || mddev->pers == NULL) {
220 bio_io_error(bio);
221 return 0;
222 }
223 rcu_read_lock();
NeilBrowna2826aa2009-12-14 12:49:49 +1100224 if (mddev->suspended || mddev->barrier) {
NeilBrown409c57f2009-03-31 14:39:39 +1100225 DEFINE_WAIT(__wait);
226 for (;;) {
227 prepare_to_wait(&mddev->sb_wait, &__wait,
228 TASK_UNINTERRUPTIBLE);
NeilBrowna2826aa2009-12-14 12:49:49 +1100229 if (!mddev->suspended && !mddev->barrier)
NeilBrown409c57f2009-03-31 14:39:39 +1100230 break;
231 rcu_read_unlock();
232 schedule();
233 rcu_read_lock();
234 }
235 finish_wait(&mddev->sb_wait, &__wait);
236 }
237 atomic_inc(&mddev->active_io);
238 rcu_read_unlock();
239 rv = mddev->pers->make_request(q, bio);
240 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
241 wake_up(&mddev->sb_wait);
242
243 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
NeilBrown409c57f2009-03-31 14:39:39 +1100246static void mddev_suspend(mddev_t *mddev)
247{
248 BUG_ON(mddev->suspended);
249 mddev->suspended = 1;
250 synchronize_rcu();
251 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
252 mddev->pers->quiesce(mddev, 1);
253 md_unregister_thread(mddev->thread);
254 mddev->thread = NULL;
255 /* we now know that no code is executing in the personality module,
256 * except possibly the tail end of a ->bi_end_io function, but that
257 * is certain to complete before the module has a chance to get
258 * unloaded
259 */
260}
261
262static void mddev_resume(mddev_t *mddev)
263{
264 mddev->suspended = 0;
265 wake_up(&mddev->sb_wait);
266 mddev->pers->quiesce(mddev, 0);
267}
268
NeilBrown3fa841d2009-09-23 18:10:29 +1000269int mddev_congested(mddev_t *mddev, int bits)
270{
NeilBrowna2826aa2009-12-14 12:49:49 +1100271 if (mddev->barrier)
272 return 1;
NeilBrown3fa841d2009-09-23 18:10:29 +1000273 return mddev->suspended;
274}
275EXPORT_SYMBOL(mddev_congested);
276
NeilBrowna2826aa2009-12-14 12:49:49 +1100277/*
278 * Generic barrier handling for md
279 */
280
281#define POST_REQUEST_BARRIER ((void*)1)
282
283static void md_end_barrier(struct bio *bio, int err)
284{
285 mdk_rdev_t *rdev = bio->bi_private;
286 mddev_t *mddev = rdev->mddev;
287 if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER)
288 set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags);
289
290 rdev_dec_pending(rdev, mddev);
291
292 if (atomic_dec_and_test(&mddev->flush_pending)) {
293 if (mddev->barrier == POST_REQUEST_BARRIER) {
294 /* This was a post-request barrier */
295 mddev->barrier = NULL;
296 wake_up(&mddev->sb_wait);
297 } else
298 /* The pre-request barrier has finished */
299 schedule_work(&mddev->barrier_work);
300 }
301 bio_put(bio);
302}
303
304static void submit_barriers(mddev_t *mddev)
305{
306 mdk_rdev_t *rdev;
307
308 rcu_read_lock();
309 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
310 if (rdev->raid_disk >= 0 &&
311 !test_bit(Faulty, &rdev->flags)) {
312 /* Take two references, one is dropped
313 * when request finishes, one after
314 * we reclaim rcu_read_lock
315 */
316 struct bio *bi;
317 atomic_inc(&rdev->nr_pending);
318 atomic_inc(&rdev->nr_pending);
319 rcu_read_unlock();
320 bi = bio_alloc(GFP_KERNEL, 0);
321 bi->bi_end_io = md_end_barrier;
322 bi->bi_private = rdev;
323 bi->bi_bdev = rdev->bdev;
324 atomic_inc(&mddev->flush_pending);
325 submit_bio(WRITE_BARRIER, bi);
326 rcu_read_lock();
327 rdev_dec_pending(rdev, mddev);
328 }
329 rcu_read_unlock();
330}
331
332static void md_submit_barrier(struct work_struct *ws)
333{
334 mddev_t *mddev = container_of(ws, mddev_t, barrier_work);
335 struct bio *bio = mddev->barrier;
336
337 atomic_set(&mddev->flush_pending, 1);
338
339 if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags))
340 bio_endio(bio, -EOPNOTSUPP);
341 else if (bio->bi_size == 0)
342 /* an empty barrier - all done */
343 bio_endio(bio, 0);
344 else {
345 bio->bi_rw &= ~(1<<BIO_RW_BARRIER);
346 if (mddev->pers->make_request(mddev->queue, bio))
347 generic_make_request(bio);
348 mddev->barrier = POST_REQUEST_BARRIER;
349 submit_barriers(mddev);
350 }
351 if (atomic_dec_and_test(&mddev->flush_pending)) {
352 mddev->barrier = NULL;
353 wake_up(&mddev->sb_wait);
354 }
355}
356
357void md_barrier_request(mddev_t *mddev, struct bio *bio)
358{
359 spin_lock_irq(&mddev->write_lock);
360 wait_event_lock_irq(mddev->sb_wait,
361 !mddev->barrier,
362 mddev->write_lock, /*nothing*/);
363 mddev->barrier = bio;
364 spin_unlock_irq(&mddev->write_lock);
365
366 atomic_set(&mddev->flush_pending, 1);
367 INIT_WORK(&mddev->barrier_work, md_submit_barrier);
368
369 submit_barriers(mddev);
370
371 if (atomic_dec_and_test(&mddev->flush_pending))
372 schedule_work(&mddev->barrier_work);
373}
374EXPORT_SYMBOL(md_barrier_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376static inline mddev_t *mddev_get(mddev_t *mddev)
377{
378 atomic_inc(&mddev->active);
379 return mddev;
380}
381
Dan Williams5fd3a172009-03-04 00:57:25 -0700382static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384static void mddev_put(mddev_t *mddev)
385{
386 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
387 return;
NeilBrownd3374822009-01-09 08:31:10 +1100388 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100389 mddev->ctime == 0 && !mddev->hold_active) {
390 /* Array is not configured at all, and not held active,
391 * so destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 list_del(&mddev->all_mddevs);
NeilBrownd3374822009-01-09 08:31:10 +1100393 if (mddev->gendisk) {
394 /* we did a probe so need to clean up.
395 * Call schedule_work inside the spinlock
396 * so that flush_scheduled_work() after
397 * mddev_find will succeed in waiting for the
398 * work to be done.
399 */
400 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
401 schedule_work(&mddev->del_work);
402 } else
403 kfree(mddev);
404 }
405 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
408static mddev_t * mddev_find(dev_t unit)
409{
410 mddev_t *mddev, *new = NULL;
411
412 retry:
413 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
NeilBrownefeb53c2009-01-09 08:31:10 +1100415 if (unit) {
416 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
417 if (mddev->unit == unit) {
418 mddev_get(mddev);
419 spin_unlock(&all_mddevs_lock);
420 kfree(new);
421 return mddev;
422 }
423
424 if (new) {
425 list_add(&new->all_mddevs, &all_mddevs);
426 spin_unlock(&all_mddevs_lock);
427 new->hold_active = UNTIL_IOCTL;
428 return new;
429 }
430 } else if (new) {
431 /* find an unused unit number */
432 static int next_minor = 512;
433 int start = next_minor;
434 int is_free = 0;
435 int dev = 0;
436 while (!is_free) {
437 dev = MKDEV(MD_MAJOR, next_minor);
438 next_minor++;
439 if (next_minor > MINORMASK)
440 next_minor = 0;
441 if (next_minor == start) {
442 /* Oh dear, all in use. */
443 spin_unlock(&all_mddevs_lock);
444 kfree(new);
445 return NULL;
446 }
447
448 is_free = 1;
449 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
450 if (mddev->unit == dev) {
451 is_free = 0;
452 break;
453 }
454 }
455 new->unit = dev;
456 new->md_minor = MINOR(dev);
457 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 list_add(&new->all_mddevs, &all_mddevs);
459 spin_unlock(&all_mddevs_lock);
460 return new;
461 }
462 spin_unlock(&all_mddevs_lock);
463
NeilBrown9ffae0c2006-01-06 00:20:32 -0800464 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (!new)
466 return NULL;
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 new->unit = unit;
469 if (MAJOR(unit) == MD_MAJOR)
470 new->md_minor = MINOR(unit);
471 else
472 new->md_minor = MINOR(unit) >> MdpMinorShift;
473
NeilBrownc8c00a62009-08-10 12:50:52 +1000474 mutex_init(&new->open_mutex);
NeilBrowndf5b89b2006-03-27 01:18:20 -0800475 mutex_init(&new->reconfig_mutex);
NeilBrownc3d97142009-12-14 12:49:52 +1100476 mutex_init(&new->bitmap_info.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 INIT_LIST_HEAD(&new->disks);
478 INIT_LIST_HEAD(&new->all_mddevs);
479 init_timer(&new->safemode_timer);
480 atomic_set(&new->active, 1);
NeilBrownf2ea68c2008-07-21 17:05:25 +1000481 atomic_set(&new->openers, 0);
NeilBrown409c57f2009-03-31 14:39:39 +1100482 atomic_set(&new->active_io, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700483 spin_lock_init(&new->write_lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100484 atomic_set(&new->flush_pending, 0);
NeilBrown3d310eb2005-06-21 17:17:26 -0700485 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700486 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700487 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000488 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800489 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700490 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 goto retry;
493}
494
495static inline int mddev_lock(mddev_t * mddev)
496{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800497 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Dan Williamsb522adc2009-03-31 15:00:31 +1100500static inline int mddev_is_locked(mddev_t *mddev)
501{
502 return mutex_is_locked(&mddev->reconfig_mutex);
503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505static inline int mddev_trylock(mddev_t * mddev)
506{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800507 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
510static inline void mddev_unlock(mddev_t * mddev)
511{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800512 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
NeilBrown005eca52005-08-22 13:11:08 -0700514 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
NeilBrown2989ddb2006-01-06 00:20:43 -0800517static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100519 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100521 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (rdev->desc_nr == nr)
523 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return NULL;
526}
527
528static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
529{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 mdk_rdev_t *rdev;
531
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100532 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (rdev->bdev->bd_dev == dev)
534 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return NULL;
537}
538
NeilBrownd9d166c2006-01-06 00:20:51 -0800539static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800540{
541 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800542 list_for_each_entry(pers, &pers_list, list) {
543 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800544 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800545 if (strcmp(pers->name, clevel)==0)
546 return pers;
547 }
NeilBrown2604b702006-01-06 00:20:36 -0800548 return NULL;
549}
550
Andre Nollb73df2d2008-07-11 22:02:23 +1000551/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700552static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Andre Nollb73df2d2008-07-11 22:02:23 +1000554 sector_t num_sectors = bdev->bd_inode->i_size / 512;
555 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558static int alloc_disk_sb(mdk_rdev_t * rdev)
559{
560 if (rdev->sb_page)
561 MD_BUG();
562
563 rdev->sb_page = alloc_page(GFP_KERNEL);
564 if (!rdev->sb_page) {
565 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000566 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568
569 return 0;
570}
571
572static void free_disk_sb(mdk_rdev_t * rdev)
573{
574 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800575 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 rdev->sb_loaded = 0;
577 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000578 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100579 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581}
582
583
NeilBrown6712ecf2007-09-27 12:47:43 +0200584static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700585{
586 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800587 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700588
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700589 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
590 printk("md: super_written gets error=%d, uptodate=%d\n",
591 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
592 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800593 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700594 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700595
NeilBrowna9701a32005-11-08 21:39:34 -0800596 if (atomic_dec_and_test(&mddev->pending_writes))
597 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700598 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700599}
600
NeilBrown6712ecf2007-09-27 12:47:43 +0200601static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800602{
603 struct bio *bio2 = bio->bi_private;
604 mdk_rdev_t *rdev = bio2->bi_private;
605 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800606
607 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
608 error == -EOPNOTSUPP) {
609 unsigned long flags;
610 /* barriers don't appear to be supported :-( */
611 set_bit(BarriersNotsupp, &rdev->flags);
612 mddev->barriers_work = 0;
613 spin_lock_irqsave(&mddev->write_lock, flags);
614 bio2->bi_next = mddev->biolist;
615 mddev->biolist = bio2;
616 spin_unlock_irqrestore(&mddev->write_lock, flags);
617 wake_up(&mddev->sb_wait);
618 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200619 } else {
620 bio_put(bio2);
621 bio->bi_private = rdev;
622 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800623 }
NeilBrowna9701a32005-11-08 21:39:34 -0800624}
625
NeilBrown7bfa19f2005-06-21 17:17:28 -0700626void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
627 sector_t sector, int size, struct page *page)
628{
629 /* write first size bytes of page to sector of rdev
630 * Increment mddev->pending_writes before returning
631 * and decrement it on completion, waking up sb_wait
632 * if zero is reached.
633 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800634 *
635 * As we might need to resubmit the request if BIO_RW_BARRIER
636 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700637 */
638 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Jens Axboe93dbb392009-02-16 10:25:40 +0100639 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700640
641 bio->bi_bdev = rdev->bdev;
642 bio->bi_sector = sector;
643 bio_add_page(bio, page, size, 0);
644 bio->bi_private = rdev;
645 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800646 bio->bi_rw = rw;
647
NeilBrown7bfa19f2005-06-21 17:17:28 -0700648 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800649 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
650 struct bio *rbio;
651 rw |= (1<<BIO_RW_BARRIER);
652 rbio = bio_clone(bio, GFP_NOIO);
653 rbio->bi_private = bio;
654 rbio->bi_end_io = super_written_barrier;
655 submit_bio(rw, rbio);
656 } else
657 submit_bio(rw, bio);
658}
659
660void md_super_wait(mddev_t *mddev)
661{
662 /* wait for all superblock writes that were scheduled to complete.
663 * if any had to be retried (due to BARRIER problems), retry them
664 */
665 DEFINE_WAIT(wq);
666 for(;;) {
667 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
668 if (atomic_read(&mddev->pending_writes)==0)
669 break;
670 while (mddev->biolist) {
671 struct bio *bio;
672 spin_lock_irq(&mddev->write_lock);
673 bio = mddev->biolist;
674 mddev->biolist = bio->bi_next ;
675 bio->bi_next = NULL;
676 spin_unlock_irq(&mddev->write_lock);
677 submit_bio(bio->bi_rw, bio);
678 }
679 schedule();
680 }
681 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700682}
683
NeilBrown6712ecf2007-09-27 12:47:43 +0200684static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
NeilBrowna654b9d82005-06-21 17:17:27 -0700689int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 struct page *page, int rw)
691{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700692 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 struct completion event;
694 int ret;
695
Jens Axboe93dbb392009-02-16 10:25:40 +0100696 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 bio->bi_bdev = bdev;
699 bio->bi_sector = sector;
700 bio_add_page(bio, page, size, 0);
701 init_completion(&event);
702 bio->bi_private = &event;
703 bio->bi_end_io = bi_complete;
704 submit_bio(rw, bio);
705 wait_for_completion(&event);
706
707 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
708 bio_put(bio);
709 return ret;
710}
NeilBrowna8745db2006-01-06 00:20:34 -0800711EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
NeilBrown0002b272005-09-09 16:23:53 -0700713static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 char b[BDEVNAME_SIZE];
716 if (!rdev->sb_page) {
717 MD_BUG();
718 return -EINVAL;
719 }
720 if (rdev->sb_loaded)
721 return 0;
722
723
Andre Noll0f420352008-07-11 22:02:23 +1000724 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 goto fail;
726 rdev->sb_loaded = 1;
727 return 0;
728
729fail:
730 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
731 bdevname(rdev->bdev,b));
732 return -EINVAL;
733}
734
735static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
736{
Andre Noll05710462008-07-11 22:02:20 +1000737 return sb1->set_uuid0 == sb2->set_uuid0 &&
738 sb1->set_uuid1 == sb2->set_uuid1 &&
739 sb1->set_uuid2 == sb2->set_uuid2 &&
740 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
744{
745 int ret;
746 mdp_super_t *tmp1, *tmp2;
747
748 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
749 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
750
751 if (!tmp1 || !tmp2) {
752 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100753 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 goto abort;
755 }
756
757 *tmp1 = *sb1;
758 *tmp2 = *sb2;
759
760 /*
761 * nr_disks is not constant
762 */
763 tmp1->nr_disks = 0;
764 tmp2->nr_disks = 0;
765
Andre Nollce0c8e02008-07-11 22:02:20 +1000766 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700768 kfree(tmp1);
769 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return ret;
771}
772
NeilBrown4d167f02007-05-09 02:35:37 -0700773
774static u32 md_csum_fold(u32 csum)
775{
776 csum = (csum & 0xffff) + (csum >> 16);
777 return (csum & 0xffff) + (csum >> 16);
778}
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780static unsigned int calc_sb_csum(mdp_super_t * sb)
781{
NeilBrown4d167f02007-05-09 02:35:37 -0700782 u64 newcsum = 0;
783 u32 *sb32 = (u32*)sb;
784 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 unsigned int disk_csum, csum;
786
787 disk_csum = sb->sb_csum;
788 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700789
790 for (i = 0; i < MD_SB_BYTES/4 ; i++)
791 newcsum += sb32[i];
792 csum = (newcsum & 0xffffffff) + (newcsum>>32);
793
794
795#ifdef CONFIG_ALPHA
796 /* This used to use csum_partial, which was wrong for several
797 * reasons including that different results are returned on
798 * different architectures. It isn't critical that we get exactly
799 * the same return value as before (we always csum_fold before
800 * testing, and that removes any differences). However as we
801 * know that csum_partial always returned a 16bit value on
802 * alphas, do a fold to maximise conformity to previous behaviour.
803 */
804 sb->sb_csum = md_csum_fold(disk_csum);
805#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700807#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return csum;
809}
810
811
812/*
813 * Handle superblock details.
814 * We want to be able to handle multiple superblock formats
815 * so we have a common interface to them all, and an array of
816 * different handlers.
817 * We rely on user-space to write the initial superblock, and support
818 * reading and updating of superblocks.
819 * Interface methods are:
820 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
821 * loads and validates a superblock on dev.
822 * if refdev != NULL, compare superblocks on both devices
823 * Return:
824 * 0 - dev has a superblock that is compatible with refdev
825 * 1 - dev has a superblock that is compatible and newer than refdev
826 * so dev should be used as the refdev in future
827 * -EINVAL superblock incompatible or invalid
828 * -othererror e.g. -EIO
829 *
830 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
831 * Verify that dev is acceptable into mddev.
832 * The first time, mddev->raid_disks will be 0, and data from
833 * dev should be merged in. Subsequent calls check that dev
834 * is new enough. Return 0 or -EINVAL
835 *
836 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
837 * Update the superblock for rdev with data in mddev
838 * This does not write to disc.
839 *
840 */
841
842struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000843 char *name;
844 struct module *owner;
845 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
846 int minor_version);
847 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
848 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
849 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000850 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851};
852
853/*
Andre Noll0894cc32009-06-18 08:49:23 +1000854 * Check that the given mddev has no bitmap.
855 *
856 * This function is called from the run method of all personalities that do not
857 * support bitmaps. It prints an error message and returns non-zero if mddev
858 * has a bitmap. Otherwise, it returns 0.
859 *
860 */
861int md_check_no_bitmap(mddev_t *mddev)
862{
NeilBrownc3d97142009-12-14 12:49:52 +1100863 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000864 return 0;
865 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
866 mdname(mddev), mddev->pers->name);
867 return 1;
868}
869EXPORT_SYMBOL(md_check_no_bitmap);
870
871/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 * load_super for 0.90.0
873 */
874static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
875{
876 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
877 mdp_super_t *sb;
878 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 /*
Andre Noll0f420352008-07-11 22:02:23 +1000881 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 * it's at the end of the disk.
883 *
884 * It also happens to be a multiple of 4Kb.
885 */
Andre Noll0f420352008-07-11 22:02:23 +1000886 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
NeilBrown0002b272005-09-09 16:23:53 -0700888 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (ret) return ret;
890
891 ret = -EINVAL;
892
893 bdevname(rdev->bdev, b);
894 sb = (mdp_super_t*)page_address(rdev->sb_page);
895
896 if (sb->md_magic != MD_SB_MAGIC) {
897 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
898 b);
899 goto abort;
900 }
901
902 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800903 sb->minor_version < 90 ||
904 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
906 sb->major_version, sb->minor_version,
907 b);
908 goto abort;
909 }
910
911 if (sb->raid_disks <= 0)
912 goto abort;
913
NeilBrown4d167f02007-05-09 02:35:37 -0700914 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
916 b);
917 goto abort;
918 }
919
920 rdev->preferred_minor = sb->md_minor;
921 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700922 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 if (sb->level == LEVEL_MULTIPATH)
925 rdev->desc_nr = -1;
926 else
927 rdev->desc_nr = sb->this_disk.number;
928
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700929 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700931 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 __u64 ev1, ev2;
933 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
934 if (!uuid_equal(refsb, sb)) {
935 printk(KERN_WARNING "md: %s has different UUID to %s\n",
936 b, bdevname(refdev->bdev,b2));
937 goto abort;
938 }
939 if (!sb_equal(refsb, sb)) {
940 printk(KERN_WARNING "md: %s has same UUID"
941 " but different superblock to %s\n",
942 b, bdevname(refdev->bdev, b2));
943 goto abort;
944 }
945 ev1 = md_event(sb);
946 ev2 = md_event(refsb);
947 if (ev1 > ev2)
948 ret = 1;
949 else
950 ret = 0;
951 }
NeilBrown8190e752009-06-18 08:48:58 +1000952 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Andre Nolldd8ac332009-03-31 14:33:13 +1100954 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -0800955 /* "this cannot possibly happen" ... */
956 ret = -EINVAL;
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 abort:
959 return ret;
960}
961
962/*
963 * validate_super for 0.90.0
964 */
965static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
966{
967 mdp_disk_t *desc;
968 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700969 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
NeilBrown41158c72005-06-21 17:17:25 -0700971 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800972 clear_bit(Faulty, &rdev->flags);
973 clear_bit(In_sync, &rdev->flags);
974 clear_bit(WriteMostly, &rdev->flags);
975 clear_bit(BarriersNotsupp, &rdev->flags);
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (mddev->raid_disks == 0) {
978 mddev->major_version = 0;
979 mddev->minor_version = sb->minor_version;
980 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800981 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +1000982 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 mddev->ctime = sb->ctime;
984 mddev->utime = sb->utime;
985 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800986 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 mddev->layout = sb->layout;
988 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +1100989 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -0700990 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +1100991 mddev->bitmap_info.offset = 0;
992 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
NeilBrownf6705572006-03-27 01:18:11 -0800994 if (mddev->minor_version >= 91) {
995 mddev->reshape_position = sb->reshape_position;
996 mddev->delta_disks = sb->delta_disks;
997 mddev->new_level = sb->new_level;
998 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +1000999 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001000 } else {
1001 mddev->reshape_position = MaxSector;
1002 mddev->delta_disks = 0;
1003 mddev->new_level = mddev->level;
1004 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001005 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001006 }
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (sb->state & (1<<MD_SB_CLEAN))
1009 mddev->recovery_cp = MaxSector;
1010 else {
1011 if (sb->events_hi == sb->cp_events_hi &&
1012 sb->events_lo == sb->cp_events_lo) {
1013 mddev->recovery_cp = sb->recovery_cp;
1014 } else
1015 mddev->recovery_cp = 0;
1016 }
1017
1018 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1019 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1020 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1021 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1022
1023 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001024
1025 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001026 mddev->bitmap_info.file == NULL)
1027 mddev->bitmap_info.offset =
1028 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001029
NeilBrown41158c72005-06-21 17:17:25 -07001030 } else if (mddev->pers == NULL) {
1031 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 ++ev1;
1033 if (ev1 < mddev->events)
1034 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001035 } else if (mddev->bitmap) {
1036 /* if adding to array with a bitmap, then we can accept an
1037 * older device ... but not too old.
1038 */
NeilBrown41158c72005-06-21 17:17:25 -07001039 if (ev1 < mddev->bitmap->events_cleared)
1040 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001041 } else {
1042 if (ev1 < mddev->events)
1043 /* just a hot-add of a new device, leave raid_disk at -1 */
1044 return 0;
1045 }
NeilBrown41158c72005-06-21 17:17:25 -07001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 desc = sb->disks + rdev->desc_nr;
1049
1050 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001051 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001052 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1053 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001054 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001056 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1057 /* active but not in sync implies recovery up to
1058 * reshape position. We don't know exactly where
1059 * that is, so set to zero for now */
1060 if (mddev->minor_version >= 91) {
1061 rdev->recovery_offset = 0;
1062 rdev->raid_disk = desc->raid_disk;
1063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001065 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1066 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001067 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001068 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return 0;
1070}
1071
1072/*
1073 * sync_super for 0.90.0
1074 */
1075static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1076{
1077 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 mdk_rdev_t *rdev2;
1079 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 /* make rdev->sb match mddev data..
1083 *
1084 * 1/ zero out disks
1085 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1086 * 3/ any empty disks < next_spare become removed
1087 *
1088 * disks[0] gets initialised to REMOVED because
1089 * we cannot be sure from other fields if it has
1090 * been initialised or not.
1091 */
1092 int i;
1093 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1094
NeilBrown61181562005-09-09 16:24:02 -07001095 rdev->sb_size = MD_SB_BYTES;
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 sb = (mdp_super_t*)page_address(rdev->sb_page);
1098
1099 memset(sb, 0, sizeof(*sb));
1100
1101 sb->md_magic = MD_SB_MAGIC;
1102 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 sb->patch_version = mddev->patch_version;
1104 sb->gvalid_words = 0; /* ignored */
1105 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1106 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1107 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1108 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1109
1110 sb->ctime = mddev->ctime;
1111 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001112 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 sb->raid_disks = mddev->raid_disks;
1114 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001115 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 sb->utime = mddev->utime;
1117 sb->state = 0;
1118 sb->events_hi = (mddev->events>>32);
1119 sb->events_lo = (u32)mddev->events;
1120
NeilBrownf6705572006-03-27 01:18:11 -08001121 if (mddev->reshape_position == MaxSector)
1122 sb->minor_version = 90;
1123 else {
1124 sb->minor_version = 91;
1125 sb->reshape_position = mddev->reshape_position;
1126 sb->new_level = mddev->new_level;
1127 sb->delta_disks = mddev->delta_disks;
1128 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001129 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001130 }
1131 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 if (mddev->in_sync)
1133 {
1134 sb->recovery_cp = mddev->recovery_cp;
1135 sb->cp_events_hi = (mddev->events>>32);
1136 sb->cp_events_lo = (u32)mddev->events;
1137 if (mddev->recovery_cp == MaxSector)
1138 sb->state = (1<< MD_SB_CLEAN);
1139 } else
1140 sb->recovery_cp = 0;
1141
1142 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001143 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
NeilBrownc3d97142009-12-14 12:49:52 +11001145 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001146 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001149 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001151 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001152 int is_active = test_bit(In_sync, &rdev2->flags);
1153
1154 if (rdev2->raid_disk >= 0 &&
1155 sb->minor_version >= 91)
1156 /* we have nowhere to store the recovery_offset,
1157 * but if it is not below the reshape_position,
1158 * we can piggy-back on that.
1159 */
1160 is_active = 1;
1161 if (rdev2->raid_disk < 0 ||
1162 test_bit(Faulty, &rdev2->flags))
1163 is_active = 0;
1164 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001165 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001167 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001168 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 d = &sb->disks[rdev2->desc_nr];
1170 nr_disks++;
1171 d->number = rdev2->desc_nr;
1172 d->major = MAJOR(rdev2->bdev->bd_dev);
1173 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001174 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 d->raid_disk = rdev2->raid_disk;
1176 else
1177 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001178 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001180 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001182 if (test_bit(In_sync, &rdev2->flags))
1183 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 active++;
1185 working++;
1186 } else {
1187 d->state = 0;
1188 spare++;
1189 working++;
1190 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001191 if (test_bit(WriteMostly, &rdev2->flags))
1192 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 /* now set the "removed" and "faulty" bits on any missing devices */
1195 for (i=0 ; i < mddev->raid_disks ; i++) {
1196 mdp_disk_t *d = &sb->disks[i];
1197 if (d->state == 0 && d->number == 0) {
1198 d->number = i;
1199 d->raid_disk = i;
1200 d->state = (1<<MD_DISK_REMOVED);
1201 d->state |= (1<<MD_DISK_FAULTY);
1202 failed++;
1203 }
1204 }
1205 sb->nr_disks = nr_disks;
1206 sb->active_disks = active;
1207 sb->working_disks = working;
1208 sb->failed_disks = failed;
1209 sb->spare_disks = spare;
1210
1211 sb->this_disk = sb->disks[rdev->desc_nr];
1212 sb->sb_csum = calc_sb_csum(sb);
1213}
1214
1215/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001216 * rdev_size_change for 0.90.0
1217 */
1218static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001219super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001220{
Andre Noll58c0fed2009-03-31 14:33:13 +11001221 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001222 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001223 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001224 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001225 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001226 if (!num_sectors || num_sectors > rdev->sb_start)
1227 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001228 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001229 rdev->sb_page);
1230 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001231 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001232}
1233
1234
1235/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 * version 1 superblock
1237 */
1238
NeilBrown1c05b4b2006-10-21 10:24:08 -07001239static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001241 __le32 disk_csum;
1242 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 unsigned long long newcsum;
1244 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001245 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 int i;
1247
1248 disk_csum = sb->sb_csum;
1249 sb->sb_csum = 0;
1250 newcsum = 0;
1251 for (i=0; size>=4; size -= 4 )
1252 newcsum += le32_to_cpu(*isuper++);
1253
1254 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001255 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1258 sb->sb_csum = disk_csum;
1259 return cpu_to_le32(csum);
1260}
1261
1262static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1263{
1264 struct mdp_superblock_1 *sb;
1265 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001266 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001268 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 /*
Andre Noll0f420352008-07-11 22:02:23 +10001271 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 * It is always aligned to a 4K boundary and
1273 * depeding on minor_version, it can be:
1274 * 0: At least 8K, but less than 12K, from end of device
1275 * 1: At start of device
1276 * 2: 4K from start of device.
1277 */
1278 switch(minor_version) {
1279 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001280 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1281 sb_start -= 8*2;
1282 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 break;
1284 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001285 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 break;
1287 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001288 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 break;
1290 default:
1291 return -EINVAL;
1292 }
Andre Noll0f420352008-07-11 22:02:23 +10001293 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
NeilBrown0002b272005-09-09 16:23:53 -07001295 /* superblock is rarely larger than 1K, but it can be larger,
1296 * and it is safe to read 4k, so we do that
1297 */
1298 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (ret) return ret;
1300
1301
1302 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1303
1304 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1305 sb->major_version != cpu_to_le32(1) ||
1306 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001307 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001308 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return -EINVAL;
1310
1311 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1312 printk("md: invalid superblock checksum on %s\n",
1313 bdevname(rdev->bdev,b));
1314 return -EINVAL;
1315 }
1316 if (le64_to_cpu(sb->data_size) < 10) {
1317 printk("md: data_size too small on %s\n",
1318 bdevname(rdev->bdev,b));
1319 return -EINVAL;
1320 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001321
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 rdev->preferred_minor = 0xffff;
1323 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001324 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
NeilBrown0002b272005-09-09 16:23:53 -07001326 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001327 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001328 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001329 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1330
1331 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001332 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001333 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001334
NeilBrown31b65a02006-07-10 04:44:14 -07001335 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1336 rdev->desc_nr = -1;
1337 else
1338 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1339
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001340 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001341 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001342 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 __u64 ev1, ev2;
1344 struct mdp_superblock_1 *refsb =
1345 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1346
1347 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1348 sb->level != refsb->level ||
1349 sb->layout != refsb->layout ||
1350 sb->chunksize != refsb->chunksize) {
1351 printk(KERN_WARNING "md: %s has strangely different"
1352 " superblock to %s\n",
1353 bdevname(rdev->bdev,b),
1354 bdevname(refdev->bdev,b2));
1355 return -EINVAL;
1356 }
1357 ev1 = le64_to_cpu(sb->events);
1358 ev2 = le64_to_cpu(refsb->events);
1359
1360 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001361 ret = 1;
1362 else
1363 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
NeilBrowna1801f82008-03-04 14:29:31 -08001365 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001366 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1367 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001369 rdev->sectors = rdev->sb_start;
1370 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001372 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001373 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001374 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001375 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376}
1377
1378static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1379{
1380 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001381 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
NeilBrown41158c72005-06-21 17:17:25 -07001383 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001384 clear_bit(Faulty, &rdev->flags);
1385 clear_bit(In_sync, &rdev->flags);
1386 clear_bit(WriteMostly, &rdev->flags);
1387 clear_bit(BarriersNotsupp, &rdev->flags);
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (mddev->raid_disks == 0) {
1390 mddev->major_version = 1;
1391 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001392 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001393 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1395 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1396 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001397 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 mddev->layout = le32_to_cpu(sb->layout);
1399 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001400 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001401 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001402 mddev->bitmap_info.offset = 0;
1403 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1406 memcpy(mddev->uuid, sb->set_uuid, 16);
1407
1408 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001409
NeilBrown71c08052005-09-09 16:23:51 -07001410 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001411 mddev->bitmap_info.file == NULL )
1412 mddev->bitmap_info.offset =
1413 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001414
NeilBrownf6705572006-03-27 01:18:11 -08001415 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1416 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1417 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1418 mddev->new_level = le32_to_cpu(sb->new_level);
1419 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001420 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001421 } else {
1422 mddev->reshape_position = MaxSector;
1423 mddev->delta_disks = 0;
1424 mddev->new_level = mddev->level;
1425 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001426 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001427 }
1428
NeilBrown41158c72005-06-21 17:17:25 -07001429 } else if (mddev->pers == NULL) {
1430 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 ++ev1;
1432 if (ev1 < mddev->events)
1433 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001434 } else if (mddev->bitmap) {
1435 /* If adding to array with a bitmap, then we can accept an
1436 * older device, but not too old.
1437 */
NeilBrown41158c72005-06-21 17:17:25 -07001438 if (ev1 < mddev->bitmap->events_cleared)
1439 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001440 } else {
1441 if (ev1 < mddev->events)
1442 /* just a hot-add of a new device, leave raid_disk at -1 */
1443 return 0;
1444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 if (mddev->level != LEVEL_MULTIPATH) {
1446 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001447 if (rdev->desc_nr < 0 ||
1448 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1449 role = 0xffff;
1450 rdev->desc_nr = -1;
1451 } else
1452 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 switch(role) {
1454 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 break;
1456 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001457 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 break;
1459 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001460 if ((le32_to_cpu(sb->feature_map) &
1461 MD_FEATURE_RECOVERY_OFFSET))
1462 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1463 else
1464 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 rdev->raid_disk = role;
1466 break;
1467 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001468 if (sb->devflags & WriteMostly1)
1469 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001470 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001471 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 return 0;
1474}
1475
1476static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1477{
1478 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 mdk_rdev_t *rdev2;
1480 int max_dev, i;
1481 /* make rdev->sb match mddev and rdev data. */
1482
1483 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1484
1485 sb->feature_map = 0;
1486 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001487 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 memset(sb->pad1, 0, sizeof(sb->pad1));
1489 memset(sb->pad2, 0, sizeof(sb->pad2));
1490 memset(sb->pad3, 0, sizeof(sb->pad3));
1491
1492 sb->utime = cpu_to_le64((__u64)mddev->utime);
1493 sb->events = cpu_to_le64(mddev->events);
1494 if (mddev->in_sync)
1495 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1496 else
1497 sb->resync_offset = cpu_to_le64(0);
1498
NeilBrown1c05b4b2006-10-21 10:24:08 -07001499 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001500
NeilBrownf0ca3402006-02-02 14:28:04 -08001501 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001502 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001503 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001504 sb->level = cpu_to_le32(mddev->level);
1505 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001506
NeilBrownc3d97142009-12-14 12:49:52 +11001507 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1508 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001509 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001510 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001511
1512 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001513 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001514 sb->feature_map |=
1515 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1516 sb->recovery_offset =
1517 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001518 }
1519
NeilBrownf6705572006-03-27 01:18:11 -08001520 if (mddev->reshape_position != MaxSector) {
1521 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1522 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1523 sb->new_layout = cpu_to_le32(mddev->new_layout);
1524 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1525 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001526 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001527 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001530 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 if (rdev2->desc_nr+1 > max_dev)
1532 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001533
NeilBrown70471da2009-08-03 10:59:57 +10001534 if (max_dev > le32_to_cpu(sb->max_dev)) {
1535 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001536 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001537 rdev->sb_size = max_dev * 2 + 256;
1538 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1539 if (rdev->sb_size & bmask)
1540 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 for (i=0; i<max_dev;i++)
1543 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1544
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001545 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001547 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001549 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001551 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001552 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 else
1554 sb->dev_roles[i] = cpu_to_le16(0xffff);
1555 }
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 sb->sb_csum = calc_sb_1_csum(sb);
1558}
1559
Chris Webb0cd17fe2008-06-28 08:31:46 +10001560static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001561super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001562{
1563 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001564 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001565 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001566 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001567 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001568 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001569 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1570 max_sectors -= rdev->data_offset;
1571 if (!num_sectors || num_sectors > max_sectors)
1572 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001573 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001574 /* minor version 0 with bitmap we can't move */
1575 return 0;
1576 } else {
1577 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001578 sector_t sb_start;
1579 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1580 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001581 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001582 if (!num_sectors || num_sectors > max_sectors)
1583 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001584 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001585 }
1586 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001587 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001588 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001589 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001590 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001591 rdev->sb_page);
1592 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001593 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001594}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Adrian Bunk75c96f82005-05-05 16:16:09 -07001596static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 [0] = {
1598 .name = "0.90.0",
1599 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001600 .load_super = super_90_load,
1601 .validate_super = super_90_validate,
1602 .sync_super = super_90_sync,
1603 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 },
1605 [1] = {
1606 .name = "md-1",
1607 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001608 .load_super = super_1_load,
1609 .validate_super = super_1_validate,
1610 .sync_super = super_1_sync,
1611 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 },
1613};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1616{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001617 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
NeilBrown4b809912008-07-21 17:05:25 +10001619 rcu_read_lock();
1620 rdev_for_each_rcu(rdev, mddev1)
1621 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001622 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001623 rdev2->bdev->bd_contains) {
1624 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001625 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001626 }
1627 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 return 0;
1629}
1630
1631static LIST_HEAD(pending_raid_disks);
1632
Andre Nollac5e7112009-08-03 10:59:47 +10001633/*
1634 * Try to register data integrity profile for an mddev
1635 *
1636 * This is called when an array is started and after a disk has been kicked
1637 * from the array. It only succeeds if all working and active component devices
1638 * are integrity capable with matching profiles.
1639 */
1640int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001641{
Andre Nollac5e7112009-08-03 10:59:47 +10001642 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001643
Andre Nollac5e7112009-08-03 10:59:47 +10001644 if (list_empty(&mddev->disks))
1645 return 0; /* nothing to do */
1646 if (blk_get_integrity(mddev->gendisk))
1647 return 0; /* already registered */
1648 list_for_each_entry(rdev, &mddev->disks, same_set) {
1649 /* skip spares and non-functional disks */
1650 if (test_bit(Faulty, &rdev->flags))
1651 continue;
1652 if (rdev->raid_disk < 0)
1653 continue;
1654 /*
1655 * If at least one rdev is not integrity capable, we can not
1656 * enable data integrity for the md device.
1657 */
1658 if (!bdev_get_integrity(rdev->bdev))
1659 return -EINVAL;
1660 if (!reference) {
1661 /* Use the first rdev as the reference */
1662 reference = rdev;
1663 continue;
1664 }
1665 /* does this rdev's profile match the reference profile? */
1666 if (blk_integrity_compare(reference->bdev->bd_disk,
1667 rdev->bdev->bd_disk) < 0)
1668 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001669 }
Andre Nollac5e7112009-08-03 10:59:47 +10001670 /*
1671 * All component devices are integrity capable and have matching
1672 * profiles, register the common profile for the md device.
1673 */
1674 if (blk_integrity_register(mddev->gendisk,
1675 bdev_get_integrity(reference->bdev)) != 0) {
1676 printk(KERN_ERR "md: failed to register integrity for %s\n",
1677 mdname(mddev));
1678 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001679 }
Andre Nollac5e7112009-08-03 10:59:47 +10001680 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1681 mdname(mddev));
1682 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001683}
Andre Nollac5e7112009-08-03 10:59:47 +10001684EXPORT_SYMBOL(md_integrity_register);
1685
1686/* Disable data integrity if non-capable/non-matching disk is being added */
1687void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1688{
1689 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1690 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1691
1692 if (!bi_mddev) /* nothing to do */
1693 return;
1694 if (rdev->raid_disk < 0) /* skip spares */
1695 return;
1696 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1697 rdev->bdev->bd_disk) >= 0)
1698 return;
1699 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1700 blk_integrity_unregister(mddev->gendisk);
1701}
1702EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1705{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001706 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001707 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001708 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001709 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711 if (rdev->mddev) {
1712 MD_BUG();
1713 return -EINVAL;
1714 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001715
1716 /* prevent duplicates */
1717 if (find_rdev(mddev, rdev->bdev->bd_dev))
1718 return -EEXIST;
1719
Andre Nolldd8ac332009-03-31 14:33:13 +11001720 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1721 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1722 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001723 if (mddev->pers) {
1724 /* Cannot change size, so fail
1725 * If mddev->level <= 0, then we don't care
1726 * about aligning sizes (e.g. linear)
1727 */
1728 if (mddev->level > 0)
1729 return -ENOSPC;
1730 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001731 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 /* Verify rdev->desc_nr is unique.
1735 * If it is -1, assign a free number, else
1736 * check number is not in use
1737 */
1738 if (rdev->desc_nr < 0) {
1739 int choice = 0;
1740 if (mddev->pers) choice = mddev->raid_disks;
1741 while (find_rdev_nr(mddev, choice))
1742 choice++;
1743 rdev->desc_nr = choice;
1744 } else {
1745 if (find_rdev_nr(mddev, rdev->desc_nr))
1746 return -EBUSY;
1747 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001748 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1749 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1750 mdname(mddev), mddev->max_disks);
1751 return -EBUSY;
1752 }
NeilBrown19133a42005-11-08 21:39:35 -08001753 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001754 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001755 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001758 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001759
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001760 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001761 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001762
Tejun Heo0762b8b2008-08-25 19:56:12 +09001763 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001764 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1765 kobject_del(&rdev->kobj);
1766 goto fail;
1767 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001768 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1769
NeilBrown4b809912008-07-21 17:05:25 +10001770 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001771 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001772
1773 /* May as well allow recovery to be retried once */
1774 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001777
1778 fail:
1779 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1780 b, mdname(mddev));
1781 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782}
1783
NeilBrown177a99b2008-02-06 01:39:56 -08001784static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001785{
1786 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1787 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001788 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001789}
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1792{
1793 char b[BDEVNAME_SIZE];
1794 if (!rdev->mddev) {
1795 MD_BUG();
1796 return;
1797 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001798 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001799 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1801 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001802 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001803 sysfs_put(rdev->sysfs_state);
1804 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001805 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001806 * writing to 'remove' to "dev/state". We also need
1807 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001808 */
NeilBrown4b809912008-07-21 17:05:25 +10001809 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001810 INIT_WORK(&rdev->del_work, md_delayed_delete);
1811 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001812 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
1815/*
1816 * prevent the device from being mounted, repartitioned or
1817 * otherwise reused by a RAID array (or any other kernel
1818 * subsystem), by bd_claiming the device.
1819 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001820static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
1822 int err = 0;
1823 struct block_device *bdev;
1824 char b[BDEVNAME_SIZE];
1825
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001826 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 if (IS_ERR(bdev)) {
1828 printk(KERN_ERR "md: could not open %s.\n",
1829 __bdevname(dev, b));
1830 return PTR_ERR(bdev);
1831 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001832 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 if (err) {
1834 printk(KERN_ERR "md: could not bd_claim %s.\n",
1835 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001836 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return err;
1838 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001839 if (!shared)
1840 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 rdev->bdev = bdev;
1842 return err;
1843}
1844
1845static void unlock_rdev(mdk_rdev_t *rdev)
1846{
1847 struct block_device *bdev = rdev->bdev;
1848 rdev->bdev = NULL;
1849 if (!bdev)
1850 MD_BUG();
1851 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001852 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853}
1854
1855void md_autodetect_dev(dev_t dev);
1856
1857static void export_rdev(mdk_rdev_t * rdev)
1858{
1859 char b[BDEVNAME_SIZE];
1860 printk(KERN_INFO "md: export_rdev(%s)\n",
1861 bdevname(rdev->bdev,b));
1862 if (rdev->mddev)
1863 MD_BUG();
1864 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001866 if (test_bit(AutoDetected, &rdev->flags))
1867 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868#endif
1869 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001870 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871}
1872
1873static void kick_rdev_from_array(mdk_rdev_t * rdev)
1874{
1875 unbind_rdev_from_array(rdev);
1876 export_rdev(rdev);
1877}
1878
1879static void export_array(mddev_t *mddev)
1880{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001881 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
NeilBrownd089c6a2008-02-06 01:39:59 -08001883 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (!rdev->mddev) {
1885 MD_BUG();
1886 continue;
1887 }
1888 kick_rdev_from_array(rdev);
1889 }
1890 if (!list_empty(&mddev->disks))
1891 MD_BUG();
1892 mddev->raid_disks = 0;
1893 mddev->major_version = 0;
1894}
1895
1896static void print_desc(mdp_disk_t *desc)
1897{
1898 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1899 desc->major,desc->minor,desc->raid_disk,desc->state);
1900}
1901
Cheng Renquancd2ac932009-01-09 08:31:08 +11001902static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
1904 int i;
1905
1906 printk(KERN_INFO
1907 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1908 sb->major_version, sb->minor_version, sb->patch_version,
1909 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1910 sb->ctime);
1911 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1912 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1913 sb->md_minor, sb->layout, sb->chunk_size);
1914 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1915 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1916 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1917 sb->failed_disks, sb->spare_disks,
1918 sb->sb_csum, (unsigned long)sb->events_lo);
1919
1920 printk(KERN_INFO);
1921 for (i = 0; i < MD_SB_DISKS; i++) {
1922 mdp_disk_t *desc;
1923
1924 desc = sb->disks + i;
1925 if (desc->number || desc->major || desc->minor ||
1926 desc->raid_disk || (desc->state && (desc->state != 4))) {
1927 printk(" D %2d: ", i);
1928 print_desc(desc);
1929 }
1930 }
1931 printk(KERN_INFO "md: THIS: ");
1932 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
1934
Cheng Renquancd2ac932009-01-09 08:31:08 +11001935static void print_sb_1(struct mdp_superblock_1 *sb)
1936{
1937 __u8 *uuid;
1938
1939 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001940 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08001941 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001942 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001943 le32_to_cpu(sb->major_version),
1944 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08001945 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11001946 sb->set_name,
1947 (unsigned long long)le64_to_cpu(sb->ctime)
1948 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1949
1950 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001951 printk(KERN_INFO
1952 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11001953 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08001954 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001955 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1956 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001957 le32_to_cpu(sb->level),
1958 (unsigned long long)le64_to_cpu(sb->size),
1959 le32_to_cpu(sb->raid_disks),
1960 le32_to_cpu(sb->layout),
1961 le32_to_cpu(sb->chunksize),
1962 (unsigned long long)le64_to_cpu(sb->data_offset),
1963 (unsigned long long)le64_to_cpu(sb->data_size),
1964 (unsigned long long)le64_to_cpu(sb->super_offset),
1965 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1966 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08001967 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11001968 sb->devflags,
1969 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1970 (unsigned long long)le64_to_cpu(sb->events),
1971 (unsigned long long)le64_to_cpu(sb->resync_offset),
1972 le32_to_cpu(sb->sb_csum),
1973 le32_to_cpu(sb->max_dev)
1974 );
1975}
1976
1977static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
1979 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11001980 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1981 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08001982 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1983 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11001985 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1986 switch (major_version) {
1987 case 0:
1988 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1989 break;
1990 case 1:
1991 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1992 break;
1993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 } else
1995 printk(KERN_INFO "md: no rdev superblock!\n");
1996}
1997
Adrian Bunk5e563412006-06-26 00:27:42 -07001998static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002000 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 mdk_rdev_t *rdev;
2002 mddev_t *mddev;
2003 char b[BDEVNAME_SIZE];
2004
2005 printk("\n");
2006 printk("md: **********************************\n");
2007 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2008 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002009 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
NeilBrown32a76272005-06-21 17:17:14 -07002011 if (mddev->bitmap)
2012 bitmap_print_sb(mddev->bitmap);
2013 else
2014 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002015 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 printk("<%s>", bdevname(rdev->bdev,b));
2017 printk("\n");
2018
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002019 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002020 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
2022 printk("md: **********************************\n");
2023 printk("\n");
2024}
2025
2026
NeilBrown42543762006-06-26 00:27:57 -07002027static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
NeilBrown42543762006-06-26 00:27:57 -07002029 /* Update each superblock (in-memory image), but
2030 * if we are allowed to, skip spares which already
2031 * have the right event counter, or have one earlier
2032 * (which would mean they aren't being marked as dirty
2033 * with the rest of the array)
2034 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
NeilBrown5e865102009-11-12 12:08:04 +11002037 /* First make sure individual recovery_offsets are correct */
2038 list_for_each_entry(rdev, &mddev->disks, same_set) {
2039 if (rdev->raid_disk >= 0 &&
2040 !test_bit(In_sync, &rdev->flags) &&
2041 mddev->curr_resync_completed > rdev->recovery_offset)
2042 rdev->recovery_offset = mddev->curr_resync_completed;
2043
2044 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002045 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002046 if (rdev->sb_events == mddev->events ||
2047 (nospares &&
2048 rdev->raid_disk < 0 &&
2049 (rdev->sb_events&1)==0 &&
2050 rdev->sb_events+1 == mddev->events)) {
2051 /* Don't update this superblock */
2052 rdev->sb_loaded = 2;
2053 } else {
2054 super_types[mddev->major_version].
2055 sync_super(mddev, rdev);
2056 rdev->sb_loaded = 1;
2057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 }
2059}
2060
NeilBrown850b2b42006-10-03 01:15:46 -07002061static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002064 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002065 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
NeilBrown1b57f132009-06-18 08:48:19 +10002067 mddev->utime = get_seconds();
NeilBrown8377bc82008-04-30 00:52:28 -07002068 if (mddev->external)
2069 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08002071 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002072
NeilBrown850b2b42006-10-03 01:15:46 -07002073 set_bit(MD_CHANGE_PENDING, &mddev->flags);
2074 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2075 force_change = 1;
2076 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2077 /* just a clean<-> dirty transition, possibly leave spares alone,
2078 * though if events isn't the right even/odd, we will have to do
2079 * spares after all
2080 */
2081 nospares = 1;
2082 if (force_change)
2083 nospares = 0;
2084 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002085 /* If the array is degraded, then skipping spares is both
2086 * dangerous and fairly pointless.
2087 * Dangerous because a device that was removed from the array
2088 * might have a event_count that still looks up-to-date,
2089 * so it can be re-added without a resync.
2090 * Pointless because if there are any spares to skip,
2091 * then a recovery will happen and soon that array won't
2092 * be degraded any more and the spare can go back to sleep then.
2093 */
NeilBrown850b2b42006-10-03 01:15:46 -07002094 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002095
NeilBrown06d91a52005-06-21 17:17:12 -07002096 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002097
2098 /* If this is just a dirty<->clean transition, and the array is clean
2099 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002100 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002101 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08002102 && (mddev->events & 1)
2103 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07002104 mddev->events--;
2105 else {
2106 /* otherwise we have to go forward and ... */
2107 mddev->events ++;
2108 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
NeilBrown51d56682009-08-13 09:54:02 +10002109 /* .. if the array isn't clean, an 'even' event must also go
2110 * to spares. */
2111 if ((mddev->events&1)==0)
NeilBrown42543762006-06-26 00:27:57 -07002112 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002113 } else {
NeilBrown51d56682009-08-13 09:54:02 +10002114 /* otherwise an 'odd' event must go to spares */
2115 if ((mddev->events&1))
NeilBrown42543762006-06-26 00:27:57 -07002116 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002117 }
2118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
2120 if (!mddev->events) {
2121 /*
2122 * oops, this 64-bit counter should never wrap.
2123 * Either we are in around ~1 trillion A.C., assuming
2124 * 1 reboot per second, or we have a bug:
2125 */
2126 MD_BUG();
2127 mddev->events --;
2128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 /*
2131 * do not write anything to disk if using
2132 * nonpersistent superblocks
2133 */
NeilBrown06d91a52005-06-21 17:17:12 -07002134 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08002135 if (!mddev->external)
2136 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2137
NeilBrowna9701a32005-11-08 21:39:34 -08002138 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002139 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return;
NeilBrown06d91a52005-06-21 17:17:12 -07002141 }
NeilBrowne6910632008-02-06 01:39:51 -08002142 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002143 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 dprintk(KERN_INFO
2146 "md: updating %s RAID superblock on device (in sync %d)\n",
2147 mdname(mddev),mddev->in_sync);
2148
NeilBrown4ad13662007-07-17 04:06:13 -07002149 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002150 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 char b[BDEVNAME_SIZE];
2152 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002153 if (rdev->sb_loaded != 1)
2154 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002155 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 dprintk("(skipping faulty ");
2157
2158 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002159 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002160 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002161 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002162 rdev->sb_page);
2163 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2164 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002165 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002166 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 } else
2169 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002170 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 /* only need to write one superblock... */
2172 break;
2173 }
NeilBrowna9701a32005-11-08 21:39:34 -08002174 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002175 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002176
NeilBrowna9701a32005-11-08 21:39:34 -08002177 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002178 if (mddev->in_sync != sync_req ||
2179 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002180 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002181 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002182 goto repeat;
2183 }
NeilBrown850b2b42006-10-03 01:15:46 -07002184 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002185 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002186 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002187 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2188 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190}
2191
Andre Noll7f6ce762008-03-23 18:34:54 +01002192/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002193 * We want to accept with case. For this we use cmd_match.
2194 */
2195static int cmd_match(const char *cmd, const char *str)
2196{
2197 /* See if cmd, written into a sysfs file, matches
2198 * str. They must either be the same, or cmd can
2199 * have a trailing newline
2200 */
2201 while (*cmd && *str && *cmd == *str) {
2202 cmd++;
2203 str++;
2204 }
2205 if (*cmd == '\n')
2206 cmd++;
2207 if (*str || *cmd)
2208 return 0;
2209 return 1;
2210}
2211
NeilBrown86e6ffd2005-11-08 21:39:24 -08002212struct rdev_sysfs_entry {
2213 struct attribute attr;
2214 ssize_t (*show)(mdk_rdev_t *, char *);
2215 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2216};
2217
2218static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002219state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002220{
2221 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002222 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002223
NeilBrownb2d444d2005-11-08 21:39:31 -08002224 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002225 len+= sprintf(page+len, "%sfaulty",sep);
2226 sep = ",";
2227 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002228 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002229 len += sprintf(page+len, "%sin_sync",sep);
2230 sep = ",";
2231 }
NeilBrownf6556752006-06-26 00:28:01 -07002232 if (test_bit(WriteMostly, &rdev->flags)) {
2233 len += sprintf(page+len, "%swrite_mostly",sep);
2234 sep = ",";
2235 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002236 if (test_bit(Blocked, &rdev->flags)) {
2237 len += sprintf(page+len, "%sblocked", sep);
2238 sep = ",";
2239 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002240 if (!test_bit(Faulty, &rdev->flags) &&
2241 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002242 len += sprintf(page+len, "%sspare", sep);
2243 sep = ",";
2244 }
2245 return len+sprintf(page+len, "\n");
2246}
2247
NeilBrown45dc2de2006-06-26 00:27:58 -07002248static ssize_t
2249state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2250{
2251 /* can write
2252 * faulty - simulates and error
2253 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002254 * writemostly - sets write_mostly
2255 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002256 * blocked - sets the Blocked flag
2257 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002258 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002259 */
2260 int err = -EINVAL;
2261 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2262 md_error(rdev->mddev, rdev);
2263 err = 0;
2264 } else if (cmd_match(buf, "remove")) {
2265 if (rdev->raid_disk >= 0)
2266 err = -EBUSY;
2267 else {
2268 mddev_t *mddev = rdev->mddev;
2269 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002270 if (mddev->pers)
2271 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002272 md_new_event(mddev);
2273 err = 0;
2274 }
NeilBrownf6556752006-06-26 00:28:01 -07002275 } else if (cmd_match(buf, "writemostly")) {
2276 set_bit(WriteMostly, &rdev->flags);
2277 err = 0;
2278 } else if (cmd_match(buf, "-writemostly")) {
2279 clear_bit(WriteMostly, &rdev->flags);
2280 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002281 } else if (cmd_match(buf, "blocked")) {
2282 set_bit(Blocked, &rdev->flags);
2283 err = 0;
2284 } else if (cmd_match(buf, "-blocked")) {
2285 clear_bit(Blocked, &rdev->flags);
2286 wake_up(&rdev->blocked_wait);
2287 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2288 md_wakeup_thread(rdev->mddev->thread);
2289
2290 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002291 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2292 set_bit(In_sync, &rdev->flags);
2293 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002294 }
NeilBrown3c0ee632008-10-21 13:25:28 +11002295 if (!err && rdev->sysfs_state)
2296 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002297 return err ? err : len;
2298}
NeilBrown80ca3a42006-07-10 04:44:18 -07002299static struct rdev_sysfs_entry rdev_state =
2300__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002301
2302static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002303errors_show(mdk_rdev_t *rdev, char *page)
2304{
2305 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2306}
2307
2308static ssize_t
2309errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2310{
2311 char *e;
2312 unsigned long n = simple_strtoul(buf, &e, 10);
2313 if (*buf && (*e == 0 || *e == '\n')) {
2314 atomic_set(&rdev->corrected_errors, n);
2315 return len;
2316 }
2317 return -EINVAL;
2318}
2319static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002320__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002321
NeilBrown014236d2006-01-06 00:20:55 -08002322static ssize_t
2323slot_show(mdk_rdev_t *rdev, char *page)
2324{
2325 if (rdev->raid_disk < 0)
2326 return sprintf(page, "none\n");
2327 else
2328 return sprintf(page, "%d\n", rdev->raid_disk);
2329}
2330
2331static ssize_t
2332slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2333{
2334 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002335 int err;
2336 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002337 int slot = simple_strtoul(buf, &e, 10);
2338 if (strncmp(buf, "none", 4)==0)
2339 slot = -1;
2340 else if (e==buf || (*e && *e!= '\n'))
2341 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002342 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002343 /* Setting 'slot' on an active array requires also
2344 * updating the 'rd%d' link, and communicating
2345 * with the personality with ->hot_*_disk.
2346 * For now we only support removing
2347 * failed/spare devices. This normally happens automatically,
2348 * but not when the metadata is externally managed.
2349 */
NeilBrownc303da62008-02-06 01:39:51 -08002350 if (rdev->raid_disk == -1)
2351 return -EEXIST;
2352 /* personality does all needed checks */
2353 if (rdev->mddev->pers->hot_add_disk == NULL)
2354 return -EINVAL;
2355 err = rdev->mddev->pers->
2356 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2357 if (err)
2358 return err;
2359 sprintf(nm, "rd%d", rdev->raid_disk);
2360 sysfs_remove_link(&rdev->mddev->kobj, nm);
2361 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2362 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002363 } else if (rdev->mddev->pers) {
2364 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002365 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002366 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002367 */
2368
2369 if (rdev->raid_disk != -1)
2370 return -EBUSY;
2371
2372 if (rdev->mddev->pers->hot_add_disk == NULL)
2373 return -EINVAL;
2374
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002375 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002376 if (rdev2->raid_disk == slot)
2377 return -EEXIST;
2378
2379 rdev->raid_disk = slot;
2380 if (test_bit(In_sync, &rdev->flags))
2381 rdev->saved_raid_disk = slot;
2382 else
2383 rdev->saved_raid_disk = -1;
2384 err = rdev->mddev->pers->
2385 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002386 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002387 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002388 return err;
Neil Brown52664732008-06-28 08:31:44 +10002389 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002390 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002391 sprintf(nm, "rd%d", rdev->raid_disk);
2392 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2393 printk(KERN_WARNING
2394 "md: cannot register "
2395 "%s for %s\n",
2396 nm, mdname(rdev->mddev));
2397
2398 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002399 } else {
2400 if (slot >= rdev->mddev->raid_disks)
2401 return -ENOSPC;
2402 rdev->raid_disk = slot;
2403 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002404 clear_bit(Faulty, &rdev->flags);
2405 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002406 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002407 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002408 }
NeilBrown014236d2006-01-06 00:20:55 -08002409 return len;
2410}
2411
2412
2413static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002414__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002415
NeilBrown93c8cad2006-01-06 00:20:56 -08002416static ssize_t
2417offset_show(mdk_rdev_t *rdev, char *page)
2418{
Andrew Morton6961ece2006-01-06 00:20:59 -08002419 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002420}
2421
2422static ssize_t
2423offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2424{
2425 char *e;
2426 unsigned long long offset = simple_strtoull(buf, &e, 10);
2427 if (e==buf || (*e && *e != '\n'))
2428 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002429 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002430 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002431 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002432 /* Must set offset before size, so overlap checks
2433 * can be sane */
2434 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002435 rdev->data_offset = offset;
2436 return len;
2437}
2438
2439static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002440__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002441
NeilBrown83303b62006-01-06 00:21:06 -08002442static ssize_t
2443rdev_size_show(mdk_rdev_t *rdev, char *page)
2444{
Andre Nolldd8ac332009-03-31 14:33:13 +11002445 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002446}
2447
NeilBrownc5d79ad2008-02-06 01:39:54 -08002448static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2449{
2450 /* check if two start/length pairs overlap */
2451 if (s1+l1 <= s2)
2452 return 0;
2453 if (s2+l2 <= s1)
2454 return 0;
2455 return 1;
2456}
2457
Dan Williamsb522adc2009-03-31 15:00:31 +11002458static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2459{
2460 unsigned long long blocks;
2461 sector_t new;
2462
2463 if (strict_strtoull(buf, 10, &blocks) < 0)
2464 return -EINVAL;
2465
2466 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2467 return -EINVAL; /* sector conversion overflow */
2468
2469 new = blocks * 2;
2470 if (new != blocks * 2)
2471 return -EINVAL; /* unsigned long long to sector_t overflow */
2472
2473 *sectors = new;
2474 return 0;
2475}
2476
NeilBrown83303b62006-01-06 00:21:06 -08002477static ssize_t
2478rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2479{
NeilBrown27c529b2008-03-04 14:29:33 -08002480 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002481 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002482 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002483
Dan Williamsb522adc2009-03-31 15:00:31 +11002484 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002485 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002486 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002487 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002488 sectors = super_types[my_mddev->major_version].
2489 rdev_size_change(rdev, sectors);
2490 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002491 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002492 } else if (!sectors)
2493 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2494 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002495 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002496 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002497 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002498
Andre Nolldd8ac332009-03-31 14:33:13 +11002499 rdev->sectors = sectors;
2500 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002501 /* need to check that all other rdevs with the same ->bdev
2502 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002503 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002504 * we have to change it back, we will have the lock again.
2505 */
2506 mddev_t *mddev;
2507 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002508 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002509
NeilBrown27c529b2008-03-04 14:29:33 -08002510 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002511 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002512 mdk_rdev_t *rdev2;
2513
2514 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002515 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002516 if (test_bit(AllReserved, &rdev2->flags) ||
2517 (rdev->bdev == rdev2->bdev &&
2518 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002519 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002520 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002521 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002522 overlap = 1;
2523 break;
2524 }
2525 mddev_unlock(mddev);
2526 if (overlap) {
2527 mddev_put(mddev);
2528 break;
2529 }
2530 }
NeilBrown27c529b2008-03-04 14:29:33 -08002531 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002532 if (overlap) {
2533 /* Someone else could have slipped in a size
2534 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002535 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002536 * safe, and trust userspace not to race with
2537 * itself
2538 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002539 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002540 return -EBUSY;
2541 }
2542 }
NeilBrown83303b62006-01-06 00:21:06 -08002543 return len;
2544}
2545
2546static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002547__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002548
Dan Williams06e3c812009-12-12 21:17:12 -07002549
2550static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2551{
2552 unsigned long long recovery_start = rdev->recovery_offset;
2553
2554 if (test_bit(In_sync, &rdev->flags) ||
2555 recovery_start == MaxSector)
2556 return sprintf(page, "none\n");
2557
2558 return sprintf(page, "%llu\n", recovery_start);
2559}
2560
2561static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2562{
2563 unsigned long long recovery_start;
2564
2565 if (cmd_match(buf, "none"))
2566 recovery_start = MaxSector;
2567 else if (strict_strtoull(buf, 10, &recovery_start))
2568 return -EINVAL;
2569
2570 if (rdev->mddev->pers &&
2571 rdev->raid_disk >= 0)
2572 return -EBUSY;
2573
2574 rdev->recovery_offset = recovery_start;
2575 if (recovery_start == MaxSector)
2576 set_bit(In_sync, &rdev->flags);
2577 else
2578 clear_bit(In_sync, &rdev->flags);
2579 return len;
2580}
2581
2582static struct rdev_sysfs_entry rdev_recovery_start =
2583__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2584
NeilBrown86e6ffd2005-11-08 21:39:24 -08002585static struct attribute *rdev_default_attrs[] = {
2586 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002587 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002588 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002589 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002590 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002591 &rdev_recovery_start.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002592 NULL,
2593};
2594static ssize_t
2595rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2596{
2597 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2598 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002599 mddev_t *mddev = rdev->mddev;
2600 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002601
2602 if (!entry->show)
2603 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002604
2605 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2606 if (!rv) {
2607 if (rdev->mddev == NULL)
2608 rv = -EBUSY;
2609 else
2610 rv = entry->show(rdev, page);
2611 mddev_unlock(mddev);
2612 }
2613 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002614}
2615
2616static ssize_t
2617rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2618 const char *page, size_t length)
2619{
2620 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2621 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002622 ssize_t rv;
2623 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002624
2625 if (!entry->store)
2626 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002627 if (!capable(CAP_SYS_ADMIN))
2628 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002629 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002630 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002631 if (rdev->mddev == NULL)
2632 rv = -EBUSY;
2633 else
2634 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002635 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002636 }
2637 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002638}
2639
2640static void rdev_free(struct kobject *ko)
2641{
2642 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2643 kfree(rdev);
2644}
2645static struct sysfs_ops rdev_sysfs_ops = {
2646 .show = rdev_attr_show,
2647 .store = rdev_attr_store,
2648};
2649static struct kobj_type rdev_ktype = {
2650 .release = rdev_free,
2651 .sysfs_ops = &rdev_sysfs_ops,
2652 .default_attrs = rdev_default_attrs,
2653};
2654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655/*
2656 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2657 *
2658 * mark the device faulty if:
2659 *
2660 * - the device is nonexistent (zero size)
2661 * - the device has no valid superblock
2662 *
2663 * a faulty rdev _never_ has rdev->sb set.
2664 */
2665static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2666{
2667 char b[BDEVNAME_SIZE];
2668 int err;
2669 mdk_rdev_t *rdev;
2670 sector_t size;
2671
NeilBrown9ffae0c2006-01-06 00:20:32 -08002672 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 if (!rdev) {
2674 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2675 return ERR_PTR(-ENOMEM);
2676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
2678 if ((err = alloc_disk_sb(rdev)))
2679 goto abort_free;
2680
NeilBrownc5d79ad2008-02-06 01:39:54 -08002681 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 if (err)
2683 goto abort_free;
2684
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002685 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002686
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002688 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002689 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002690 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002692 rdev->sb_events = 0;
Robert Becker1e509152009-12-14 12:49:58 +11002693 rdev->last_read_error.tv_sec = 0;
2694 rdev->last_read_error.tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002696 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002697 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
2699 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2700 if (!size) {
2701 printk(KERN_WARNING
2702 "md: %s has zero or unknown size, marking faulty!\n",
2703 bdevname(rdev->bdev,b));
2704 err = -EINVAL;
2705 goto abort_free;
2706 }
2707
2708 if (super_format >= 0) {
2709 err = super_types[super_format].
2710 load_super(rdev, NULL, super_minor);
2711 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002712 printk(KERN_WARNING
2713 "md: %s does not have a valid v%d.%d "
2714 "superblock, not importing!\n",
2715 bdevname(rdev->bdev,b),
2716 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 goto abort_free;
2718 }
2719 if (err < 0) {
2720 printk(KERN_WARNING
2721 "md: could not read %s's sb, not importing!\n",
2722 bdevname(rdev->bdev,b));
2723 goto abort_free;
2724 }
2725 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002728 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
2730 return rdev;
2731
2732abort_free:
2733 if (rdev->sb_page) {
2734 if (rdev->bdev)
2735 unlock_rdev(rdev);
2736 free_disk_sb(rdev);
2737 }
2738 kfree(rdev);
2739 return ERR_PTR(err);
2740}
2741
2742/*
2743 * Check a full RAID array for plausibility
2744 */
2745
2746
NeilBrowna757e642005-04-16 15:26:42 -07002747static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
2749 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002750 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 char b[BDEVNAME_SIZE];
2752
2753 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002754 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 switch (super_types[mddev->major_version].
2756 load_super(rdev, freshest, mddev->minor_version)) {
2757 case 1:
2758 freshest = rdev;
2759 break;
2760 case 0:
2761 break;
2762 default:
2763 printk( KERN_ERR \
2764 "md: fatal superblock inconsistency in %s"
2765 " -- removing from array\n",
2766 bdevname(rdev->bdev,b));
2767 kick_rdev_from_array(rdev);
2768 }
2769
2770
2771 super_types[mddev->major_version].
2772 validate_super(mddev, freshest);
2773
2774 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002775 rdev_for_each(rdev, tmp, mddev) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002776 if (rdev->desc_nr >= mddev->max_disks ||
2777 i > mddev->max_disks) {
2778 printk(KERN_WARNING
2779 "md: %s: %s: only %d devices permitted\n",
2780 mdname(mddev), bdevname(rdev->bdev, b),
2781 mddev->max_disks);
2782 kick_rdev_from_array(rdev);
2783 continue;
2784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 if (rdev != freshest)
2786 if (super_types[mddev->major_version].
2787 validate_super(mddev, rdev)) {
2788 printk(KERN_WARNING "md: kicking non-fresh %s"
2789 " from array!\n",
2790 bdevname(rdev->bdev,b));
2791 kick_rdev_from_array(rdev);
2792 continue;
2793 }
2794 if (mddev->level == LEVEL_MULTIPATH) {
2795 rdev->desc_nr = i++;
2796 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002797 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002798 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002799 rdev->raid_disk = -1;
2800 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 }
2802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803}
2804
NeilBrown72e02072009-12-14 12:49:55 +11002805/* Read a fixed-point number.
2806 * Numbers in sysfs attributes should be in "standard" units where
2807 * possible, so time should be in seconds.
2808 * However we internally use a a much smaller unit such as
2809 * milliseconds or jiffies.
2810 * This function takes a decimal number with a possible fractional
2811 * component, and produces an integer which is the result of
2812 * multiplying that number by 10^'scale'.
2813 * all without any floating-point arithmetic.
2814 */
2815int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2816{
2817 unsigned long result = 0;
2818 long decimals = -1;
2819 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2820 if (*cp == '.')
2821 decimals = 0;
2822 else if (decimals < scale) {
2823 unsigned int value;
2824 value = *cp - '0';
2825 result = result * 10 + value;
2826 if (decimals >= 0)
2827 decimals++;
2828 }
2829 cp++;
2830 }
2831 if (*cp == '\n')
2832 cp++;
2833 if (*cp)
2834 return -EINVAL;
2835 if (decimals < 0)
2836 decimals = 0;
2837 while (decimals < scale) {
2838 result *= 10;
2839 decimals ++;
2840 }
2841 *res = result;
2842 return 0;
2843}
2844
2845
NeilBrown19052c02008-08-05 15:54:13 +10002846static void md_safemode_timeout(unsigned long data);
2847
NeilBrowneae17012005-11-08 21:39:23 -08002848static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002849safe_delay_show(mddev_t *mddev, char *page)
2850{
2851 int msec = (mddev->safemode_delay*1000)/HZ;
2852 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2853}
2854static ssize_t
2855safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2856{
NeilBrown16f17b32006-06-26 00:27:37 -07002857 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07002858
NeilBrown72e02072009-12-14 12:49:55 +11002859 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002860 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07002861 if (msec == 0)
2862 mddev->safemode_delay = 0;
2863 else {
NeilBrown19052c02008-08-05 15:54:13 +10002864 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002865 mddev->safemode_delay = (msec*HZ)/1000;
2866 if (mddev->safemode_delay == 0)
2867 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002868 if (mddev->safemode_delay < old_delay)
2869 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002870 }
2871 return len;
2872}
2873static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002874__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002875
2876static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002877level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002878{
NeilBrown2604b702006-01-06 00:20:36 -08002879 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002880 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002881 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002882 else if (mddev->clevel[0])
2883 return sprintf(page, "%s\n", mddev->clevel);
2884 else if (mddev->level != LEVEL_NONE)
2885 return sprintf(page, "%d\n", mddev->level);
2886 else
2887 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002888}
2889
NeilBrownd9d166c2006-01-06 00:20:51 -08002890static ssize_t
2891level_store(mddev_t *mddev, const char *buf, size_t len)
2892{
NeilBrown245f46c2009-03-31 14:39:39 +11002893 char level[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002894 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002895 struct mdk_personality *pers;
2896 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002897 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002898
2899 if (mddev->pers == NULL) {
2900 if (len == 0)
2901 return 0;
2902 if (len >= sizeof(mddev->clevel))
2903 return -ENOSPC;
2904 strncpy(mddev->clevel, buf, len);
2905 if (mddev->clevel[len-1] == '\n')
2906 len--;
2907 mddev->clevel[len] = 0;
2908 mddev->level = LEVEL_NONE;
2909 return rv;
2910 }
2911
2912 /* request to change the personality. Need to ensure:
2913 * - array is not engaged in resync/recovery/reshape
2914 * - old personality can be suspended
2915 * - new personality will access other array.
2916 */
2917
2918 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownd9d166c2006-01-06 00:20:51 -08002919 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002920
2921 if (!mddev->pers->quiesce) {
2922 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2923 mdname(mddev), mddev->pers->name);
2924 return -EINVAL;
2925 }
2926
2927 /* Now find the new personality */
2928 if (len == 0 || len >= sizeof(level))
2929 return -EINVAL;
2930 strncpy(level, buf, len);
2931 if (level[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002932 len--;
NeilBrown245f46c2009-03-31 14:39:39 +11002933 level[len] = 0;
2934
2935 request_module("md-%s", level);
2936 spin_lock(&pers_lock);
2937 pers = find_pers(LEVEL_NONE, level);
2938 if (!pers || !try_module_get(pers->owner)) {
2939 spin_unlock(&pers_lock);
2940 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2941 return -EINVAL;
2942 }
2943 spin_unlock(&pers_lock);
2944
2945 if (pers == mddev->pers) {
2946 /* Nothing to do! */
2947 module_put(pers->owner);
2948 return rv;
2949 }
2950 if (!pers->takeover) {
2951 module_put(pers->owner);
2952 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2953 mdname(mddev), level);
2954 return -EINVAL;
2955 }
2956
2957 /* ->takeover must set new_* and/or delta_disks
2958 * if it succeeds, and may set them when it fails.
2959 */
2960 priv = pers->takeover(mddev);
2961 if (IS_ERR(priv)) {
2962 mddev->new_level = mddev->level;
2963 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002964 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002965 mddev->raid_disks -= mddev->delta_disks;
2966 mddev->delta_disks = 0;
2967 module_put(pers->owner);
2968 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2969 mdname(mddev), level);
2970 return PTR_ERR(priv);
2971 }
2972
2973 /* Looks like we have a winner */
2974 mddev_suspend(mddev);
2975 mddev->pers->stop(mddev);
2976 module_put(mddev->pers->owner);
NeilBrown3a981b02009-08-03 10:59:55 +10002977 /* Invalidate devices that are now superfluous */
2978 list_for_each_entry(rdev, &mddev->disks, same_set)
2979 if (rdev->raid_disk >= mddev->raid_disks) {
2980 rdev->raid_disk = -1;
2981 clear_bit(In_sync, &rdev->flags);
2982 }
NeilBrown245f46c2009-03-31 14:39:39 +11002983 mddev->pers = pers;
2984 mddev->private = priv;
2985 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2986 mddev->level = mddev->new_level;
2987 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002988 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002989 mddev->delta_disks = 0;
2990 pers->run(mddev);
2991 mddev_resume(mddev);
2992 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2993 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2994 md_wakeup_thread(mddev->thread);
NeilBrownd9d166c2006-01-06 00:20:51 -08002995 return rv;
2996}
2997
2998static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002999__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003000
NeilBrownd4dbd022006-06-26 00:27:59 -07003001
3002static ssize_t
3003layout_show(mddev_t *mddev, char *page)
3004{
3005 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003006 if (mddev->reshape_position != MaxSector &&
3007 mddev->layout != mddev->new_layout)
3008 return sprintf(page, "%d (%d)\n",
3009 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003010 return sprintf(page, "%d\n", mddev->layout);
3011}
3012
3013static ssize_t
3014layout_store(mddev_t *mddev, const char *buf, size_t len)
3015{
3016 char *e;
3017 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003018
3019 if (!*buf || (*e && *e != '\n'))
3020 return -EINVAL;
3021
NeilBrownb3546032009-03-31 14:56:41 +11003022 if (mddev->pers) {
3023 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003024 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003025 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003026 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003027 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003028 if (err) {
3029 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003030 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003031 }
NeilBrownb3546032009-03-31 14:56:41 +11003032 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003033 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003034 if (mddev->reshape_position == MaxSector)
3035 mddev->layout = n;
3036 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003037 return len;
3038}
3039static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003040__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003041
3042
NeilBrowneae17012005-11-08 21:39:23 -08003043static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003044raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003045{
NeilBrownbb636542005-11-08 21:39:45 -08003046 if (mddev->raid_disks == 0)
3047 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003048 if (mddev->reshape_position != MaxSector &&
3049 mddev->delta_disks != 0)
3050 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3051 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003052 return sprintf(page, "%d\n", mddev->raid_disks);
3053}
3054
NeilBrownda943b992006-01-06 00:20:54 -08003055static int update_raid_disks(mddev_t *mddev, int raid_disks);
3056
3057static ssize_t
3058raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3059{
NeilBrownda943b992006-01-06 00:20:54 -08003060 char *e;
3061 int rv = 0;
3062 unsigned long n = simple_strtoul(buf, &e, 10);
3063
3064 if (!*buf || (*e && *e != '\n'))
3065 return -EINVAL;
3066
3067 if (mddev->pers)
3068 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003069 else if (mddev->reshape_position != MaxSector) {
3070 int olddisks = mddev->raid_disks - mddev->delta_disks;
3071 mddev->delta_disks = n - olddisks;
3072 mddev->raid_disks = n;
3073 } else
NeilBrownda943b992006-01-06 00:20:54 -08003074 mddev->raid_disks = n;
3075 return rv ? rv : len;
3076}
3077static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003078__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003079
NeilBrown24dd4692005-11-08 21:39:26 -08003080static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003081chunk_size_show(mddev_t *mddev, char *page)
3082{
NeilBrown08a02ec2007-05-09 02:35:38 -07003083 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003084 mddev->chunk_sectors != mddev->new_chunk_sectors)
3085 return sprintf(page, "%d (%d)\n",
3086 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003087 mddev->chunk_sectors << 9);
3088 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003089}
3090
3091static ssize_t
3092chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3093{
NeilBrown3b343802006-01-06 00:20:47 -08003094 char *e;
3095 unsigned long n = simple_strtoul(buf, &e, 10);
3096
NeilBrown3b343802006-01-06 00:20:47 -08003097 if (!*buf || (*e && *e != '\n'))
3098 return -EINVAL;
3099
NeilBrownb3546032009-03-31 14:56:41 +11003100 if (mddev->pers) {
3101 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003102 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003103 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003104 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003105 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003106 if (err) {
3107 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003108 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003109 }
NeilBrownb3546032009-03-31 14:56:41 +11003110 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003111 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003112 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003113 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003114 }
NeilBrown3b343802006-01-06 00:20:47 -08003115 return len;
3116}
3117static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003118__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003119
NeilBrowna94213b2006-06-26 00:28:00 -07003120static ssize_t
3121resync_start_show(mddev_t *mddev, char *page)
3122{
NeilBrownd1a7c502009-03-31 15:24:32 +11003123 if (mddev->recovery_cp == MaxSector)
3124 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003125 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3126}
3127
3128static ssize_t
3129resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3130{
NeilBrowna94213b2006-06-26 00:28:00 -07003131 char *e;
3132 unsigned long long n = simple_strtoull(buf, &e, 10);
3133
3134 if (mddev->pers)
3135 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003136 if (cmd_match(buf, "none"))
3137 n = MaxSector;
3138 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003139 return -EINVAL;
3140
3141 mddev->recovery_cp = n;
3142 return len;
3143}
3144static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003145__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003146
NeilBrown9e653b62006-06-26 00:27:58 -07003147/*
3148 * The array state can be:
3149 *
3150 * clear
3151 * No devices, no size, no level
3152 * Equivalent to STOP_ARRAY ioctl
3153 * inactive
3154 * May have some settings, but array is not active
3155 * all IO results in error
3156 * When written, doesn't tear down array, but just stops it
3157 * suspended (not supported yet)
3158 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003159 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003160 * readonly
3161 * no resync can happen. no superblocks get written.
3162 * write requests fail
3163 * read-auto
3164 * like readonly, but behaves like 'clean' on a write request.
3165 *
3166 * clean - no pending writes, but otherwise active.
3167 * When written to inactive array, starts without resync
3168 * If a write request arrives then
3169 * if metadata is known, mark 'dirty' and switch to 'active'.
3170 * if not known, block and switch to write-pending
3171 * If written to an active array that has pending writes, then fails.
3172 * active
3173 * fully active: IO and resync can be happening.
3174 * When written to inactive array, starts with resync
3175 *
3176 * write-pending
3177 * clean, but writes are blocked waiting for 'active' to be written.
3178 *
3179 * active-idle
3180 * like active, but no writes have been seen for a while (100msec).
3181 *
3182 */
3183enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3184 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003185static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003186 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3187 "write-pending", "active-idle", NULL };
3188
3189static int match_word(const char *word, char **list)
3190{
3191 int n;
3192 for (n=0; list[n]; n++)
3193 if (cmd_match(word, list[n]))
3194 break;
3195 return n;
3196}
3197
3198static ssize_t
3199array_state_show(mddev_t *mddev, char *page)
3200{
3201 enum array_state st = inactive;
3202
3203 if (mddev->pers)
3204 switch(mddev->ro) {
3205 case 1:
3206 st = readonly;
3207 break;
3208 case 2:
3209 st = read_auto;
3210 break;
3211 case 0:
3212 if (mddev->in_sync)
3213 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08003214 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3215 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003216 else if (mddev->safemode)
3217 st = active_idle;
3218 else
3219 st = active;
3220 }
3221 else {
3222 if (list_empty(&mddev->disks) &&
3223 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003224 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003225 st = clear;
3226 else
3227 st = inactive;
3228 }
3229 return sprintf(page, "%s\n", array_states[st]);
3230}
3231
Neil Browndf5b20c2008-07-11 22:02:22 +10003232static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003233static int do_md_run(mddev_t * mddev);
3234static int restart_array(mddev_t *mddev);
3235
3236static ssize_t
3237array_state_store(mddev_t *mddev, const char *buf, size_t len)
3238{
3239 int err = -EINVAL;
3240 enum array_state st = match_word(buf, array_states);
3241 switch(st) {
3242 case bad_word:
3243 break;
3244 case clear:
3245 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003246 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003247 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003248 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003249 break;
3250 case inactive:
3251 /* stopping an active array */
3252 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003253 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003254 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003255 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003256 } else
3257 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003258 break;
3259 case suspended:
3260 break; /* not supported yet */
3261 case readonly:
3262 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10003263 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003264 else {
3265 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003266 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003267 err = do_md_run(mddev);
3268 }
3269 break;
3270 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003271 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003272 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10003273 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003274 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003275 err = restart_array(mddev);
3276 if (err == 0) {
3277 mddev->ro = 2;
3278 set_disk_ro(mddev->gendisk, 0);
3279 }
NeilBrown9e653b62006-06-26 00:27:58 -07003280 } else {
3281 mddev->ro = 2;
3282 err = do_md_run(mddev);
3283 }
3284 break;
3285 case clean:
3286 if (mddev->pers) {
3287 restart_array(mddev);
3288 spin_lock_irq(&mddev->write_lock);
3289 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003290 if (mddev->in_sync == 0) {
3291 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003292 if (mddev->safemode == 1)
3293 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003294 if (mddev->persistent)
3295 set_bit(MD_CHANGE_CLEAN,
3296 &mddev->flags);
3297 }
3298 err = 0;
3299 } else
3300 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003301 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003302 } else
3303 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003304 break;
3305 case active:
3306 if (mddev->pers) {
3307 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003308 if (mddev->external)
3309 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003310 wake_up(&mddev->sb_wait);
3311 err = 0;
3312 } else {
3313 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003314 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003315 err = do_md_run(mddev);
3316 }
3317 break;
3318 case write_pending:
3319 case active_idle:
3320 /* these cannot be set */
3321 break;
3322 }
3323 if (err)
3324 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003325 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003326 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003327 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003328 }
NeilBrown9e653b62006-06-26 00:27:58 -07003329}
NeilBrown80ca3a42006-07-10 04:44:18 -07003330static struct md_sysfs_entry md_array_state =
3331__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003332
NeilBrown6d7ff732006-01-06 00:21:16 -08003333static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003334max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3335 return sprintf(page, "%d\n",
3336 atomic_read(&mddev->max_corr_read_errors));
3337}
3338
3339static ssize_t
3340max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3341{
3342 char *e;
3343 unsigned long n = simple_strtoul(buf, &e, 10);
3344
3345 if (*buf && (*e == 0 || *e == '\n')) {
3346 atomic_set(&mddev->max_corr_read_errors, n);
3347 return len;
3348 }
3349 return -EINVAL;
3350}
3351
3352static struct md_sysfs_entry max_corr_read_errors =
3353__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3354 max_corrected_read_errors_store);
3355
3356static ssize_t
NeilBrown6d7ff732006-01-06 00:21:16 -08003357null_show(mddev_t *mddev, char *page)
3358{
3359 return -EINVAL;
3360}
3361
3362static ssize_t
3363new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3364{
3365 /* buf must be %d:%d\n? giving major and minor numbers */
3366 /* The new device is added to the array.
3367 * If the array has a persistent superblock, we read the
3368 * superblock to initialise info and check validity.
3369 * Otherwise, only checking done is that in bind_rdev_to_array,
3370 * which mainly checks size.
3371 */
3372 char *e;
3373 int major = simple_strtoul(buf, &e, 10);
3374 int minor;
3375 dev_t dev;
3376 mdk_rdev_t *rdev;
3377 int err;
3378
3379 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3380 return -EINVAL;
3381 minor = simple_strtoul(e+1, &e, 10);
3382 if (*e && *e != '\n')
3383 return -EINVAL;
3384 dev = MKDEV(major, minor);
3385 if (major != MAJOR(dev) ||
3386 minor != MINOR(dev))
3387 return -EOVERFLOW;
3388
3389
3390 if (mddev->persistent) {
3391 rdev = md_import_device(dev, mddev->major_version,
3392 mddev->minor_version);
3393 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3394 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3395 mdk_rdev_t, same_set);
3396 err = super_types[mddev->major_version]
3397 .load_super(rdev, rdev0, mddev->minor_version);
3398 if (err < 0)
3399 goto out;
3400 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003401 } else if (mddev->external)
3402 rdev = md_import_device(dev, -2, -1);
3403 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003404 rdev = md_import_device(dev, -1, -1);
3405
3406 if (IS_ERR(rdev))
3407 return PTR_ERR(rdev);
3408 err = bind_rdev_to_array(rdev, mddev);
3409 out:
3410 if (err)
3411 export_rdev(rdev);
3412 return err ? err : len;
3413}
3414
3415static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003416__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003417
3418static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003419bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3420{
3421 char *end;
3422 unsigned long chunk, end_chunk;
3423
3424 if (!mddev->bitmap)
3425 goto out;
3426 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3427 while (*buf) {
3428 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3429 if (buf == end) break;
3430 if (*end == '-') { /* range */
3431 buf = end + 1;
3432 end_chunk = simple_strtoul(buf, &end, 0);
3433 if (buf == end) break;
3434 }
3435 if (*end && !isspace(*end)) break;
3436 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003437 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003438 }
3439 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3440out:
3441 return len;
3442}
3443
3444static struct md_sysfs_entry md_bitmap =
3445__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3446
3447static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003448size_show(mddev_t *mddev, char *page)
3449{
Andre Noll58c0fed2009-03-31 14:33:13 +11003450 return sprintf(page, "%llu\n",
3451 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003452}
3453
Andre Nolld71f9f82008-07-11 22:02:22 +10003454static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003455
3456static ssize_t
3457size_store(mddev_t *mddev, const char *buf, size_t len)
3458{
3459 /* If array is inactive, we can reduce the component size, but
3460 * not increase it (except from 0).
3461 * If array is active, we can try an on-line resize
3462 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003463 sector_t sectors;
3464 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003465
Andre Noll58c0fed2009-03-31 14:33:13 +11003466 if (err < 0)
3467 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003468 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003469 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003470 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003471 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003472 if (mddev->dev_sectors == 0 ||
3473 mddev->dev_sectors > sectors)
3474 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003475 else
3476 err = -ENOSPC;
3477 }
3478 return err ? err : len;
3479}
3480
3481static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003482__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003483
NeilBrown8bb93aa2006-01-06 00:20:50 -08003484
3485/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003486 * This is one of
3487 * 'none' for arrays with no metadata (good luck...)
3488 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003489 * or N.M for internally known formats
3490 */
3491static ssize_t
3492metadata_show(mddev_t *mddev, char *page)
3493{
3494 if (mddev->persistent)
3495 return sprintf(page, "%d.%d\n",
3496 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003497 else if (mddev->external)
3498 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003499 else
3500 return sprintf(page, "none\n");
3501}
3502
3503static ssize_t
3504metadata_store(mddev_t *mddev, const char *buf, size_t len)
3505{
3506 int major, minor;
3507 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003508 /* Changing the details of 'external' metadata is
3509 * always permitted. Otherwise there must be
3510 * no devices attached to the array.
3511 */
3512 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3513 ;
3514 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003515 return -EBUSY;
3516
3517 if (cmd_match(buf, "none")) {
3518 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003519 mddev->external = 0;
3520 mddev->major_version = 0;
3521 mddev->minor_version = 90;
3522 return len;
3523 }
3524 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003525 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003526 if (namelen >= sizeof(mddev->metadata_type))
3527 namelen = sizeof(mddev->metadata_type)-1;
3528 strncpy(mddev->metadata_type, buf+9, namelen);
3529 mddev->metadata_type[namelen] = 0;
3530 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3531 mddev->metadata_type[--namelen] = 0;
3532 mddev->persistent = 0;
3533 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003534 mddev->major_version = 0;
3535 mddev->minor_version = 90;
3536 return len;
3537 }
3538 major = simple_strtoul(buf, &e, 10);
3539 if (e==buf || *e != '.')
3540 return -EINVAL;
3541 buf = e+1;
3542 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003543 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003544 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003545 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003546 return -ENOENT;
3547 mddev->major_version = major;
3548 mddev->minor_version = minor;
3549 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003550 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003551 return len;
3552}
3553
3554static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003555__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003556
NeilBrowna35b0d62006-01-06 00:20:49 -08003557static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003558action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003559{
NeilBrown7eec3142005-11-08 21:39:44 -08003560 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003561 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3562 type = "frozen";
3563 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003564 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003565 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3566 type = "reshape";
3567 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003568 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3569 type = "resync";
3570 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3571 type = "check";
3572 else
3573 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003574 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003575 type = "recover";
3576 }
3577 return sprintf(page, "%s\n", type);
3578}
3579
3580static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003581action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003582{
NeilBrown7eec3142005-11-08 21:39:44 -08003583 if (!mddev->pers || !mddev->pers->sync_request)
3584 return -EINVAL;
3585
NeilBrownb6a9ce62009-05-26 09:41:17 +10003586 if (cmd_match(page, "frozen"))
3587 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3588 else
3589 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3590
3591 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003592 if (mddev->sync_thread) {
3593 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3594 md_unregister_thread(mddev->sync_thread);
3595 mddev->sync_thread = NULL;
3596 mddev->recovery = 0;
3597 }
NeilBrown03c902e2006-01-06 00:20:46 -08003598 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3599 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003600 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003601 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003602 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003603 else if (cmd_match(page, "recover")) {
3604 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3605 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3606 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003607 int err;
3608 if (mddev->pers->start_reshape == NULL)
3609 return -EINVAL;
3610 err = mddev->pers->start_reshape(mddev);
3611 if (err)
3612 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003613 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003614 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003615 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003616 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003617 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003618 return -EINVAL;
3619 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3620 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003621 }
NeilBrown03c902e2006-01-06 00:20:46 -08003622 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003623 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003624 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003625 return len;
3626}
3627
NeilBrown9d888832005-11-08 21:39:26 -08003628static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003629mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003630{
3631 return sprintf(page, "%llu\n",
3632 (unsigned long long) mddev->resync_mismatches);
3633}
3634
NeilBrown80ca3a42006-07-10 04:44:18 -07003635static struct md_sysfs_entry md_scan_mode =
3636__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003637
NeilBrown96de1e62005-11-08 21:39:39 -08003638
NeilBrown80ca3a42006-07-10 04:44:18 -07003639static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003640
NeilBrown88202a02006-01-06 00:21:36 -08003641static ssize_t
3642sync_min_show(mddev_t *mddev, char *page)
3643{
3644 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3645 mddev->sync_speed_min ? "local": "system");
3646}
3647
3648static ssize_t
3649sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3650{
3651 int min;
3652 char *e;
3653 if (strncmp(buf, "system", 6)==0) {
3654 mddev->sync_speed_min = 0;
3655 return len;
3656 }
3657 min = simple_strtoul(buf, &e, 10);
3658 if (buf == e || (*e && *e != '\n') || min <= 0)
3659 return -EINVAL;
3660 mddev->sync_speed_min = min;
3661 return len;
3662}
3663
3664static struct md_sysfs_entry md_sync_min =
3665__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3666
3667static ssize_t
3668sync_max_show(mddev_t *mddev, char *page)
3669{
3670 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3671 mddev->sync_speed_max ? "local": "system");
3672}
3673
3674static ssize_t
3675sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3676{
3677 int max;
3678 char *e;
3679 if (strncmp(buf, "system", 6)==0) {
3680 mddev->sync_speed_max = 0;
3681 return len;
3682 }
3683 max = simple_strtoul(buf, &e, 10);
3684 if (buf == e || (*e && *e != '\n') || max <= 0)
3685 return -EINVAL;
3686 mddev->sync_speed_max = max;
3687 return len;
3688}
3689
3690static struct md_sysfs_entry md_sync_max =
3691__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3692
Iustin Popd7f3d292007-10-16 23:30:54 -07003693static ssize_t
3694degraded_show(mddev_t *mddev, char *page)
3695{
3696 return sprintf(page, "%d\n", mddev->degraded);
3697}
3698static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003699
3700static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003701sync_force_parallel_show(mddev_t *mddev, char *page)
3702{
3703 return sprintf(page, "%d\n", mddev->parallel_resync);
3704}
3705
3706static ssize_t
3707sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3708{
3709 long n;
3710
3711 if (strict_strtol(buf, 10, &n))
3712 return -EINVAL;
3713
3714 if (n != 0 && n != 1)
3715 return -EINVAL;
3716
3717 mddev->parallel_resync = n;
3718
3719 if (mddev->sync_thread)
3720 wake_up(&resync_wait);
3721
3722 return len;
3723}
3724
3725/* force parallel resync, even with shared block devices */
3726static struct md_sysfs_entry md_sync_force_parallel =
3727__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3728 sync_force_parallel_show, sync_force_parallel_store);
3729
3730static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003731sync_speed_show(mddev_t *mddev, char *page)
3732{
3733 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003734 if (mddev->curr_resync == 0)
3735 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003736 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3737 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003738 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003739 db = resync - mddev->resync_mark_cnt;
3740 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003741}
3742
NeilBrown80ca3a42006-07-10 04:44:18 -07003743static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003744
3745static ssize_t
3746sync_completed_show(mddev_t *mddev, char *page)
3747{
Andre Noll58c0fed2009-03-31 14:33:13 +11003748 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003749
NeilBrownacb180b2009-04-14 16:28:34 +10003750 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3751 return sprintf(page, "none\n");
3752
NeilBrown88202a02006-01-06 00:21:36 -08003753 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003754 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003755 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003756 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003757
NeilBrownacb180b2009-04-14 16:28:34 +10003758 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003759 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003760}
3761
NeilBrown80ca3a42006-07-10 04:44:18 -07003762static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003763
NeilBrowne464eaf2006-03-27 01:18:14 -08003764static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003765min_sync_show(mddev_t *mddev, char *page)
3766{
3767 return sprintf(page, "%llu\n",
3768 (unsigned long long)mddev->resync_min);
3769}
3770static ssize_t
3771min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3772{
3773 unsigned long long min;
3774 if (strict_strtoull(buf, 10, &min))
3775 return -EINVAL;
3776 if (min > mddev->resync_max)
3777 return -EINVAL;
3778 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3779 return -EBUSY;
3780
3781 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003782 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003783 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003784 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003785 return -EINVAL;
3786 }
3787 mddev->resync_min = min;
3788
3789 return len;
3790}
3791
3792static struct md_sysfs_entry md_min_sync =
3793__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3794
3795static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003796max_sync_show(mddev_t *mddev, char *page)
3797{
3798 if (mddev->resync_max == MaxSector)
3799 return sprintf(page, "max\n");
3800 else
3801 return sprintf(page, "%llu\n",
3802 (unsigned long long)mddev->resync_max);
3803}
3804static ssize_t
3805max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3806{
3807 if (strncmp(buf, "max", 3) == 0)
3808 mddev->resync_max = MaxSector;
3809 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003810 unsigned long long max;
3811 if (strict_strtoull(buf, 10, &max))
3812 return -EINVAL;
3813 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003814 return -EINVAL;
3815 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003816 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003817 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3818 return -EBUSY;
3819
3820 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003821 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003822 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003823 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003824 return -EINVAL;
3825 }
3826 mddev->resync_max = max;
3827 }
3828 wake_up(&mddev->recovery_wait);
3829 return len;
3830}
3831
3832static struct md_sysfs_entry md_max_sync =
3833__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3834
3835static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003836suspend_lo_show(mddev_t *mddev, char *page)
3837{
3838 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3839}
3840
3841static ssize_t
3842suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3843{
3844 char *e;
3845 unsigned long long new = simple_strtoull(buf, &e, 10);
3846
NeilBrownb8d966e2009-07-01 11:14:04 +10003847 if (mddev->pers == NULL ||
3848 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003849 return -EINVAL;
3850 if (buf == e || (*e && *e != '\n'))
3851 return -EINVAL;
3852 if (new >= mddev->suspend_hi ||
3853 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3854 mddev->suspend_lo = new;
3855 mddev->pers->quiesce(mddev, 2);
3856 return len;
3857 } else
3858 return -EINVAL;
3859}
3860static struct md_sysfs_entry md_suspend_lo =
3861__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3862
3863
3864static ssize_t
3865suspend_hi_show(mddev_t *mddev, char *page)
3866{
3867 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3868}
3869
3870static ssize_t
3871suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3872{
3873 char *e;
3874 unsigned long long new = simple_strtoull(buf, &e, 10);
3875
NeilBrownb8d966e2009-07-01 11:14:04 +10003876 if (mddev->pers == NULL ||
3877 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003878 return -EINVAL;
3879 if (buf == e || (*e && *e != '\n'))
3880 return -EINVAL;
3881 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3882 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3883 mddev->suspend_hi = new;
3884 mddev->pers->quiesce(mddev, 1);
3885 mddev->pers->quiesce(mddev, 0);
3886 return len;
3887 } else
3888 return -EINVAL;
3889}
3890static struct md_sysfs_entry md_suspend_hi =
3891__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3892
NeilBrown08a02ec2007-05-09 02:35:38 -07003893static ssize_t
3894reshape_position_show(mddev_t *mddev, char *page)
3895{
3896 if (mddev->reshape_position != MaxSector)
3897 return sprintf(page, "%llu\n",
3898 (unsigned long long)mddev->reshape_position);
3899 strcpy(page, "none\n");
3900 return 5;
3901}
3902
3903static ssize_t
3904reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3905{
3906 char *e;
3907 unsigned long long new = simple_strtoull(buf, &e, 10);
3908 if (mddev->pers)
3909 return -EBUSY;
3910 if (buf == e || (*e && *e != '\n'))
3911 return -EINVAL;
3912 mddev->reshape_position = new;
3913 mddev->delta_disks = 0;
3914 mddev->new_level = mddev->level;
3915 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003916 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07003917 return len;
3918}
3919
3920static struct md_sysfs_entry md_reshape_position =
3921__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3922 reshape_position_store);
3923
Dan Williamsb522adc2009-03-31 15:00:31 +11003924static ssize_t
3925array_size_show(mddev_t *mddev, char *page)
3926{
3927 if (mddev->external_size)
3928 return sprintf(page, "%llu\n",
3929 (unsigned long long)mddev->array_sectors/2);
3930 else
3931 return sprintf(page, "default\n");
3932}
3933
3934static ssize_t
3935array_size_store(mddev_t *mddev, const char *buf, size_t len)
3936{
3937 sector_t sectors;
3938
3939 if (strncmp(buf, "default", 7) == 0) {
3940 if (mddev->pers)
3941 sectors = mddev->pers->size(mddev, 0, 0);
3942 else
3943 sectors = mddev->array_sectors;
3944
3945 mddev->external_size = 0;
3946 } else {
3947 if (strict_blocks_to_sectors(buf, &sectors) < 0)
3948 return -EINVAL;
3949 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10003950 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11003951
3952 mddev->external_size = 1;
3953 }
3954
3955 mddev->array_sectors = sectors;
3956 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10003957 if (mddev->pers)
3958 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11003959
3960 return len;
3961}
3962
3963static struct md_sysfs_entry md_array_size =
3964__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
3965 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08003966
NeilBrowneae17012005-11-08 21:39:23 -08003967static struct attribute *md_default_attrs[] = {
3968 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003969 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003970 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003971 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003972 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003973 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003974 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08003975 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003976 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003977 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003978 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11003979 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11003980 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003981 NULL,
3982};
3983
3984static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003985 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003986 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003987 &md_sync_min.attr,
3988 &md_sync_max.attr,
3989 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003990 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003991 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003992 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003993 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003994 &md_suspend_lo.attr,
3995 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003996 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003997 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003998 NULL,
3999};
NeilBrown411036f2005-11-08 21:39:40 -08004000static struct attribute_group md_redundancy_group = {
4001 .name = NULL,
4002 .attrs = md_redundancy_attrs,
4003};
4004
NeilBrowneae17012005-11-08 21:39:23 -08004005
4006static ssize_t
4007md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4008{
4009 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4010 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004011 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004012
4013 if (!entry->show)
4014 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004015 rv = mddev_lock(mddev);
4016 if (!rv) {
4017 rv = entry->show(mddev, page);
4018 mddev_unlock(mddev);
4019 }
NeilBrown96de1e62005-11-08 21:39:39 -08004020 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004021}
4022
4023static ssize_t
4024md_attr_store(struct kobject *kobj, struct attribute *attr,
4025 const char *page, size_t length)
4026{
4027 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4028 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004029 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004030
4031 if (!entry->store)
4032 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004033 if (!capable(CAP_SYS_ADMIN))
4034 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004035 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004036 if (mddev->hold_active == UNTIL_IOCTL)
4037 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004038 if (!rv) {
4039 rv = entry->store(mddev, page, length);
4040 mddev_unlock(mddev);
4041 }
NeilBrown96de1e62005-11-08 21:39:39 -08004042 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004043}
4044
4045static void md_free(struct kobject *ko)
4046{
4047 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004048
4049 if (mddev->sysfs_state)
4050 sysfs_put(mddev->sysfs_state);
4051
4052 if (mddev->gendisk) {
4053 del_gendisk(mddev->gendisk);
4054 put_disk(mddev->gendisk);
4055 }
4056 if (mddev->queue)
4057 blk_cleanup_queue(mddev->queue);
4058
NeilBrowneae17012005-11-08 21:39:23 -08004059 kfree(mddev);
4060}
4061
4062static struct sysfs_ops md_sysfs_ops = {
4063 .show = md_attr_show,
4064 .store = md_attr_store,
4065};
4066static struct kobj_type md_ktype = {
4067 .release = md_free,
4068 .sysfs_ops = &md_sysfs_ops,
4069 .default_attrs = md_default_attrs,
4070};
4071
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072int mdp_major = 0;
4073
Dan Williams5fd3a172009-03-04 00:57:25 -07004074static void mddev_delayed_delete(struct work_struct *ws)
4075{
4076 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4077
4078 if (mddev->private == &md_redundancy_group) {
4079 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
4080 if (mddev->sysfs_action)
4081 sysfs_put(mddev->sysfs_action);
4082 mddev->sysfs_action = NULL;
4083 mddev->private = NULL;
4084 }
NeilBrown43a70502009-12-14 12:49:55 +11004085 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004086 kobject_del(&mddev->kobj);
4087 kobject_put(&mddev->kobj);
4088}
4089
NeilBrownefeb53c2009-01-09 08:31:10 +11004090static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004092 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 mddev_t *mddev = mddev_find(dev);
4094 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004095 int partitioned;
4096 int shift;
4097 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004098 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099
4100 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004101 return -ENODEV;
4102
4103 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4104 shift = partitioned ? MdpMinorShift : 0;
4105 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106
NeilBrownd3374822009-01-09 08:31:10 +11004107 /* wait for any previous instance if this device
4108 * to be completed removed (mddev_delayed_delete).
4109 */
4110 flush_scheduled_work();
4111
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004112 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004113 error = -EEXIST;
4114 if (mddev->gendisk)
4115 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004116
4117 if (name) {
4118 /* Need to ensure that 'name' is not a duplicate.
4119 */
4120 mddev_t *mddev2;
4121 spin_lock(&all_mddevs_lock);
4122
4123 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4124 if (mddev2->gendisk &&
4125 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4126 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004127 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004128 }
4129 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 }
NeilBrown8b765392009-01-09 08:31:08 +11004131
NeilBrown0909dc42009-07-01 12:27:21 +10004132 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004133 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004134 if (!mddev->queue)
4135 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004136 mddev->queue->queuedata = mddev;
4137
NeilBrown8b765392009-01-09 08:31:08 +11004138 /* Can be unlocked because the queue is new: no concurrency */
4139 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4140
NeilBrown409c57f2009-03-31 14:39:39 +11004141 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004142
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 disk = alloc_disk(1 << shift);
4144 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004145 blk_cleanup_queue(mddev->queue);
4146 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004147 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004149 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004151 if (name)
4152 strcpy(disk->disk_name, name);
4153 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004155 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 disk->fops = &md_fops;
4158 disk->private_data = mddev;
4159 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004160 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004161 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004162 * remove it now.
4163 */
4164 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 add_disk(disk);
4166 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004167 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4168 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004169 if (error) {
4170 /* This isn't possible, but as kobject_init_and_add is marked
4171 * __must_check, we must do something with the result
4172 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004173 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4174 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004175 error = 0;
4176 }
NeilBrown43a70502009-12-14 12:49:55 +11004177 if (sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4178 printk(KERN_DEBUG "pointless warning\n");
NeilBrown0909dc42009-07-01 12:27:21 +10004179 abort:
4180 mutex_unlock(&disks_mutex);
4181 if (!error) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004182 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11004183 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
4184 }
NeilBrownd3374822009-01-09 08:31:10 +11004185 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004186 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004187}
4188
4189static struct kobject *md_probe(dev_t dev, int *part, void *data)
4190{
4191 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 return NULL;
4193}
4194
NeilBrownefeb53c2009-01-09 08:31:10 +11004195static int add_named_array(const char *val, struct kernel_param *kp)
4196{
4197 /* val must be "md_*" where * is not all digits.
4198 * We allocate an array with a large free minor number, and
4199 * set the name to val. val must not already be an active name.
4200 */
4201 int len = strlen(val);
4202 char buf[DISK_NAME_LEN];
4203
4204 while (len && val[len-1] == '\n')
4205 len--;
4206 if (len >= DISK_NAME_LEN)
4207 return -E2BIG;
4208 strlcpy(buf, val, len+1);
4209 if (strncmp(buf, "md_", 3) != 0)
4210 return -EINVAL;
4211 return md_alloc(0, buf);
4212}
4213
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214static void md_safemode_timeout(unsigned long data)
4215{
4216 mddev_t *mddev = (mddev_t *) data;
4217
Neil Brown0fd62b82008-06-28 08:31:36 +10004218 if (!atomic_read(&mddev->writes_pending)) {
4219 mddev->safemode = 1;
4220 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11004221 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 md_wakeup_thread(mddev->thread);
4224}
4225
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004226static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
4228static int do_md_run(mddev_t * mddev)
4229{
NeilBrown2604b702006-01-06 00:20:36 -08004230 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 mdk_rdev_t *rdev;
4232 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08004233 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234
NeilBrowna757e642005-04-16 15:26:42 -07004235 if (list_empty(&mddev->disks))
4236 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238
4239 if (mddev->pers)
4240 return -EBUSY;
4241
4242 /*
4243 * Analyze all RAID superblock(s)
4244 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004245 if (!mddev->raid_disks) {
4246 if (!mddev->persistent)
4247 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004248 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
NeilBrownd9d166c2006-01-06 00:20:51 -08004251 if (mddev->level != LEVEL_NONE)
4252 request_module("md-level-%d", mddev->level);
4253 else if (mddev->clevel[0])
4254 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255
4256 /*
4257 * Drop all container device buffers, from now on
4258 * the only valid external interface is through the md
4259 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004261 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004262 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 continue;
4264 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004265 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004266
4267 /* perform some consistency tests on the device.
4268 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004269 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004270 */
Andre Noll0f420352008-07-11 22:02:23 +10004271 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004272 if (mddev->dev_sectors &&
4273 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004274 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004275 printk("md: %s: data overlaps metadata\n",
4276 mdname(mddev));
4277 return -EINVAL;
4278 }
4279 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004280 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004281 > rdev->data_offset) {
4282 printk("md: %s: metadata overlaps data\n",
4283 mdname(mddev));
4284 return -EINVAL;
4285 }
4286 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004287 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 }
4289
4290 md_probe(mddev->unit, NULL, NULL);
4291 disk = mddev->gendisk;
4292 if (!disk)
4293 return -ENOMEM;
4294
4295 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004296 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004297 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004299 if (mddev->level != LEVEL_NONE)
4300 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4301 mddev->level);
4302 else
4303 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4304 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 return -EINVAL;
4306 }
NeilBrown2604b702006-01-06 00:20:36 -08004307 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004309 if (mddev->level != pers->level) {
4310 mddev->level = pers->level;
4311 mddev->new_level = pers->level;
4312 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004313 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314
NeilBrownf6705572006-03-27 01:18:11 -08004315 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004316 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004317 /* This personality cannot handle reshaping... */
4318 mddev->pers = NULL;
4319 module_put(pers->owner);
4320 return -EINVAL;
4321 }
4322
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004323 if (pers->sync_request) {
4324 /* Warn if this is a potentially silly
4325 * configuration.
4326 */
4327 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4328 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004329 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004330
4331 list_for_each_entry(rdev, &mddev->disks, same_set)
4332 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004333 if (rdev < rdev2 &&
4334 rdev->bdev->bd_contains ==
4335 rdev2->bdev->bd_contains) {
4336 printk(KERN_WARNING
4337 "%s: WARNING: %s appears to be"
4338 " on the same physical disk as"
4339 " %s.\n",
4340 mdname(mddev),
4341 bdevname(rdev->bdev,b),
4342 bdevname(rdev2->bdev,b2));
4343 warned = 1;
4344 }
4345 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004346
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004347 if (warned)
4348 printk(KERN_WARNING
4349 "True protection against single-disk"
4350 " failure might be compromised.\n");
4351 }
4352
NeilBrown657390d2005-08-26 18:34:16 -07004353 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004354 /* may be over-ridden by personality */
4355 mddev->resync_max_sectors = mddev->dev_sectors;
4356
NeilBrowna9701a32005-11-08 21:39:34 -08004357 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004358 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
NeilBrown0f9552b2009-12-30 12:08:50 +11004360 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004361 mddev->ro = 2; /* read-only, but switch on first write */
4362
NeilBrownb15c2e52006-01-06 00:20:16 -08004363 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004364 if (err)
4365 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004366 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4367 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4368 " but 'external_size' not in effect?\n", __func__);
4369 printk(KERN_ERR
4370 "md: invalid array_size %llu > default size %llu\n",
4371 (unsigned long long)mddev->array_sectors / 2,
4372 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4373 err = -EINVAL;
4374 mddev->pers->stop(mddev);
4375 }
4376 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004377 err = bitmap_create(mddev);
4378 if (err) {
4379 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4380 mdname(mddev), err);
4381 mddev->pers->stop(mddev);
4382 }
4383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 module_put(mddev->pers->owner);
4386 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004387 bitmap_destroy(mddev);
4388 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004390 if (mddev->pers->sync_request) {
4391 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4392 printk(KERN_WARNING
4393 "md: cannot register extra attributes for %s\n",
4394 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004395 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004396 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004397 mddev->ro = 0;
4398
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004400 atomic_set(&mddev->max_corr_read_errors,
4401 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 mddev->safemode = 0;
4403 mddev->safemode_timer.function = md_safemode_timeout;
4404 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004405 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004407
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004408 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004409 if (rdev->raid_disk >= 0) {
4410 char nm[20];
4411 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004412 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4413 printk("md: cannot register %s for %s\n",
4414 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416
4417 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4418
NeilBrown850b2b42006-10-03 01:15:46 -07004419 if (mddev->flags)
4420 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
Andre Nollf233ea52008-07-21 17:05:22 +10004422 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
NeilBrown0b8c9de2006-07-10 04:44:15 -07004424 md_wakeup_thread(mddev->thread);
4425 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004426
NeilBrown449aad32009-08-03 10:59:58 +10004427 revalidate_disk(mddev->gendisk);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004428 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08004429 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004430 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004431 if (mddev->sysfs_action)
4432 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004433 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09004434 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 return 0;
4436}
4437
4438static int restart_array(mddev_t *mddev)
4439{
4440 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441
Andre Noll80fab1d2008-07-11 22:02:21 +10004442 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004444 return -ENXIO;
4445 if (!mddev->pers)
4446 return -EINVAL;
4447 if (!mddev->ro)
4448 return -EBUSY;
4449 mddev->safemode = 0;
4450 mddev->ro = 0;
4451 set_disk_ro(disk, 0);
4452 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4453 mdname(mddev));
4454 /* Kick recovery or resync if necessary */
4455 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4456 md_wakeup_thread(mddev->thread);
4457 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004458 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004459 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460}
4461
NeilBrownacc55e22006-06-26 00:27:47 -07004462/* similar to deny_write_access, but accounts for our holding a reference
4463 * to the file ourselves */
4464static int deny_bitmap_write_access(struct file * file)
4465{
4466 struct inode *inode = file->f_mapping->host;
4467
4468 spin_lock(&inode->i_lock);
4469 if (atomic_read(&inode->i_writecount) > 1) {
4470 spin_unlock(&inode->i_lock);
4471 return -ETXTBSY;
4472 }
4473 atomic_set(&inode->i_writecount, -1);
4474 spin_unlock(&inode->i_lock);
4475
4476 return 0;
4477}
4478
NeilBrown43a70502009-12-14 12:49:55 +11004479void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004480{
4481 struct inode *inode = file->f_mapping->host;
4482
4483 spin_lock(&inode->i_lock);
4484 atomic_set(&inode->i_writecount, 1);
4485 spin_unlock(&inode->i_lock);
4486}
4487
NeilBrown9e653b62006-06-26 00:27:58 -07004488/* mode:
4489 * 0 - completely stop and dis-assemble array
4490 * 1 - switch to readonly
4491 * 2 - stop but do not disassemble array
4492 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004493static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494{
4495 int err = 0;
4496 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004497 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498
NeilBrownc8c00a62009-08-10 12:50:52 +10004499 mutex_lock(&mddev->open_mutex);
NeilBrownf2ea68c2008-07-21 17:05:25 +10004500 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004501 printk("md: %s still in use.\n",mdname(mddev));
NeilBrownc8c00a62009-08-10 12:50:52 +10004502 err = -EBUSY;
4503 } else if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504
4505 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004506 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4508 md_unregister_thread(mddev->sync_thread);
4509 mddev->sync_thread = NULL;
4510 }
4511
4512 del_timer_sync(&mddev->safemode_timer);
4513
NeilBrown9e653b62006-06-26 00:27:58 -07004514 switch(mode) {
4515 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004517 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 goto out;
4519 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004520 break;
4521 case 0: /* disassemble */
4522 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004523 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004524 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 if (mddev->ro)
4526 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004527
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004529 mddev->queue->merge_bvec_fn = NULL;
4530 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004531 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 module_put(mddev->pers->owner);
Dan Williams5fd3a172009-03-04 00:57:25 -07004533 if (mddev->pers->sync_request)
4534 mddev->private = &md_redundancy_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004536 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004537 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004538
NeilBrownc4647292009-05-07 12:51:06 +10004539 list_for_each_entry(rdev, &mddev->disks, same_set)
4540 if (rdev->raid_disk >= 0) {
4541 char nm[20];
4542 sprintf(nm, "rd%d", rdev->raid_disk);
4543 sysfs_remove_link(&mddev->kobj, nm);
4544 }
4545
NeilBrown0d4ca602006-12-10 02:20:44 -08004546 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004547 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08004548
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 if (mddev->ro)
4550 mddev->ro = 0;
4551 }
NeilBrown850b2b42006-10-03 01:15:46 -07004552 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 /* mark array as shutdown cleanly */
4554 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07004555 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 }
NeilBrown9e653b62006-06-26 00:27:58 -07004557 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004559 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown80ffb3c2009-08-18 10:35:26 +10004560 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 }
NeilBrownc8c00a62009-08-10 12:50:52 +10004562out:
4563 mutex_unlock(&mddev->open_mutex);
4564 if (err)
4565 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 /*
4567 * Free resources if final stop
4568 */
NeilBrown9e653b62006-06-26 00:27:58 -07004569 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004570
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4572
NeilBrown978f9462006-02-02 14:28:05 -08004573 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004574 if (mddev->bitmap_info.file) {
4575 restore_bitmap_write_access(mddev->bitmap_info.file);
4576 fput(mddev->bitmap_info.file);
4577 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004578 }
NeilBrownc3d97142009-12-14 12:49:52 +11004579 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004580
NeilBrown177a99b2008-02-06 01:39:56 -08004581 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07004582 flush_scheduled_work();
4583
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 export_array(mddev);
4585
Andre Nollf233ea52008-07-21 17:05:22 +10004586 mddev->array_sectors = 0;
Dan Williamsb522adc2009-03-31 15:00:31 +11004587 mddev->external_size = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004588 mddev->dev_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004589 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004590 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004591 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004592 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004593 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004594 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004595 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004596 mddev->level = LEVEL_NONE;
4597 mddev->clevel[0] = 0;
4598 mddev->flags = 0;
4599 mddev->ro = 0;
4600 mddev->metadata_type[0] = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10004601 mddev->chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004602 mddev->ctime = mddev->utime = 0;
4603 mddev->layout = 0;
4604 mddev->max_disks = 0;
4605 mddev->events = 0;
4606 mddev->delta_disks = 0;
4607 mddev->new_level = LEVEL_NONE;
4608 mddev->new_layout = 0;
Andre Noll664e7c42009-06-18 08:45:27 +10004609 mddev->new_chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004610 mddev->curr_resync = 0;
4611 mddev->resync_mismatches = 0;
4612 mddev->suspend_lo = mddev->suspend_hi = 0;
4613 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4614 mddev->recovery = 0;
4615 mddev->in_sync = 0;
4616 mddev->changed = 0;
4617 mddev->degraded = 0;
4618 mddev->barriers_work = 0;
4619 mddev->safemode = 0;
NeilBrownc3d97142009-12-14 12:49:52 +11004620 mddev->bitmap_info.offset = 0;
4621 mddev->bitmap_info.default_offset = 0;
NeilBrown42a04b52009-12-14 12:49:53 +11004622 mddev->bitmap_info.chunksize = 0;
4623 mddev->bitmap_info.daemon_sleep = 0;
4624 mddev->bitmap_info.max_write_behind = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004625 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004626 if (mddev->hold_active == UNTIL_STOP)
4627 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004628
NeilBrowna8a55c32006-06-26 00:27:39 -07004629 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4631 mdname(mddev));
4632 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004633 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004634 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004635 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 return err;
4637}
4638
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004639#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640static void autorun_array(mddev_t *mddev)
4641{
4642 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 int err;
4644
NeilBrowna757e642005-04-16 15:26:42 -07004645 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647
4648 printk(KERN_INFO "md: running: ");
4649
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004650 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 char b[BDEVNAME_SIZE];
4652 printk("<%s>", bdevname(rdev->bdev,b));
4653 }
4654 printk("\n");
4655
NeilBrownd710e132008-10-13 11:55:12 +11004656 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 if (err) {
4658 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004659 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 }
4661}
4662
4663/*
4664 * lets try to run arrays based on all disks that have arrived
4665 * until now. (those are in pending_raid_disks)
4666 *
4667 * the method: pick the first pending disk, collect all disks with
4668 * the same UUID, remove all from the pending list and put them into
4669 * the 'same_array' list. Then order this list based on superblock
4670 * update time (freshest comes first), kick out 'old' disks and
4671 * compare superblocks. If everything's fine then run it.
4672 *
4673 * If "unit" is allocated, then bump its reference count
4674 */
4675static void autorun_devices(int part)
4676{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004677 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 mddev_t *mddev;
4679 char b[BDEVNAME_SIZE];
4680
4681 printk(KERN_INFO "md: autorun ...\n");
4682 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004683 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004685 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 rdev0 = list_entry(pending_raid_disks.next,
4687 mdk_rdev_t, same_set);
4688
4689 printk(KERN_INFO "md: considering %s ...\n",
4690 bdevname(rdev0->bdev,b));
4691 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004692 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 if (super_90_load(rdev, rdev0, 0) >= 0) {
4694 printk(KERN_INFO "md: adding %s ...\n",
4695 bdevname(rdev->bdev,b));
4696 list_move(&rdev->same_set, &candidates);
4697 }
4698 /*
4699 * now we have a set of devices, with all of them having
4700 * mostly sane superblocks. It's time to allocate the
4701 * mddev.
4702 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004703 if (part) {
4704 dev = MKDEV(mdp_major,
4705 rdev0->preferred_minor << MdpMinorShift);
4706 unit = MINOR(dev) >> MdpMinorShift;
4707 } else {
4708 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4709 unit = MINOR(dev);
4710 }
4711 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4713 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4714 break;
4715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716
4717 md_probe(dev, NULL, NULL);
4718 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004719 if (!mddev || !mddev->gendisk) {
4720 if (mddev)
4721 mddev_put(mddev);
4722 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 "md: cannot allocate memory for md drive.\n");
4724 break;
4725 }
4726 if (mddev_lock(mddev))
4727 printk(KERN_WARNING "md: %s locked, cannot run\n",
4728 mdname(mddev));
4729 else if (mddev->raid_disks || mddev->major_version
4730 || !list_empty(&mddev->disks)) {
4731 printk(KERN_WARNING
4732 "md: %s already running, cannot run %s\n",
4733 mdname(mddev), bdevname(rdev0->bdev,b));
4734 mddev_unlock(mddev);
4735 } else {
4736 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004737 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004738 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 list_del_init(&rdev->same_set);
4740 if (bind_rdev_to_array(rdev, mddev))
4741 export_rdev(rdev);
4742 }
4743 autorun_array(mddev);
4744 mddev_unlock(mddev);
4745 }
4746 /* on success, candidates will be empty, on error
4747 * it won't...
4748 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004749 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004750 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 mddev_put(mddev);
4754 }
4755 printk(KERN_INFO "md: ... autorun DONE.\n");
4756}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004757#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759static int get_version(void __user * arg)
4760{
4761 mdu_version_t ver;
4762
4763 ver.major = MD_MAJOR_VERSION;
4764 ver.minor = MD_MINOR_VERSION;
4765 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4766
4767 if (copy_to_user(arg, &ver, sizeof(ver)))
4768 return -EFAULT;
4769
4770 return 0;
4771}
4772
4773static int get_array_info(mddev_t * mddev, void __user * arg)
4774{
4775 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004776 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778
NeilBrowna9f326e2009-09-23 18:06:41 +10004779 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004780 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004782 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 failed++;
4784 else {
4785 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004786 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004787 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 else
4789 spare++;
4790 }
4791 }
4792
4793 info.major_version = mddev->major_version;
4794 info.minor_version = mddev->minor_version;
4795 info.patch_version = MD_PATCHLEVEL_VERSION;
4796 info.ctime = mddev->ctime;
4797 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004798 info.size = mddev->dev_sectors / 2;
4799 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004800 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 info.nr_disks = nr;
4802 info.raid_disks = mddev->raid_disks;
4803 info.md_minor = mddev->md_minor;
4804 info.not_persistent= !mddev->persistent;
4805
4806 info.utime = mddev->utime;
4807 info.state = 0;
4808 if (mddev->in_sync)
4809 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11004810 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07004811 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004812 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 info.working_disks = working;
4814 info.failed_disks = failed;
4815 info.spare_disks = spare;
4816
4817 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004818 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819
4820 if (copy_to_user(arg, &info, sizeof(info)))
4821 return -EFAULT;
4822
4823 return 0;
4824}
4825
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004826static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004827{
4828 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4829 char *ptr, *buf = NULL;
4830 int err = -ENOMEM;
4831
Dan Williamsb5470dc2008-06-27 21:44:04 -07004832 if (md_allow_write(mddev))
4833 file = kmalloc(sizeof(*file), GFP_NOIO);
4834 else
4835 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004836
NeilBrown32a76272005-06-21 17:17:14 -07004837 if (!file)
4838 goto out;
4839
4840 /* bitmap disabled, zero the first byte and copy out */
4841 if (!mddev->bitmap || !mddev->bitmap->file) {
4842 file->pathname[0] = '\0';
4843 goto copy_out;
4844 }
4845
4846 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4847 if (!buf)
4848 goto out;
4849
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004850 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4851 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004852 goto out;
4853
4854 strcpy(file->pathname, ptr);
4855
4856copy_out:
4857 err = 0;
4858 if (copy_to_user(arg, file, sizeof(*file)))
4859 err = -EFAULT;
4860out:
4861 kfree(buf);
4862 kfree(file);
4863 return err;
4864}
4865
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866static int get_disk_info(mddev_t * mddev, void __user * arg)
4867{
4868 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 mdk_rdev_t *rdev;
4870
4871 if (copy_from_user(&info, arg, sizeof(info)))
4872 return -EFAULT;
4873
Andre Noll26ef3792008-07-11 22:02:21 +10004874 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 if (rdev) {
4876 info.major = MAJOR(rdev->bdev->bd_dev);
4877 info.minor = MINOR(rdev->bdev->bd_dev);
4878 info.raid_disk = rdev->raid_disk;
4879 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004880 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004882 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 info.state |= (1<<MD_DISK_ACTIVE);
4884 info.state |= (1<<MD_DISK_SYNC);
4885 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004886 if (test_bit(WriteMostly, &rdev->flags))
4887 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 } else {
4889 info.major = info.minor = 0;
4890 info.raid_disk = -1;
4891 info.state = (1<<MD_DISK_REMOVED);
4892 }
4893
4894 if (copy_to_user(arg, &info, sizeof(info)))
4895 return -EFAULT;
4896
4897 return 0;
4898}
4899
4900static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4901{
4902 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4903 mdk_rdev_t *rdev;
4904 dev_t dev = MKDEV(info->major,info->minor);
4905
4906 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4907 return -EOVERFLOW;
4908
4909 if (!mddev->raid_disks) {
4910 int err;
4911 /* expecting a device which has a superblock */
4912 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4913 if (IS_ERR(rdev)) {
4914 printk(KERN_WARNING
4915 "md: md_import_device returned %ld\n",
4916 PTR_ERR(rdev));
4917 return PTR_ERR(rdev);
4918 }
4919 if (!list_empty(&mddev->disks)) {
4920 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4921 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10004922 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 .load_super(rdev, rdev0, mddev->minor_version);
4924 if (err < 0) {
4925 printk(KERN_WARNING
4926 "md: %s has different UUID to %s\n",
4927 bdevname(rdev->bdev,b),
4928 bdevname(rdev0->bdev,b2));
4929 export_rdev(rdev);
4930 return -EINVAL;
4931 }
4932 }
4933 err = bind_rdev_to_array(rdev, mddev);
4934 if (err)
4935 export_rdev(rdev);
4936 return err;
4937 }
4938
4939 /*
4940 * add_new_disk can be used once the array is assembled
4941 * to add "hot spares". They must already have a superblock
4942 * written
4943 */
4944 if (mddev->pers) {
4945 int err;
4946 if (!mddev->pers->hot_add_disk) {
4947 printk(KERN_WARNING
4948 "%s: personality does not support diskops!\n",
4949 mdname(mddev));
4950 return -EINVAL;
4951 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004952 if (mddev->persistent)
4953 rdev = md_import_device(dev, mddev->major_version,
4954 mddev->minor_version);
4955 else
4956 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 if (IS_ERR(rdev)) {
4958 printk(KERN_WARNING
4959 "md: md_import_device returned %ld\n",
4960 PTR_ERR(rdev));
4961 return PTR_ERR(rdev);
4962 }
NeilBrown41158c72005-06-21 17:17:25 -07004963 /* set save_raid_disk if appropriate */
4964 if (!mddev->persistent) {
4965 if (info->state & (1<<MD_DISK_SYNC) &&
4966 info->raid_disk < mddev->raid_disks)
4967 rdev->raid_disk = info->raid_disk;
4968 else
4969 rdev->raid_disk = -1;
4970 } else
4971 super_types[mddev->major_version].
4972 validate_super(mddev, rdev);
4973 rdev->saved_raid_disk = rdev->raid_disk;
4974
NeilBrownb2d444d2005-11-08 21:39:31 -08004975 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004976 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4977 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11004978 else
4979 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004980
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 rdev->raid_disk = -1;
4982 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004983 if (!err && !mddev->pers->hot_remove_disk) {
4984 /* If there is hot_add_disk but no hot_remove_disk
4985 * then added disks for geometry changes,
4986 * and should be added immediately.
4987 */
4988 super_types[mddev->major_version].
4989 validate_super(mddev, rdev);
4990 err = mddev->pers->hot_add_disk(mddev, rdev);
4991 if (err)
4992 unbind_rdev_from_array(rdev);
4993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 if (err)
4995 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004996 else
NeilBrown3c0ee632008-10-21 13:25:28 +11004997 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07004998
NeilBrown17571282006-12-10 02:20:52 -08004999 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005000 if (mddev->degraded)
5001 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005002 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07005003 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 return err;
5005 }
5006
5007 /* otherwise, add_new_disk is only allowed
5008 * for major_version==0 superblocks
5009 */
5010 if (mddev->major_version != 0) {
5011 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5012 mdname(mddev));
5013 return -EINVAL;
5014 }
5015
5016 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5017 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005018 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 if (IS_ERR(rdev)) {
5020 printk(KERN_WARNING
5021 "md: error, md_import_device() returned %ld\n",
5022 PTR_ERR(rdev));
5023 return PTR_ERR(rdev);
5024 }
5025 rdev->desc_nr = info->number;
5026 if (info->raid_disk < mddev->raid_disks)
5027 rdev->raid_disk = info->raid_disk;
5028 else
5029 rdev->raid_disk = -1;
5030
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005032 if (info->state & (1<<MD_DISK_SYNC))
5033 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005035 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5036 set_bit(WriteMostly, &rdev->flags);
5037
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 if (!mddev->persistent) {
5039 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10005040 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 } else
Andre Noll0f420352008-07-11 22:02:23 +10005042 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10005043 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044
NeilBrown2bf071b2006-01-06 00:20:55 -08005045 err = bind_rdev_to_array(rdev, mddev);
5046 if (err) {
5047 export_rdev(rdev);
5048 return err;
5049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 }
5051
5052 return 0;
5053}
5054
5055static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5056{
5057 char b[BDEVNAME_SIZE];
5058 mdk_rdev_t *rdev;
5059
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 rdev = find_rdev(mddev, dev);
5061 if (!rdev)
5062 return -ENXIO;
5063
5064 if (rdev->raid_disk >= 0)
5065 goto busy;
5066
5067 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005068 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005069 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070
5071 return 0;
5072busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005073 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 bdevname(rdev->bdev,b), mdname(mddev));
5075 return -EBUSY;
5076}
5077
5078static int hot_add_disk(mddev_t * mddev, dev_t dev)
5079{
5080 char b[BDEVNAME_SIZE];
5081 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 mdk_rdev_t *rdev;
5083
5084 if (!mddev->pers)
5085 return -ENODEV;
5086
5087 if (mddev->major_version != 0) {
5088 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5089 " version-0 superblocks.\n",
5090 mdname(mddev));
5091 return -EINVAL;
5092 }
5093 if (!mddev->pers->hot_add_disk) {
5094 printk(KERN_WARNING
5095 "%s: personality does not support diskops!\n",
5096 mdname(mddev));
5097 return -EINVAL;
5098 }
5099
NeilBrownd710e132008-10-13 11:55:12 +11005100 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 if (IS_ERR(rdev)) {
5102 printk(KERN_WARNING
5103 "md: error, md_import_device() returned %ld\n",
5104 PTR_ERR(rdev));
5105 return -EINVAL;
5106 }
5107
5108 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10005109 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 else
Andre Noll0f420352008-07-11 22:02:23 +10005111 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112
NeilBrown8190e752009-06-18 08:48:58 +10005113 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114
NeilBrownb2d444d2005-11-08 21:39:31 -08005115 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 printk(KERN_WARNING
5117 "md: can not hot-add faulty %s disk to %s!\n",
5118 bdevname(rdev->bdev,b), mdname(mddev));
5119 err = -EINVAL;
5120 goto abort_export;
5121 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005122 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005124 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005125 err = bind_rdev_to_array(rdev, mddev);
5126 if (err)
5127 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128
5129 /*
5130 * The rest should better be atomic, we can have disk failures
5131 * noticed in interrupt contexts ...
5132 */
5133
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 rdev->raid_disk = -1;
5135
NeilBrown850b2b42006-10-03 01:15:46 -07005136 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137
5138 /*
5139 * Kick recovery, maybe this spare has to be added to the
5140 * array immediately.
5141 */
5142 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5143 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005144 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 return 0;
5146
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147abort_export:
5148 export_rdev(rdev);
5149 return err;
5150}
5151
NeilBrown32a76272005-06-21 17:17:14 -07005152static int set_bitmap_file(mddev_t *mddev, int fd)
5153{
5154 int err;
5155
NeilBrown36fa3062005-09-09 16:23:45 -07005156 if (mddev->pers) {
5157 if (!mddev->pers->quiesce)
5158 return -EBUSY;
5159 if (mddev->recovery || mddev->sync_thread)
5160 return -EBUSY;
5161 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005162 }
5163
NeilBrown36fa3062005-09-09 16:23:45 -07005164
5165 if (fd >= 0) {
5166 if (mddev->bitmap)
5167 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005168 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005169
NeilBrownc3d97142009-12-14 12:49:52 +11005170 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005171 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5172 mdname(mddev));
5173 return -EBADF;
5174 }
5175
NeilBrownc3d97142009-12-14 12:49:52 +11005176 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005177 if (err) {
5178 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5179 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005180 fput(mddev->bitmap_info.file);
5181 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005182 return err;
5183 }
NeilBrownc3d97142009-12-14 12:49:52 +11005184 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005185 } else if (mddev->bitmap == NULL)
5186 return -ENOENT; /* cannot remove what isn't there */
5187 err = 0;
5188 if (mddev->pers) {
5189 mddev->pers->quiesce(mddev, 1);
5190 if (fd >= 0)
5191 err = bitmap_create(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07005192 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005193 bitmap_destroy(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07005194 fd = -1; /* make sure to put the file */
5195 }
NeilBrown36fa3062005-09-09 16:23:45 -07005196 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab322006-06-26 00:27:43 -07005197 }
5198 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005199 if (mddev->bitmap_info.file) {
5200 restore_bitmap_write_access(mddev->bitmap_info.file);
5201 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005202 }
NeilBrownc3d97142009-12-14 12:49:52 +11005203 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005204 }
5205
NeilBrown32a76272005-06-21 17:17:14 -07005206 return err;
5207}
5208
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209/*
5210 * set_array_info is used two different ways
5211 * The original usage is when creating a new array.
5212 * In this usage, raid_disks is > 0 and it together with
5213 * level, size, not_persistent,layout,chunksize determine the
5214 * shape of the array.
5215 * This will always create an array with a type-0.90.0 superblock.
5216 * The newer usage is when assembling an array.
5217 * In this case raid_disks will be 0, and the major_version field is
5218 * use to determine which style super-blocks are to be found on the devices.
5219 * The minor and patch _version numbers are also kept incase the
5220 * super_block handler wishes to interpret them.
5221 */
5222static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5223{
5224
5225 if (info->raid_disks == 0) {
5226 /* just setting version number for superblock loading */
5227 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005228 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 super_types[info->major_version].name == NULL) {
5230 /* maybe try to auto-load a module? */
5231 printk(KERN_INFO
5232 "md: superblock version %d not known\n",
5233 info->major_version);
5234 return -EINVAL;
5235 }
5236 mddev->major_version = info->major_version;
5237 mddev->minor_version = info->minor_version;
5238 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005239 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005240 /* ensure mddev_put doesn't delete this now that there
5241 * is some minimal configuration.
5242 */
5243 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 return 0;
5245 }
5246 mddev->major_version = MD_MAJOR_VERSION;
5247 mddev->minor_version = MD_MINOR_VERSION;
5248 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5249 mddev->ctime = get_seconds();
5250
5251 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005252 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005253 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 mddev->raid_disks = info->raid_disks;
5255 /* don't set md_minor, it is determined by which /dev/md* was
5256 * openned
5257 */
5258 if (info->state & (1<<MD_SB_CLEAN))
5259 mddev->recovery_cp = MaxSector;
5260 else
5261 mddev->recovery_cp = 0;
5262 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005263 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264
5265 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005266 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267
5268 mddev->max_disks = MD_SB_DISKS;
5269
NeilBrowne6910632008-02-06 01:39:51 -08005270 if (mddev->persistent)
5271 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005272 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273
NeilBrownc3d97142009-12-14 12:49:52 +11005274 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5275 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005276
NeilBrownf6705572006-03-27 01:18:11 -08005277 mddev->reshape_position = MaxSector;
5278
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 /*
5280 * Generate a 128 bit UUID
5281 */
5282 get_random_bytes(mddev->uuid, 16);
5283
NeilBrownf6705572006-03-27 01:18:11 -08005284 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005285 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005286 mddev->new_layout = mddev->layout;
5287 mddev->delta_disks = 0;
5288
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 return 0;
5290}
5291
Dan Williams1f403622009-03-31 14:59:03 +11005292void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5293{
Dan Williamsb522adc2009-03-31 15:00:31 +11005294 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5295
5296 if (mddev->external_size)
5297 return;
5298
Dan Williams1f403622009-03-31 14:59:03 +11005299 mddev->array_sectors = array_sectors;
5300}
5301EXPORT_SYMBOL(md_set_array_sectors);
5302
Andre Nolld71f9f82008-07-11 22:02:22 +10005303static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005304{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005305 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005306 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005307 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005308
5309 if (mddev->pers->resize == NULL)
5310 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005311 /* The "num_sectors" is the number of sectors of each device that
5312 * is used. This can only make sense for arrays with redundancy.
5313 * linear and raid0 always use whatever space is available. We can only
5314 * consider changing this number if no resync or reconstruction is
5315 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005316 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005317 * of each device. If num_sectors is zero, we find the largest size
5318 * that fits.
5319
NeilBrowna35b0d62006-01-06 00:20:49 -08005320 */
5321 if (mddev->sync_thread)
5322 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005323 if (mddev->bitmap)
5324 /* Sorry, cannot grow a bitmap yet, just remove it,
5325 * grow, and re-add.
5326 */
5327 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005328 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005329 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005330
Andre Nolld71f9f82008-07-11 22:02:22 +10005331 if (fit && (num_sectors == 0 || num_sectors > avail))
5332 num_sectors = avail;
5333 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005334 return -ENOSPC;
5335 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005336 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005337 if (!rv)
5338 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005339 return rv;
5340}
5341
NeilBrownda943b992006-01-06 00:20:54 -08005342static int update_raid_disks(mddev_t *mddev, int raid_disks)
5343{
5344 int rv;
5345 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005346 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005347 return -EINVAL;
5348 if (raid_disks <= 0 ||
5349 raid_disks >= mddev->max_disks)
5350 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005351 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005352 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005353 mddev->delta_disks = raid_disks - mddev->raid_disks;
5354
5355 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005356 return rv;
5357}
5358
5359
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360/*
5361 * update_array_info is used to change the configuration of an
5362 * on-line array.
5363 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5364 * fields in the info are checked against the array.
5365 * Any differences that cannot be handled will cause an error.
5366 * Normally, only one change can be managed at a time.
5367 */
5368static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5369{
5370 int rv = 0;
5371 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005372 int state = 0;
5373
5374 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005375 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005376 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377
5378 if (mddev->major_version != info->major_version ||
5379 mddev->minor_version != info->minor_version ||
5380/* mddev->patch_version != info->patch_version || */
5381 mddev->ctime != info->ctime ||
5382 mddev->level != info->level ||
5383/* mddev->layout != info->layout || */
5384 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005385 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005386 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5387 ((state^info->state) & 0xfffffe00)
5388 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 return -EINVAL;
5390 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005391 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5392 cnt++;
5393 if (mddev->raid_disks != info->raid_disks)
5394 cnt++;
5395 if (mddev->layout != info->layout)
5396 cnt++;
5397 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5398 cnt++;
5399 if (cnt == 0)
5400 return 0;
5401 if (cnt > 1)
5402 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403
5404 if (mddev->layout != info->layout) {
5405 /* Change layout
5406 * we don't need to do anything at the md level, the
5407 * personality will take care of it all.
5408 */
NeilBrown50ac1682009-06-18 08:47:55 +10005409 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005411 else {
5412 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005413 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005414 if (rv)
5415 mddev->new_layout = mddev->layout;
5416 return rv;
5417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005419 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005420 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421
NeilBrownda943b992006-01-06 00:20:54 -08005422 if (mddev->raid_disks != info->raid_disks)
5423 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424
NeilBrown36fa3062005-09-09 16:23:45 -07005425 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5426 if (mddev->pers->quiesce == NULL)
5427 return -EINVAL;
5428 if (mddev->recovery || mddev->sync_thread)
5429 return -EBUSY;
5430 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5431 /* add the bitmap */
5432 if (mddev->bitmap)
5433 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005434 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005435 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005436 mddev->bitmap_info.offset =
5437 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005438 mddev->pers->quiesce(mddev, 1);
5439 rv = bitmap_create(mddev);
5440 if (rv)
5441 bitmap_destroy(mddev);
5442 mddev->pers->quiesce(mddev, 0);
5443 } else {
5444 /* remove the bitmap */
5445 if (!mddev->bitmap)
5446 return -ENOENT;
5447 if (mddev->bitmap->file)
5448 return -EINVAL;
5449 mddev->pers->quiesce(mddev, 1);
5450 bitmap_destroy(mddev);
5451 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005452 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005453 }
5454 }
NeilBrown850b2b42006-10-03 01:15:46 -07005455 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 return rv;
5457}
5458
5459static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5460{
5461 mdk_rdev_t *rdev;
5462
5463 if (mddev->pers == NULL)
5464 return -ENODEV;
5465
5466 rdev = find_rdev(mddev, dev);
5467 if (!rdev)
5468 return -ENODEV;
5469
5470 md_error(mddev, rdev);
5471 return 0;
5472}
5473
Andre Noll2f9618c2008-04-25 18:57:58 +02005474/*
5475 * We have a problem here : there is no easy way to give a CHS
5476 * virtual geometry. We currently pretend that we have a 2 heads
5477 * 4 sectors (with a BIG number of cylinders...). This drives
5478 * dosfs just mad... ;-)
5479 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005480static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5481{
5482 mddev_t *mddev = bdev->bd_disk->private_data;
5483
5484 geo->heads = 2;
5485 geo->sectors = 4;
5486 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5487 return 0;
5488}
5489
Al Viroa39907f2008-03-02 10:31:15 -05005490static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 unsigned int cmd, unsigned long arg)
5492{
5493 int err = 0;
5494 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 mddev_t *mddev = NULL;
5496
5497 if (!capable(CAP_SYS_ADMIN))
5498 return -EACCES;
5499
5500 /*
5501 * Commands dealing with the RAID driver but not any
5502 * particular array:
5503 */
5504 switch (cmd)
5505 {
5506 case RAID_VERSION:
5507 err = get_version(argp);
5508 goto done;
5509
5510 case PRINT_RAID_DEBUG:
5511 err = 0;
5512 md_print_devices();
5513 goto done;
5514
5515#ifndef MODULE
5516 case RAID_AUTORUN:
5517 err = 0;
5518 autostart_arrays(arg);
5519 goto done;
5520#endif
5521 default:;
5522 }
5523
5524 /*
5525 * Commands creating/starting a new array:
5526 */
5527
Al Viroa39907f2008-03-02 10:31:15 -05005528 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
5530 if (!mddev) {
5531 BUG();
5532 goto abort;
5533 }
5534
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 err = mddev_lock(mddev);
5536 if (err) {
5537 printk(KERN_INFO
5538 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5539 err, cmd);
5540 goto abort;
5541 }
5542
5543 switch (cmd)
5544 {
5545 case SET_ARRAY_INFO:
5546 {
5547 mdu_array_info_t info;
5548 if (!arg)
5549 memset(&info, 0, sizeof(info));
5550 else if (copy_from_user(&info, argp, sizeof(info))) {
5551 err = -EFAULT;
5552 goto abort_unlock;
5553 }
5554 if (mddev->pers) {
5555 err = update_array_info(mddev, &info);
5556 if (err) {
5557 printk(KERN_WARNING "md: couldn't update"
5558 " array info. %d\n", err);
5559 goto abort_unlock;
5560 }
5561 goto done_unlock;
5562 }
5563 if (!list_empty(&mddev->disks)) {
5564 printk(KERN_WARNING
5565 "md: array %s already has disks!\n",
5566 mdname(mddev));
5567 err = -EBUSY;
5568 goto abort_unlock;
5569 }
5570 if (mddev->raid_disks) {
5571 printk(KERN_WARNING
5572 "md: array %s already initialised!\n",
5573 mdname(mddev));
5574 err = -EBUSY;
5575 goto abort_unlock;
5576 }
5577 err = set_array_info(mddev, &info);
5578 if (err) {
5579 printk(KERN_WARNING "md: couldn't set"
5580 " array info. %d\n", err);
5581 goto abort_unlock;
5582 }
5583 }
5584 goto done_unlock;
5585
5586 default:;
5587 }
5588
5589 /*
5590 * Commands querying/configuring an existing array:
5591 */
NeilBrown32a76272005-06-21 17:17:14 -07005592 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005593 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005594 if ((!mddev->raid_disks && !mddev->external)
5595 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5596 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5597 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 err = -ENODEV;
5599 goto abort_unlock;
5600 }
5601
5602 /*
5603 * Commands even a read-only array can execute:
5604 */
5605 switch (cmd)
5606 {
5607 case GET_ARRAY_INFO:
5608 err = get_array_info(mddev, argp);
5609 goto done_unlock;
5610
NeilBrown32a76272005-06-21 17:17:14 -07005611 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005612 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005613 goto done_unlock;
5614
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 case GET_DISK_INFO:
5616 err = get_disk_info(mddev, argp);
5617 goto done_unlock;
5618
5619 case RESTART_ARRAY_RW:
5620 err = restart_array(mddev);
5621 goto done_unlock;
5622
5623 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005624 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 goto done_unlock;
5626
5627 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005628 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 goto done_unlock;
5630
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 }
5632
5633 /*
5634 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005635 * superblock, so we do not allow them on read-only arrays.
5636 * However non-MD ioctls (e.g. get-size) will still come through
5637 * here and hit the 'default' below, so only disallow
5638 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005640 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005641 if (mddev->ro == 2) {
5642 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005643 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005644 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5645 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005646 } else {
5647 err = -EROFS;
5648 goto abort_unlock;
5649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 }
5651
5652 switch (cmd)
5653 {
5654 case ADD_NEW_DISK:
5655 {
5656 mdu_disk_info_t info;
5657 if (copy_from_user(&info, argp, sizeof(info)))
5658 err = -EFAULT;
5659 else
5660 err = add_new_disk(mddev, &info);
5661 goto done_unlock;
5662 }
5663
5664 case HOT_REMOVE_DISK:
5665 err = hot_remove_disk(mddev, new_decode_dev(arg));
5666 goto done_unlock;
5667
5668 case HOT_ADD_DISK:
5669 err = hot_add_disk(mddev, new_decode_dev(arg));
5670 goto done_unlock;
5671
5672 case SET_DISK_FAULTY:
5673 err = set_disk_faulty(mddev, new_decode_dev(arg));
5674 goto done_unlock;
5675
5676 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005677 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 goto done_unlock;
5679
NeilBrown32a76272005-06-21 17:17:14 -07005680 case SET_BITMAP_FILE:
5681 err = set_bitmap_file(mddev, (int)arg);
5682 goto done_unlock;
5683
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 err = -EINVAL;
5686 goto abort_unlock;
5687 }
5688
5689done_unlock:
5690abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005691 if (mddev->hold_active == UNTIL_IOCTL &&
5692 err != -EINVAL)
5693 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 mddev_unlock(mddev);
5695
5696 return err;
5697done:
5698 if (err)
5699 MD_BUG();
5700abort:
5701 return err;
5702}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005703#ifdef CONFIG_COMPAT
5704static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5705 unsigned int cmd, unsigned long arg)
5706{
5707 switch (cmd) {
5708 case HOT_REMOVE_DISK:
5709 case HOT_ADD_DISK:
5710 case SET_DISK_FAULTY:
5711 case SET_BITMAP_FILE:
5712 /* These take in integer arg, do not convert */
5713 break;
5714 default:
5715 arg = (unsigned long)compat_ptr(arg);
5716 break;
5717 }
5718
5719 return md_ioctl(bdev, mode, cmd, arg);
5720}
5721#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722
Al Viroa39907f2008-03-02 10:31:15 -05005723static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724{
5725 /*
5726 * Succeed if we can lock the mddev, which confirms that
5727 * it isn't being stopped right now.
5728 */
NeilBrownd3374822009-01-09 08:31:10 +11005729 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 int err;
5731
NeilBrownd3374822009-01-09 08:31:10 +11005732 if (mddev->gendisk != bdev->bd_disk) {
5733 /* we are racing with mddev_put which is discarding this
5734 * bd_disk.
5735 */
5736 mddev_put(mddev);
5737 /* Wait until bdev->bd_disk is definitely gone */
5738 flush_scheduled_work();
5739 /* Then retry the open from the top */
5740 return -ERESTARTSYS;
5741 }
5742 BUG_ON(mddev != bdev->bd_disk->private_data);
5743
NeilBrownc8c00a62009-08-10 12:50:52 +10005744 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 goto out;
5746
5747 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005748 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005749 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750
Al Viroa39907f2008-03-02 10:31:15 -05005751 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 out:
5753 return err;
5754}
5755
Al Viroa39907f2008-03-02 10:31:15 -05005756static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757{
Al Viroa39907f2008-03-02 10:31:15 -05005758 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005760 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005761 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 mddev_put(mddev);
5763
5764 return 0;
5765}
5766
Linus Torvalds44ce62942007-05-09 18:51:36 -07005767static int md_media_changed(struct gendisk *disk)
5768{
5769 mddev_t *mddev = disk->private_data;
5770
5771 return mddev->changed;
5772}
5773
5774static int md_revalidate(struct gendisk *disk)
5775{
5776 mddev_t *mddev = disk->private_data;
5777
5778 mddev->changed = 0;
5779 return 0;
5780}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005781static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782{
5783 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005784 .open = md_open,
5785 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005786 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005787#ifdef CONFIG_COMPAT
5788 .compat_ioctl = md_compat_ioctl,
5789#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005790 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07005791 .media_changed = md_media_changed,
5792 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793};
5794
Adrian Bunk75c96f82005-05-05 16:16:09 -07005795static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796{
5797 mdk_thread_t *thread = arg;
5798
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 /*
5800 * md_thread is a 'system-thread', it's priority should be very
5801 * high. We avoid resource deadlocks individually in each
5802 * raid personality. (RAID5 does preallocation) We also use RR and
5803 * the very same RT priority as kswapd, thus we will never get
5804 * into a priority inversion deadlock.
5805 *
5806 * we definitely have to have equal or higher priority than
5807 * bdflush, otherwise bdflush will deadlock if there are too
5808 * many dirty RAID5 blocks.
5809 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810
NeilBrown6985c432005-10-19 21:23:47 -07005811 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005812 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813
NeilBrown93588e22005-11-15 00:09:12 -08005814 /* We need to wait INTERRUPTIBLE so that
5815 * we don't add to the load-average.
5816 * That means we need to be sure no signals are
5817 * pending
5818 */
5819 if (signal_pending(current))
5820 flush_signals(current);
5821
5822 wait_event_interruptible_timeout
5823 (thread->wqueue,
5824 test_bit(THREAD_WAKEUP, &thread->flags)
5825 || kthread_should_stop(),
5826 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827
5828 clear_bit(THREAD_WAKEUP, &thread->flags);
5829
NeilBrown787453c2005-11-08 21:39:43 -08005830 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005832
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 return 0;
5834}
5835
5836void md_wakeup_thread(mdk_thread_t *thread)
5837{
5838 if (thread) {
5839 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5840 set_bit(THREAD_WAKEUP, &thread->flags);
5841 wake_up(&thread->wqueue);
5842 }
5843}
5844
5845mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5846 const char *name)
5847{
5848 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849
NeilBrown9ffae0c2006-01-06 00:20:32 -08005850 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 if (!thread)
5852 return NULL;
5853
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 init_waitqueue_head(&thread->wqueue);
5855
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 thread->run = run;
5857 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005858 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10005859 thread->tsk = kthread_run(md_thread, thread,
5860 "%s_%s",
5861 mdname(thread->mddev),
5862 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07005863 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 kfree(thread);
5865 return NULL;
5866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 return thread;
5868}
5869
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870void md_unregister_thread(mdk_thread_t *thread)
5871{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005872 if (!thread)
5873 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005874 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005875
5876 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 kfree(thread);
5878}
5879
5880void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5881{
5882 if (!mddev) {
5883 MD_BUG();
5884 return;
5885 }
5886
NeilBrownb2d444d2005-11-08 21:39:31 -08005887 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005889
5890 if (mddev->external)
5891 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005892/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5894 mdname(mddev),
5895 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5896 __builtin_return_address(0),__builtin_return_address(1),
5897 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005898*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005899 if (!mddev->pers)
5900 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 if (!mddev->pers->error_handler)
5902 return;
5903 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005904 if (mddev->degraded)
5905 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005906 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5908 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5909 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005910 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911}
5912
5913/* seq_file implementation /proc/mdstat */
5914
5915static void status_unused(struct seq_file *seq)
5916{
5917 int i = 0;
5918 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919
5920 seq_printf(seq, "unused devices: ");
5921
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005922 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 char b[BDEVNAME_SIZE];
5924 i++;
5925 seq_printf(seq, "%s ",
5926 bdevname(rdev->bdev,b));
5927 }
5928 if (!i)
5929 seq_printf(seq, "<none>");
5930
5931 seq_printf(seq, "\n");
5932}
5933
5934
5935static void status_resync(struct seq_file *seq, mddev_t * mddev)
5936{
NeilBrowndd71cf62009-05-07 12:49:35 +10005937 sector_t max_sectors, resync, res;
5938 unsigned long dt, db;
5939 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08005940 int scale;
5941 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942
NeilBrowndd71cf62009-05-07 12:49:35 +10005943 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944
5945 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10005946 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 else
NeilBrowndd71cf62009-05-07 12:49:35 +10005948 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949
5950 /*
5951 * Should not happen.
5952 */
NeilBrowndd71cf62009-05-07 12:49:35 +10005953 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 MD_BUG();
5955 return;
5956 }
NeilBrown4588b422006-03-27 01:18:04 -08005957 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10005958 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08005959 * u32, as those are the requirements for sector_div.
5960 * Thus 'scale' must be at least 10
5961 */
5962 scale = 10;
5963 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10005964 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08005965 scale++;
5966 }
5967 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10005968 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08005969
5970 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971 {
NeilBrown4588b422006-03-27 01:18:04 -08005972 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973 seq_printf(seq, "[");
5974 for (i = 0; i < x; i++)
5975 seq_printf(seq, "=");
5976 seq_printf(seq, ">");
5977 for (i = 0; i < y; i++)
5978 seq_printf(seq, ".");
5979 seq_printf(seq, "] ");
5980 }
NeilBrown4588b422006-03-27 01:18:04 -08005981 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005982 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5983 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005984 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5985 "check" :
5986 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5987 "resync" : "recovery"))),
5988 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10005989 (unsigned long long) resync/2,
5990 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991
5992 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 * dt: time from mark until now
5994 * db: blocks written from mark until now
5995 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10005996 *
5997 * rt is a sector_t, so could be 32bit or 64bit.
5998 * So we divide before multiply in case it is 32bit and close
5999 * to the limit.
6000 * We scale the divisor (db) by 32 to avoid loosing precision
6001 * near the end of resync when the number of remaining sectors
6002 * is close to 'db'.
6003 * We then divide rt by 32 after multiplying by db to compensate.
6004 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 */
6006 dt = ((jiffies - mddev->resync_mark) / HZ);
6007 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006008 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6009 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010
NeilBrowndd71cf62009-05-07 12:49:35 +10006011 rt = max_sectors - resync; /* number of remaining sectors */
6012 sector_div(rt, db/32+1);
6013 rt *= dt;
6014 rt >>= 5;
6015
6016 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6017 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018
NeilBrownff4e8d92006-07-10 04:44:16 -07006019 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020}
6021
6022static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6023{
6024 struct list_head *tmp;
6025 loff_t l = *pos;
6026 mddev_t *mddev;
6027
6028 if (l >= 0x10000)
6029 return NULL;
6030 if (!l--)
6031 /* header */
6032 return (void*)1;
6033
6034 spin_lock(&all_mddevs_lock);
6035 list_for_each(tmp,&all_mddevs)
6036 if (!l--) {
6037 mddev = list_entry(tmp, mddev_t, all_mddevs);
6038 mddev_get(mddev);
6039 spin_unlock(&all_mddevs_lock);
6040 return mddev;
6041 }
6042 spin_unlock(&all_mddevs_lock);
6043 if (!l--)
6044 return (void*)2;/* tail */
6045 return NULL;
6046}
6047
6048static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6049{
6050 struct list_head *tmp;
6051 mddev_t *next_mddev, *mddev = v;
6052
6053 ++*pos;
6054 if (v == (void*)2)
6055 return NULL;
6056
6057 spin_lock(&all_mddevs_lock);
6058 if (v == (void*)1)
6059 tmp = all_mddevs.next;
6060 else
6061 tmp = mddev->all_mddevs.next;
6062 if (tmp != &all_mddevs)
6063 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6064 else {
6065 next_mddev = (void*)2;
6066 *pos = 0x10000;
6067 }
6068 spin_unlock(&all_mddevs_lock);
6069
6070 if (v != (void*)1)
6071 mddev_put(mddev);
6072 return next_mddev;
6073
6074}
6075
6076static void md_seq_stop(struct seq_file *seq, void *v)
6077{
6078 mddev_t *mddev = v;
6079
6080 if (mddev && v != (void*)1 && v != (void*)2)
6081 mddev_put(mddev);
6082}
6083
NeilBrownd7603b72006-01-06 00:20:30 -08006084struct mdstat_info {
6085 int event;
6086};
6087
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088static int md_seq_show(struct seq_file *seq, void *v)
6089{
6090 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006091 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006093 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006094 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095
6096 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006097 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 seq_printf(seq, "Personalities : ");
6099 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006100 list_for_each_entry(pers, &pers_list, list)
6101 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102
6103 spin_unlock(&pers_lock);
6104 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006105 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006106 return 0;
6107 }
6108 if (v == (void*)2) {
6109 status_unused(seq);
6110 return 0;
6111 }
6112
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006113 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006115
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6117 seq_printf(seq, "%s : %sactive", mdname(mddev),
6118 mddev->pers ? "" : "in");
6119 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006120 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006122 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006123 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124 seq_printf(seq, " %s", mddev->pers->name);
6125 }
6126
Andre Nolldd8ac332009-03-31 14:33:13 +11006127 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006128 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 char b[BDEVNAME_SIZE];
6130 seq_printf(seq, " %s[%d]",
6131 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006132 if (test_bit(WriteMostly, &rdev->flags))
6133 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006134 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 seq_printf(seq, "(F)");
6136 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006137 } else if (rdev->raid_disk < 0)
6138 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006139 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 }
6141
6142 if (!list_empty(&mddev->disks)) {
6143 if (mddev->pers)
6144 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006145 (unsigned long long)
6146 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 else
6148 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006149 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006151 if (mddev->persistent) {
6152 if (mddev->major_version != 0 ||
6153 mddev->minor_version != 90) {
6154 seq_printf(seq," super %d.%d",
6155 mddev->major_version,
6156 mddev->minor_version);
6157 }
NeilBrowne6910632008-02-06 01:39:51 -08006158 } else if (mddev->external)
6159 seq_printf(seq, " super external:%s",
6160 mddev->metadata_type);
6161 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006162 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163
6164 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006165 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006167 if (mddev->pers->sync_request) {
6168 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006169 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006170 seq_printf(seq, "\n ");
6171 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6172 seq_printf(seq, "\tresync=DELAYED\n ");
6173 else if (mddev->recovery_cp < MaxSector)
6174 seq_printf(seq, "\tresync=PENDING\n ");
6175 }
NeilBrown32a76272005-06-21 17:17:14 -07006176 } else
6177 seq_printf(seq, "\n ");
6178
6179 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006180 unsigned long chunk_kb;
6181 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006182 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006183 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006184 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6185 "%lu%s chunk",
6186 bitmap->pages - bitmap->missing_pages,
6187 bitmap->pages,
6188 (bitmap->pages - bitmap->missing_pages)
6189 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006190 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006191 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006192 if (bitmap->file) {
6193 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006194 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006195 }
NeilBrown78d742d2005-06-21 17:17:15 -07006196
NeilBrown32a76272005-06-21 17:17:14 -07006197 seq_printf(seq, "\n");
6198 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 }
6200
6201 seq_printf(seq, "\n");
6202 }
6203 mddev_unlock(mddev);
6204
6205 return 0;
6206}
6207
Jan Engelhardt110518b2009-05-07 12:49:37 +10006208static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209 .start = md_seq_start,
6210 .next = md_seq_next,
6211 .stop = md_seq_stop,
6212 .show = md_seq_show,
6213};
6214
6215static int md_seq_open(struct inode *inode, struct file *file)
6216{
6217 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006218 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6219 if (mi == NULL)
6220 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221
6222 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006223 if (error)
6224 kfree(mi);
6225 else {
6226 struct seq_file *p = file->private_data;
6227 p->private = mi;
6228 mi->event = atomic_read(&md_event_count);
6229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230 return error;
6231}
6232
NeilBrownd7603b72006-01-06 00:20:30 -08006233static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6234{
6235 struct seq_file *m = filp->private_data;
6236 struct mdstat_info *mi = m->private;
6237 int mask;
6238
6239 poll_wait(filp, &md_event_waiters, wait);
6240
6241 /* always allow read */
6242 mask = POLLIN | POLLRDNORM;
6243
6244 if (mi->event != atomic_read(&md_event_count))
6245 mask |= POLLERR | POLLPRI;
6246 return mask;
6247}
6248
Arjan van de Venfa027c22007-02-12 00:55:33 -08006249static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006250 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251 .open = md_seq_open,
6252 .read = seq_read,
6253 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006254 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006255 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256};
6257
NeilBrown2604b702006-01-06 00:20:36 -08006258int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006261 list_add_tail(&p->list, &pers_list);
6262 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 spin_unlock(&pers_lock);
6264 return 0;
6265}
6266
NeilBrown2604b702006-01-06 00:20:36 -08006267int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268{
NeilBrown2604b702006-01-06 00:20:36 -08006269 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006271 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272 spin_unlock(&pers_lock);
6273 return 0;
6274}
6275
NeilBrowneea1bf32009-03-31 14:27:02 +11006276static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277{
6278 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006280 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281
6282 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006283 rcu_read_lock();
6284 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006286 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6287 (int)part_stat_read(&disk->part0, sectors[1]) -
6288 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006289 /* sync IO will cause sync_io to increase before the disk_stats
6290 * as sync_io is counted when a request starts, and
6291 * disk_stats is counted when it completes.
6292 * So resync activity will cause curr_events to be smaller than
6293 * when there was no such activity.
6294 * non-sync IO will cause disk_stat to increase without
6295 * increasing sync_io so curr_events will (eventually)
6296 * be larger than it was before. Once it becomes
6297 * substantially larger, the test below will cause
6298 * the array to appear non-idle, and resync will slow
6299 * down.
6300 * If there is a lot of outstanding resync activity when
6301 * we set last_event to curr_events, then all that activity
6302 * completing might cause the array to appear non-idle
6303 * and resync will be slowed down even though there might
6304 * not have been non-resync activity. This will only
6305 * happen once though. 'last_events' will soon reflect
6306 * the state where there is little or no outstanding
6307 * resync requests, and further resync activity will
6308 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006309 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006311 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312 rdev->last_events = curr_events;
6313 idle = 0;
6314 }
6315 }
NeilBrown4b809912008-07-21 17:05:25 +10006316 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 return idle;
6318}
6319
6320void md_done_sync(mddev_t *mddev, int blocks, int ok)
6321{
6322 /* another "blocks" (512byte) blocks have been synced */
6323 atomic_sub(blocks, &mddev->recovery_active);
6324 wake_up(&mddev->recovery_wait);
6325 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006326 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327 md_wakeup_thread(mddev->thread);
6328 // stop recovery, signal do_sync ....
6329 }
6330}
6331
6332
NeilBrown06d91a52005-06-21 17:17:12 -07006333/* md_write_start(mddev, bi)
6334 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006335 * in superblock) before writing, schedule a superblock update
6336 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006337 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006338void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339{
Neil Brown0fd62b82008-06-28 08:31:36 +10006340 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006341 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006342 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006343
NeilBrownf91de922005-11-08 21:39:36 -08006344 BUG_ON(mddev->ro == 1);
6345 if (mddev->ro == 2) {
6346 /* need to switch to read/write */
6347 mddev->ro = 0;
6348 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6349 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006350 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006351 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006352 }
NeilBrown06d91a52005-06-21 17:17:12 -07006353 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006354 if (mddev->safemode == 1)
6355 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006356 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006357 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006358 if (mddev->in_sync) {
6359 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006360 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006361 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006362 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006363 }
NeilBrowna9701a32005-11-08 21:39:34 -08006364 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006365 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006366 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006367 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006368 wait_event(mddev->sb_wait,
6369 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6370 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371}
6372
6373void md_write_end(mddev_t *mddev)
6374{
6375 if (atomic_dec_and_test(&mddev->writes_pending)) {
6376 if (mddev->safemode == 2)
6377 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006378 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6380 }
6381}
6382
NeilBrown2a2275d2007-01-26 00:57:11 -08006383/* md_allow_write(mddev)
6384 * Calling this ensures that the array is marked 'active' so that writes
6385 * may proceed without blocking. It is important to call this before
6386 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6387 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006388 *
6389 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6390 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006391 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006392int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006393{
6394 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006395 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006396 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006397 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006398 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006399 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006400
6401 spin_lock_irq(&mddev->write_lock);
6402 if (mddev->in_sync) {
6403 mddev->in_sync = 0;
6404 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6405 if (mddev->safemode_delay &&
6406 mddev->safemode == 0)
6407 mddev->safemode = 1;
6408 spin_unlock_irq(&mddev->write_lock);
6409 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006410 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006411 } else
6412 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006413
6414 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6415 return -EAGAIN;
6416 else
6417 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006418}
6419EXPORT_SYMBOL_GPL(md_allow_write);
6420
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421#define SYNC_MARKS 10
6422#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006423void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424{
6425 mddev_t *mddev2;
6426 unsigned int currspeed = 0,
6427 window;
NeilBrown57afd892005-06-21 17:17:13 -07006428 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429 unsigned long mark[SYNC_MARKS];
6430 sector_t mark_cnt[SYNC_MARKS];
6431 int last_mark,m;
6432 struct list_head *tmp;
6433 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006434 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006435 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006436 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437
6438 /* just incase thread restarts... */
6439 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6440 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006441 if (mddev->ro) /* never try to sync a read-only array */
6442 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443
NeilBrown61df9d92006-10-03 01:15:57 -07006444 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6445 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6446 desc = "data-check";
6447 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6448 desc = "requested-resync";
6449 else
6450 desc = "resync";
6451 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6452 desc = "reshape";
6453 else
6454 desc = "recovery";
6455
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456 /* we overload curr_resync somewhat here.
6457 * 0 == not engaged in resync at all
6458 * 2 == checking that there is no conflict with another sync
6459 * 1 == like 2, but have yielded to allow conflicting resync to
6460 * commense
6461 * other == active in resync - this many blocks
6462 *
6463 * Before starting a resync we must have set curr_resync to
6464 * 2, and then checked that every "conflicting" array has curr_resync
6465 * less than ours. When we find one that is the same or higher
6466 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6467 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6468 * This will mean we have to start checking from the beginning again.
6469 *
6470 */
6471
6472 do {
6473 mddev->curr_resync = 2;
6474
6475 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006476 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006477 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006478
6479 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006481 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482 if (mddev2 == mddev)
6483 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006484 if (!mddev->parallel_resync
6485 && mddev2->curr_resync
6486 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487 DEFINE_WAIT(wq);
6488 if (mddev < mddev2 && mddev->curr_resync == 2) {
6489 /* arbitrarily yield */
6490 mddev->curr_resync = 1;
6491 wake_up(&resync_wait);
6492 }
6493 if (mddev > mddev2 && mddev->curr_resync == 1)
6494 /* no need to wait here, we can wait the next
6495 * time 'round when curr_resync == 2
6496 */
6497 continue;
NeilBrown97441972008-09-19 11:49:54 +10006498 /* We need to wait 'interruptible' so as not to
6499 * contribute to the load average, and not to
6500 * be caught by 'softlockup'
6501 */
6502 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006503 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006504 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006505 printk(KERN_INFO "md: delaying %s of %s"
6506 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006508 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006510 if (signal_pending(current))
6511 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512 schedule();
6513 finish_wait(&resync_wait, &wq);
6514 goto try_again;
6515 }
6516 finish_wait(&resync_wait, &wq);
6517 }
6518 }
6519 } while (mddev->curr_resync < 2);
6520
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006521 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006522 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006524 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525 */
6526 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006527 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006528 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006529 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6530 j = mddev->resync_min;
6531 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006532 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006533
NeilBrownccfcc3c2006-03-27 01:18:09 -08006534 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006535 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006536 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006538 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006539 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006540 rcu_read_lock();
6541 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006542 if (rdev->raid_disk >= 0 &&
6543 !test_bit(Faulty, &rdev->flags) &&
6544 !test_bit(In_sync, &rdev->flags) &&
6545 rdev->recovery_offset < j)
6546 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006547 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549
NeilBrown61df9d92006-10-03 01:15:57 -07006550 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6551 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6552 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006553 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006554 "(but not more than %d KB/sec) for %s.\n",
6555 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556
NeilBrowneea1bf32009-03-31 14:27:02 +11006557 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006558
NeilBrown57afd892005-06-21 17:17:13 -07006559 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 for (m = 0; m < SYNC_MARKS; m++) {
6561 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006562 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563 }
6564 last_mark = 0;
6565 mddev->resync_mark = mark[last_mark];
6566 mddev->resync_mark_cnt = mark_cnt[last_mark];
6567
6568 /*
6569 * Tune reconstruction:
6570 */
6571 window = 32*(PAGE_SIZE/512);
6572 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6573 window/2,(unsigned long long) max_sectors/2);
6574
6575 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576 last_check = 0;
6577
6578 if (j>2) {
6579 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006580 "md: resuming %s of %s from checkpoint.\n",
6581 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582 mddev->curr_resync = j;
6583 }
NeilBrownefa59332009-12-14 12:49:49 +11006584 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585
6586 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006587 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588
NeilBrown57afd892005-06-21 17:17:13 -07006589 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006590
NeilBrown7a91ee12009-05-26 12:57:21 +10006591 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6592 ((mddev->curr_resync > mddev->curr_resync_completed &&
6593 (mddev->curr_resync - mddev->curr_resync_completed)
6594 > (max_sectors >> 4)) ||
6595 (j - mddev->curr_resync_completed)*2
6596 >= mddev->resync_max - mddev->curr_resync_completed
6597 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006598 /* time to update curr_resync_completed */
6599 blk_unplug(mddev->queue);
6600 wait_event(mddev->recovery_wait,
6601 atomic_read(&mddev->recovery_active) == 0);
6602 mddev->curr_resync_completed =
6603 mddev->curr_resync;
6604 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006605 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006606 }
NeilBrownacb180b2009-04-14 16:28:34 +10006607
NeilBrowne62e58a2009-07-01 13:15:35 +10006608 while (j >= mddev->resync_max && !kthread_should_stop()) {
6609 /* As this condition is controlled by user-space,
6610 * we can block indefinitely, so use '_interruptible'
6611 * to avoid triggering warnings.
6612 */
6613 flush_signals(current); /* just in case */
6614 wait_event_interruptible(mddev->recovery_wait,
6615 mddev->resync_max > j
6616 || kthread_should_stop());
6617 }
NeilBrownacb180b2009-04-14 16:28:34 +10006618
6619 if (kthread_should_stop())
6620 goto interrupted;
6621
NeilBrown57afd892005-06-21 17:17:13 -07006622 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006623 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006624 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006625 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 goto out;
6627 }
NeilBrown57afd892005-06-21 17:17:13 -07006628
6629 if (!skipped) { /* actual IO requested */
6630 io_sectors += sectors;
6631 atomic_add(sectors, &mddev->recovery_active);
6632 }
6633
Linus Torvalds1da177e2005-04-16 15:20:36 -07006634 j += sectors;
6635 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006636 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006637 if (last_check == 0)
6638 /* this is the earliers that rebuilt will be
6639 * visible in /proc/mdstat
6640 */
6641 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006642
6643 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644 continue;
6645
NeilBrown57afd892005-06-21 17:17:13 -07006646 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647
NeilBrowndfc70642008-05-23 13:04:39 -07006648 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006649 break;
6650
6651 repeat:
6652 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6653 /* step marks */
6654 int next = (last_mark+1) % SYNC_MARKS;
6655
6656 mddev->resync_mark = mark[next];
6657 mddev->resync_mark_cnt = mark_cnt[next];
6658 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006659 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660 last_mark = next;
6661 }
6662
6663
NeilBrownc6207272008-02-06 01:39:52 -08006664 if (kthread_should_stop())
6665 goto interrupted;
6666
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667
6668 /*
6669 * this loop exits only if either when we are slower than
6670 * the 'hard' speed limit, or the system was IO-idle for
6671 * a jiffy.
6672 * the system might be non-idle CPU-wise, but we only care
6673 * about not overloading the IO subsystem. (things like an
6674 * e2fsck being done on the RAID array should execute fast)
6675 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006676 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677 cond_resched();
6678
NeilBrown57afd892005-06-21 17:17:13 -07006679 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6680 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681
NeilBrown88202a02006-01-06 00:21:36 -08006682 if (currspeed > speed_min(mddev)) {
6683 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006684 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006685 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686 goto repeat;
6687 }
6688 }
6689 }
NeilBrown61df9d92006-10-03 01:15:57 -07006690 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691 /*
6692 * this also signals 'finished resyncing' to md_stop
6693 */
6694 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006695 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696
6697 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6698
6699 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006700 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701
NeilBrowndfc70642008-05-23 13:04:39 -07006702 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006703 mddev->curr_resync > 2) {
6704 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6705 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6706 if (mddev->curr_resync >= mddev->recovery_cp) {
6707 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006708 "md: checkpointing %s of %s.\n",
6709 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006710 mddev->recovery_cp = mddev->curr_resync;
6711 }
6712 } else
6713 mddev->recovery_cp = MaxSector;
6714 } else {
6715 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6716 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006717 rcu_read_lock();
6718 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006719 if (rdev->raid_disk >= 0 &&
6720 !test_bit(Faulty, &rdev->flags) &&
6721 !test_bit(In_sync, &rdev->flags) &&
6722 rdev->recovery_offset < mddev->curr_resync)
6723 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07006724 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726 }
NeilBrown17571282006-12-10 02:20:52 -08006727 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006730 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6731 /* We completed so min/max setting can be forgotten if used. */
6732 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6733 mddev->resync_min = 0;
6734 mddev->resync_max = MaxSector;
6735 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6736 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006738 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6739 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006740 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741 wake_up(&resync_wait);
6742 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6743 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006744 return;
6745
6746 interrupted:
6747 /*
6748 * got a signal, exit.
6749 */
6750 printk(KERN_INFO
6751 "md: md_do_sync() got signal ... exiting\n");
6752 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6753 goto out;
6754
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755}
NeilBrown29269552006-03-27 01:18:10 -08006756EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757
6758
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006759static int remove_and_add_spares(mddev_t *mddev)
6760{
6761 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006762 int spares = 0;
6763
NeilBrown97e4f422009-03-31 14:33:13 +11006764 mddev->curr_resync_completed = 0;
6765
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006766 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006767 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006768 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006769 (test_bit(Faulty, &rdev->flags) ||
6770 ! test_bit(In_sync, &rdev->flags)) &&
6771 atomic_read(&rdev->nr_pending)==0) {
6772 if (mddev->pers->hot_remove_disk(
6773 mddev, rdev->raid_disk)==0) {
6774 char nm[20];
6775 sprintf(nm,"rd%d", rdev->raid_disk);
6776 sysfs_remove_link(&mddev->kobj, nm);
6777 rdev->raid_disk = -1;
6778 }
6779 }
6780
NeilBrown4044ba52009-01-09 08:31:11 +11006781 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006782 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006783 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006784 !test_bit(In_sync, &rdev->flags) &&
6785 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006786 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006787 if (rdev->raid_disk < 0
6788 && !test_bit(Faulty, &rdev->flags)) {
6789 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006790 if (mddev->pers->
6791 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006792 char nm[20];
6793 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006794 if (sysfs_create_link(&mddev->kobj,
6795 &rdev->kobj, nm))
6796 printk(KERN_WARNING
6797 "md: cannot register "
6798 "%s for %s\n",
6799 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006800 spares++;
6801 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11006802 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006803 } else
6804 break;
6805 }
NeilBrowndfc70642008-05-23 13:04:39 -07006806 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006807 }
6808 return spares;
6809}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810/*
6811 * This routine is regularly called by all per-raid-array threads to
6812 * deal with generic issues like resync and super-block update.
6813 * Raid personalities that don't have a thread (linear/raid0) do not
6814 * need this as they never do any recovery or update the superblock.
6815 *
6816 * It does not do any resync itself, but rather "forks" off other threads
6817 * to do that as needed.
6818 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6819 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006820 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821 * and wakeups up this thread which will reap the thread and finish up.
6822 * This thread also removes any faulty devices (with nr_pending == 0).
6823 *
6824 * The overall approach is:
6825 * 1/ if the superblock needs updating, update it.
6826 * 2/ If a recovery thread is running, don't do anything else.
6827 * 3/ If recovery has finished, clean up, possibly marking spares active.
6828 * 4/ If there are any faulty devices, remove them.
6829 * 5/ If array is degraded, try to add spares devices
6830 * 6/ If array has spares or is not in-sync, start a resync thread.
6831 */
6832void md_check_recovery(mddev_t *mddev)
6833{
6834 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835
6836
NeilBrown5f404022005-06-21 17:17:16 -07006837 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11006838 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839
6840 if (mddev->ro)
6841 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006842
6843 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006844 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006845 printk(KERN_INFO "md: %s in immediate safe mode\n",
6846 mdname(mddev));
6847 mddev->safemode = 2;
6848 }
6849 flush_signals(current);
6850 }
6851
NeilBrownc89a8ee2008-08-05 15:54:13 +10006852 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6853 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006855 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006857 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006858 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006859 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6860 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861 ))
6862 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006863
NeilBrowndf5b89b2006-03-27 01:18:20 -08006864 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006865 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006866
NeilBrownc89a8ee2008-08-05 15:54:13 +10006867 if (mddev->ro) {
6868 /* Only thing we do on a ro array is remove
6869 * failed devices.
6870 */
6871 remove_and_add_spares(mddev);
6872 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6873 goto unlock;
6874 }
6875
NeilBrown31a59e32008-04-30 00:52:30 -07006876 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006877 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006878 spin_lock_irq(&mddev->write_lock);
6879 if (mddev->safemode &&
6880 !atomic_read(&mddev->writes_pending) &&
6881 !mddev->in_sync &&
6882 mddev->recovery_cp == MaxSector) {
6883 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006884 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006885 if (mddev->persistent)
6886 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6887 }
6888 if (mddev->safemode == 1)
6889 mddev->safemode = 0;
6890 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006891 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006892 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006893 }
NeilBrownfca4d842005-06-21 17:17:11 -07006894
NeilBrown850b2b42006-10-03 01:15:46 -07006895 if (mddev->flags)
6896 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006897
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006898 list_for_each_entry(rdev, &mddev->disks, same_set)
Neil Brown52664732008-06-28 08:31:44 +10006899 if (test_and_clear_bit(StateChanged, &rdev->flags))
NeilBrown3c0ee632008-10-21 13:25:28 +11006900 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown52664732008-06-28 08:31:44 +10006901
NeilBrown06d91a52005-06-21 17:17:12 -07006902
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6904 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6905 /* resync/recovery still happening */
6906 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6907 goto unlock;
6908 }
6909 if (mddev->sync_thread) {
6910 /* resync has finished, collect result */
6911 md_unregister_thread(mddev->sync_thread);
6912 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006913 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6914 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915 /* success...*/
6916 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006917 if (mddev->pers->spare_active(mddev))
6918 sysfs_notify(&mddev->kobj, NULL,
6919 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920 }
NeilBrowncea9c222009-03-31 15:15:05 +11006921 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6922 mddev->pers->finish_reshape)
6923 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07006924 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006925
6926 /* if array is no-longer degraded, then any saved_raid_disk
6927 * information must be scrapped
6928 */
6929 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006930 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07006931 rdev->saved_raid_disk = -1;
6932
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933 mddev->recovery = 0;
6934 /* flag recovery needed just to double check */
6935 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11006936 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006937 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938 goto unlock;
6939 }
Neil Brown72a23c22008-06-28 08:31:41 +10006940 /* Set RUNNING before clearing NEEDED to avoid
6941 * any transients in the value of "sync_action".
6942 */
6943 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6944 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006945 /* Clear some bits that don't mean anything, but
6946 * might be left set
6947 */
NeilBrown24dd4692005-11-08 21:39:26 -08006948 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6949 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006951 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6952 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953 /* no recovery is running.
6954 * remove any failed drives, then
6955 * add spares if possible.
6956 * Spare are also removed and re-added, to allow
6957 * the personality to fail the re-add.
6958 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006960 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10006961 if (mddev->pers->check_reshape == NULL ||
6962 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006963 /* Cannot proceed */
6964 goto unlock;
6965 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006966 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006967 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006968 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6969 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07006970 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006971 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006972 } else if (mddev->recovery_cp < MaxSector) {
6973 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006974 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006975 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6976 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006978
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006980 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6981 /* We are adding a device or devices to an array
6982 * which has the bitmap stored on all devices.
6983 * So make sure all bitmap pages get written
6984 */
6985 bitmap_write_all(mddev->bitmap);
6986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987 mddev->sync_thread = md_register_thread(md_do_sync,
6988 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006989 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990 if (!mddev->sync_thread) {
6991 printk(KERN_ERR "%s: could not start resync"
6992 " thread...\n",
6993 mdname(mddev));
6994 /* leave the spares where they are, it shouldn't hurt */
6995 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006996 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11006998 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006999 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000 }
7001 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007002 if (!mddev->sync_thread) {
7003 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7004 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7005 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007006 if (mddev->sysfs_action)
7007 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007009 mddev_unlock(mddev);
7010 }
7011}
7012
Dan Williams6bfe0b42008-04-30 00:52:32 -07007013void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7014{
NeilBrown3c0ee632008-10-21 13:25:28 +11007015 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007016 wait_event_timeout(rdev->blocked_wait,
7017 !test_bit(Blocked, &rdev->flags),
7018 msecs_to_jiffies(5000));
7019 rdev_dec_pending(rdev, mddev);
7020}
7021EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7022
Adrian Bunk75c96f82005-05-05 16:16:09 -07007023static int md_notify_reboot(struct notifier_block *this,
7024 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007025{
7026 struct list_head *tmp;
7027 mddev_t *mddev;
7028
7029 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7030
7031 printk(KERN_INFO "md: stopping all md devices.\n");
7032
NeilBrown29ac4aa2008-02-06 01:39:58 -08007033 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007034 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007035 /* Force a switch to readonly even array
7036 * appears to still be in use. Hence
7037 * the '100'.
7038 */
NeilBrownd710e132008-10-13 11:55:12 +11007039 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007040 mddev_unlock(mddev);
7041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042 /*
7043 * certain more exotic SCSI devices are known to be
7044 * volatile wrt too early system reboots. While the
7045 * right place to handle this issue is the given
7046 * driver, we do want to have a safe RAID driver ...
7047 */
7048 mdelay(1000*1);
7049 }
7050 return NOTIFY_DONE;
7051}
7052
Adrian Bunk75c96f82005-05-05 16:16:09 -07007053static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054 .notifier_call = md_notify_reboot,
7055 .next = NULL,
7056 .priority = INT_MAX, /* before any real devices */
7057};
7058
7059static void md_geninit(void)
7060{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7062
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07007063 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064}
7065
Adrian Bunk75c96f82005-05-05 16:16:09 -07007066static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007068 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069 return -1;
7070 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007071 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007072 return -1;
7073 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007074 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007075 md_probe, NULL, NULL);
7076 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007077 md_probe, NULL, NULL);
7078
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007080 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081
7082 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084}
7085
7086
7087#ifndef MODULE
7088
7089/*
7090 * Searches all registered partitions for autorun RAID arrays
7091 * at boot time.
7092 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007093
7094static LIST_HEAD(all_detected_devices);
7095struct detected_devices_node {
7096 struct list_head list;
7097 dev_t dev;
7098};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099
7100void md_autodetect_dev(dev_t dev)
7101{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007102 struct detected_devices_node *node_detected_dev;
7103
7104 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7105 if (node_detected_dev) {
7106 node_detected_dev->dev = dev;
7107 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7108 } else {
7109 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7110 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112}
7113
7114
7115static void autostart_arrays(int part)
7116{
7117 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007118 struct detected_devices_node *node_detected_dev;
7119 dev_t dev;
7120 int i_scanned, i_passed;
7121
7122 i_scanned = 0;
7123 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124
7125 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7126
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007127 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7128 i_scanned++;
7129 node_detected_dev = list_entry(all_detected_devices.next,
7130 struct detected_devices_node, list);
7131 list_del(&node_detected_dev->list);
7132 dev = node_detected_dev->dev;
7133 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007134 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 if (IS_ERR(rdev))
7136 continue;
7137
NeilBrownb2d444d2005-11-08 21:39:31 -08007138 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139 MD_BUG();
7140 continue;
7141 }
NeilBrownd0fae182008-03-04 14:29:31 -08007142 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007144 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007146
7147 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7148 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149
7150 autorun_devices(part);
7151}
7152
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007153#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154
7155static __exit void md_exit(void)
7156{
7157 mddev_t *mddev;
7158 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007159
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007160 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007161 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007163 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164 unregister_blkdev(mdp_major, "mdp");
7165 unregister_reboot_notifier(&md_notifier);
7166 unregister_sysctl_table(raid_table_header);
7167 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007168 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007170 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171 }
7172}
7173
Dan Williams685784a2007-07-09 11:56:42 -07007174subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007175module_exit(md_exit)
7176
NeilBrownf91de922005-11-08 21:39:36 -08007177static int get_ro(char *buffer, struct kernel_param *kp)
7178{
7179 return sprintf(buffer, "%d", start_readonly);
7180}
7181static int set_ro(const char *val, struct kernel_param *kp)
7182{
7183 char *e;
7184 int num = simple_strtoul(val, &e, 10);
7185 if (*val && (*e == '\0' || *e == '\n')) {
7186 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007187 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007188 }
7189 return -EINVAL;
7190}
7191
NeilBrown80ca3a42006-07-10 04:44:18 -07007192module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7193module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007194
NeilBrownefeb53c2009-01-09 08:31:10 +11007195module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007196
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197EXPORT_SYMBOL(register_md_personality);
7198EXPORT_SYMBOL(unregister_md_personality);
7199EXPORT_SYMBOL(md_error);
7200EXPORT_SYMBOL(md_done_sync);
7201EXPORT_SYMBOL(md_write_start);
7202EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203EXPORT_SYMBOL(md_register_thread);
7204EXPORT_SYMBOL(md_unregister_thread);
7205EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206EXPORT_SYMBOL(md_check_recovery);
7207MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007208MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007209MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007210MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);