blob: 69f659e46aa6204d39dc98c6fb752a8eaaf0856a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/buffer_head.h> /* for invalidate_bdev */
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080042#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110043#include <linux/hdreg.h>
44#include <linux/proc_fs.h>
45#include <linux/random.h>
46#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070047#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110048#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110049#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110050#include <linux/raid/md_p.h>
51#include <linux/raid/md_u.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110052#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110053#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#define DEBUG 0
56#define dprintk(x...) ((void)(DEBUG && printk(x)))
57
58
59#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110060static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#endif
62
NeilBrown2604b702006-01-06 00:20:36 -080063static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static DEFINE_SPINLOCK(pers_lock);
65
Adrian Bunk5e563412006-06-26 00:27:42 -070066static void md_print_devices(void);
67
Bernd Schubert90b08712008-05-23 13:04:38 -070068static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
69
Adrian Bunk5e563412006-06-26 00:27:42 -070070#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
Robert Becker1e509152009-12-14 12:49:58 +110073 * Default number of read corrections we'll attempt on an rdev
74 * before ejecting it from the array. We divide the read error
75 * count by 2 for every hour elapsed between read errors.
76 */
77#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
78/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
80 * is 1000 KB/sec, so the extra system load does not show up that much.
81 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070082 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 * subsystem is idle. There is also an 'absolute maximum' reconstruction
84 * speed limit - in case reconstruction slows down your system despite
85 * idle IO detection.
86 *
87 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080088 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 */
90
91static int sysctl_speed_limit_min = 1000;
92static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080093static inline int speed_min(mddev_t *mddev)
94{
95 return mddev->sync_speed_min ?
96 mddev->sync_speed_min : sysctl_speed_limit_min;
97}
98
99static inline int speed_max(mddev_t *mddev)
100{
101 return mddev->sync_speed_max ?
102 mddev->sync_speed_max : sysctl_speed_limit_max;
103}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105static struct ctl_table_header *raid_table_header;
106
107static ctl_table raid_table[] = {
108 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 .procname = "speed_limit_min",
110 .data = &sysctl_speed_limit_min,
111 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700112 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800113 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 },
115 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 .procname = "speed_limit_max",
117 .data = &sysctl_speed_limit_max,
118 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700119 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800120 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800122 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123};
124
125static ctl_table raid_dir_table[] = {
126 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 .procname = "raid",
128 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700129 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 .child = raid_table,
131 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800132 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133};
134
135static ctl_table raid_root_table[] = {
136 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .procname = "dev",
138 .maxlen = 0,
139 .mode = 0555,
140 .child = raid_dir_table,
141 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800142 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700145static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
NeilBrownf91de922005-11-08 21:39:36 -0800147static int start_readonly;
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/*
NeilBrownd7603b72006-01-06 00:20:30 -0800150 * We have a system wide 'event count' that is incremented
151 * on any 'interesting' event, and readers of /proc/mdstat
152 * can use 'poll' or 'select' to find out when the event
153 * count increases.
154 *
155 * Events are:
156 * start array, stop array, error, add device, remove device,
157 * start build, activate spare
158 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800159static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800160static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800161void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800162{
163 atomic_inc(&md_event_count);
164 wake_up(&md_event_waiters);
165}
NeilBrown29269552006-03-27 01:18:10 -0800166EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800167
NeilBrownc331eb02006-05-30 21:27:13 -0700168/* Alternate version that can be called from interrupts
169 * when calling sysfs_notify isn't needed.
170 */
Adrian Bunk05381952006-06-26 00:28:01 -0700171static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700172{
173 atomic_inc(&md_event_count);
174 wake_up(&md_event_waiters);
175}
176
NeilBrownd7603b72006-01-06 00:20:30 -0800177/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * Enables to iterate over all existing md arrays
179 * all_mddevs_lock protects this list.
180 */
181static LIST_HEAD(all_mddevs);
182static DEFINE_SPINLOCK(all_mddevs_lock);
183
184
185/*
186 * iterates through all used mddevs in the system.
187 * We take care to grab the all_mddevs_lock whenever navigating
188 * the list, and to always hold a refcount when unlocked.
189 * Any code which breaks out of this loop while own
190 * a reference to the current mddev and must mddev_put it.
191 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800192#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 \
194 for (({ spin_lock(&all_mddevs_lock); \
195 tmp = all_mddevs.next; \
196 mddev = NULL;}); \
197 ({ if (tmp != &all_mddevs) \
198 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
199 spin_unlock(&all_mddevs_lock); \
200 if (mddev) mddev_put(mddev); \
201 mddev = list_entry(tmp, mddev_t, all_mddevs); \
202 tmp != &all_mddevs;}); \
203 ({ spin_lock(&all_mddevs_lock); \
204 tmp = tmp->next;}) \
205 )
206
207
NeilBrown409c57f2009-03-31 14:39:39 +1100208/* Rather than calling directly into the personality make_request function,
209 * IO requests come here first so that we can check if the device is
210 * being suspended pending a reconfiguration.
211 * We hold a refcount over the call to ->make_request. By the time that
212 * call has finished, the bio has been linked into some internal structure
213 * and so is visible to ->quiesce(), so we don't need the refcount any more.
214 */
215static int md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
NeilBrown49077322010-03-25 16:20:56 +1100217 const int rw = bio_data_dir(bio);
NeilBrown409c57f2009-03-31 14:39:39 +1100218 mddev_t *mddev = q->queuedata;
219 int rv;
NeilBrown49077322010-03-25 16:20:56 +1100220 int cpu;
221
NeilBrown409c57f2009-03-31 14:39:39 +1100222 if (mddev == NULL || mddev->pers == NULL) {
223 bio_io_error(bio);
224 return 0;
225 }
226 rcu_read_lock();
NeilBrowna2826aa2009-12-14 12:49:49 +1100227 if (mddev->suspended || mddev->barrier) {
NeilBrown409c57f2009-03-31 14:39:39 +1100228 DEFINE_WAIT(__wait);
229 for (;;) {
230 prepare_to_wait(&mddev->sb_wait, &__wait,
231 TASK_UNINTERRUPTIBLE);
NeilBrowna2826aa2009-12-14 12:49:49 +1100232 if (!mddev->suspended && !mddev->barrier)
NeilBrown409c57f2009-03-31 14:39:39 +1100233 break;
234 rcu_read_unlock();
235 schedule();
236 rcu_read_lock();
237 }
238 finish_wait(&mddev->sb_wait, &__wait);
239 }
240 atomic_inc(&mddev->active_io);
241 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100242
NeilBrown21a52c62010-04-01 15:02:13 +1100243 rv = mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100244
245 cpu = part_stat_lock();
246 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
247 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
248 bio_sectors(bio));
249 part_stat_unlock();
250
NeilBrown409c57f2009-03-31 14:39:39 +1100251 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
252 wake_up(&mddev->sb_wait);
253
254 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
NeilBrown9e35b992010-04-06 14:23:02 +1000257/* mddev_suspend makes sure no new requests are submitted
258 * to the device, and that any requests that have been submitted
259 * are completely handled.
260 * Once ->stop is called and completes, the module will be completely
261 * unused.
262 */
NeilBrown409c57f2009-03-31 14:39:39 +1100263static void mddev_suspend(mddev_t *mddev)
264{
265 BUG_ON(mddev->suspended);
266 mddev->suspended = 1;
267 synchronize_rcu();
268 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
269 mddev->pers->quiesce(mddev, 1);
NeilBrown409c57f2009-03-31 14:39:39 +1100270}
271
272static void mddev_resume(mddev_t *mddev)
273{
274 mddev->suspended = 0;
275 wake_up(&mddev->sb_wait);
276 mddev->pers->quiesce(mddev, 0);
277}
278
NeilBrown3fa841d2009-09-23 18:10:29 +1000279int mddev_congested(mddev_t *mddev, int bits)
280{
NeilBrowna2826aa2009-12-14 12:49:49 +1100281 if (mddev->barrier)
282 return 1;
NeilBrown3fa841d2009-09-23 18:10:29 +1000283 return mddev->suspended;
284}
285EXPORT_SYMBOL(mddev_congested);
286
NeilBrowna2826aa2009-12-14 12:49:49 +1100287/*
288 * Generic barrier handling for md
289 */
290
291#define POST_REQUEST_BARRIER ((void*)1)
292
293static void md_end_barrier(struct bio *bio, int err)
294{
295 mdk_rdev_t *rdev = bio->bi_private;
296 mddev_t *mddev = rdev->mddev;
297 if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER)
298 set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags);
299
300 rdev_dec_pending(rdev, mddev);
301
302 if (atomic_dec_and_test(&mddev->flush_pending)) {
303 if (mddev->barrier == POST_REQUEST_BARRIER) {
304 /* This was a post-request barrier */
305 mddev->barrier = NULL;
306 wake_up(&mddev->sb_wait);
307 } else
308 /* The pre-request barrier has finished */
309 schedule_work(&mddev->barrier_work);
310 }
311 bio_put(bio);
312}
313
314static void submit_barriers(mddev_t *mddev)
315{
316 mdk_rdev_t *rdev;
317
318 rcu_read_lock();
319 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
320 if (rdev->raid_disk >= 0 &&
321 !test_bit(Faulty, &rdev->flags)) {
322 /* Take two references, one is dropped
323 * when request finishes, one after
324 * we reclaim rcu_read_lock
325 */
326 struct bio *bi;
327 atomic_inc(&rdev->nr_pending);
328 atomic_inc(&rdev->nr_pending);
329 rcu_read_unlock();
330 bi = bio_alloc(GFP_KERNEL, 0);
331 bi->bi_end_io = md_end_barrier;
332 bi->bi_private = rdev;
333 bi->bi_bdev = rdev->bdev;
334 atomic_inc(&mddev->flush_pending);
335 submit_bio(WRITE_BARRIER, bi);
336 rcu_read_lock();
337 rdev_dec_pending(rdev, mddev);
338 }
339 rcu_read_unlock();
340}
341
342static void md_submit_barrier(struct work_struct *ws)
343{
344 mddev_t *mddev = container_of(ws, mddev_t, barrier_work);
345 struct bio *bio = mddev->barrier;
346
347 atomic_set(&mddev->flush_pending, 1);
348
349 if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags))
350 bio_endio(bio, -EOPNOTSUPP);
351 else if (bio->bi_size == 0)
352 /* an empty barrier - all done */
353 bio_endio(bio, 0);
354 else {
355 bio->bi_rw &= ~(1<<BIO_RW_BARRIER);
NeilBrown21a52c62010-04-01 15:02:13 +1100356 if (mddev->pers->make_request(mddev, bio))
NeilBrowna2826aa2009-12-14 12:49:49 +1100357 generic_make_request(bio);
358 mddev->barrier = POST_REQUEST_BARRIER;
359 submit_barriers(mddev);
360 }
361 if (atomic_dec_and_test(&mddev->flush_pending)) {
362 mddev->barrier = NULL;
363 wake_up(&mddev->sb_wait);
364 }
365}
366
367void md_barrier_request(mddev_t *mddev, struct bio *bio)
368{
369 spin_lock_irq(&mddev->write_lock);
370 wait_event_lock_irq(mddev->sb_wait,
371 !mddev->barrier,
372 mddev->write_lock, /*nothing*/);
373 mddev->barrier = bio;
374 spin_unlock_irq(&mddev->write_lock);
375
376 atomic_set(&mddev->flush_pending, 1);
377 INIT_WORK(&mddev->barrier_work, md_submit_barrier);
378
379 submit_barriers(mddev);
380
381 if (atomic_dec_and_test(&mddev->flush_pending))
382 schedule_work(&mddev->barrier_work);
383}
384EXPORT_SYMBOL(md_barrier_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386static inline mddev_t *mddev_get(mddev_t *mddev)
387{
388 atomic_inc(&mddev->active);
389 return mddev;
390}
391
Dan Williams5fd3a172009-03-04 00:57:25 -0700392static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394static void mddev_put(mddev_t *mddev)
395{
396 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
397 return;
NeilBrownd3374822009-01-09 08:31:10 +1100398 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100399 mddev->ctime == 0 && !mddev->hold_active) {
400 /* Array is not configured at all, and not held active,
401 * so destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 list_del(&mddev->all_mddevs);
NeilBrownd3374822009-01-09 08:31:10 +1100403 if (mddev->gendisk) {
404 /* we did a probe so need to clean up.
405 * Call schedule_work inside the spinlock
406 * so that flush_scheduled_work() after
407 * mddev_find will succeed in waiting for the
408 * work to be done.
409 */
410 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
411 schedule_work(&mddev->del_work);
412 } else
413 kfree(mddev);
414 }
415 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
NeilBrownfafd7fb2010-04-01 15:55:30 +1100418static void mddev_init(mddev_t *mddev)
419{
420 mutex_init(&mddev->open_mutex);
421 mutex_init(&mddev->reconfig_mutex);
422 mutex_init(&mddev->bitmap_info.mutex);
423 INIT_LIST_HEAD(&mddev->disks);
424 INIT_LIST_HEAD(&mddev->all_mddevs);
425 init_timer(&mddev->safemode_timer);
426 atomic_set(&mddev->active, 1);
427 atomic_set(&mddev->openers, 0);
428 atomic_set(&mddev->active_io, 0);
429 spin_lock_init(&mddev->write_lock);
430 atomic_set(&mddev->flush_pending, 0);
431 init_waitqueue_head(&mddev->sb_wait);
432 init_waitqueue_head(&mddev->recovery_wait);
433 mddev->reshape_position = MaxSector;
434 mddev->resync_min = 0;
435 mddev->resync_max = MaxSector;
436 mddev->level = LEVEL_NONE;
437}
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439static mddev_t * mddev_find(dev_t unit)
440{
441 mddev_t *mddev, *new = NULL;
442
443 retry:
444 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
NeilBrownefeb53c2009-01-09 08:31:10 +1100446 if (unit) {
447 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
448 if (mddev->unit == unit) {
449 mddev_get(mddev);
450 spin_unlock(&all_mddevs_lock);
451 kfree(new);
452 return mddev;
453 }
454
455 if (new) {
456 list_add(&new->all_mddevs, &all_mddevs);
457 spin_unlock(&all_mddevs_lock);
458 new->hold_active = UNTIL_IOCTL;
459 return new;
460 }
461 } else if (new) {
462 /* find an unused unit number */
463 static int next_minor = 512;
464 int start = next_minor;
465 int is_free = 0;
466 int dev = 0;
467 while (!is_free) {
468 dev = MKDEV(MD_MAJOR, next_minor);
469 next_minor++;
470 if (next_minor > MINORMASK)
471 next_minor = 0;
472 if (next_minor == start) {
473 /* Oh dear, all in use. */
474 spin_unlock(&all_mddevs_lock);
475 kfree(new);
476 return NULL;
477 }
478
479 is_free = 1;
480 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
481 if (mddev->unit == dev) {
482 is_free = 0;
483 break;
484 }
485 }
486 new->unit = dev;
487 new->md_minor = MINOR(dev);
488 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 list_add(&new->all_mddevs, &all_mddevs);
490 spin_unlock(&all_mddevs_lock);
491 return new;
492 }
493 spin_unlock(&all_mddevs_lock);
494
NeilBrown9ffae0c2006-01-06 00:20:32 -0800495 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (!new)
497 return NULL;
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 new->unit = unit;
500 if (MAJOR(unit) == MD_MAJOR)
501 new->md_minor = MINOR(unit);
502 else
503 new->md_minor = MINOR(unit) >> MdpMinorShift;
504
NeilBrownfafd7fb2010-04-01 15:55:30 +1100505 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 goto retry;
508}
509
510static inline int mddev_lock(mddev_t * mddev)
511{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800512 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Dan Williamsb522adc2009-03-31 15:00:31 +1100515static inline int mddev_is_locked(mddev_t *mddev)
516{
517 return mutex_is_locked(&mddev->reconfig_mutex);
518}
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520static inline int mddev_trylock(mddev_t * mddev)
521{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800522 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
NeilBrownb6eb1272010-04-15 10:13:47 +1000525static struct attribute_group md_redundancy_group;
526
NeilBrowna64c8762010-04-14 17:15:37 +1000527static void mddev_unlock(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
NeilBrowna64c8762010-04-14 17:15:37 +1000529 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000530 /* These cannot be removed under reconfig_mutex as
531 * an access to the files will try to take reconfig_mutex
532 * while holding the file unremovable, which leads to
533 * a deadlock.
534 * So hold open_mutex instead - we are allowed to take
535 * it while holding reconfig_mutex, and md_run can
536 * use it to wait for the remove to complete.
537 */
NeilBrowna64c8762010-04-14 17:15:37 +1000538 struct attribute_group *to_remove = mddev->to_remove;
539 mddev->to_remove = NULL;
NeilBrownb6eb1272010-04-15 10:13:47 +1000540 mutex_lock(&mddev->open_mutex);
541 mutex_unlock(&mddev->reconfig_mutex);
542
NeilBrowna64c8762010-04-14 17:15:37 +1000543 if (to_remove != &md_redundancy_group)
544 sysfs_remove_group(&mddev->kobj, to_remove);
545 if (mddev->pers == NULL ||
546 mddev->pers->sync_request == NULL) {
547 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
548 if (mddev->sysfs_action)
549 sysfs_put(mddev->sysfs_action);
550 mddev->sysfs_action = NULL;
551 }
NeilBrownb6eb1272010-04-15 10:13:47 +1000552 mutex_unlock(&mddev->open_mutex);
553 } else
554 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
NeilBrown005eca52005-08-22 13:11:08 -0700556 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
NeilBrown2989ddb2006-01-06 00:20:43 -0800559static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100561 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100563 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (rdev->desc_nr == nr)
565 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return NULL;
568}
569
570static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
571{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 mdk_rdev_t *rdev;
573
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100574 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (rdev->bdev->bd_dev == dev)
576 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return NULL;
579}
580
NeilBrownd9d166c2006-01-06 00:20:51 -0800581static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800582{
583 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800584 list_for_each_entry(pers, &pers_list, list) {
585 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800586 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800587 if (strcmp(pers->name, clevel)==0)
588 return pers;
589 }
NeilBrown2604b702006-01-06 00:20:36 -0800590 return NULL;
591}
592
Andre Nollb73df2d2008-07-11 22:02:23 +1000593/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700594static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Andre Nollb73df2d2008-07-11 22:02:23 +1000596 sector_t num_sectors = bdev->bd_inode->i_size / 512;
597 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600static int alloc_disk_sb(mdk_rdev_t * rdev)
601{
602 if (rdev->sb_page)
603 MD_BUG();
604
605 rdev->sb_page = alloc_page(GFP_KERNEL);
606 if (!rdev->sb_page) {
607 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000608 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610
611 return 0;
612}
613
614static void free_disk_sb(mdk_rdev_t * rdev)
615{
616 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800617 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 rdev->sb_loaded = 0;
619 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000620 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100621 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623}
624
625
NeilBrown6712ecf2007-09-27 12:47:43 +0200626static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700627{
628 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800629 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700630
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700631 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
632 printk("md: super_written gets error=%d, uptodate=%d\n",
633 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
634 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800635 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700636 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700637
NeilBrowna9701a32005-11-08 21:39:34 -0800638 if (atomic_dec_and_test(&mddev->pending_writes))
639 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700640 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700641}
642
NeilBrown6712ecf2007-09-27 12:47:43 +0200643static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800644{
645 struct bio *bio2 = bio->bi_private;
646 mdk_rdev_t *rdev = bio2->bi_private;
647 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800648
649 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
650 error == -EOPNOTSUPP) {
651 unsigned long flags;
652 /* barriers don't appear to be supported :-( */
653 set_bit(BarriersNotsupp, &rdev->flags);
654 mddev->barriers_work = 0;
655 spin_lock_irqsave(&mddev->write_lock, flags);
656 bio2->bi_next = mddev->biolist;
657 mddev->biolist = bio2;
658 spin_unlock_irqrestore(&mddev->write_lock, flags);
659 wake_up(&mddev->sb_wait);
660 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200661 } else {
662 bio_put(bio2);
663 bio->bi_private = rdev;
664 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800665 }
NeilBrowna9701a32005-11-08 21:39:34 -0800666}
667
NeilBrown7bfa19f2005-06-21 17:17:28 -0700668void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
669 sector_t sector, int size, struct page *page)
670{
671 /* write first size bytes of page to sector of rdev
672 * Increment mddev->pending_writes before returning
673 * and decrement it on completion, waking up sb_wait
674 * if zero is reached.
675 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800676 *
677 * As we might need to resubmit the request if BIO_RW_BARRIER
678 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700679 */
680 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Jens Axboe93dbb392009-02-16 10:25:40 +0100681 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700682
683 bio->bi_bdev = rdev->bdev;
684 bio->bi_sector = sector;
685 bio_add_page(bio, page, size, 0);
686 bio->bi_private = rdev;
687 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800688 bio->bi_rw = rw;
689
NeilBrown7bfa19f2005-06-21 17:17:28 -0700690 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800691 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
692 struct bio *rbio;
693 rw |= (1<<BIO_RW_BARRIER);
694 rbio = bio_clone(bio, GFP_NOIO);
695 rbio->bi_private = bio;
696 rbio->bi_end_io = super_written_barrier;
697 submit_bio(rw, rbio);
698 } else
699 submit_bio(rw, bio);
700}
701
702void md_super_wait(mddev_t *mddev)
703{
704 /* wait for all superblock writes that were scheduled to complete.
705 * if any had to be retried (due to BARRIER problems), retry them
706 */
707 DEFINE_WAIT(wq);
708 for(;;) {
709 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
710 if (atomic_read(&mddev->pending_writes)==0)
711 break;
712 while (mddev->biolist) {
713 struct bio *bio;
714 spin_lock_irq(&mddev->write_lock);
715 bio = mddev->biolist;
716 mddev->biolist = bio->bi_next ;
717 bio->bi_next = NULL;
718 spin_unlock_irq(&mddev->write_lock);
719 submit_bio(bio->bi_rw, bio);
720 }
721 schedule();
722 }
723 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700724}
725
NeilBrown6712ecf2007-09-27 12:47:43 +0200726static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
NeilBrowna654b9d82005-06-21 17:17:27 -0700731int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 struct page *page, int rw)
733{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700734 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 struct completion event;
736 int ret;
737
Jens Axboe93dbb392009-02-16 10:25:40 +0100738 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 bio->bi_bdev = bdev;
741 bio->bi_sector = sector;
742 bio_add_page(bio, page, size, 0);
743 init_completion(&event);
744 bio->bi_private = &event;
745 bio->bi_end_io = bi_complete;
746 submit_bio(rw, bio);
747 wait_for_completion(&event);
748
749 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
750 bio_put(bio);
751 return ret;
752}
NeilBrowna8745db2006-01-06 00:20:34 -0800753EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
NeilBrown0002b272005-09-09 16:23:53 -0700755static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 char b[BDEVNAME_SIZE];
758 if (!rdev->sb_page) {
759 MD_BUG();
760 return -EINVAL;
761 }
762 if (rdev->sb_loaded)
763 return 0;
764
765
Andre Noll0f420352008-07-11 22:02:23 +1000766 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 goto fail;
768 rdev->sb_loaded = 1;
769 return 0;
770
771fail:
772 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
773 bdevname(rdev->bdev,b));
774 return -EINVAL;
775}
776
777static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
778{
Andre Noll05710462008-07-11 22:02:20 +1000779 return sb1->set_uuid0 == sb2->set_uuid0 &&
780 sb1->set_uuid1 == sb2->set_uuid1 &&
781 sb1->set_uuid2 == sb2->set_uuid2 &&
782 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783}
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
786{
787 int ret;
788 mdp_super_t *tmp1, *tmp2;
789
790 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
791 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
792
793 if (!tmp1 || !tmp2) {
794 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100795 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 goto abort;
797 }
798
799 *tmp1 = *sb1;
800 *tmp2 = *sb2;
801
802 /*
803 * nr_disks is not constant
804 */
805 tmp1->nr_disks = 0;
806 tmp2->nr_disks = 0;
807
Andre Nollce0c8e02008-07-11 22:02:20 +1000808 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700810 kfree(tmp1);
811 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return ret;
813}
814
NeilBrown4d167f02007-05-09 02:35:37 -0700815
816static u32 md_csum_fold(u32 csum)
817{
818 csum = (csum & 0xffff) + (csum >> 16);
819 return (csum & 0xffff) + (csum >> 16);
820}
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822static unsigned int calc_sb_csum(mdp_super_t * sb)
823{
NeilBrown4d167f02007-05-09 02:35:37 -0700824 u64 newcsum = 0;
825 u32 *sb32 = (u32*)sb;
826 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 unsigned int disk_csum, csum;
828
829 disk_csum = sb->sb_csum;
830 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700831
832 for (i = 0; i < MD_SB_BYTES/4 ; i++)
833 newcsum += sb32[i];
834 csum = (newcsum & 0xffffffff) + (newcsum>>32);
835
836
837#ifdef CONFIG_ALPHA
838 /* This used to use csum_partial, which was wrong for several
839 * reasons including that different results are returned on
840 * different architectures. It isn't critical that we get exactly
841 * the same return value as before (we always csum_fold before
842 * testing, and that removes any differences). However as we
843 * know that csum_partial always returned a 16bit value on
844 * alphas, do a fold to maximise conformity to previous behaviour.
845 */
846 sb->sb_csum = md_csum_fold(disk_csum);
847#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700849#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return csum;
851}
852
853
854/*
855 * Handle superblock details.
856 * We want to be able to handle multiple superblock formats
857 * so we have a common interface to them all, and an array of
858 * different handlers.
859 * We rely on user-space to write the initial superblock, and support
860 * reading and updating of superblocks.
861 * Interface methods are:
862 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
863 * loads and validates a superblock on dev.
864 * if refdev != NULL, compare superblocks on both devices
865 * Return:
866 * 0 - dev has a superblock that is compatible with refdev
867 * 1 - dev has a superblock that is compatible and newer than refdev
868 * so dev should be used as the refdev in future
869 * -EINVAL superblock incompatible or invalid
870 * -othererror e.g. -EIO
871 *
872 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
873 * Verify that dev is acceptable into mddev.
874 * The first time, mddev->raid_disks will be 0, and data from
875 * dev should be merged in. Subsequent calls check that dev
876 * is new enough. Return 0 or -EINVAL
877 *
878 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
879 * Update the superblock for rdev with data in mddev
880 * This does not write to disc.
881 *
882 */
883
884struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000885 char *name;
886 struct module *owner;
887 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
888 int minor_version);
889 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
890 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
891 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000892 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893};
894
895/*
Andre Noll0894cc32009-06-18 08:49:23 +1000896 * Check that the given mddev has no bitmap.
897 *
898 * This function is called from the run method of all personalities that do not
899 * support bitmaps. It prints an error message and returns non-zero if mddev
900 * has a bitmap. Otherwise, it returns 0.
901 *
902 */
903int md_check_no_bitmap(mddev_t *mddev)
904{
NeilBrownc3d97142009-12-14 12:49:52 +1100905 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000906 return 0;
907 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
908 mdname(mddev), mddev->pers->name);
909 return 1;
910}
911EXPORT_SYMBOL(md_check_no_bitmap);
912
913/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 * load_super for 0.90.0
915 */
916static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
917{
918 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
919 mdp_super_t *sb;
920 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 /*
Andre Noll0f420352008-07-11 22:02:23 +1000923 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 * it's at the end of the disk.
925 *
926 * It also happens to be a multiple of 4Kb.
927 */
Andre Noll0f420352008-07-11 22:02:23 +1000928 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
NeilBrown0002b272005-09-09 16:23:53 -0700930 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (ret) return ret;
932
933 ret = -EINVAL;
934
935 bdevname(rdev->bdev, b);
936 sb = (mdp_super_t*)page_address(rdev->sb_page);
937
938 if (sb->md_magic != MD_SB_MAGIC) {
939 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
940 b);
941 goto abort;
942 }
943
944 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800945 sb->minor_version < 90 ||
946 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
948 sb->major_version, sb->minor_version,
949 b);
950 goto abort;
951 }
952
953 if (sb->raid_disks <= 0)
954 goto abort;
955
NeilBrown4d167f02007-05-09 02:35:37 -0700956 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
958 b);
959 goto abort;
960 }
961
962 rdev->preferred_minor = sb->md_minor;
963 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700964 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 if (sb->level == LEVEL_MULTIPATH)
967 rdev->desc_nr = -1;
968 else
969 rdev->desc_nr = sb->this_disk.number;
970
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700971 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700973 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 __u64 ev1, ev2;
975 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
976 if (!uuid_equal(refsb, sb)) {
977 printk(KERN_WARNING "md: %s has different UUID to %s\n",
978 b, bdevname(refdev->bdev,b2));
979 goto abort;
980 }
981 if (!sb_equal(refsb, sb)) {
982 printk(KERN_WARNING "md: %s has same UUID"
983 " but different superblock to %s\n",
984 b, bdevname(refdev->bdev, b2));
985 goto abort;
986 }
987 ev1 = md_event(sb);
988 ev2 = md_event(refsb);
989 if (ev1 > ev2)
990 ret = 1;
991 else
992 ret = 0;
993 }
NeilBrown8190e752009-06-18 08:48:58 +1000994 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Andre Nolldd8ac332009-03-31 14:33:13 +1100996 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -0800997 /* "this cannot possibly happen" ... */
998 ret = -EINVAL;
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 abort:
1001 return ret;
1002}
1003
1004/*
1005 * validate_super for 0.90.0
1006 */
1007static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1008{
1009 mdp_disk_t *desc;
1010 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001011 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
NeilBrown41158c72005-06-21 17:17:25 -07001013 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001014 clear_bit(Faulty, &rdev->flags);
1015 clear_bit(In_sync, &rdev->flags);
1016 clear_bit(WriteMostly, &rdev->flags);
1017 clear_bit(BarriersNotsupp, &rdev->flags);
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (mddev->raid_disks == 0) {
1020 mddev->major_version = 0;
1021 mddev->minor_version = sb->minor_version;
1022 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001023 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001024 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 mddev->ctime = sb->ctime;
1026 mddev->utime = sb->utime;
1027 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001028 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 mddev->layout = sb->layout;
1030 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +11001031 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001032 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001033 mddev->bitmap_info.offset = 0;
1034 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
NeilBrownf6705572006-03-27 01:18:11 -08001036 if (mddev->minor_version >= 91) {
1037 mddev->reshape_position = sb->reshape_position;
1038 mddev->delta_disks = sb->delta_disks;
1039 mddev->new_level = sb->new_level;
1040 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001041 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001042 } else {
1043 mddev->reshape_position = MaxSector;
1044 mddev->delta_disks = 0;
1045 mddev->new_level = mddev->level;
1046 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001047 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001048 }
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (sb->state & (1<<MD_SB_CLEAN))
1051 mddev->recovery_cp = MaxSector;
1052 else {
1053 if (sb->events_hi == sb->cp_events_hi &&
1054 sb->events_lo == sb->cp_events_lo) {
1055 mddev->recovery_cp = sb->recovery_cp;
1056 } else
1057 mddev->recovery_cp = 0;
1058 }
1059
1060 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1061 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1062 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1063 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1064
1065 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001066
1067 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001068 mddev->bitmap_info.file == NULL)
1069 mddev->bitmap_info.offset =
1070 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001071
NeilBrown41158c72005-06-21 17:17:25 -07001072 } else if (mddev->pers == NULL) {
1073 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 ++ev1;
1075 if (ev1 < mddev->events)
1076 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001077 } else if (mddev->bitmap) {
1078 /* if adding to array with a bitmap, then we can accept an
1079 * older device ... but not too old.
1080 */
NeilBrown41158c72005-06-21 17:17:25 -07001081 if (ev1 < mddev->bitmap->events_cleared)
1082 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001083 } else {
1084 if (ev1 < mddev->events)
1085 /* just a hot-add of a new device, leave raid_disk at -1 */
1086 return 0;
1087 }
NeilBrown41158c72005-06-21 17:17:25 -07001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 desc = sb->disks + rdev->desc_nr;
1091
1092 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001093 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001094 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1095 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001096 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001098 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1099 /* active but not in sync implies recovery up to
1100 * reshape position. We don't know exactly where
1101 * that is, so set to zero for now */
1102 if (mddev->minor_version >= 91) {
1103 rdev->recovery_offset = 0;
1104 rdev->raid_disk = desc->raid_disk;
1105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001107 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1108 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001109 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001110 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return 0;
1112}
1113
1114/*
1115 * sync_super for 0.90.0
1116 */
1117static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1118{
1119 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 mdk_rdev_t *rdev2;
1121 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 /* make rdev->sb match mddev data..
1125 *
1126 * 1/ zero out disks
1127 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1128 * 3/ any empty disks < next_spare become removed
1129 *
1130 * disks[0] gets initialised to REMOVED because
1131 * we cannot be sure from other fields if it has
1132 * been initialised or not.
1133 */
1134 int i;
1135 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1136
NeilBrown61181562005-09-09 16:24:02 -07001137 rdev->sb_size = MD_SB_BYTES;
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 sb = (mdp_super_t*)page_address(rdev->sb_page);
1140
1141 memset(sb, 0, sizeof(*sb));
1142
1143 sb->md_magic = MD_SB_MAGIC;
1144 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 sb->patch_version = mddev->patch_version;
1146 sb->gvalid_words = 0; /* ignored */
1147 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1148 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1149 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1150 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1151
1152 sb->ctime = mddev->ctime;
1153 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001154 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 sb->raid_disks = mddev->raid_disks;
1156 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001157 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 sb->utime = mddev->utime;
1159 sb->state = 0;
1160 sb->events_hi = (mddev->events>>32);
1161 sb->events_lo = (u32)mddev->events;
1162
NeilBrownf6705572006-03-27 01:18:11 -08001163 if (mddev->reshape_position == MaxSector)
1164 sb->minor_version = 90;
1165 else {
1166 sb->minor_version = 91;
1167 sb->reshape_position = mddev->reshape_position;
1168 sb->new_level = mddev->new_level;
1169 sb->delta_disks = mddev->delta_disks;
1170 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001171 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001172 }
1173 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (mddev->in_sync)
1175 {
1176 sb->recovery_cp = mddev->recovery_cp;
1177 sb->cp_events_hi = (mddev->events>>32);
1178 sb->cp_events_lo = (u32)mddev->events;
1179 if (mddev->recovery_cp == MaxSector)
1180 sb->state = (1<< MD_SB_CLEAN);
1181 } else
1182 sb->recovery_cp = 0;
1183
1184 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001185 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
NeilBrownc3d97142009-12-14 12:49:52 +11001187 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001188 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001191 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001193 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001194 int is_active = test_bit(In_sync, &rdev2->flags);
1195
1196 if (rdev2->raid_disk >= 0 &&
1197 sb->minor_version >= 91)
1198 /* we have nowhere to store the recovery_offset,
1199 * but if it is not below the reshape_position,
1200 * we can piggy-back on that.
1201 */
1202 is_active = 1;
1203 if (rdev2->raid_disk < 0 ||
1204 test_bit(Faulty, &rdev2->flags))
1205 is_active = 0;
1206 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001207 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001209 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001210 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 d = &sb->disks[rdev2->desc_nr];
1212 nr_disks++;
1213 d->number = rdev2->desc_nr;
1214 d->major = MAJOR(rdev2->bdev->bd_dev);
1215 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001216 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 d->raid_disk = rdev2->raid_disk;
1218 else
1219 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001220 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001222 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001224 if (test_bit(In_sync, &rdev2->flags))
1225 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 active++;
1227 working++;
1228 } else {
1229 d->state = 0;
1230 spare++;
1231 working++;
1232 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001233 if (test_bit(WriteMostly, &rdev2->flags))
1234 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 /* now set the "removed" and "faulty" bits on any missing devices */
1237 for (i=0 ; i < mddev->raid_disks ; i++) {
1238 mdp_disk_t *d = &sb->disks[i];
1239 if (d->state == 0 && d->number == 0) {
1240 d->number = i;
1241 d->raid_disk = i;
1242 d->state = (1<<MD_DISK_REMOVED);
1243 d->state |= (1<<MD_DISK_FAULTY);
1244 failed++;
1245 }
1246 }
1247 sb->nr_disks = nr_disks;
1248 sb->active_disks = active;
1249 sb->working_disks = working;
1250 sb->failed_disks = failed;
1251 sb->spare_disks = spare;
1252
1253 sb->this_disk = sb->disks[rdev->desc_nr];
1254 sb->sb_csum = calc_sb_csum(sb);
1255}
1256
1257/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001258 * rdev_size_change for 0.90.0
1259 */
1260static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001261super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001262{
Andre Noll58c0fed2009-03-31 14:33:13 +11001263 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001264 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001265 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001266 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001267 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001268 if (!num_sectors || num_sectors > rdev->sb_start)
1269 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001270 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001271 rdev->sb_page);
1272 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001273 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001274}
1275
1276
1277/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 * version 1 superblock
1279 */
1280
NeilBrown1c05b4b2006-10-21 10:24:08 -07001281static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001283 __le32 disk_csum;
1284 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 unsigned long long newcsum;
1286 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001287 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 int i;
1289
1290 disk_csum = sb->sb_csum;
1291 sb->sb_csum = 0;
1292 newcsum = 0;
1293 for (i=0; size>=4; size -= 4 )
1294 newcsum += le32_to_cpu(*isuper++);
1295
1296 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001297 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1300 sb->sb_csum = disk_csum;
1301 return cpu_to_le32(csum);
1302}
1303
1304static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1305{
1306 struct mdp_superblock_1 *sb;
1307 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001308 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001310 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 /*
Andre Noll0f420352008-07-11 22:02:23 +10001313 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 * It is always aligned to a 4K boundary and
1315 * depeding on minor_version, it can be:
1316 * 0: At least 8K, but less than 12K, from end of device
1317 * 1: At start of device
1318 * 2: 4K from start of device.
1319 */
1320 switch(minor_version) {
1321 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001322 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1323 sb_start -= 8*2;
1324 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 break;
1326 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001327 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 break;
1329 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001330 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 break;
1332 default:
1333 return -EINVAL;
1334 }
Andre Noll0f420352008-07-11 22:02:23 +10001335 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
NeilBrown0002b272005-09-09 16:23:53 -07001337 /* superblock is rarely larger than 1K, but it can be larger,
1338 * and it is safe to read 4k, so we do that
1339 */
1340 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (ret) return ret;
1342
1343
1344 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1345
1346 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1347 sb->major_version != cpu_to_le32(1) ||
1348 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001349 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001350 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return -EINVAL;
1352
1353 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1354 printk("md: invalid superblock checksum on %s\n",
1355 bdevname(rdev->bdev,b));
1356 return -EINVAL;
1357 }
1358 if (le64_to_cpu(sb->data_size) < 10) {
1359 printk("md: data_size too small on %s\n",
1360 bdevname(rdev->bdev,b));
1361 return -EINVAL;
1362 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 rdev->preferred_minor = 0xffff;
1365 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001366 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
NeilBrown0002b272005-09-09 16:23:53 -07001368 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001369 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001370 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001371 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1372
1373 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001374 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001375 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001376
NeilBrown31b65a02006-07-10 04:44:14 -07001377 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1378 rdev->desc_nr = -1;
1379 else
1380 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1381
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001382 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001383 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001384 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 __u64 ev1, ev2;
1386 struct mdp_superblock_1 *refsb =
1387 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1388
1389 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1390 sb->level != refsb->level ||
1391 sb->layout != refsb->layout ||
1392 sb->chunksize != refsb->chunksize) {
1393 printk(KERN_WARNING "md: %s has strangely different"
1394 " superblock to %s\n",
1395 bdevname(rdev->bdev,b),
1396 bdevname(refdev->bdev,b2));
1397 return -EINVAL;
1398 }
1399 ev1 = le64_to_cpu(sb->events);
1400 ev2 = le64_to_cpu(refsb->events);
1401
1402 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001403 ret = 1;
1404 else
1405 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
NeilBrowna1801f82008-03-04 14:29:31 -08001407 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001408 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1409 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001411 rdev->sectors = rdev->sb_start;
1412 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001414 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001415 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001416 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001417 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
1420static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1421{
1422 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001423 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
NeilBrown41158c72005-06-21 17:17:25 -07001425 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001426 clear_bit(Faulty, &rdev->flags);
1427 clear_bit(In_sync, &rdev->flags);
1428 clear_bit(WriteMostly, &rdev->flags);
1429 clear_bit(BarriersNotsupp, &rdev->flags);
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if (mddev->raid_disks == 0) {
1432 mddev->major_version = 1;
1433 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001434 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001435 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1437 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1438 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001439 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 mddev->layout = le32_to_cpu(sb->layout);
1441 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001442 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001443 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001444 mddev->bitmap_info.offset = 0;
1445 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1448 memcpy(mddev->uuid, sb->set_uuid, 16);
1449
1450 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001451
NeilBrown71c08052005-09-09 16:23:51 -07001452 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001453 mddev->bitmap_info.file == NULL )
1454 mddev->bitmap_info.offset =
1455 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001456
NeilBrownf6705572006-03-27 01:18:11 -08001457 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1458 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1459 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1460 mddev->new_level = le32_to_cpu(sb->new_level);
1461 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001462 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001463 } else {
1464 mddev->reshape_position = MaxSector;
1465 mddev->delta_disks = 0;
1466 mddev->new_level = mddev->level;
1467 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001468 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001469 }
1470
NeilBrown41158c72005-06-21 17:17:25 -07001471 } else if (mddev->pers == NULL) {
1472 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 ++ev1;
1474 if (ev1 < mddev->events)
1475 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001476 } else if (mddev->bitmap) {
1477 /* If adding to array with a bitmap, then we can accept an
1478 * older device, but not too old.
1479 */
NeilBrown41158c72005-06-21 17:17:25 -07001480 if (ev1 < mddev->bitmap->events_cleared)
1481 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001482 } else {
1483 if (ev1 < mddev->events)
1484 /* just a hot-add of a new device, leave raid_disk at -1 */
1485 return 0;
1486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 if (mddev->level != LEVEL_MULTIPATH) {
1488 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001489 if (rdev->desc_nr < 0 ||
1490 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1491 role = 0xffff;
1492 rdev->desc_nr = -1;
1493 } else
1494 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 switch(role) {
1496 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 break;
1498 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001499 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 break;
1501 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001502 if ((le32_to_cpu(sb->feature_map) &
1503 MD_FEATURE_RECOVERY_OFFSET))
1504 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1505 else
1506 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 rdev->raid_disk = role;
1508 break;
1509 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001510 if (sb->devflags & WriteMostly1)
1511 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001512 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001513 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return 0;
1516}
1517
1518static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1519{
1520 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 mdk_rdev_t *rdev2;
1522 int max_dev, i;
1523 /* make rdev->sb match mddev and rdev data. */
1524
1525 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1526
1527 sb->feature_map = 0;
1528 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001529 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 memset(sb->pad1, 0, sizeof(sb->pad1));
1531 memset(sb->pad2, 0, sizeof(sb->pad2));
1532 memset(sb->pad3, 0, sizeof(sb->pad3));
1533
1534 sb->utime = cpu_to_le64((__u64)mddev->utime);
1535 sb->events = cpu_to_le64(mddev->events);
1536 if (mddev->in_sync)
1537 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1538 else
1539 sb->resync_offset = cpu_to_le64(0);
1540
NeilBrown1c05b4b2006-10-21 10:24:08 -07001541 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001542
NeilBrownf0ca3402006-02-02 14:28:04 -08001543 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001544 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001545 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001546 sb->level = cpu_to_le32(mddev->level);
1547 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001548
NeilBrownc3d97142009-12-14 12:49:52 +11001549 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1550 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001551 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001552 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001553
1554 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001555 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001556 sb->feature_map |=
1557 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1558 sb->recovery_offset =
1559 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001560 }
1561
NeilBrownf6705572006-03-27 01:18:11 -08001562 if (mddev->reshape_position != MaxSector) {
1563 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1564 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1565 sb->new_layout = cpu_to_le32(mddev->new_layout);
1566 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1567 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001568 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001569 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001572 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (rdev2->desc_nr+1 > max_dev)
1574 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001575
NeilBrown70471da2009-08-03 10:59:57 +10001576 if (max_dev > le32_to_cpu(sb->max_dev)) {
1577 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001578 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001579 rdev->sb_size = max_dev * 2 + 256;
1580 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1581 if (rdev->sb_size & bmask)
1582 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 for (i=0; i<max_dev;i++)
1585 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1586
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001587 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001589 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001591 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001593 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001594 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 else
1596 sb->dev_roles[i] = cpu_to_le16(0xffff);
1597 }
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 sb->sb_csum = calc_sb_1_csum(sb);
1600}
1601
Chris Webb0cd17fe2008-06-28 08:31:46 +10001602static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001603super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001604{
1605 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001606 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001607 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001608 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001609 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001610 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001611 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1612 max_sectors -= rdev->data_offset;
1613 if (!num_sectors || num_sectors > max_sectors)
1614 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001615 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001616 /* minor version 0 with bitmap we can't move */
1617 return 0;
1618 } else {
1619 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001620 sector_t sb_start;
1621 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1622 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001623 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001624 if (!num_sectors || num_sectors > max_sectors)
1625 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001626 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001627 }
1628 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001629 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001630 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001631 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001632 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001633 rdev->sb_page);
1634 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001635 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001636}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Adrian Bunk75c96f82005-05-05 16:16:09 -07001638static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 [0] = {
1640 .name = "0.90.0",
1641 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001642 .load_super = super_90_load,
1643 .validate_super = super_90_validate,
1644 .sync_super = super_90_sync,
1645 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 },
1647 [1] = {
1648 .name = "md-1",
1649 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001650 .load_super = super_1_load,
1651 .validate_super = super_1_validate,
1652 .sync_super = super_1_sync,
1653 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 },
1655};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1658{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001659 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
NeilBrown4b809912008-07-21 17:05:25 +10001661 rcu_read_lock();
1662 rdev_for_each_rcu(rdev, mddev1)
1663 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001664 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001665 rdev2->bdev->bd_contains) {
1666 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001667 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001668 }
1669 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return 0;
1671}
1672
1673static LIST_HEAD(pending_raid_disks);
1674
Andre Nollac5e7112009-08-03 10:59:47 +10001675/*
1676 * Try to register data integrity profile for an mddev
1677 *
1678 * This is called when an array is started and after a disk has been kicked
1679 * from the array. It only succeeds if all working and active component devices
1680 * are integrity capable with matching profiles.
1681 */
1682int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001683{
Andre Nollac5e7112009-08-03 10:59:47 +10001684 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001685
Andre Nollac5e7112009-08-03 10:59:47 +10001686 if (list_empty(&mddev->disks))
1687 return 0; /* nothing to do */
1688 if (blk_get_integrity(mddev->gendisk))
1689 return 0; /* already registered */
1690 list_for_each_entry(rdev, &mddev->disks, same_set) {
1691 /* skip spares and non-functional disks */
1692 if (test_bit(Faulty, &rdev->flags))
1693 continue;
1694 if (rdev->raid_disk < 0)
1695 continue;
1696 /*
1697 * If at least one rdev is not integrity capable, we can not
1698 * enable data integrity for the md device.
1699 */
1700 if (!bdev_get_integrity(rdev->bdev))
1701 return -EINVAL;
1702 if (!reference) {
1703 /* Use the first rdev as the reference */
1704 reference = rdev;
1705 continue;
1706 }
1707 /* does this rdev's profile match the reference profile? */
1708 if (blk_integrity_compare(reference->bdev->bd_disk,
1709 rdev->bdev->bd_disk) < 0)
1710 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001711 }
Andre Nollac5e7112009-08-03 10:59:47 +10001712 /*
1713 * All component devices are integrity capable and have matching
1714 * profiles, register the common profile for the md device.
1715 */
1716 if (blk_integrity_register(mddev->gendisk,
1717 bdev_get_integrity(reference->bdev)) != 0) {
1718 printk(KERN_ERR "md: failed to register integrity for %s\n",
1719 mdname(mddev));
1720 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001721 }
Andre Nollac5e7112009-08-03 10:59:47 +10001722 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1723 mdname(mddev));
1724 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001725}
Andre Nollac5e7112009-08-03 10:59:47 +10001726EXPORT_SYMBOL(md_integrity_register);
1727
1728/* Disable data integrity if non-capable/non-matching disk is being added */
1729void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1730{
1731 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1732 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1733
1734 if (!bi_mddev) /* nothing to do */
1735 return;
1736 if (rdev->raid_disk < 0) /* skip spares */
1737 return;
1738 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1739 rdev->bdev->bd_disk) >= 0)
1740 return;
1741 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1742 blk_integrity_unregister(mddev->gendisk);
1743}
1744EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1747{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001748 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001749 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001750 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001751 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 if (rdev->mddev) {
1754 MD_BUG();
1755 return -EINVAL;
1756 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001757
1758 /* prevent duplicates */
1759 if (find_rdev(mddev, rdev->bdev->bd_dev))
1760 return -EEXIST;
1761
Andre Nolldd8ac332009-03-31 14:33:13 +11001762 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1763 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1764 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001765 if (mddev->pers) {
1766 /* Cannot change size, so fail
1767 * If mddev->level <= 0, then we don't care
1768 * about aligning sizes (e.g. linear)
1769 */
1770 if (mddev->level > 0)
1771 return -ENOSPC;
1772 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001773 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
1776 /* Verify rdev->desc_nr is unique.
1777 * If it is -1, assign a free number, else
1778 * check number is not in use
1779 */
1780 if (rdev->desc_nr < 0) {
1781 int choice = 0;
1782 if (mddev->pers) choice = mddev->raid_disks;
1783 while (find_rdev_nr(mddev, choice))
1784 choice++;
1785 rdev->desc_nr = choice;
1786 } else {
1787 if (find_rdev_nr(mddev, rdev->desc_nr))
1788 return -EBUSY;
1789 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001790 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1791 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1792 mdname(mddev), mddev->max_disks);
1793 return -EBUSY;
1794 }
NeilBrown19133a42005-11-08 21:39:35 -08001795 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001796 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001797 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001800 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001801
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001802 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001803 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001804
Tejun Heo0762b8b2008-08-25 19:56:12 +09001805 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001806 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1807 kobject_del(&rdev->kobj);
1808 goto fail;
1809 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001810 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1811
NeilBrown4b809912008-07-21 17:05:25 +10001812 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001813 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001814
1815 /* May as well allow recovery to be retried once */
1816 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001819
1820 fail:
1821 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1822 b, mdname(mddev));
1823 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824}
1825
NeilBrown177a99b2008-02-06 01:39:56 -08001826static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001827{
1828 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1829 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001830 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001831}
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1834{
1835 char b[BDEVNAME_SIZE];
1836 if (!rdev->mddev) {
1837 MD_BUG();
1838 return;
1839 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001840 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001841 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1843 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001844 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001845 sysfs_put(rdev->sysfs_state);
1846 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001847 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001848 * writing to 'remove' to "dev/state". We also need
1849 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001850 */
NeilBrown4b809912008-07-21 17:05:25 +10001851 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001852 INIT_WORK(&rdev->del_work, md_delayed_delete);
1853 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001854 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855}
1856
1857/*
1858 * prevent the device from being mounted, repartitioned or
1859 * otherwise reused by a RAID array (or any other kernel
1860 * subsystem), by bd_claiming the device.
1861 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001862static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
1864 int err = 0;
1865 struct block_device *bdev;
1866 char b[BDEVNAME_SIZE];
1867
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001868 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 if (IS_ERR(bdev)) {
1870 printk(KERN_ERR "md: could not open %s.\n",
1871 __bdevname(dev, b));
1872 return PTR_ERR(bdev);
1873 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001874 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 if (err) {
1876 printk(KERN_ERR "md: could not bd_claim %s.\n",
1877 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001878 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 return err;
1880 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001881 if (!shared)
1882 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 rdev->bdev = bdev;
1884 return err;
1885}
1886
1887static void unlock_rdev(mdk_rdev_t *rdev)
1888{
1889 struct block_device *bdev = rdev->bdev;
1890 rdev->bdev = NULL;
1891 if (!bdev)
1892 MD_BUG();
1893 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001894 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895}
1896
1897void md_autodetect_dev(dev_t dev);
1898
1899static void export_rdev(mdk_rdev_t * rdev)
1900{
1901 char b[BDEVNAME_SIZE];
1902 printk(KERN_INFO "md: export_rdev(%s)\n",
1903 bdevname(rdev->bdev,b));
1904 if (rdev->mddev)
1905 MD_BUG();
1906 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001908 if (test_bit(AutoDetected, &rdev->flags))
1909 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910#endif
1911 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001912 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913}
1914
1915static void kick_rdev_from_array(mdk_rdev_t * rdev)
1916{
1917 unbind_rdev_from_array(rdev);
1918 export_rdev(rdev);
1919}
1920
1921static void export_array(mddev_t *mddev)
1922{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001923 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
NeilBrownd089c6a2008-02-06 01:39:59 -08001925 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 if (!rdev->mddev) {
1927 MD_BUG();
1928 continue;
1929 }
1930 kick_rdev_from_array(rdev);
1931 }
1932 if (!list_empty(&mddev->disks))
1933 MD_BUG();
1934 mddev->raid_disks = 0;
1935 mddev->major_version = 0;
1936}
1937
1938static void print_desc(mdp_disk_t *desc)
1939{
1940 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1941 desc->major,desc->minor,desc->raid_disk,desc->state);
1942}
1943
Cheng Renquancd2ac932009-01-09 08:31:08 +11001944static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
1946 int i;
1947
1948 printk(KERN_INFO
1949 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1950 sb->major_version, sb->minor_version, sb->patch_version,
1951 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1952 sb->ctime);
1953 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1954 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1955 sb->md_minor, sb->layout, sb->chunk_size);
1956 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1957 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1958 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1959 sb->failed_disks, sb->spare_disks,
1960 sb->sb_csum, (unsigned long)sb->events_lo);
1961
1962 printk(KERN_INFO);
1963 for (i = 0; i < MD_SB_DISKS; i++) {
1964 mdp_disk_t *desc;
1965
1966 desc = sb->disks + i;
1967 if (desc->number || desc->major || desc->minor ||
1968 desc->raid_disk || (desc->state && (desc->state != 4))) {
1969 printk(" D %2d: ", i);
1970 print_desc(desc);
1971 }
1972 }
1973 printk(KERN_INFO "md: THIS: ");
1974 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975}
1976
Cheng Renquancd2ac932009-01-09 08:31:08 +11001977static void print_sb_1(struct mdp_superblock_1 *sb)
1978{
1979 __u8 *uuid;
1980
1981 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001982 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08001983 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001984 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001985 le32_to_cpu(sb->major_version),
1986 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08001987 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11001988 sb->set_name,
1989 (unsigned long long)le64_to_cpu(sb->ctime)
1990 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1991
1992 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001993 printk(KERN_INFO
1994 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11001995 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08001996 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001997 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1998 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001999 le32_to_cpu(sb->level),
2000 (unsigned long long)le64_to_cpu(sb->size),
2001 le32_to_cpu(sb->raid_disks),
2002 le32_to_cpu(sb->layout),
2003 le32_to_cpu(sb->chunksize),
2004 (unsigned long long)le64_to_cpu(sb->data_offset),
2005 (unsigned long long)le64_to_cpu(sb->data_size),
2006 (unsigned long long)le64_to_cpu(sb->super_offset),
2007 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2008 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002009 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002010 sb->devflags,
2011 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2012 (unsigned long long)le64_to_cpu(sb->events),
2013 (unsigned long long)le64_to_cpu(sb->resync_offset),
2014 le32_to_cpu(sb->sb_csum),
2015 le32_to_cpu(sb->max_dev)
2016 );
2017}
2018
2019static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
2021 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002022 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2023 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002024 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2025 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002027 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2028 switch (major_version) {
2029 case 0:
2030 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
2031 break;
2032 case 1:
2033 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
2034 break;
2035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 } else
2037 printk(KERN_INFO "md: no rdev superblock!\n");
2038}
2039
Adrian Bunk5e563412006-06-26 00:27:42 -07002040static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002042 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 mdk_rdev_t *rdev;
2044 mddev_t *mddev;
2045 char b[BDEVNAME_SIZE];
2046
2047 printk("\n");
2048 printk("md: **********************************\n");
2049 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2050 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002051 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
NeilBrown32a76272005-06-21 17:17:14 -07002053 if (mddev->bitmap)
2054 bitmap_print_sb(mddev->bitmap);
2055 else
2056 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002057 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 printk("<%s>", bdevname(rdev->bdev,b));
2059 printk("\n");
2060
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002061 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002062 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
2064 printk("md: **********************************\n");
2065 printk("\n");
2066}
2067
2068
NeilBrown42543762006-06-26 00:27:57 -07002069static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
NeilBrown42543762006-06-26 00:27:57 -07002071 /* Update each superblock (in-memory image), but
2072 * if we are allowed to, skip spares which already
2073 * have the right event counter, or have one earlier
2074 * (which would mean they aren't being marked as dirty
2075 * with the rest of the array)
2076 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
NeilBrown5e865102009-11-12 12:08:04 +11002079 /* First make sure individual recovery_offsets are correct */
2080 list_for_each_entry(rdev, &mddev->disks, same_set) {
2081 if (rdev->raid_disk >= 0 &&
2082 !test_bit(In_sync, &rdev->flags) &&
2083 mddev->curr_resync_completed > rdev->recovery_offset)
2084 rdev->recovery_offset = mddev->curr_resync_completed;
2085
2086 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002087 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002088 if (rdev->sb_events == mddev->events ||
2089 (nospares &&
2090 rdev->raid_disk < 0 &&
2091 (rdev->sb_events&1)==0 &&
2092 rdev->sb_events+1 == mddev->events)) {
2093 /* Don't update this superblock */
2094 rdev->sb_loaded = 2;
2095 } else {
2096 super_types[mddev->major_version].
2097 sync_super(mddev, rdev);
2098 rdev->sb_loaded = 1;
2099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 }
2101}
2102
NeilBrown850b2b42006-10-03 01:15:46 -07002103static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002106 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002107 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
NeilBrown1b57f132009-06-18 08:48:19 +10002109 mddev->utime = get_seconds();
NeilBrown8377bc82008-04-30 00:52:28 -07002110 if (mddev->external)
2111 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08002113 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002114
NeilBrown850b2b42006-10-03 01:15:46 -07002115 set_bit(MD_CHANGE_PENDING, &mddev->flags);
2116 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2117 force_change = 1;
2118 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2119 /* just a clean<-> dirty transition, possibly leave spares alone,
2120 * though if events isn't the right even/odd, we will have to do
2121 * spares after all
2122 */
2123 nospares = 1;
2124 if (force_change)
2125 nospares = 0;
2126 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002127 /* If the array is degraded, then skipping spares is both
2128 * dangerous and fairly pointless.
2129 * Dangerous because a device that was removed from the array
2130 * might have a event_count that still looks up-to-date,
2131 * so it can be re-added without a resync.
2132 * Pointless because if there are any spares to skip,
2133 * then a recovery will happen and soon that array won't
2134 * be degraded any more and the spare can go back to sleep then.
2135 */
NeilBrown850b2b42006-10-03 01:15:46 -07002136 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002137
NeilBrown06d91a52005-06-21 17:17:12 -07002138 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002139
2140 /* If this is just a dirty<->clean transition, and the array is clean
2141 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002142 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002143 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08002144 && (mddev->events & 1)
2145 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07002146 mddev->events--;
2147 else {
2148 /* otherwise we have to go forward and ... */
2149 mddev->events ++;
2150 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
NeilBrown51d56682009-08-13 09:54:02 +10002151 /* .. if the array isn't clean, an 'even' event must also go
2152 * to spares. */
2153 if ((mddev->events&1)==0)
NeilBrown42543762006-06-26 00:27:57 -07002154 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002155 } else {
NeilBrown51d56682009-08-13 09:54:02 +10002156 /* otherwise an 'odd' event must go to spares */
2157 if ((mddev->events&1))
NeilBrown42543762006-06-26 00:27:57 -07002158 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002159 }
2160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 if (!mddev->events) {
2163 /*
2164 * oops, this 64-bit counter should never wrap.
2165 * Either we are in around ~1 trillion A.C., assuming
2166 * 1 reboot per second, or we have a bug:
2167 */
2168 MD_BUG();
2169 mddev->events --;
2170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 /*
2173 * do not write anything to disk if using
2174 * nonpersistent superblocks
2175 */
NeilBrown06d91a52005-06-21 17:17:12 -07002176 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08002177 if (!mddev->external)
2178 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2179
NeilBrowna9701a32005-11-08 21:39:34 -08002180 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002181 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return;
NeilBrown06d91a52005-06-21 17:17:12 -07002183 }
NeilBrowne6910632008-02-06 01:39:51 -08002184 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002185 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187 dprintk(KERN_INFO
2188 "md: updating %s RAID superblock on device (in sync %d)\n",
2189 mdname(mddev),mddev->in_sync);
2190
NeilBrown4ad13662007-07-17 04:06:13 -07002191 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002192 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 char b[BDEVNAME_SIZE];
2194 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002195 if (rdev->sb_loaded != 1)
2196 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002197 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 dprintk("(skipping faulty ");
2199
2200 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002201 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002202 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002203 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002204 rdev->sb_page);
2205 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2206 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002207 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002208 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002209
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 } else
2211 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002212 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 /* only need to write one superblock... */
2214 break;
2215 }
NeilBrowna9701a32005-11-08 21:39:34 -08002216 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002217 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002218
NeilBrowna9701a32005-11-08 21:39:34 -08002219 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002220 if (mddev->in_sync != sync_req ||
2221 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002222 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002223 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002224 goto repeat;
2225 }
NeilBrown850b2b42006-10-03 01:15:46 -07002226 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002227 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002228 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002229 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2230 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232}
2233
Andre Noll7f6ce762008-03-23 18:34:54 +01002234/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002235 * We want to accept with case. For this we use cmd_match.
2236 */
2237static int cmd_match(const char *cmd, const char *str)
2238{
2239 /* See if cmd, written into a sysfs file, matches
2240 * str. They must either be the same, or cmd can
2241 * have a trailing newline
2242 */
2243 while (*cmd && *str && *cmd == *str) {
2244 cmd++;
2245 str++;
2246 }
2247 if (*cmd == '\n')
2248 cmd++;
2249 if (*str || *cmd)
2250 return 0;
2251 return 1;
2252}
2253
NeilBrown86e6ffd2005-11-08 21:39:24 -08002254struct rdev_sysfs_entry {
2255 struct attribute attr;
2256 ssize_t (*show)(mdk_rdev_t *, char *);
2257 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2258};
2259
2260static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002261state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002262{
2263 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002264 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002265
NeilBrownb2d444d2005-11-08 21:39:31 -08002266 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002267 len+= sprintf(page+len, "%sfaulty",sep);
2268 sep = ",";
2269 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002270 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002271 len += sprintf(page+len, "%sin_sync",sep);
2272 sep = ",";
2273 }
NeilBrownf6556752006-06-26 00:28:01 -07002274 if (test_bit(WriteMostly, &rdev->flags)) {
2275 len += sprintf(page+len, "%swrite_mostly",sep);
2276 sep = ",";
2277 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002278 if (test_bit(Blocked, &rdev->flags)) {
2279 len += sprintf(page+len, "%sblocked", sep);
2280 sep = ",";
2281 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002282 if (!test_bit(Faulty, &rdev->flags) &&
2283 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002284 len += sprintf(page+len, "%sspare", sep);
2285 sep = ",";
2286 }
2287 return len+sprintf(page+len, "\n");
2288}
2289
NeilBrown45dc2de2006-06-26 00:27:58 -07002290static ssize_t
2291state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2292{
2293 /* can write
2294 * faulty - simulates and error
2295 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002296 * writemostly - sets write_mostly
2297 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002298 * blocked - sets the Blocked flag
2299 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002300 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002301 */
2302 int err = -EINVAL;
2303 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2304 md_error(rdev->mddev, rdev);
2305 err = 0;
2306 } else if (cmd_match(buf, "remove")) {
2307 if (rdev->raid_disk >= 0)
2308 err = -EBUSY;
2309 else {
2310 mddev_t *mddev = rdev->mddev;
2311 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002312 if (mddev->pers)
2313 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002314 md_new_event(mddev);
2315 err = 0;
2316 }
NeilBrownf6556752006-06-26 00:28:01 -07002317 } else if (cmd_match(buf, "writemostly")) {
2318 set_bit(WriteMostly, &rdev->flags);
2319 err = 0;
2320 } else if (cmd_match(buf, "-writemostly")) {
2321 clear_bit(WriteMostly, &rdev->flags);
2322 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002323 } else if (cmd_match(buf, "blocked")) {
2324 set_bit(Blocked, &rdev->flags);
2325 err = 0;
2326 } else if (cmd_match(buf, "-blocked")) {
2327 clear_bit(Blocked, &rdev->flags);
2328 wake_up(&rdev->blocked_wait);
2329 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2330 md_wakeup_thread(rdev->mddev->thread);
2331
2332 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002333 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2334 set_bit(In_sync, &rdev->flags);
2335 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002336 }
NeilBrown3c0ee632008-10-21 13:25:28 +11002337 if (!err && rdev->sysfs_state)
2338 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002339 return err ? err : len;
2340}
NeilBrown80ca3a42006-07-10 04:44:18 -07002341static struct rdev_sysfs_entry rdev_state =
2342__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002343
2344static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002345errors_show(mdk_rdev_t *rdev, char *page)
2346{
2347 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2348}
2349
2350static ssize_t
2351errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2352{
2353 char *e;
2354 unsigned long n = simple_strtoul(buf, &e, 10);
2355 if (*buf && (*e == 0 || *e == '\n')) {
2356 atomic_set(&rdev->corrected_errors, n);
2357 return len;
2358 }
2359 return -EINVAL;
2360}
2361static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002362__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002363
NeilBrown014236d2006-01-06 00:20:55 -08002364static ssize_t
2365slot_show(mdk_rdev_t *rdev, char *page)
2366{
2367 if (rdev->raid_disk < 0)
2368 return sprintf(page, "none\n");
2369 else
2370 return sprintf(page, "%d\n", rdev->raid_disk);
2371}
2372
2373static ssize_t
2374slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2375{
2376 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002377 int err;
2378 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002379 int slot = simple_strtoul(buf, &e, 10);
2380 if (strncmp(buf, "none", 4)==0)
2381 slot = -1;
2382 else if (e==buf || (*e && *e!= '\n'))
2383 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002384 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002385 /* Setting 'slot' on an active array requires also
2386 * updating the 'rd%d' link, and communicating
2387 * with the personality with ->hot_*_disk.
2388 * For now we only support removing
2389 * failed/spare devices. This normally happens automatically,
2390 * but not when the metadata is externally managed.
2391 */
NeilBrownc303da62008-02-06 01:39:51 -08002392 if (rdev->raid_disk == -1)
2393 return -EEXIST;
2394 /* personality does all needed checks */
2395 if (rdev->mddev->pers->hot_add_disk == NULL)
2396 return -EINVAL;
2397 err = rdev->mddev->pers->
2398 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2399 if (err)
2400 return err;
2401 sprintf(nm, "rd%d", rdev->raid_disk);
2402 sysfs_remove_link(&rdev->mddev->kobj, nm);
Maciej Trelab7103102010-04-14 16:58:16 +10002403 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002404 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2405 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002406 } else if (rdev->mddev->pers) {
2407 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002408 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002409 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002410 */
2411
2412 if (rdev->raid_disk != -1)
2413 return -EBUSY;
2414
2415 if (rdev->mddev->pers->hot_add_disk == NULL)
2416 return -EINVAL;
2417
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002418 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002419 if (rdev2->raid_disk == slot)
2420 return -EEXIST;
2421
2422 rdev->raid_disk = slot;
2423 if (test_bit(In_sync, &rdev->flags))
2424 rdev->saved_raid_disk = slot;
2425 else
2426 rdev->saved_raid_disk = -1;
2427 err = rdev->mddev->pers->
2428 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002429 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002430 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002431 return err;
Neil Brown52664732008-06-28 08:31:44 +10002432 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002433 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002434 sprintf(nm, "rd%d", rdev->raid_disk);
2435 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2436 printk(KERN_WARNING
2437 "md: cannot register "
2438 "%s for %s\n",
2439 nm, mdname(rdev->mddev));
2440
2441 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002442 } else {
2443 if (slot >= rdev->mddev->raid_disks)
2444 return -ENOSPC;
2445 rdev->raid_disk = slot;
2446 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002447 clear_bit(Faulty, &rdev->flags);
2448 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002449 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002450 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002451 }
NeilBrown014236d2006-01-06 00:20:55 -08002452 return len;
2453}
2454
2455
2456static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002457__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002458
NeilBrown93c8cad2006-01-06 00:20:56 -08002459static ssize_t
2460offset_show(mdk_rdev_t *rdev, char *page)
2461{
Andrew Morton6961ece2006-01-06 00:20:59 -08002462 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002463}
2464
2465static ssize_t
2466offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2467{
2468 char *e;
2469 unsigned long long offset = simple_strtoull(buf, &e, 10);
2470 if (e==buf || (*e && *e != '\n'))
2471 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002472 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002473 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002474 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002475 /* Must set offset before size, so overlap checks
2476 * can be sane */
2477 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002478 rdev->data_offset = offset;
2479 return len;
2480}
2481
2482static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002483__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002484
NeilBrown83303b62006-01-06 00:21:06 -08002485static ssize_t
2486rdev_size_show(mdk_rdev_t *rdev, char *page)
2487{
Andre Nolldd8ac332009-03-31 14:33:13 +11002488 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002489}
2490
NeilBrownc5d79ad2008-02-06 01:39:54 -08002491static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2492{
2493 /* check if two start/length pairs overlap */
2494 if (s1+l1 <= s2)
2495 return 0;
2496 if (s2+l2 <= s1)
2497 return 0;
2498 return 1;
2499}
2500
Dan Williamsb522adc2009-03-31 15:00:31 +11002501static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2502{
2503 unsigned long long blocks;
2504 sector_t new;
2505
2506 if (strict_strtoull(buf, 10, &blocks) < 0)
2507 return -EINVAL;
2508
2509 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2510 return -EINVAL; /* sector conversion overflow */
2511
2512 new = blocks * 2;
2513 if (new != blocks * 2)
2514 return -EINVAL; /* unsigned long long to sector_t overflow */
2515
2516 *sectors = new;
2517 return 0;
2518}
2519
NeilBrown83303b62006-01-06 00:21:06 -08002520static ssize_t
2521rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2522{
NeilBrown27c529b2008-03-04 14:29:33 -08002523 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002524 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002525 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002526
Dan Williamsb522adc2009-03-31 15:00:31 +11002527 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002528 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002529 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002530 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002531 sectors = super_types[my_mddev->major_version].
2532 rdev_size_change(rdev, sectors);
2533 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002534 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002535 } else if (!sectors)
2536 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2537 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002538 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002539 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002540 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002541
Andre Nolldd8ac332009-03-31 14:33:13 +11002542 rdev->sectors = sectors;
2543 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002544 /* need to check that all other rdevs with the same ->bdev
2545 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002546 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002547 * we have to change it back, we will have the lock again.
2548 */
2549 mddev_t *mddev;
2550 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002551 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002552
NeilBrown27c529b2008-03-04 14:29:33 -08002553 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002554 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002555 mdk_rdev_t *rdev2;
2556
2557 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002558 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002559 if (test_bit(AllReserved, &rdev2->flags) ||
2560 (rdev->bdev == rdev2->bdev &&
2561 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002562 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002563 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002564 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002565 overlap = 1;
2566 break;
2567 }
2568 mddev_unlock(mddev);
2569 if (overlap) {
2570 mddev_put(mddev);
2571 break;
2572 }
2573 }
NeilBrown27c529b2008-03-04 14:29:33 -08002574 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002575 if (overlap) {
2576 /* Someone else could have slipped in a size
2577 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002578 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002579 * safe, and trust userspace not to race with
2580 * itself
2581 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002582 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002583 return -EBUSY;
2584 }
2585 }
NeilBrown83303b62006-01-06 00:21:06 -08002586 return len;
2587}
2588
2589static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002590__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002591
Dan Williams06e3c812009-12-12 21:17:12 -07002592
2593static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2594{
2595 unsigned long long recovery_start = rdev->recovery_offset;
2596
2597 if (test_bit(In_sync, &rdev->flags) ||
2598 recovery_start == MaxSector)
2599 return sprintf(page, "none\n");
2600
2601 return sprintf(page, "%llu\n", recovery_start);
2602}
2603
2604static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2605{
2606 unsigned long long recovery_start;
2607
2608 if (cmd_match(buf, "none"))
2609 recovery_start = MaxSector;
2610 else if (strict_strtoull(buf, 10, &recovery_start))
2611 return -EINVAL;
2612
2613 if (rdev->mddev->pers &&
2614 rdev->raid_disk >= 0)
2615 return -EBUSY;
2616
2617 rdev->recovery_offset = recovery_start;
2618 if (recovery_start == MaxSector)
2619 set_bit(In_sync, &rdev->flags);
2620 else
2621 clear_bit(In_sync, &rdev->flags);
2622 return len;
2623}
2624
2625static struct rdev_sysfs_entry rdev_recovery_start =
2626__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2627
NeilBrown86e6ffd2005-11-08 21:39:24 -08002628static struct attribute *rdev_default_attrs[] = {
2629 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002630 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002631 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002632 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002633 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002634 &rdev_recovery_start.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002635 NULL,
2636};
2637static ssize_t
2638rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2639{
2640 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2641 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002642 mddev_t *mddev = rdev->mddev;
2643 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002644
2645 if (!entry->show)
2646 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002647
2648 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2649 if (!rv) {
2650 if (rdev->mddev == NULL)
2651 rv = -EBUSY;
2652 else
2653 rv = entry->show(rdev, page);
2654 mddev_unlock(mddev);
2655 }
2656 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002657}
2658
2659static ssize_t
2660rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2661 const char *page, size_t length)
2662{
2663 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2664 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002665 ssize_t rv;
2666 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002667
2668 if (!entry->store)
2669 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002670 if (!capable(CAP_SYS_ADMIN))
2671 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002672 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002673 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002674 if (rdev->mddev == NULL)
2675 rv = -EBUSY;
2676 else
2677 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002678 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002679 }
2680 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002681}
2682
2683static void rdev_free(struct kobject *ko)
2684{
2685 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2686 kfree(rdev);
2687}
2688static struct sysfs_ops rdev_sysfs_ops = {
2689 .show = rdev_attr_show,
2690 .store = rdev_attr_store,
2691};
2692static struct kobj_type rdev_ktype = {
2693 .release = rdev_free,
2694 .sysfs_ops = &rdev_sysfs_ops,
2695 .default_attrs = rdev_default_attrs,
2696};
2697
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698/*
2699 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2700 *
2701 * mark the device faulty if:
2702 *
2703 * - the device is nonexistent (zero size)
2704 * - the device has no valid superblock
2705 *
2706 * a faulty rdev _never_ has rdev->sb set.
2707 */
2708static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2709{
2710 char b[BDEVNAME_SIZE];
2711 int err;
2712 mdk_rdev_t *rdev;
2713 sector_t size;
2714
NeilBrown9ffae0c2006-01-06 00:20:32 -08002715 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 if (!rdev) {
2717 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2718 return ERR_PTR(-ENOMEM);
2719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
2721 if ((err = alloc_disk_sb(rdev)))
2722 goto abort_free;
2723
NeilBrownc5d79ad2008-02-06 01:39:54 -08002724 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 if (err)
2726 goto abort_free;
2727
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002728 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002729
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002731 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002732 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002733 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002735 rdev->sb_events = 0;
Robert Becker1e509152009-12-14 12:49:58 +11002736 rdev->last_read_error.tv_sec = 0;
2737 rdev->last_read_error.tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002739 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002740 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
2742 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2743 if (!size) {
2744 printk(KERN_WARNING
2745 "md: %s has zero or unknown size, marking faulty!\n",
2746 bdevname(rdev->bdev,b));
2747 err = -EINVAL;
2748 goto abort_free;
2749 }
2750
2751 if (super_format >= 0) {
2752 err = super_types[super_format].
2753 load_super(rdev, NULL, super_minor);
2754 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002755 printk(KERN_WARNING
2756 "md: %s does not have a valid v%d.%d "
2757 "superblock, not importing!\n",
2758 bdevname(rdev->bdev,b),
2759 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 goto abort_free;
2761 }
2762 if (err < 0) {
2763 printk(KERN_WARNING
2764 "md: could not read %s's sb, not importing!\n",
2765 bdevname(rdev->bdev,b));
2766 goto abort_free;
2767 }
2768 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002771 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
2773 return rdev;
2774
2775abort_free:
2776 if (rdev->sb_page) {
2777 if (rdev->bdev)
2778 unlock_rdev(rdev);
2779 free_disk_sb(rdev);
2780 }
2781 kfree(rdev);
2782 return ERR_PTR(err);
2783}
2784
2785/*
2786 * Check a full RAID array for plausibility
2787 */
2788
2789
NeilBrowna757e642005-04-16 15:26:42 -07002790static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
2792 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002793 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 char b[BDEVNAME_SIZE];
2795
2796 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002797 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 switch (super_types[mddev->major_version].
2799 load_super(rdev, freshest, mddev->minor_version)) {
2800 case 1:
2801 freshest = rdev;
2802 break;
2803 case 0:
2804 break;
2805 default:
2806 printk( KERN_ERR \
2807 "md: fatal superblock inconsistency in %s"
2808 " -- removing from array\n",
2809 bdevname(rdev->bdev,b));
2810 kick_rdev_from_array(rdev);
2811 }
2812
2813
2814 super_types[mddev->major_version].
2815 validate_super(mddev, freshest);
2816
2817 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002818 rdev_for_each(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10002819 if (mddev->max_disks &&
2820 (rdev->desc_nr >= mddev->max_disks ||
2821 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002822 printk(KERN_WARNING
2823 "md: %s: %s: only %d devices permitted\n",
2824 mdname(mddev), bdevname(rdev->bdev, b),
2825 mddev->max_disks);
2826 kick_rdev_from_array(rdev);
2827 continue;
2828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 if (rdev != freshest)
2830 if (super_types[mddev->major_version].
2831 validate_super(mddev, rdev)) {
2832 printk(KERN_WARNING "md: kicking non-fresh %s"
2833 " from array!\n",
2834 bdevname(rdev->bdev,b));
2835 kick_rdev_from_array(rdev);
2836 continue;
2837 }
2838 if (mddev->level == LEVEL_MULTIPATH) {
2839 rdev->desc_nr = i++;
2840 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002841 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002842 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002843 rdev->raid_disk = -1;
2844 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 }
2846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847}
2848
NeilBrown72e02072009-12-14 12:49:55 +11002849/* Read a fixed-point number.
2850 * Numbers in sysfs attributes should be in "standard" units where
2851 * possible, so time should be in seconds.
2852 * However we internally use a a much smaller unit such as
2853 * milliseconds or jiffies.
2854 * This function takes a decimal number with a possible fractional
2855 * component, and produces an integer which is the result of
2856 * multiplying that number by 10^'scale'.
2857 * all without any floating-point arithmetic.
2858 */
2859int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2860{
2861 unsigned long result = 0;
2862 long decimals = -1;
2863 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2864 if (*cp == '.')
2865 decimals = 0;
2866 else if (decimals < scale) {
2867 unsigned int value;
2868 value = *cp - '0';
2869 result = result * 10 + value;
2870 if (decimals >= 0)
2871 decimals++;
2872 }
2873 cp++;
2874 }
2875 if (*cp == '\n')
2876 cp++;
2877 if (*cp)
2878 return -EINVAL;
2879 if (decimals < 0)
2880 decimals = 0;
2881 while (decimals < scale) {
2882 result *= 10;
2883 decimals ++;
2884 }
2885 *res = result;
2886 return 0;
2887}
2888
2889
NeilBrown19052c02008-08-05 15:54:13 +10002890static void md_safemode_timeout(unsigned long data);
2891
NeilBrowneae17012005-11-08 21:39:23 -08002892static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002893safe_delay_show(mddev_t *mddev, char *page)
2894{
2895 int msec = (mddev->safemode_delay*1000)/HZ;
2896 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2897}
2898static ssize_t
2899safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2900{
NeilBrown16f17b32006-06-26 00:27:37 -07002901 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07002902
NeilBrown72e02072009-12-14 12:49:55 +11002903 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002904 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07002905 if (msec == 0)
2906 mddev->safemode_delay = 0;
2907 else {
NeilBrown19052c02008-08-05 15:54:13 +10002908 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002909 mddev->safemode_delay = (msec*HZ)/1000;
2910 if (mddev->safemode_delay == 0)
2911 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002912 if (mddev->safemode_delay < old_delay)
2913 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002914 }
2915 return len;
2916}
2917static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002918__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002919
2920static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002921level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002922{
NeilBrown2604b702006-01-06 00:20:36 -08002923 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002924 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002925 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002926 else if (mddev->clevel[0])
2927 return sprintf(page, "%s\n", mddev->clevel);
2928 else if (mddev->level != LEVEL_NONE)
2929 return sprintf(page, "%d\n", mddev->level);
2930 else
2931 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002932}
2933
NeilBrownd9d166c2006-01-06 00:20:51 -08002934static ssize_t
2935level_store(mddev_t *mddev, const char *buf, size_t len)
2936{
NeilBrown245f46c2009-03-31 14:39:39 +11002937 char level[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002938 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002939 struct mdk_personality *pers;
2940 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002941 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002942
2943 if (mddev->pers == NULL) {
2944 if (len == 0)
2945 return 0;
2946 if (len >= sizeof(mddev->clevel))
2947 return -ENOSPC;
2948 strncpy(mddev->clevel, buf, len);
2949 if (mddev->clevel[len-1] == '\n')
2950 len--;
2951 mddev->clevel[len] = 0;
2952 mddev->level = LEVEL_NONE;
2953 return rv;
2954 }
2955
2956 /* request to change the personality. Need to ensure:
2957 * - array is not engaged in resync/recovery/reshape
2958 * - old personality can be suspended
2959 * - new personality will access other array.
2960 */
2961
2962 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownd9d166c2006-01-06 00:20:51 -08002963 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002964
2965 if (!mddev->pers->quiesce) {
2966 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2967 mdname(mddev), mddev->pers->name);
2968 return -EINVAL;
2969 }
2970
2971 /* Now find the new personality */
2972 if (len == 0 || len >= sizeof(level))
2973 return -EINVAL;
2974 strncpy(level, buf, len);
2975 if (level[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002976 len--;
NeilBrown245f46c2009-03-31 14:39:39 +11002977 level[len] = 0;
2978
2979 request_module("md-%s", level);
2980 spin_lock(&pers_lock);
2981 pers = find_pers(LEVEL_NONE, level);
2982 if (!pers || !try_module_get(pers->owner)) {
2983 spin_unlock(&pers_lock);
2984 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2985 return -EINVAL;
2986 }
2987 spin_unlock(&pers_lock);
2988
2989 if (pers == mddev->pers) {
2990 /* Nothing to do! */
2991 module_put(pers->owner);
2992 return rv;
2993 }
2994 if (!pers->takeover) {
2995 module_put(pers->owner);
2996 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2997 mdname(mddev), level);
2998 return -EINVAL;
2999 }
3000
3001 /* ->takeover must set new_* and/or delta_disks
3002 * if it succeeds, and may set them when it fails.
3003 */
3004 priv = pers->takeover(mddev);
3005 if (IS_ERR(priv)) {
3006 mddev->new_level = mddev->level;
3007 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003008 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003009 mddev->raid_disks -= mddev->delta_disks;
3010 mddev->delta_disks = 0;
3011 module_put(pers->owner);
3012 printk(KERN_WARNING "md: %s: %s would not accept array\n",
3013 mdname(mddev), level);
3014 return PTR_ERR(priv);
3015 }
3016
3017 /* Looks like we have a winner */
3018 mddev_suspend(mddev);
3019 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003020
3021 if (mddev->pers->sync_request == NULL &&
3022 pers->sync_request != NULL) {
3023 /* need to add the md_redundancy_group */
3024 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3025 printk(KERN_WARNING
3026 "md: cannot register extra attributes for %s\n",
3027 mdname(mddev));
3028 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3029 }
3030 if (mddev->pers->sync_request != NULL &&
3031 pers->sync_request == NULL) {
3032 /* need to remove the md_redundancy_group */
3033 if (mddev->to_remove == NULL)
3034 mddev->to_remove = &md_redundancy_group;
3035 }
3036
Trela Maciej54071b32010-03-08 16:02:42 +11003037 if (mddev->pers->sync_request == NULL &&
3038 mddev->external) {
3039 /* We are converting from a no-redundancy array
3040 * to a redundancy array and metadata is managed
3041 * externally so we need to be sure that writes
3042 * won't block due to a need to transition
3043 * clean->dirty
3044 * until external management is started.
3045 */
3046 mddev->in_sync = 0;
3047 mddev->safemode_delay = 0;
3048 mddev->safemode = 0;
3049 }
3050
NeilBrown245f46c2009-03-31 14:39:39 +11003051 module_put(mddev->pers->owner);
NeilBrown3a981b02009-08-03 10:59:55 +10003052 /* Invalidate devices that are now superfluous */
3053 list_for_each_entry(rdev, &mddev->disks, same_set)
3054 if (rdev->raid_disk >= mddev->raid_disks) {
3055 rdev->raid_disk = -1;
3056 clear_bit(In_sync, &rdev->flags);
3057 }
NeilBrown245f46c2009-03-31 14:39:39 +11003058 mddev->pers = pers;
3059 mddev->private = priv;
3060 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3061 mddev->level = mddev->new_level;
3062 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003063 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003064 mddev->delta_disks = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003065 if (mddev->pers->sync_request == NULL) {
3066 /* this is now an array without redundancy, so
3067 * it must always be in_sync
3068 */
3069 mddev->in_sync = 1;
3070 del_timer_sync(&mddev->safemode_timer);
3071 }
NeilBrown245f46c2009-03-31 14:39:39 +11003072 pers->run(mddev);
3073 mddev_resume(mddev);
3074 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3075 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3076 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003077 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003078 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003079 return rv;
3080}
3081
3082static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003083__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003084
NeilBrownd4dbd022006-06-26 00:27:59 -07003085
3086static ssize_t
3087layout_show(mddev_t *mddev, char *page)
3088{
3089 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003090 if (mddev->reshape_position != MaxSector &&
3091 mddev->layout != mddev->new_layout)
3092 return sprintf(page, "%d (%d)\n",
3093 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003094 return sprintf(page, "%d\n", mddev->layout);
3095}
3096
3097static ssize_t
3098layout_store(mddev_t *mddev, const char *buf, size_t len)
3099{
3100 char *e;
3101 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003102
3103 if (!*buf || (*e && *e != '\n'))
3104 return -EINVAL;
3105
NeilBrownb3546032009-03-31 14:56:41 +11003106 if (mddev->pers) {
3107 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003108 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003109 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003110 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003111 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003112 if (err) {
3113 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003114 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003115 }
NeilBrownb3546032009-03-31 14:56:41 +11003116 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003117 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003118 if (mddev->reshape_position == MaxSector)
3119 mddev->layout = n;
3120 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003121 return len;
3122}
3123static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003124__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003125
3126
NeilBrowneae17012005-11-08 21:39:23 -08003127static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003128raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003129{
NeilBrownbb636542005-11-08 21:39:45 -08003130 if (mddev->raid_disks == 0)
3131 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003132 if (mddev->reshape_position != MaxSector &&
3133 mddev->delta_disks != 0)
3134 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3135 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003136 return sprintf(page, "%d\n", mddev->raid_disks);
3137}
3138
NeilBrownda943b992006-01-06 00:20:54 -08003139static int update_raid_disks(mddev_t *mddev, int raid_disks);
3140
3141static ssize_t
3142raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3143{
NeilBrownda943b992006-01-06 00:20:54 -08003144 char *e;
3145 int rv = 0;
3146 unsigned long n = simple_strtoul(buf, &e, 10);
3147
3148 if (!*buf || (*e && *e != '\n'))
3149 return -EINVAL;
3150
3151 if (mddev->pers)
3152 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003153 else if (mddev->reshape_position != MaxSector) {
3154 int olddisks = mddev->raid_disks - mddev->delta_disks;
3155 mddev->delta_disks = n - olddisks;
3156 mddev->raid_disks = n;
3157 } else
NeilBrownda943b992006-01-06 00:20:54 -08003158 mddev->raid_disks = n;
3159 return rv ? rv : len;
3160}
3161static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003162__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003163
NeilBrown24dd4692005-11-08 21:39:26 -08003164static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003165chunk_size_show(mddev_t *mddev, char *page)
3166{
NeilBrown08a02ec2007-05-09 02:35:38 -07003167 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003168 mddev->chunk_sectors != mddev->new_chunk_sectors)
3169 return sprintf(page, "%d (%d)\n",
3170 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003171 mddev->chunk_sectors << 9);
3172 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003173}
3174
3175static ssize_t
3176chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3177{
NeilBrown3b343802006-01-06 00:20:47 -08003178 char *e;
3179 unsigned long n = simple_strtoul(buf, &e, 10);
3180
NeilBrown3b343802006-01-06 00:20:47 -08003181 if (!*buf || (*e && *e != '\n'))
3182 return -EINVAL;
3183
NeilBrownb3546032009-03-31 14:56:41 +11003184 if (mddev->pers) {
3185 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003186 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003187 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003188 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003189 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003190 if (err) {
3191 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003192 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003193 }
NeilBrownb3546032009-03-31 14:56:41 +11003194 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003195 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003196 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003197 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003198 }
NeilBrown3b343802006-01-06 00:20:47 -08003199 return len;
3200}
3201static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003202__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003203
NeilBrowna94213b2006-06-26 00:28:00 -07003204static ssize_t
3205resync_start_show(mddev_t *mddev, char *page)
3206{
NeilBrownd1a7c502009-03-31 15:24:32 +11003207 if (mddev->recovery_cp == MaxSector)
3208 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003209 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3210}
3211
3212static ssize_t
3213resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3214{
NeilBrowna94213b2006-06-26 00:28:00 -07003215 char *e;
3216 unsigned long long n = simple_strtoull(buf, &e, 10);
3217
3218 if (mddev->pers)
3219 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003220 if (cmd_match(buf, "none"))
3221 n = MaxSector;
3222 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003223 return -EINVAL;
3224
3225 mddev->recovery_cp = n;
3226 return len;
3227}
3228static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003229__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003230
NeilBrown9e653b62006-06-26 00:27:58 -07003231/*
3232 * The array state can be:
3233 *
3234 * clear
3235 * No devices, no size, no level
3236 * Equivalent to STOP_ARRAY ioctl
3237 * inactive
3238 * May have some settings, but array is not active
3239 * all IO results in error
3240 * When written, doesn't tear down array, but just stops it
3241 * suspended (not supported yet)
3242 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003243 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003244 * readonly
3245 * no resync can happen. no superblocks get written.
3246 * write requests fail
3247 * read-auto
3248 * like readonly, but behaves like 'clean' on a write request.
3249 *
3250 * clean - no pending writes, but otherwise active.
3251 * When written to inactive array, starts without resync
3252 * If a write request arrives then
3253 * if metadata is known, mark 'dirty' and switch to 'active'.
3254 * if not known, block and switch to write-pending
3255 * If written to an active array that has pending writes, then fails.
3256 * active
3257 * fully active: IO and resync can be happening.
3258 * When written to inactive array, starts with resync
3259 *
3260 * write-pending
3261 * clean, but writes are blocked waiting for 'active' to be written.
3262 *
3263 * active-idle
3264 * like active, but no writes have been seen for a while (100msec).
3265 *
3266 */
3267enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3268 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003269static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003270 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3271 "write-pending", "active-idle", NULL };
3272
3273static int match_word(const char *word, char **list)
3274{
3275 int n;
3276 for (n=0; list[n]; n++)
3277 if (cmd_match(word, list[n]))
3278 break;
3279 return n;
3280}
3281
3282static ssize_t
3283array_state_show(mddev_t *mddev, char *page)
3284{
3285 enum array_state st = inactive;
3286
3287 if (mddev->pers)
3288 switch(mddev->ro) {
3289 case 1:
3290 st = readonly;
3291 break;
3292 case 2:
3293 st = read_auto;
3294 break;
3295 case 0:
3296 if (mddev->in_sync)
3297 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08003298 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3299 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003300 else if (mddev->safemode)
3301 st = active_idle;
3302 else
3303 st = active;
3304 }
3305 else {
3306 if (list_empty(&mddev->disks) &&
3307 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003308 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003309 st = clear;
3310 else
3311 st = inactive;
3312 }
3313 return sprintf(page, "%s\n", array_states[st]);
3314}
3315
Neil Browndf5b20c2008-07-11 22:02:22 +10003316static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrowna4bd82d2010-03-29 13:23:10 +11003317static int md_set_readonly(mddev_t * mddev, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003318static int do_md_run(mddev_t * mddev);
3319static int restart_array(mddev_t *mddev);
3320
3321static ssize_t
3322array_state_store(mddev_t *mddev, const char *buf, size_t len)
3323{
3324 int err = -EINVAL;
3325 enum array_state st = match_word(buf, array_states);
3326 switch(st) {
3327 case bad_word:
3328 break;
3329 case clear:
3330 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003331 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003332 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003333 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003334 break;
3335 case inactive:
3336 /* stopping an active array */
3337 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003338 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003339 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003340 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003341 } else
3342 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003343 break;
3344 case suspended:
3345 break; /* not supported yet */
3346 case readonly:
3347 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003348 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003349 else {
3350 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003351 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003352 err = do_md_run(mddev);
3353 }
3354 break;
3355 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003356 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003357 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003358 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003359 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003360 err = restart_array(mddev);
3361 if (err == 0) {
3362 mddev->ro = 2;
3363 set_disk_ro(mddev->gendisk, 0);
3364 }
NeilBrown9e653b62006-06-26 00:27:58 -07003365 } else {
3366 mddev->ro = 2;
3367 err = do_md_run(mddev);
3368 }
3369 break;
3370 case clean:
3371 if (mddev->pers) {
3372 restart_array(mddev);
3373 spin_lock_irq(&mddev->write_lock);
3374 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003375 if (mddev->in_sync == 0) {
3376 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003377 if (mddev->safemode == 1)
3378 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003379 if (mddev->persistent)
3380 set_bit(MD_CHANGE_CLEAN,
3381 &mddev->flags);
3382 }
3383 err = 0;
3384 } else
3385 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003386 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003387 } else
3388 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003389 break;
3390 case active:
3391 if (mddev->pers) {
3392 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003393 if (mddev->external)
3394 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003395 wake_up(&mddev->sb_wait);
3396 err = 0;
3397 } else {
3398 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003399 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003400 err = do_md_run(mddev);
3401 }
3402 break;
3403 case write_pending:
3404 case active_idle:
3405 /* these cannot be set */
3406 break;
3407 }
3408 if (err)
3409 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003410 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003411 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003412 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003413 }
NeilBrown9e653b62006-06-26 00:27:58 -07003414}
NeilBrown80ca3a42006-07-10 04:44:18 -07003415static struct md_sysfs_entry md_array_state =
3416__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003417
NeilBrown6d7ff732006-01-06 00:21:16 -08003418static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003419max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3420 return sprintf(page, "%d\n",
3421 atomic_read(&mddev->max_corr_read_errors));
3422}
3423
3424static ssize_t
3425max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3426{
3427 char *e;
3428 unsigned long n = simple_strtoul(buf, &e, 10);
3429
3430 if (*buf && (*e == 0 || *e == '\n')) {
3431 atomic_set(&mddev->max_corr_read_errors, n);
3432 return len;
3433 }
3434 return -EINVAL;
3435}
3436
3437static struct md_sysfs_entry max_corr_read_errors =
3438__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3439 max_corrected_read_errors_store);
3440
3441static ssize_t
NeilBrown6d7ff732006-01-06 00:21:16 -08003442null_show(mddev_t *mddev, char *page)
3443{
3444 return -EINVAL;
3445}
3446
3447static ssize_t
3448new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3449{
3450 /* buf must be %d:%d\n? giving major and minor numbers */
3451 /* The new device is added to the array.
3452 * If the array has a persistent superblock, we read the
3453 * superblock to initialise info and check validity.
3454 * Otherwise, only checking done is that in bind_rdev_to_array,
3455 * which mainly checks size.
3456 */
3457 char *e;
3458 int major = simple_strtoul(buf, &e, 10);
3459 int minor;
3460 dev_t dev;
3461 mdk_rdev_t *rdev;
3462 int err;
3463
3464 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3465 return -EINVAL;
3466 minor = simple_strtoul(e+1, &e, 10);
3467 if (*e && *e != '\n')
3468 return -EINVAL;
3469 dev = MKDEV(major, minor);
3470 if (major != MAJOR(dev) ||
3471 minor != MINOR(dev))
3472 return -EOVERFLOW;
3473
3474
3475 if (mddev->persistent) {
3476 rdev = md_import_device(dev, mddev->major_version,
3477 mddev->minor_version);
3478 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3479 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3480 mdk_rdev_t, same_set);
3481 err = super_types[mddev->major_version]
3482 .load_super(rdev, rdev0, mddev->minor_version);
3483 if (err < 0)
3484 goto out;
3485 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003486 } else if (mddev->external)
3487 rdev = md_import_device(dev, -2, -1);
3488 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003489 rdev = md_import_device(dev, -1, -1);
3490
3491 if (IS_ERR(rdev))
3492 return PTR_ERR(rdev);
3493 err = bind_rdev_to_array(rdev, mddev);
3494 out:
3495 if (err)
3496 export_rdev(rdev);
3497 return err ? err : len;
3498}
3499
3500static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003501__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003502
3503static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003504bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3505{
3506 char *end;
3507 unsigned long chunk, end_chunk;
3508
3509 if (!mddev->bitmap)
3510 goto out;
3511 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3512 while (*buf) {
3513 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3514 if (buf == end) break;
3515 if (*end == '-') { /* range */
3516 buf = end + 1;
3517 end_chunk = simple_strtoul(buf, &end, 0);
3518 if (buf == end) break;
3519 }
3520 if (*end && !isspace(*end)) break;
3521 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003522 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003523 }
3524 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3525out:
3526 return len;
3527}
3528
3529static struct md_sysfs_entry md_bitmap =
3530__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3531
3532static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003533size_show(mddev_t *mddev, char *page)
3534{
Andre Noll58c0fed2009-03-31 14:33:13 +11003535 return sprintf(page, "%llu\n",
3536 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003537}
3538
Andre Nolld71f9f82008-07-11 22:02:22 +10003539static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003540
3541static ssize_t
3542size_store(mddev_t *mddev, const char *buf, size_t len)
3543{
3544 /* If array is inactive, we can reduce the component size, but
3545 * not increase it (except from 0).
3546 * If array is active, we can try an on-line resize
3547 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003548 sector_t sectors;
3549 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003550
Andre Noll58c0fed2009-03-31 14:33:13 +11003551 if (err < 0)
3552 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003553 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003554 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003555 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003556 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003557 if (mddev->dev_sectors == 0 ||
3558 mddev->dev_sectors > sectors)
3559 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003560 else
3561 err = -ENOSPC;
3562 }
3563 return err ? err : len;
3564}
3565
3566static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003567__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003568
NeilBrown8bb93aa2006-01-06 00:20:50 -08003569
3570/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003571 * This is one of
3572 * 'none' for arrays with no metadata (good luck...)
3573 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003574 * or N.M for internally known formats
3575 */
3576static ssize_t
3577metadata_show(mddev_t *mddev, char *page)
3578{
3579 if (mddev->persistent)
3580 return sprintf(page, "%d.%d\n",
3581 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003582 else if (mddev->external)
3583 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003584 else
3585 return sprintf(page, "none\n");
3586}
3587
3588static ssize_t
3589metadata_store(mddev_t *mddev, const char *buf, size_t len)
3590{
3591 int major, minor;
3592 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003593 /* Changing the details of 'external' metadata is
3594 * always permitted. Otherwise there must be
3595 * no devices attached to the array.
3596 */
3597 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3598 ;
3599 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003600 return -EBUSY;
3601
3602 if (cmd_match(buf, "none")) {
3603 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003604 mddev->external = 0;
3605 mddev->major_version = 0;
3606 mddev->minor_version = 90;
3607 return len;
3608 }
3609 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003610 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003611 if (namelen >= sizeof(mddev->metadata_type))
3612 namelen = sizeof(mddev->metadata_type)-1;
3613 strncpy(mddev->metadata_type, buf+9, namelen);
3614 mddev->metadata_type[namelen] = 0;
3615 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3616 mddev->metadata_type[--namelen] = 0;
3617 mddev->persistent = 0;
3618 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003619 mddev->major_version = 0;
3620 mddev->minor_version = 90;
3621 return len;
3622 }
3623 major = simple_strtoul(buf, &e, 10);
3624 if (e==buf || *e != '.')
3625 return -EINVAL;
3626 buf = e+1;
3627 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003628 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003629 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003630 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003631 return -ENOENT;
3632 mddev->major_version = major;
3633 mddev->minor_version = minor;
3634 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003635 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003636 return len;
3637}
3638
3639static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003640__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003641
NeilBrowna35b0d62006-01-06 00:20:49 -08003642static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003643action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003644{
NeilBrown7eec3142005-11-08 21:39:44 -08003645 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003646 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3647 type = "frozen";
3648 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003649 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003650 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3651 type = "reshape";
3652 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003653 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3654 type = "resync";
3655 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3656 type = "check";
3657 else
3658 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003659 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003660 type = "recover";
3661 }
3662 return sprintf(page, "%s\n", type);
3663}
3664
3665static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003666action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003667{
NeilBrown7eec3142005-11-08 21:39:44 -08003668 if (!mddev->pers || !mddev->pers->sync_request)
3669 return -EINVAL;
3670
NeilBrownb6a9ce62009-05-26 09:41:17 +10003671 if (cmd_match(page, "frozen"))
3672 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3673 else
3674 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3675
3676 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003677 if (mddev->sync_thread) {
3678 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3679 md_unregister_thread(mddev->sync_thread);
3680 mddev->sync_thread = NULL;
3681 mddev->recovery = 0;
3682 }
NeilBrown03c902e2006-01-06 00:20:46 -08003683 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3684 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003685 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003686 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003687 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003688 else if (cmd_match(page, "recover")) {
3689 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3690 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3691 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003692 int err;
3693 if (mddev->pers->start_reshape == NULL)
3694 return -EINVAL;
3695 err = mddev->pers->start_reshape(mddev);
3696 if (err)
3697 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003698 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003699 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003700 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003701 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003702 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003703 return -EINVAL;
3704 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3705 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003706 }
NeilBrown03c902e2006-01-06 00:20:46 -08003707 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003708 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003709 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003710 return len;
3711}
3712
NeilBrown9d888832005-11-08 21:39:26 -08003713static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003714mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003715{
3716 return sprintf(page, "%llu\n",
3717 (unsigned long long) mddev->resync_mismatches);
3718}
3719
NeilBrown80ca3a42006-07-10 04:44:18 -07003720static struct md_sysfs_entry md_scan_mode =
3721__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003722
NeilBrown96de1e62005-11-08 21:39:39 -08003723
NeilBrown80ca3a42006-07-10 04:44:18 -07003724static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003725
NeilBrown88202a02006-01-06 00:21:36 -08003726static ssize_t
3727sync_min_show(mddev_t *mddev, char *page)
3728{
3729 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3730 mddev->sync_speed_min ? "local": "system");
3731}
3732
3733static ssize_t
3734sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3735{
3736 int min;
3737 char *e;
3738 if (strncmp(buf, "system", 6)==0) {
3739 mddev->sync_speed_min = 0;
3740 return len;
3741 }
3742 min = simple_strtoul(buf, &e, 10);
3743 if (buf == e || (*e && *e != '\n') || min <= 0)
3744 return -EINVAL;
3745 mddev->sync_speed_min = min;
3746 return len;
3747}
3748
3749static struct md_sysfs_entry md_sync_min =
3750__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3751
3752static ssize_t
3753sync_max_show(mddev_t *mddev, char *page)
3754{
3755 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3756 mddev->sync_speed_max ? "local": "system");
3757}
3758
3759static ssize_t
3760sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3761{
3762 int max;
3763 char *e;
3764 if (strncmp(buf, "system", 6)==0) {
3765 mddev->sync_speed_max = 0;
3766 return len;
3767 }
3768 max = simple_strtoul(buf, &e, 10);
3769 if (buf == e || (*e && *e != '\n') || max <= 0)
3770 return -EINVAL;
3771 mddev->sync_speed_max = max;
3772 return len;
3773}
3774
3775static struct md_sysfs_entry md_sync_max =
3776__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3777
Iustin Popd7f3d292007-10-16 23:30:54 -07003778static ssize_t
3779degraded_show(mddev_t *mddev, char *page)
3780{
3781 return sprintf(page, "%d\n", mddev->degraded);
3782}
3783static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003784
3785static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003786sync_force_parallel_show(mddev_t *mddev, char *page)
3787{
3788 return sprintf(page, "%d\n", mddev->parallel_resync);
3789}
3790
3791static ssize_t
3792sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3793{
3794 long n;
3795
3796 if (strict_strtol(buf, 10, &n))
3797 return -EINVAL;
3798
3799 if (n != 0 && n != 1)
3800 return -EINVAL;
3801
3802 mddev->parallel_resync = n;
3803
3804 if (mddev->sync_thread)
3805 wake_up(&resync_wait);
3806
3807 return len;
3808}
3809
3810/* force parallel resync, even with shared block devices */
3811static struct md_sysfs_entry md_sync_force_parallel =
3812__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3813 sync_force_parallel_show, sync_force_parallel_store);
3814
3815static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003816sync_speed_show(mddev_t *mddev, char *page)
3817{
3818 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003819 if (mddev->curr_resync == 0)
3820 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003821 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3822 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003823 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003824 db = resync - mddev->resync_mark_cnt;
3825 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003826}
3827
NeilBrown80ca3a42006-07-10 04:44:18 -07003828static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003829
3830static ssize_t
3831sync_completed_show(mddev_t *mddev, char *page)
3832{
Andre Noll58c0fed2009-03-31 14:33:13 +11003833 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003834
NeilBrownacb180b2009-04-14 16:28:34 +10003835 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3836 return sprintf(page, "none\n");
3837
NeilBrown88202a02006-01-06 00:21:36 -08003838 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003839 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003840 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003841 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003842
NeilBrownacb180b2009-04-14 16:28:34 +10003843 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003844 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003845}
3846
NeilBrown80ca3a42006-07-10 04:44:18 -07003847static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003848
NeilBrowne464eaf2006-03-27 01:18:14 -08003849static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003850min_sync_show(mddev_t *mddev, char *page)
3851{
3852 return sprintf(page, "%llu\n",
3853 (unsigned long long)mddev->resync_min);
3854}
3855static ssize_t
3856min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3857{
3858 unsigned long long min;
3859 if (strict_strtoull(buf, 10, &min))
3860 return -EINVAL;
3861 if (min > mddev->resync_max)
3862 return -EINVAL;
3863 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3864 return -EBUSY;
3865
3866 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003867 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003868 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003869 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003870 return -EINVAL;
3871 }
3872 mddev->resync_min = min;
3873
3874 return len;
3875}
3876
3877static struct md_sysfs_entry md_min_sync =
3878__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3879
3880static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003881max_sync_show(mddev_t *mddev, char *page)
3882{
3883 if (mddev->resync_max == MaxSector)
3884 return sprintf(page, "max\n");
3885 else
3886 return sprintf(page, "%llu\n",
3887 (unsigned long long)mddev->resync_max);
3888}
3889static ssize_t
3890max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3891{
3892 if (strncmp(buf, "max", 3) == 0)
3893 mddev->resync_max = MaxSector;
3894 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003895 unsigned long long max;
3896 if (strict_strtoull(buf, 10, &max))
3897 return -EINVAL;
3898 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003899 return -EINVAL;
3900 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003901 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003902 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3903 return -EBUSY;
3904
3905 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003906 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003907 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003908 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003909 return -EINVAL;
3910 }
3911 mddev->resync_max = max;
3912 }
3913 wake_up(&mddev->recovery_wait);
3914 return len;
3915}
3916
3917static struct md_sysfs_entry md_max_sync =
3918__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3919
3920static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003921suspend_lo_show(mddev_t *mddev, char *page)
3922{
3923 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3924}
3925
3926static ssize_t
3927suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3928{
3929 char *e;
3930 unsigned long long new = simple_strtoull(buf, &e, 10);
3931
NeilBrownb8d966e2009-07-01 11:14:04 +10003932 if (mddev->pers == NULL ||
3933 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003934 return -EINVAL;
3935 if (buf == e || (*e && *e != '\n'))
3936 return -EINVAL;
3937 if (new >= mddev->suspend_hi ||
3938 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3939 mddev->suspend_lo = new;
3940 mddev->pers->quiesce(mddev, 2);
3941 return len;
3942 } else
3943 return -EINVAL;
3944}
3945static struct md_sysfs_entry md_suspend_lo =
3946__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3947
3948
3949static ssize_t
3950suspend_hi_show(mddev_t *mddev, char *page)
3951{
3952 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3953}
3954
3955static ssize_t
3956suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3957{
3958 char *e;
3959 unsigned long long new = simple_strtoull(buf, &e, 10);
3960
NeilBrownb8d966e2009-07-01 11:14:04 +10003961 if (mddev->pers == NULL ||
3962 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003963 return -EINVAL;
3964 if (buf == e || (*e && *e != '\n'))
3965 return -EINVAL;
3966 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3967 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3968 mddev->suspend_hi = new;
3969 mddev->pers->quiesce(mddev, 1);
3970 mddev->pers->quiesce(mddev, 0);
3971 return len;
3972 } else
3973 return -EINVAL;
3974}
3975static struct md_sysfs_entry md_suspend_hi =
3976__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3977
NeilBrown08a02ec2007-05-09 02:35:38 -07003978static ssize_t
3979reshape_position_show(mddev_t *mddev, char *page)
3980{
3981 if (mddev->reshape_position != MaxSector)
3982 return sprintf(page, "%llu\n",
3983 (unsigned long long)mddev->reshape_position);
3984 strcpy(page, "none\n");
3985 return 5;
3986}
3987
3988static ssize_t
3989reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3990{
3991 char *e;
3992 unsigned long long new = simple_strtoull(buf, &e, 10);
3993 if (mddev->pers)
3994 return -EBUSY;
3995 if (buf == e || (*e && *e != '\n'))
3996 return -EINVAL;
3997 mddev->reshape_position = new;
3998 mddev->delta_disks = 0;
3999 mddev->new_level = mddev->level;
4000 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004001 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004002 return len;
4003}
4004
4005static struct md_sysfs_entry md_reshape_position =
4006__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4007 reshape_position_store);
4008
Dan Williamsb522adc2009-03-31 15:00:31 +11004009static ssize_t
4010array_size_show(mddev_t *mddev, char *page)
4011{
4012 if (mddev->external_size)
4013 return sprintf(page, "%llu\n",
4014 (unsigned long long)mddev->array_sectors/2);
4015 else
4016 return sprintf(page, "default\n");
4017}
4018
4019static ssize_t
4020array_size_store(mddev_t *mddev, const char *buf, size_t len)
4021{
4022 sector_t sectors;
4023
4024 if (strncmp(buf, "default", 7) == 0) {
4025 if (mddev->pers)
4026 sectors = mddev->pers->size(mddev, 0, 0);
4027 else
4028 sectors = mddev->array_sectors;
4029
4030 mddev->external_size = 0;
4031 } else {
4032 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4033 return -EINVAL;
4034 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004035 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004036
4037 mddev->external_size = 1;
4038 }
4039
4040 mddev->array_sectors = sectors;
4041 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004042 if (mddev->pers)
4043 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11004044
4045 return len;
4046}
4047
4048static struct md_sysfs_entry md_array_size =
4049__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4050 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004051
NeilBrowneae17012005-11-08 21:39:23 -08004052static struct attribute *md_default_attrs[] = {
4053 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004054 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004055 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004056 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004057 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004058 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004059 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004060 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004061 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004062 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004063 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004064 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004065 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004066 NULL,
4067};
4068
4069static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004070 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004071 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004072 &md_sync_min.attr,
4073 &md_sync_max.attr,
4074 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004075 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004076 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004077 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004078 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004079 &md_suspend_lo.attr,
4080 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004081 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004082 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004083 NULL,
4084};
NeilBrown411036f2005-11-08 21:39:40 -08004085static struct attribute_group md_redundancy_group = {
4086 .name = NULL,
4087 .attrs = md_redundancy_attrs,
4088};
4089
NeilBrowneae17012005-11-08 21:39:23 -08004090
4091static ssize_t
4092md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4093{
4094 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4095 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004096 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004097
4098 if (!entry->show)
4099 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004100 rv = mddev_lock(mddev);
4101 if (!rv) {
4102 rv = entry->show(mddev, page);
4103 mddev_unlock(mddev);
4104 }
NeilBrown96de1e62005-11-08 21:39:39 -08004105 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004106}
4107
4108static ssize_t
4109md_attr_store(struct kobject *kobj, struct attribute *attr,
4110 const char *page, size_t length)
4111{
4112 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4113 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004114 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004115
4116 if (!entry->store)
4117 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004118 if (!capable(CAP_SYS_ADMIN))
4119 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004120 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004121 if (mddev->hold_active == UNTIL_IOCTL)
4122 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004123 if (!rv) {
4124 rv = entry->store(mddev, page, length);
4125 mddev_unlock(mddev);
4126 }
NeilBrown96de1e62005-11-08 21:39:39 -08004127 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004128}
4129
4130static void md_free(struct kobject *ko)
4131{
4132 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004133
4134 if (mddev->sysfs_state)
4135 sysfs_put(mddev->sysfs_state);
4136
4137 if (mddev->gendisk) {
4138 del_gendisk(mddev->gendisk);
4139 put_disk(mddev->gendisk);
4140 }
4141 if (mddev->queue)
4142 blk_cleanup_queue(mddev->queue);
4143
NeilBrowneae17012005-11-08 21:39:23 -08004144 kfree(mddev);
4145}
4146
4147static struct sysfs_ops md_sysfs_ops = {
4148 .show = md_attr_show,
4149 .store = md_attr_store,
4150};
4151static struct kobj_type md_ktype = {
4152 .release = md_free,
4153 .sysfs_ops = &md_sysfs_ops,
4154 .default_attrs = md_default_attrs,
4155};
4156
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157int mdp_major = 0;
4158
Dan Williams5fd3a172009-03-04 00:57:25 -07004159static void mddev_delayed_delete(struct work_struct *ws)
4160{
4161 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4162
NeilBrown43a70502009-12-14 12:49:55 +11004163 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004164 kobject_del(&mddev->kobj);
4165 kobject_put(&mddev->kobj);
4166}
4167
NeilBrownefeb53c2009-01-09 08:31:10 +11004168static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004170 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 mddev_t *mddev = mddev_find(dev);
4172 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004173 int partitioned;
4174 int shift;
4175 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004176 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
4178 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004179 return -ENODEV;
4180
4181 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4182 shift = partitioned ? MdpMinorShift : 0;
4183 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184
NeilBrownd3374822009-01-09 08:31:10 +11004185 /* wait for any previous instance if this device
4186 * to be completed removed (mddev_delayed_delete).
4187 */
4188 flush_scheduled_work();
4189
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004190 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004191 error = -EEXIST;
4192 if (mddev->gendisk)
4193 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004194
4195 if (name) {
4196 /* Need to ensure that 'name' is not a duplicate.
4197 */
4198 mddev_t *mddev2;
4199 spin_lock(&all_mddevs_lock);
4200
4201 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4202 if (mddev2->gendisk &&
4203 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4204 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004205 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004206 }
4207 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 }
NeilBrown8b765392009-01-09 08:31:08 +11004209
NeilBrown0909dc42009-07-01 12:27:21 +10004210 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004211 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004212 if (!mddev->queue)
4213 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004214 mddev->queue->queuedata = mddev;
4215
NeilBrown8b765392009-01-09 08:31:08 +11004216 /* Can be unlocked because the queue is new: no concurrency */
4217 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4218
NeilBrown409c57f2009-03-31 14:39:39 +11004219 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004220
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 disk = alloc_disk(1 << shift);
4222 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004223 blk_cleanup_queue(mddev->queue);
4224 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004225 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004227 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004229 if (name)
4230 strcpy(disk->disk_name, name);
4231 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004233 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 disk->fops = &md_fops;
4236 disk->private_data = mddev;
4237 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004238 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004239 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004240 * remove it now.
4241 */
4242 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 add_disk(disk);
4244 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004245 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4246 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004247 if (error) {
4248 /* This isn't possible, but as kobject_init_and_add is marked
4249 * __must_check, we must do something with the result
4250 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004251 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4252 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004253 error = 0;
4254 }
NeilBrown43a70502009-12-14 12:49:55 +11004255 if (sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4256 printk(KERN_DEBUG "pointless warning\n");
NeilBrown0909dc42009-07-01 12:27:21 +10004257 abort:
4258 mutex_unlock(&disks_mutex);
4259 if (!error) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004260 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11004261 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
4262 }
NeilBrownd3374822009-01-09 08:31:10 +11004263 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004264 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004265}
4266
4267static struct kobject *md_probe(dev_t dev, int *part, void *data)
4268{
4269 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 return NULL;
4271}
4272
NeilBrownefeb53c2009-01-09 08:31:10 +11004273static int add_named_array(const char *val, struct kernel_param *kp)
4274{
4275 /* val must be "md_*" where * is not all digits.
4276 * We allocate an array with a large free minor number, and
4277 * set the name to val. val must not already be an active name.
4278 */
4279 int len = strlen(val);
4280 char buf[DISK_NAME_LEN];
4281
4282 while (len && val[len-1] == '\n')
4283 len--;
4284 if (len >= DISK_NAME_LEN)
4285 return -E2BIG;
4286 strlcpy(buf, val, len+1);
4287 if (strncmp(buf, "md_", 3) != 0)
4288 return -EINVAL;
4289 return md_alloc(0, buf);
4290}
4291
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292static void md_safemode_timeout(unsigned long data)
4293{
4294 mddev_t *mddev = (mddev_t *) data;
4295
Neil Brown0fd62b82008-06-28 08:31:36 +10004296 if (!atomic_read(&mddev->writes_pending)) {
4297 mddev->safemode = 1;
4298 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11004299 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 md_wakeup_thread(mddev->thread);
4302}
4303
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004304static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305
NeilBrownfe60b012010-03-29 11:10:42 +11004306static int md_run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307{
NeilBrown2604b702006-01-06 00:20:36 -08004308 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 mdk_rdev_t *rdev;
NeilBrown2604b702006-01-06 00:20:36 -08004310 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311
NeilBrowna757e642005-04-16 15:26:42 -07004312 if (list_empty(&mddev->disks))
4313 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315
4316 if (mddev->pers)
4317 return -EBUSY;
4318
NeilBrownb6eb1272010-04-15 10:13:47 +10004319 /* These two calls synchronise us with the
4320 * sysfs_remove_group calls in mddev_unlock,
4321 * so they must have completed.
4322 */
4323 mutex_lock(&mddev->open_mutex);
4324 mutex_unlock(&mddev->open_mutex);
4325
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 /*
4327 * Analyze all RAID superblock(s)
4328 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004329 if (!mddev->raid_disks) {
4330 if (!mddev->persistent)
4331 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004332 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
NeilBrownd9d166c2006-01-06 00:20:51 -08004335 if (mddev->level != LEVEL_NONE)
4336 request_module("md-level-%d", mddev->level);
4337 else if (mddev->clevel[0])
4338 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
4340 /*
4341 * Drop all container device buffers, from now on
4342 * the only valid external interface is through the md
4343 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004345 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004346 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 continue;
4348 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004349 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004350
4351 /* perform some consistency tests on the device.
4352 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004353 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004354 */
Andre Noll0f420352008-07-11 22:02:23 +10004355 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004356 if (mddev->dev_sectors &&
4357 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004358 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004359 printk("md: %s: data overlaps metadata\n",
4360 mdname(mddev));
4361 return -EINVAL;
4362 }
4363 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004364 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004365 > rdev->data_offset) {
4366 printk("md: %s: metadata overlaps data\n",
4367 mdname(mddev));
4368 return -EINVAL;
4369 }
4370 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004371 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 }
4373
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004375 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004376 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004378 if (mddev->level != LEVEL_NONE)
4379 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4380 mddev->level);
4381 else
4382 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4383 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 return -EINVAL;
4385 }
NeilBrown2604b702006-01-06 00:20:36 -08004386 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004388 if (mddev->level != pers->level) {
4389 mddev->level = pers->level;
4390 mddev->new_level = pers->level;
4391 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004392 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393
NeilBrownf6705572006-03-27 01:18:11 -08004394 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004395 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004396 /* This personality cannot handle reshaping... */
4397 mddev->pers = NULL;
4398 module_put(pers->owner);
4399 return -EINVAL;
4400 }
4401
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004402 if (pers->sync_request) {
4403 /* Warn if this is a potentially silly
4404 * configuration.
4405 */
4406 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4407 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004408 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004409
4410 list_for_each_entry(rdev, &mddev->disks, same_set)
4411 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004412 if (rdev < rdev2 &&
4413 rdev->bdev->bd_contains ==
4414 rdev2->bdev->bd_contains) {
4415 printk(KERN_WARNING
4416 "%s: WARNING: %s appears to be"
4417 " on the same physical disk as"
4418 " %s.\n",
4419 mdname(mddev),
4420 bdevname(rdev->bdev,b),
4421 bdevname(rdev2->bdev,b2));
4422 warned = 1;
4423 }
4424 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004425
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004426 if (warned)
4427 printk(KERN_WARNING
4428 "True protection against single-disk"
4429 " failure might be compromised.\n");
4430 }
4431
NeilBrown657390d2005-08-26 18:34:16 -07004432 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004433 /* may be over-ridden by personality */
4434 mddev->resync_max_sectors = mddev->dev_sectors;
4435
NeilBrowna9701a32005-11-08 21:39:34 -08004436 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004437 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438
NeilBrown0f9552b2009-12-30 12:08:50 +11004439 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004440 mddev->ro = 2; /* read-only, but switch on first write */
4441
NeilBrownb15c2e52006-01-06 00:20:16 -08004442 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004443 if (err)
4444 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004445 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4446 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4447 " but 'external_size' not in effect?\n", __func__);
4448 printk(KERN_ERR
4449 "md: invalid array_size %llu > default size %llu\n",
4450 (unsigned long long)mddev->array_sectors / 2,
4451 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4452 err = -EINVAL;
4453 mddev->pers->stop(mddev);
4454 }
4455 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004456 err = bitmap_create(mddev);
4457 if (err) {
4458 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4459 mdname(mddev), err);
4460 mddev->pers->stop(mddev);
4461 }
4462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 module_put(mddev->pers->owner);
4465 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004466 bitmap_destroy(mddev);
4467 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004469 if (mddev->pers->sync_request) {
4470 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4471 printk(KERN_WARNING
4472 "md: cannot register extra attributes for %s\n",
4473 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004474 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004475 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004476 mddev->ro = 0;
4477
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004479 atomic_set(&mddev->max_corr_read_errors,
4480 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 mddev->safemode = 0;
4482 mddev->safemode_timer.function = md_safemode_timeout;
4483 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004484 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004486
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004487 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004488 if (rdev->raid_disk >= 0) {
4489 char nm[20];
4490 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004491 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4492 printk("md: cannot register %s for %s\n",
4493 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495
4496 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4497
NeilBrown850b2b42006-10-03 01:15:46 -07004498 if (mddev->flags)
4499 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500
NeilBrown0b8c9de2006-07-10 04:44:15 -07004501 md_wakeup_thread(mddev->thread);
4502 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004503
NeilBrownd7603b72006-01-06 00:20:30 -08004504 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004505 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004506 if (mddev->sysfs_action)
4507 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004508 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 return 0;
4510}
4511
NeilBrownfe60b012010-03-29 11:10:42 +11004512static int do_md_run(mddev_t *mddev)
4513{
4514 int err;
4515
4516 err = md_run(mddev);
4517 if (err)
4518 goto out;
4519
4520 set_capacity(mddev->gendisk, mddev->array_sectors);
4521 revalidate_disk(mddev->gendisk);
4522 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4523out:
4524 return err;
4525}
4526
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527static int restart_array(mddev_t *mddev)
4528{
4529 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530
Andre Noll80fab1d2008-07-11 22:02:21 +10004531 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004533 return -ENXIO;
4534 if (!mddev->pers)
4535 return -EINVAL;
4536 if (!mddev->ro)
4537 return -EBUSY;
4538 mddev->safemode = 0;
4539 mddev->ro = 0;
4540 set_disk_ro(disk, 0);
4541 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4542 mdname(mddev));
4543 /* Kick recovery or resync if necessary */
4544 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4545 md_wakeup_thread(mddev->thread);
4546 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004547 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004548 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549}
4550
NeilBrownacc55e22006-06-26 00:27:47 -07004551/* similar to deny_write_access, but accounts for our holding a reference
4552 * to the file ourselves */
4553static int deny_bitmap_write_access(struct file * file)
4554{
4555 struct inode *inode = file->f_mapping->host;
4556
4557 spin_lock(&inode->i_lock);
4558 if (atomic_read(&inode->i_writecount) > 1) {
4559 spin_unlock(&inode->i_lock);
4560 return -ETXTBSY;
4561 }
4562 atomic_set(&inode->i_writecount, -1);
4563 spin_unlock(&inode->i_lock);
4564
4565 return 0;
4566}
4567
NeilBrown43a70502009-12-14 12:49:55 +11004568void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004569{
4570 struct inode *inode = file->f_mapping->host;
4571
4572 spin_lock(&inode->i_lock);
4573 atomic_set(&inode->i_writecount, 1);
4574 spin_unlock(&inode->i_lock);
4575}
4576
NeilBrown6177b472010-03-29 11:37:13 +11004577static void md_clean(mddev_t *mddev)
4578{
4579 mddev->array_sectors = 0;
4580 mddev->external_size = 0;
4581 mddev->dev_sectors = 0;
4582 mddev->raid_disks = 0;
4583 mddev->recovery_cp = 0;
4584 mddev->resync_min = 0;
4585 mddev->resync_max = MaxSector;
4586 mddev->reshape_position = MaxSector;
4587 mddev->external = 0;
4588 mddev->persistent = 0;
4589 mddev->level = LEVEL_NONE;
4590 mddev->clevel[0] = 0;
4591 mddev->flags = 0;
4592 mddev->ro = 0;
4593 mddev->metadata_type[0] = 0;
4594 mddev->chunk_sectors = 0;
4595 mddev->ctime = mddev->utime = 0;
4596 mddev->layout = 0;
4597 mddev->max_disks = 0;
4598 mddev->events = 0;
4599 mddev->delta_disks = 0;
4600 mddev->new_level = LEVEL_NONE;
4601 mddev->new_layout = 0;
4602 mddev->new_chunk_sectors = 0;
4603 mddev->curr_resync = 0;
4604 mddev->resync_mismatches = 0;
4605 mddev->suspend_lo = mddev->suspend_hi = 0;
4606 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4607 mddev->recovery = 0;
4608 mddev->in_sync = 0;
4609 mddev->degraded = 0;
4610 mddev->barriers_work = 0;
4611 mddev->safemode = 0;
4612 mddev->bitmap_info.offset = 0;
4613 mddev->bitmap_info.default_offset = 0;
4614 mddev->bitmap_info.chunksize = 0;
4615 mddev->bitmap_info.daemon_sleep = 0;
4616 mddev->bitmap_info.max_write_behind = 0;
4617}
4618
NeilBrowna047e122010-03-29 12:07:53 +11004619static void md_stop_writes(mddev_t *mddev)
4620{
4621 if (mddev->sync_thread) {
4622 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4623 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4624 md_unregister_thread(mddev->sync_thread);
4625 mddev->sync_thread = NULL;
4626 }
4627
4628 del_timer_sync(&mddev->safemode_timer);
4629
4630 bitmap_flush(mddev);
4631 md_super_wait(mddev);
4632
4633 if (!mddev->in_sync || mddev->flags) {
4634 /* mark array as shutdown cleanly */
4635 mddev->in_sync = 1;
4636 md_update_sb(mddev, 1);
4637 }
4638}
4639
NeilBrown6177b472010-03-29 11:37:13 +11004640static void md_stop(mddev_t *mddev)
4641{
NeilBrowncca9cf92010-04-01 12:08:16 +11004642 md_stop_writes(mddev);
4643
NeilBrown6177b472010-03-29 11:37:13 +11004644 mddev->pers->stop(mddev);
4645 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4646 mddev->to_remove = &md_redundancy_group;
4647 module_put(mddev->pers->owner);
4648 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11004649 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11004650}
4651
NeilBrowna4bd82d2010-03-29 13:23:10 +11004652static int md_set_readonly(mddev_t *mddev, int is_open)
4653{
4654 int err = 0;
4655 mutex_lock(&mddev->open_mutex);
4656 if (atomic_read(&mddev->openers) > is_open) {
4657 printk("md: %s still in use.\n",mdname(mddev));
4658 err = -EBUSY;
4659 goto out;
4660 }
4661 if (mddev->pers) {
4662 md_stop_writes(mddev);
4663
4664 err = -ENXIO;
4665 if (mddev->ro==1)
4666 goto out;
4667 mddev->ro = 1;
4668 set_disk_ro(mddev->gendisk, 1);
4669 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4670 sysfs_notify_dirent(mddev->sysfs_state);
4671 err = 0;
4672 }
4673out:
4674 mutex_unlock(&mddev->open_mutex);
4675 return err;
4676}
4677
NeilBrown9e653b62006-06-26 00:27:58 -07004678/* mode:
4679 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07004680 * 2 - stop but do not disassemble array
4681 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004682static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683{
4684 int err = 0;
4685 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004686 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
NeilBrownc8c00a62009-08-10 12:50:52 +10004688 mutex_lock(&mddev->open_mutex);
NeilBrownf2ea68c2008-07-21 17:05:25 +10004689 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004690 printk("md: %s still in use.\n",mdname(mddev));
NeilBrownc8c00a62009-08-10 12:50:52 +10004691 err = -EBUSY;
4692 } else if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693
NeilBrowna4bd82d2010-03-29 13:23:10 +11004694 if (mddev->ro)
4695 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004696
NeilBrowna4bd82d2010-03-29 13:23:10 +11004697 md_stop(mddev);
4698 mddev->queue->merge_bvec_fn = NULL;
4699 mddev->queue->unplug_fn = NULL;
4700 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004701
NeilBrowna4bd82d2010-03-29 13:23:10 +11004702 /* tell userspace to handle 'inactive' */
4703 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004704
NeilBrowna4bd82d2010-03-29 13:23:10 +11004705 list_for_each_entry(rdev, &mddev->disks, same_set)
4706 if (rdev->raid_disk >= 0) {
4707 char nm[20];
4708 sprintf(nm, "rd%d", rdev->raid_disk);
4709 sysfs_remove_link(&mddev->kobj, nm);
4710 }
NeilBrownc4647292009-05-07 12:51:06 +10004711
NeilBrowna4bd82d2010-03-29 13:23:10 +11004712 set_capacity(disk, 0);
4713 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08004714
NeilBrowna4bd82d2010-03-29 13:23:10 +11004715 if (mddev->ro)
4716 mddev->ro = 0;
4717
NeilBrown80ffb3c2009-08-18 10:35:26 +10004718 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 }
NeilBrownc8c00a62009-08-10 12:50:52 +10004720 mutex_unlock(&mddev->open_mutex);
4721 if (err)
4722 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 /*
4724 * Free resources if final stop
4725 */
NeilBrown9e653b62006-06-26 00:27:58 -07004726 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004727
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4729
NeilBrown978f9462006-02-02 14:28:05 -08004730 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004731 if (mddev->bitmap_info.file) {
4732 restore_bitmap_write_access(mddev->bitmap_info.file);
4733 fput(mddev->bitmap_info.file);
4734 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004735 }
NeilBrownc3d97142009-12-14 12:49:52 +11004736 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004737
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 export_array(mddev);
4739
NeilBrown6177b472010-03-29 11:37:13 +11004740 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11004741 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004742 if (mddev->hold_active == UNTIL_STOP)
4743 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004744
NeilBrowna4bd82d2010-03-29 13:23:10 +11004745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004747 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004748 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004749 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 return err;
4751}
4752
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004753#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754static void autorun_array(mddev_t *mddev)
4755{
4756 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 int err;
4758
NeilBrowna757e642005-04-16 15:26:42 -07004759 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761
4762 printk(KERN_INFO "md: running: ");
4763
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004764 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 char b[BDEVNAME_SIZE];
4766 printk("<%s>", bdevname(rdev->bdev,b));
4767 }
4768 printk("\n");
4769
NeilBrownd710e132008-10-13 11:55:12 +11004770 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 if (err) {
4772 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004773 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 }
4775}
4776
4777/*
4778 * lets try to run arrays based on all disks that have arrived
4779 * until now. (those are in pending_raid_disks)
4780 *
4781 * the method: pick the first pending disk, collect all disks with
4782 * the same UUID, remove all from the pending list and put them into
4783 * the 'same_array' list. Then order this list based on superblock
4784 * update time (freshest comes first), kick out 'old' disks and
4785 * compare superblocks. If everything's fine then run it.
4786 *
4787 * If "unit" is allocated, then bump its reference count
4788 */
4789static void autorun_devices(int part)
4790{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004791 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 mddev_t *mddev;
4793 char b[BDEVNAME_SIZE];
4794
4795 printk(KERN_INFO "md: autorun ...\n");
4796 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004797 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004799 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 rdev0 = list_entry(pending_raid_disks.next,
4801 mdk_rdev_t, same_set);
4802
4803 printk(KERN_INFO "md: considering %s ...\n",
4804 bdevname(rdev0->bdev,b));
4805 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004806 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 if (super_90_load(rdev, rdev0, 0) >= 0) {
4808 printk(KERN_INFO "md: adding %s ...\n",
4809 bdevname(rdev->bdev,b));
4810 list_move(&rdev->same_set, &candidates);
4811 }
4812 /*
4813 * now we have a set of devices, with all of them having
4814 * mostly sane superblocks. It's time to allocate the
4815 * mddev.
4816 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004817 if (part) {
4818 dev = MKDEV(mdp_major,
4819 rdev0->preferred_minor << MdpMinorShift);
4820 unit = MINOR(dev) >> MdpMinorShift;
4821 } else {
4822 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4823 unit = MINOR(dev);
4824 }
4825 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4827 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4828 break;
4829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830
4831 md_probe(dev, NULL, NULL);
4832 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004833 if (!mddev || !mddev->gendisk) {
4834 if (mddev)
4835 mddev_put(mddev);
4836 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 "md: cannot allocate memory for md drive.\n");
4838 break;
4839 }
4840 if (mddev_lock(mddev))
4841 printk(KERN_WARNING "md: %s locked, cannot run\n",
4842 mdname(mddev));
4843 else if (mddev->raid_disks || mddev->major_version
4844 || !list_empty(&mddev->disks)) {
4845 printk(KERN_WARNING
4846 "md: %s already running, cannot run %s\n",
4847 mdname(mddev), bdevname(rdev0->bdev,b));
4848 mddev_unlock(mddev);
4849 } else {
4850 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004851 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004852 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 list_del_init(&rdev->same_set);
4854 if (bind_rdev_to_array(rdev, mddev))
4855 export_rdev(rdev);
4856 }
4857 autorun_array(mddev);
4858 mddev_unlock(mddev);
4859 }
4860 /* on success, candidates will be empty, on error
4861 * it won't...
4862 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004863 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004864 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 mddev_put(mddev);
4868 }
4869 printk(KERN_INFO "md: ... autorun DONE.\n");
4870}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004871#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873static int get_version(void __user * arg)
4874{
4875 mdu_version_t ver;
4876
4877 ver.major = MD_MAJOR_VERSION;
4878 ver.minor = MD_MINOR_VERSION;
4879 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4880
4881 if (copy_to_user(arg, &ver, sizeof(ver)))
4882 return -EFAULT;
4883
4884 return 0;
4885}
4886
4887static int get_array_info(mddev_t * mddev, void __user * arg)
4888{
4889 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004890 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892
NeilBrowna9f326e2009-09-23 18:06:41 +10004893 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004894 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004896 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 failed++;
4898 else {
4899 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004900 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004901 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 else
4903 spare++;
4904 }
4905 }
4906
4907 info.major_version = mddev->major_version;
4908 info.minor_version = mddev->minor_version;
4909 info.patch_version = MD_PATCHLEVEL_VERSION;
4910 info.ctime = mddev->ctime;
4911 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004912 info.size = mddev->dev_sectors / 2;
4913 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004914 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 info.nr_disks = nr;
4916 info.raid_disks = mddev->raid_disks;
4917 info.md_minor = mddev->md_minor;
4918 info.not_persistent= !mddev->persistent;
4919
4920 info.utime = mddev->utime;
4921 info.state = 0;
4922 if (mddev->in_sync)
4923 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11004924 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07004925 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004926 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 info.working_disks = working;
4928 info.failed_disks = failed;
4929 info.spare_disks = spare;
4930
4931 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004932 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933
4934 if (copy_to_user(arg, &info, sizeof(info)))
4935 return -EFAULT;
4936
4937 return 0;
4938}
4939
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004940static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004941{
4942 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4943 char *ptr, *buf = NULL;
4944 int err = -ENOMEM;
4945
Dan Williamsb5470dc2008-06-27 21:44:04 -07004946 if (md_allow_write(mddev))
4947 file = kmalloc(sizeof(*file), GFP_NOIO);
4948 else
4949 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004950
NeilBrown32a76272005-06-21 17:17:14 -07004951 if (!file)
4952 goto out;
4953
4954 /* bitmap disabled, zero the first byte and copy out */
4955 if (!mddev->bitmap || !mddev->bitmap->file) {
4956 file->pathname[0] = '\0';
4957 goto copy_out;
4958 }
4959
4960 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4961 if (!buf)
4962 goto out;
4963
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004964 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4965 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004966 goto out;
4967
4968 strcpy(file->pathname, ptr);
4969
4970copy_out:
4971 err = 0;
4972 if (copy_to_user(arg, file, sizeof(*file)))
4973 err = -EFAULT;
4974out:
4975 kfree(buf);
4976 kfree(file);
4977 return err;
4978}
4979
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980static int get_disk_info(mddev_t * mddev, void __user * arg)
4981{
4982 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 mdk_rdev_t *rdev;
4984
4985 if (copy_from_user(&info, arg, sizeof(info)))
4986 return -EFAULT;
4987
Andre Noll26ef3792008-07-11 22:02:21 +10004988 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 if (rdev) {
4990 info.major = MAJOR(rdev->bdev->bd_dev);
4991 info.minor = MINOR(rdev->bdev->bd_dev);
4992 info.raid_disk = rdev->raid_disk;
4993 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004994 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004996 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 info.state |= (1<<MD_DISK_ACTIVE);
4998 info.state |= (1<<MD_DISK_SYNC);
4999 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005000 if (test_bit(WriteMostly, &rdev->flags))
5001 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 } else {
5003 info.major = info.minor = 0;
5004 info.raid_disk = -1;
5005 info.state = (1<<MD_DISK_REMOVED);
5006 }
5007
5008 if (copy_to_user(arg, &info, sizeof(info)))
5009 return -EFAULT;
5010
5011 return 0;
5012}
5013
5014static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5015{
5016 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5017 mdk_rdev_t *rdev;
5018 dev_t dev = MKDEV(info->major,info->minor);
5019
5020 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5021 return -EOVERFLOW;
5022
5023 if (!mddev->raid_disks) {
5024 int err;
5025 /* expecting a device which has a superblock */
5026 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5027 if (IS_ERR(rdev)) {
5028 printk(KERN_WARNING
5029 "md: md_import_device returned %ld\n",
5030 PTR_ERR(rdev));
5031 return PTR_ERR(rdev);
5032 }
5033 if (!list_empty(&mddev->disks)) {
5034 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5035 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005036 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 .load_super(rdev, rdev0, mddev->minor_version);
5038 if (err < 0) {
5039 printk(KERN_WARNING
5040 "md: %s has different UUID to %s\n",
5041 bdevname(rdev->bdev,b),
5042 bdevname(rdev0->bdev,b2));
5043 export_rdev(rdev);
5044 return -EINVAL;
5045 }
5046 }
5047 err = bind_rdev_to_array(rdev, mddev);
5048 if (err)
5049 export_rdev(rdev);
5050 return err;
5051 }
5052
5053 /*
5054 * add_new_disk can be used once the array is assembled
5055 * to add "hot spares". They must already have a superblock
5056 * written
5057 */
5058 if (mddev->pers) {
5059 int err;
5060 if (!mddev->pers->hot_add_disk) {
5061 printk(KERN_WARNING
5062 "%s: personality does not support diskops!\n",
5063 mdname(mddev));
5064 return -EINVAL;
5065 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005066 if (mddev->persistent)
5067 rdev = md_import_device(dev, mddev->major_version,
5068 mddev->minor_version);
5069 else
5070 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 if (IS_ERR(rdev)) {
5072 printk(KERN_WARNING
5073 "md: md_import_device returned %ld\n",
5074 PTR_ERR(rdev));
5075 return PTR_ERR(rdev);
5076 }
NeilBrown41158c72005-06-21 17:17:25 -07005077 /* set save_raid_disk if appropriate */
5078 if (!mddev->persistent) {
5079 if (info->state & (1<<MD_DISK_SYNC) &&
5080 info->raid_disk < mddev->raid_disks)
5081 rdev->raid_disk = info->raid_disk;
5082 else
5083 rdev->raid_disk = -1;
5084 } else
5085 super_types[mddev->major_version].
5086 validate_super(mddev, rdev);
5087 rdev->saved_raid_disk = rdev->raid_disk;
5088
NeilBrownb2d444d2005-11-08 21:39:31 -08005089 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005090 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5091 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005092 else
5093 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005094
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 rdev->raid_disk = -1;
5096 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005097 if (!err && !mddev->pers->hot_remove_disk) {
5098 /* If there is hot_add_disk but no hot_remove_disk
5099 * then added disks for geometry changes,
5100 * and should be added immediately.
5101 */
5102 super_types[mddev->major_version].
5103 validate_super(mddev, rdev);
5104 err = mddev->pers->hot_add_disk(mddev, rdev);
5105 if (err)
5106 unbind_rdev_from_array(rdev);
5107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 if (err)
5109 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005110 else
NeilBrown3c0ee632008-10-21 13:25:28 +11005111 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005112
NeilBrown17571282006-12-10 02:20:52 -08005113 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005114 if (mddev->degraded)
5115 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005116 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07005117 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 return err;
5119 }
5120
5121 /* otherwise, add_new_disk is only allowed
5122 * for major_version==0 superblocks
5123 */
5124 if (mddev->major_version != 0) {
5125 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5126 mdname(mddev));
5127 return -EINVAL;
5128 }
5129
5130 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5131 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005132 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 if (IS_ERR(rdev)) {
5134 printk(KERN_WARNING
5135 "md: error, md_import_device() returned %ld\n",
5136 PTR_ERR(rdev));
5137 return PTR_ERR(rdev);
5138 }
5139 rdev->desc_nr = info->number;
5140 if (info->raid_disk < mddev->raid_disks)
5141 rdev->raid_disk = info->raid_disk;
5142 else
5143 rdev->raid_disk = -1;
5144
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005146 if (info->state & (1<<MD_DISK_SYNC))
5147 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005149 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5150 set_bit(WriteMostly, &rdev->flags);
5151
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 if (!mddev->persistent) {
5153 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10005154 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 } else
Andre Noll0f420352008-07-11 22:02:23 +10005156 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10005157 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158
NeilBrown2bf071b2006-01-06 00:20:55 -08005159 err = bind_rdev_to_array(rdev, mddev);
5160 if (err) {
5161 export_rdev(rdev);
5162 return err;
5163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 }
5165
5166 return 0;
5167}
5168
5169static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5170{
5171 char b[BDEVNAME_SIZE];
5172 mdk_rdev_t *rdev;
5173
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 rdev = find_rdev(mddev, dev);
5175 if (!rdev)
5176 return -ENXIO;
5177
5178 if (rdev->raid_disk >= 0)
5179 goto busy;
5180
5181 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005182 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005183 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184
5185 return 0;
5186busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005187 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 bdevname(rdev->bdev,b), mdname(mddev));
5189 return -EBUSY;
5190}
5191
5192static int hot_add_disk(mddev_t * mddev, dev_t dev)
5193{
5194 char b[BDEVNAME_SIZE];
5195 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 mdk_rdev_t *rdev;
5197
5198 if (!mddev->pers)
5199 return -ENODEV;
5200
5201 if (mddev->major_version != 0) {
5202 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5203 " version-0 superblocks.\n",
5204 mdname(mddev));
5205 return -EINVAL;
5206 }
5207 if (!mddev->pers->hot_add_disk) {
5208 printk(KERN_WARNING
5209 "%s: personality does not support diskops!\n",
5210 mdname(mddev));
5211 return -EINVAL;
5212 }
5213
NeilBrownd710e132008-10-13 11:55:12 +11005214 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 if (IS_ERR(rdev)) {
5216 printk(KERN_WARNING
5217 "md: error, md_import_device() returned %ld\n",
5218 PTR_ERR(rdev));
5219 return -EINVAL;
5220 }
5221
5222 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10005223 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 else
Andre Noll0f420352008-07-11 22:02:23 +10005225 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226
NeilBrown8190e752009-06-18 08:48:58 +10005227 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228
NeilBrownb2d444d2005-11-08 21:39:31 -08005229 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 printk(KERN_WARNING
5231 "md: can not hot-add faulty %s disk to %s!\n",
5232 bdevname(rdev->bdev,b), mdname(mddev));
5233 err = -EINVAL;
5234 goto abort_export;
5235 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005236 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005238 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005239 err = bind_rdev_to_array(rdev, mddev);
5240 if (err)
5241 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242
5243 /*
5244 * The rest should better be atomic, we can have disk failures
5245 * noticed in interrupt contexts ...
5246 */
5247
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 rdev->raid_disk = -1;
5249
NeilBrown850b2b42006-10-03 01:15:46 -07005250 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251
5252 /*
5253 * Kick recovery, maybe this spare has to be added to the
5254 * array immediately.
5255 */
5256 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5257 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005258 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 return 0;
5260
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261abort_export:
5262 export_rdev(rdev);
5263 return err;
5264}
5265
NeilBrown32a76272005-06-21 17:17:14 -07005266static int set_bitmap_file(mddev_t *mddev, int fd)
5267{
5268 int err;
5269
NeilBrown36fa3062005-09-09 16:23:45 -07005270 if (mddev->pers) {
5271 if (!mddev->pers->quiesce)
5272 return -EBUSY;
5273 if (mddev->recovery || mddev->sync_thread)
5274 return -EBUSY;
5275 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005276 }
5277
NeilBrown36fa3062005-09-09 16:23:45 -07005278
5279 if (fd >= 0) {
5280 if (mddev->bitmap)
5281 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005282 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005283
NeilBrownc3d97142009-12-14 12:49:52 +11005284 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005285 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5286 mdname(mddev));
5287 return -EBADF;
5288 }
5289
NeilBrownc3d97142009-12-14 12:49:52 +11005290 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005291 if (err) {
5292 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5293 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005294 fput(mddev->bitmap_info.file);
5295 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005296 return err;
5297 }
NeilBrownc3d97142009-12-14 12:49:52 +11005298 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005299 } else if (mddev->bitmap == NULL)
5300 return -ENOENT; /* cannot remove what isn't there */
5301 err = 0;
5302 if (mddev->pers) {
5303 mddev->pers->quiesce(mddev, 1);
5304 if (fd >= 0)
5305 err = bitmap_create(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07005306 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005307 bitmap_destroy(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07005308 fd = -1; /* make sure to put the file */
5309 }
NeilBrown36fa3062005-09-09 16:23:45 -07005310 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab322006-06-26 00:27:43 -07005311 }
5312 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005313 if (mddev->bitmap_info.file) {
5314 restore_bitmap_write_access(mddev->bitmap_info.file);
5315 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005316 }
NeilBrownc3d97142009-12-14 12:49:52 +11005317 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005318 }
5319
NeilBrown32a76272005-06-21 17:17:14 -07005320 return err;
5321}
5322
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323/*
5324 * set_array_info is used two different ways
5325 * The original usage is when creating a new array.
5326 * In this usage, raid_disks is > 0 and it together with
5327 * level, size, not_persistent,layout,chunksize determine the
5328 * shape of the array.
5329 * This will always create an array with a type-0.90.0 superblock.
5330 * The newer usage is when assembling an array.
5331 * In this case raid_disks will be 0, and the major_version field is
5332 * use to determine which style super-blocks are to be found on the devices.
5333 * The minor and patch _version numbers are also kept incase the
5334 * super_block handler wishes to interpret them.
5335 */
5336static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5337{
5338
5339 if (info->raid_disks == 0) {
5340 /* just setting version number for superblock loading */
5341 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005342 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 super_types[info->major_version].name == NULL) {
5344 /* maybe try to auto-load a module? */
5345 printk(KERN_INFO
5346 "md: superblock version %d not known\n",
5347 info->major_version);
5348 return -EINVAL;
5349 }
5350 mddev->major_version = info->major_version;
5351 mddev->minor_version = info->minor_version;
5352 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005353 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005354 /* ensure mddev_put doesn't delete this now that there
5355 * is some minimal configuration.
5356 */
5357 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 return 0;
5359 }
5360 mddev->major_version = MD_MAJOR_VERSION;
5361 mddev->minor_version = MD_MINOR_VERSION;
5362 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5363 mddev->ctime = get_seconds();
5364
5365 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005366 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005367 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 mddev->raid_disks = info->raid_disks;
5369 /* don't set md_minor, it is determined by which /dev/md* was
5370 * openned
5371 */
5372 if (info->state & (1<<MD_SB_CLEAN))
5373 mddev->recovery_cp = MaxSector;
5374 else
5375 mddev->recovery_cp = 0;
5376 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005377 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378
5379 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005380 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381
5382 mddev->max_disks = MD_SB_DISKS;
5383
NeilBrowne6910632008-02-06 01:39:51 -08005384 if (mddev->persistent)
5385 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005386 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387
NeilBrownc3d97142009-12-14 12:49:52 +11005388 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5389 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005390
NeilBrownf6705572006-03-27 01:18:11 -08005391 mddev->reshape_position = MaxSector;
5392
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 /*
5394 * Generate a 128 bit UUID
5395 */
5396 get_random_bytes(mddev->uuid, 16);
5397
NeilBrownf6705572006-03-27 01:18:11 -08005398 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005399 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005400 mddev->new_layout = mddev->layout;
5401 mddev->delta_disks = 0;
5402
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 return 0;
5404}
5405
Dan Williams1f403622009-03-31 14:59:03 +11005406void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5407{
Dan Williamsb522adc2009-03-31 15:00:31 +11005408 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5409
5410 if (mddev->external_size)
5411 return;
5412
Dan Williams1f403622009-03-31 14:59:03 +11005413 mddev->array_sectors = array_sectors;
5414}
5415EXPORT_SYMBOL(md_set_array_sectors);
5416
Andre Nolld71f9f82008-07-11 22:02:22 +10005417static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005418{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005419 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005420 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005421 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005422
5423 if (mddev->pers->resize == NULL)
5424 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005425 /* The "num_sectors" is the number of sectors of each device that
5426 * is used. This can only make sense for arrays with redundancy.
5427 * linear and raid0 always use whatever space is available. We can only
5428 * consider changing this number if no resync or reconstruction is
5429 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005430 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005431 * of each device. If num_sectors is zero, we find the largest size
5432 * that fits.
5433
NeilBrowna35b0d62006-01-06 00:20:49 -08005434 */
5435 if (mddev->sync_thread)
5436 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005437 if (mddev->bitmap)
5438 /* Sorry, cannot grow a bitmap yet, just remove it,
5439 * grow, and re-add.
5440 */
5441 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005442 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005443 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005444
Andre Nolld71f9f82008-07-11 22:02:22 +10005445 if (fit && (num_sectors == 0 || num_sectors > avail))
5446 num_sectors = avail;
5447 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005448 return -ENOSPC;
5449 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005450 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005451 if (!rv)
5452 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005453 return rv;
5454}
5455
NeilBrownda943b992006-01-06 00:20:54 -08005456static int update_raid_disks(mddev_t *mddev, int raid_disks)
5457{
5458 int rv;
5459 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005460 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005461 return -EINVAL;
5462 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005463 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08005464 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005465 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005466 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005467 mddev->delta_disks = raid_disks - mddev->raid_disks;
5468
5469 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005470 return rv;
5471}
5472
5473
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474/*
5475 * update_array_info is used to change the configuration of an
5476 * on-line array.
5477 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5478 * fields in the info are checked against the array.
5479 * Any differences that cannot be handled will cause an error.
5480 * Normally, only one change can be managed at a time.
5481 */
5482static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5483{
5484 int rv = 0;
5485 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005486 int state = 0;
5487
5488 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005489 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005490 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491
5492 if (mddev->major_version != info->major_version ||
5493 mddev->minor_version != info->minor_version ||
5494/* mddev->patch_version != info->patch_version || */
5495 mddev->ctime != info->ctime ||
5496 mddev->level != info->level ||
5497/* mddev->layout != info->layout || */
5498 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005499 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005500 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5501 ((state^info->state) & 0xfffffe00)
5502 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 return -EINVAL;
5504 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005505 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5506 cnt++;
5507 if (mddev->raid_disks != info->raid_disks)
5508 cnt++;
5509 if (mddev->layout != info->layout)
5510 cnt++;
5511 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5512 cnt++;
5513 if (cnt == 0)
5514 return 0;
5515 if (cnt > 1)
5516 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517
5518 if (mddev->layout != info->layout) {
5519 /* Change layout
5520 * we don't need to do anything at the md level, the
5521 * personality will take care of it all.
5522 */
NeilBrown50ac1682009-06-18 08:47:55 +10005523 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005525 else {
5526 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005527 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005528 if (rv)
5529 mddev->new_layout = mddev->layout;
5530 return rv;
5531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005533 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005534 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535
NeilBrownda943b992006-01-06 00:20:54 -08005536 if (mddev->raid_disks != info->raid_disks)
5537 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538
NeilBrown36fa3062005-09-09 16:23:45 -07005539 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5540 if (mddev->pers->quiesce == NULL)
5541 return -EINVAL;
5542 if (mddev->recovery || mddev->sync_thread)
5543 return -EBUSY;
5544 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5545 /* add the bitmap */
5546 if (mddev->bitmap)
5547 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005548 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005549 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005550 mddev->bitmap_info.offset =
5551 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005552 mddev->pers->quiesce(mddev, 1);
5553 rv = bitmap_create(mddev);
5554 if (rv)
5555 bitmap_destroy(mddev);
5556 mddev->pers->quiesce(mddev, 0);
5557 } else {
5558 /* remove the bitmap */
5559 if (!mddev->bitmap)
5560 return -ENOENT;
5561 if (mddev->bitmap->file)
5562 return -EINVAL;
5563 mddev->pers->quiesce(mddev, 1);
5564 bitmap_destroy(mddev);
5565 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005566 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005567 }
5568 }
NeilBrown850b2b42006-10-03 01:15:46 -07005569 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 return rv;
5571}
5572
5573static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5574{
5575 mdk_rdev_t *rdev;
5576
5577 if (mddev->pers == NULL)
5578 return -ENODEV;
5579
5580 rdev = find_rdev(mddev, dev);
5581 if (!rdev)
5582 return -ENODEV;
5583
5584 md_error(mddev, rdev);
5585 return 0;
5586}
5587
Andre Noll2f9618c2008-04-25 18:57:58 +02005588/*
5589 * We have a problem here : there is no easy way to give a CHS
5590 * virtual geometry. We currently pretend that we have a 2 heads
5591 * 4 sectors (with a BIG number of cylinders...). This drives
5592 * dosfs just mad... ;-)
5593 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005594static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5595{
5596 mddev_t *mddev = bdev->bd_disk->private_data;
5597
5598 geo->heads = 2;
5599 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11005600 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005601 return 0;
5602}
5603
Al Viroa39907f2008-03-02 10:31:15 -05005604static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 unsigned int cmd, unsigned long arg)
5606{
5607 int err = 0;
5608 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10005610 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611
5612 if (!capable(CAP_SYS_ADMIN))
5613 return -EACCES;
5614
5615 /*
5616 * Commands dealing with the RAID driver but not any
5617 * particular array:
5618 */
5619 switch (cmd)
5620 {
5621 case RAID_VERSION:
5622 err = get_version(argp);
5623 goto done;
5624
5625 case PRINT_RAID_DEBUG:
5626 err = 0;
5627 md_print_devices();
5628 goto done;
5629
5630#ifndef MODULE
5631 case RAID_AUTORUN:
5632 err = 0;
5633 autostart_arrays(arg);
5634 goto done;
5635#endif
5636 default:;
5637 }
5638
5639 /*
5640 * Commands creating/starting a new array:
5641 */
5642
Al Viroa39907f2008-03-02 10:31:15 -05005643 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644
5645 if (!mddev) {
5646 BUG();
5647 goto abort;
5648 }
5649
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 err = mddev_lock(mddev);
5651 if (err) {
5652 printk(KERN_INFO
5653 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5654 err, cmd);
5655 goto abort;
5656 }
5657
5658 switch (cmd)
5659 {
5660 case SET_ARRAY_INFO:
5661 {
5662 mdu_array_info_t info;
5663 if (!arg)
5664 memset(&info, 0, sizeof(info));
5665 else if (copy_from_user(&info, argp, sizeof(info))) {
5666 err = -EFAULT;
5667 goto abort_unlock;
5668 }
5669 if (mddev->pers) {
5670 err = update_array_info(mddev, &info);
5671 if (err) {
5672 printk(KERN_WARNING "md: couldn't update"
5673 " array info. %d\n", err);
5674 goto abort_unlock;
5675 }
5676 goto done_unlock;
5677 }
5678 if (!list_empty(&mddev->disks)) {
5679 printk(KERN_WARNING
5680 "md: array %s already has disks!\n",
5681 mdname(mddev));
5682 err = -EBUSY;
5683 goto abort_unlock;
5684 }
5685 if (mddev->raid_disks) {
5686 printk(KERN_WARNING
5687 "md: array %s already initialised!\n",
5688 mdname(mddev));
5689 err = -EBUSY;
5690 goto abort_unlock;
5691 }
5692 err = set_array_info(mddev, &info);
5693 if (err) {
5694 printk(KERN_WARNING "md: couldn't set"
5695 " array info. %d\n", err);
5696 goto abort_unlock;
5697 }
5698 }
5699 goto done_unlock;
5700
5701 default:;
5702 }
5703
5704 /*
5705 * Commands querying/configuring an existing array:
5706 */
NeilBrown32a76272005-06-21 17:17:14 -07005707 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005708 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005709 if ((!mddev->raid_disks && !mddev->external)
5710 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5711 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5712 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 err = -ENODEV;
5714 goto abort_unlock;
5715 }
5716
5717 /*
5718 * Commands even a read-only array can execute:
5719 */
5720 switch (cmd)
5721 {
5722 case GET_ARRAY_INFO:
5723 err = get_array_info(mddev, argp);
5724 goto done_unlock;
5725
NeilBrown32a76272005-06-21 17:17:14 -07005726 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005727 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005728 goto done_unlock;
5729
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 case GET_DISK_INFO:
5731 err = get_disk_info(mddev, argp);
5732 goto done_unlock;
5733
5734 case RESTART_ARRAY_RW:
5735 err = restart_array(mddev);
5736 goto done_unlock;
5737
5738 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005739 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 goto done_unlock;
5741
5742 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11005743 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 goto done_unlock;
5745
Dan Williamse2218352010-05-12 08:25:37 +10005746 case BLKROSET:
5747 if (get_user(ro, (int __user *)(arg))) {
5748 err = -EFAULT;
5749 goto done_unlock;
5750 }
5751 err = -EINVAL;
5752
5753 /* if the bdev is going readonly the value of mddev->ro
5754 * does not matter, no writes are coming
5755 */
5756 if (ro)
5757 goto done_unlock;
5758
5759 /* are we are already prepared for writes? */
5760 if (mddev->ro != 1)
5761 goto done_unlock;
5762
5763 /* transitioning to readauto need only happen for
5764 * arrays that call md_write_start
5765 */
5766 if (mddev->pers) {
5767 err = restart_array(mddev);
5768 if (err == 0) {
5769 mddev->ro = 2;
5770 set_disk_ro(mddev->gendisk, 0);
5771 }
5772 }
5773 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 }
5775
5776 /*
5777 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005778 * superblock, so we do not allow them on read-only arrays.
5779 * However non-MD ioctls (e.g. get-size) will still come through
5780 * here and hit the 'default' below, so only disallow
5781 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005783 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005784 if (mddev->ro == 2) {
5785 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005786 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005787 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5788 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005789 } else {
5790 err = -EROFS;
5791 goto abort_unlock;
5792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 }
5794
5795 switch (cmd)
5796 {
5797 case ADD_NEW_DISK:
5798 {
5799 mdu_disk_info_t info;
5800 if (copy_from_user(&info, argp, sizeof(info)))
5801 err = -EFAULT;
5802 else
5803 err = add_new_disk(mddev, &info);
5804 goto done_unlock;
5805 }
5806
5807 case HOT_REMOVE_DISK:
5808 err = hot_remove_disk(mddev, new_decode_dev(arg));
5809 goto done_unlock;
5810
5811 case HOT_ADD_DISK:
5812 err = hot_add_disk(mddev, new_decode_dev(arg));
5813 goto done_unlock;
5814
5815 case SET_DISK_FAULTY:
5816 err = set_disk_faulty(mddev, new_decode_dev(arg));
5817 goto done_unlock;
5818
5819 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005820 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 goto done_unlock;
5822
NeilBrown32a76272005-06-21 17:17:14 -07005823 case SET_BITMAP_FILE:
5824 err = set_bitmap_file(mddev, (int)arg);
5825 goto done_unlock;
5826
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 err = -EINVAL;
5829 goto abort_unlock;
5830 }
5831
5832done_unlock:
5833abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005834 if (mddev->hold_active == UNTIL_IOCTL &&
5835 err != -EINVAL)
5836 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 mddev_unlock(mddev);
5838
5839 return err;
5840done:
5841 if (err)
5842 MD_BUG();
5843abort:
5844 return err;
5845}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005846#ifdef CONFIG_COMPAT
5847static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5848 unsigned int cmd, unsigned long arg)
5849{
5850 switch (cmd) {
5851 case HOT_REMOVE_DISK:
5852 case HOT_ADD_DISK:
5853 case SET_DISK_FAULTY:
5854 case SET_BITMAP_FILE:
5855 /* These take in integer arg, do not convert */
5856 break;
5857 default:
5858 arg = (unsigned long)compat_ptr(arg);
5859 break;
5860 }
5861
5862 return md_ioctl(bdev, mode, cmd, arg);
5863}
5864#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865
Al Viroa39907f2008-03-02 10:31:15 -05005866static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867{
5868 /*
5869 * Succeed if we can lock the mddev, which confirms that
5870 * it isn't being stopped right now.
5871 */
NeilBrownd3374822009-01-09 08:31:10 +11005872 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 int err;
5874
NeilBrownd3374822009-01-09 08:31:10 +11005875 if (mddev->gendisk != bdev->bd_disk) {
5876 /* we are racing with mddev_put which is discarding this
5877 * bd_disk.
5878 */
5879 mddev_put(mddev);
5880 /* Wait until bdev->bd_disk is definitely gone */
5881 flush_scheduled_work();
5882 /* Then retry the open from the top */
5883 return -ERESTARTSYS;
5884 }
5885 BUG_ON(mddev != bdev->bd_disk->private_data);
5886
NeilBrownc8c00a62009-08-10 12:50:52 +10005887 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 goto out;
5889
5890 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005891 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005892 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 out:
5895 return err;
5896}
5897
Al Viroa39907f2008-03-02 10:31:15 -05005898static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899{
Al Viroa39907f2008-03-02 10:31:15 -05005900 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005902 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005903 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 mddev_put(mddev);
5905
5906 return 0;
5907}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005908static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909{
5910 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005911 .open = md_open,
5912 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005913 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005914#ifdef CONFIG_COMPAT
5915 .compat_ioctl = md_compat_ioctl,
5916#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005917 .getgeo = md_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918};
5919
Adrian Bunk75c96f82005-05-05 16:16:09 -07005920static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921{
5922 mdk_thread_t *thread = arg;
5923
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 /*
5925 * md_thread is a 'system-thread', it's priority should be very
5926 * high. We avoid resource deadlocks individually in each
5927 * raid personality. (RAID5 does preallocation) We also use RR and
5928 * the very same RT priority as kswapd, thus we will never get
5929 * into a priority inversion deadlock.
5930 *
5931 * we definitely have to have equal or higher priority than
5932 * bdflush, otherwise bdflush will deadlock if there are too
5933 * many dirty RAID5 blocks.
5934 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935
NeilBrown6985c432005-10-19 21:23:47 -07005936 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005937 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938
NeilBrown93588e22005-11-15 00:09:12 -08005939 /* We need to wait INTERRUPTIBLE so that
5940 * we don't add to the load-average.
5941 * That means we need to be sure no signals are
5942 * pending
5943 */
5944 if (signal_pending(current))
5945 flush_signals(current);
5946
5947 wait_event_interruptible_timeout
5948 (thread->wqueue,
5949 test_bit(THREAD_WAKEUP, &thread->flags)
5950 || kthread_should_stop(),
5951 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952
5953 clear_bit(THREAD_WAKEUP, &thread->flags);
5954
NeilBrown787453c2005-11-08 21:39:43 -08005955 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005957
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958 return 0;
5959}
5960
5961void md_wakeup_thread(mdk_thread_t *thread)
5962{
5963 if (thread) {
5964 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5965 set_bit(THREAD_WAKEUP, &thread->flags);
5966 wake_up(&thread->wqueue);
5967 }
5968}
5969
5970mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5971 const char *name)
5972{
5973 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974
NeilBrown9ffae0c2006-01-06 00:20:32 -08005975 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 if (!thread)
5977 return NULL;
5978
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 init_waitqueue_head(&thread->wqueue);
5980
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 thread->run = run;
5982 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005983 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10005984 thread->tsk = kthread_run(md_thread, thread,
5985 "%s_%s",
5986 mdname(thread->mddev),
5987 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07005988 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 kfree(thread);
5990 return NULL;
5991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 return thread;
5993}
5994
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995void md_unregister_thread(mdk_thread_t *thread)
5996{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005997 if (!thread)
5998 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005999 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07006000
6001 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 kfree(thread);
6003}
6004
6005void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6006{
6007 if (!mddev) {
6008 MD_BUG();
6009 return;
6010 }
6011
NeilBrownb2d444d2005-11-08 21:39:31 -08006012 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006014
6015 if (mddev->external)
6016 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07006017/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6019 mdname(mddev),
6020 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6021 __builtin_return_address(0),__builtin_return_address(1),
6022 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07006023*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07006024 if (!mddev->pers)
6025 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026 if (!mddev->pers->error_handler)
6027 return;
6028 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006029 if (mddev->degraded)
6030 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc0cc75f2010-03-22 10:28:51 +11006031 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6033 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6034 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07006035 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036}
6037
6038/* seq_file implementation /proc/mdstat */
6039
6040static void status_unused(struct seq_file *seq)
6041{
6042 int i = 0;
6043 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044
6045 seq_printf(seq, "unused devices: ");
6046
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006047 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 char b[BDEVNAME_SIZE];
6049 i++;
6050 seq_printf(seq, "%s ",
6051 bdevname(rdev->bdev,b));
6052 }
6053 if (!i)
6054 seq_printf(seq, "<none>");
6055
6056 seq_printf(seq, "\n");
6057}
6058
6059
6060static void status_resync(struct seq_file *seq, mddev_t * mddev)
6061{
NeilBrowndd71cf62009-05-07 12:49:35 +10006062 sector_t max_sectors, resync, res;
6063 unsigned long dt, db;
6064 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006065 int scale;
6066 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067
NeilBrowndd71cf62009-05-07 12:49:35 +10006068 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069
6070 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006071 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006073 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074
6075 /*
6076 * Should not happen.
6077 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006078 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 MD_BUG();
6080 return;
6081 }
NeilBrown4588b422006-03-27 01:18:04 -08006082 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006083 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006084 * u32, as those are the requirements for sector_div.
6085 * Thus 'scale' must be at least 10
6086 */
6087 scale = 10;
6088 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006089 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006090 scale++;
6091 }
6092 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006093 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006094
6095 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 {
NeilBrown4588b422006-03-27 01:18:04 -08006097 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 seq_printf(seq, "[");
6099 for (i = 0; i < x; i++)
6100 seq_printf(seq, "=");
6101 seq_printf(seq, ">");
6102 for (i = 0; i < y; i++)
6103 seq_printf(seq, ".");
6104 seq_printf(seq, "] ");
6105 }
NeilBrown4588b422006-03-27 01:18:04 -08006106 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006107 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6108 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006109 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6110 "check" :
6111 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6112 "resync" : "recovery"))),
6113 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006114 (unsigned long long) resync/2,
6115 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116
6117 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 * dt: time from mark until now
6119 * db: blocks written from mark until now
6120 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006121 *
6122 * rt is a sector_t, so could be 32bit or 64bit.
6123 * So we divide before multiply in case it is 32bit and close
6124 * to the limit.
6125 * We scale the divisor (db) by 32 to avoid loosing precision
6126 * near the end of resync when the number of remaining sectors
6127 * is close to 'db'.
6128 * We then divide rt by 32 after multiplying by db to compensate.
6129 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130 */
6131 dt = ((jiffies - mddev->resync_mark) / HZ);
6132 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006133 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6134 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135
NeilBrowndd71cf62009-05-07 12:49:35 +10006136 rt = max_sectors - resync; /* number of remaining sectors */
6137 sector_div(rt, db/32+1);
6138 rt *= dt;
6139 rt >>= 5;
6140
6141 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6142 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143
NeilBrownff4e8d92006-07-10 04:44:16 -07006144 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145}
6146
6147static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6148{
6149 struct list_head *tmp;
6150 loff_t l = *pos;
6151 mddev_t *mddev;
6152
6153 if (l >= 0x10000)
6154 return NULL;
6155 if (!l--)
6156 /* header */
6157 return (void*)1;
6158
6159 spin_lock(&all_mddevs_lock);
6160 list_for_each(tmp,&all_mddevs)
6161 if (!l--) {
6162 mddev = list_entry(tmp, mddev_t, all_mddevs);
6163 mddev_get(mddev);
6164 spin_unlock(&all_mddevs_lock);
6165 return mddev;
6166 }
6167 spin_unlock(&all_mddevs_lock);
6168 if (!l--)
6169 return (void*)2;/* tail */
6170 return NULL;
6171}
6172
6173static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6174{
6175 struct list_head *tmp;
6176 mddev_t *next_mddev, *mddev = v;
6177
6178 ++*pos;
6179 if (v == (void*)2)
6180 return NULL;
6181
6182 spin_lock(&all_mddevs_lock);
6183 if (v == (void*)1)
6184 tmp = all_mddevs.next;
6185 else
6186 tmp = mddev->all_mddevs.next;
6187 if (tmp != &all_mddevs)
6188 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6189 else {
6190 next_mddev = (void*)2;
6191 *pos = 0x10000;
6192 }
6193 spin_unlock(&all_mddevs_lock);
6194
6195 if (v != (void*)1)
6196 mddev_put(mddev);
6197 return next_mddev;
6198
6199}
6200
6201static void md_seq_stop(struct seq_file *seq, void *v)
6202{
6203 mddev_t *mddev = v;
6204
6205 if (mddev && v != (void*)1 && v != (void*)2)
6206 mddev_put(mddev);
6207}
6208
NeilBrownd7603b72006-01-06 00:20:30 -08006209struct mdstat_info {
6210 int event;
6211};
6212
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213static int md_seq_show(struct seq_file *seq, void *v)
6214{
6215 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006216 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006218 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006219 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220
6221 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006222 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223 seq_printf(seq, "Personalities : ");
6224 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006225 list_for_each_entry(pers, &pers_list, list)
6226 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227
6228 spin_unlock(&pers_lock);
6229 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006230 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 return 0;
6232 }
6233 if (v == (void*)2) {
6234 status_unused(seq);
6235 return 0;
6236 }
6237
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006238 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006240
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6242 seq_printf(seq, "%s : %sactive", mdname(mddev),
6243 mddev->pers ? "" : "in");
6244 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006245 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006247 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006248 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 seq_printf(seq, " %s", mddev->pers->name);
6250 }
6251
Andre Nolldd8ac332009-03-31 14:33:13 +11006252 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006253 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254 char b[BDEVNAME_SIZE];
6255 seq_printf(seq, " %s[%d]",
6256 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006257 if (test_bit(WriteMostly, &rdev->flags))
6258 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006259 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260 seq_printf(seq, "(F)");
6261 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006262 } else if (rdev->raid_disk < 0)
6263 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006264 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 }
6266
6267 if (!list_empty(&mddev->disks)) {
6268 if (mddev->pers)
6269 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006270 (unsigned long long)
6271 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272 else
6273 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006274 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006276 if (mddev->persistent) {
6277 if (mddev->major_version != 0 ||
6278 mddev->minor_version != 90) {
6279 seq_printf(seq," super %d.%d",
6280 mddev->major_version,
6281 mddev->minor_version);
6282 }
NeilBrowne6910632008-02-06 01:39:51 -08006283 } else if (mddev->external)
6284 seq_printf(seq, " super external:%s",
6285 mddev->metadata_type);
6286 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006287 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288
6289 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006290 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006292 if (mddev->pers->sync_request) {
6293 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006294 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006295 seq_printf(seq, "\n ");
6296 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6297 seq_printf(seq, "\tresync=DELAYED\n ");
6298 else if (mddev->recovery_cp < MaxSector)
6299 seq_printf(seq, "\tresync=PENDING\n ");
6300 }
NeilBrown32a76272005-06-21 17:17:14 -07006301 } else
6302 seq_printf(seq, "\n ");
6303
6304 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006305 unsigned long chunk_kb;
6306 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006307 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006308 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006309 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6310 "%lu%s chunk",
6311 bitmap->pages - bitmap->missing_pages,
6312 bitmap->pages,
6313 (bitmap->pages - bitmap->missing_pages)
6314 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006315 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006316 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006317 if (bitmap->file) {
6318 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006319 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006320 }
NeilBrown78d742d2005-06-21 17:17:15 -07006321
NeilBrown32a76272005-06-21 17:17:14 -07006322 seq_printf(seq, "\n");
6323 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 }
6325
6326 seq_printf(seq, "\n");
6327 }
6328 mddev_unlock(mddev);
6329
6330 return 0;
6331}
6332
Jan Engelhardt110518b2009-05-07 12:49:37 +10006333static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 .start = md_seq_start,
6335 .next = md_seq_next,
6336 .stop = md_seq_stop,
6337 .show = md_seq_show,
6338};
6339
6340static int md_seq_open(struct inode *inode, struct file *file)
6341{
6342 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006343 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6344 if (mi == NULL)
6345 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346
6347 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006348 if (error)
6349 kfree(mi);
6350 else {
6351 struct seq_file *p = file->private_data;
6352 p->private = mi;
6353 mi->event = atomic_read(&md_event_count);
6354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355 return error;
6356}
6357
NeilBrownd7603b72006-01-06 00:20:30 -08006358static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6359{
6360 struct seq_file *m = filp->private_data;
6361 struct mdstat_info *mi = m->private;
6362 int mask;
6363
6364 poll_wait(filp, &md_event_waiters, wait);
6365
6366 /* always allow read */
6367 mask = POLLIN | POLLRDNORM;
6368
6369 if (mi->event != atomic_read(&md_event_count))
6370 mask |= POLLERR | POLLPRI;
6371 return mask;
6372}
6373
Arjan van de Venfa027c22007-02-12 00:55:33 -08006374static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006375 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376 .open = md_seq_open,
6377 .read = seq_read,
6378 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006379 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006380 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381};
6382
NeilBrown2604b702006-01-06 00:20:36 -08006383int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006386 list_add_tail(&p->list, &pers_list);
6387 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388 spin_unlock(&pers_lock);
6389 return 0;
6390}
6391
NeilBrown2604b702006-01-06 00:20:36 -08006392int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393{
NeilBrown2604b702006-01-06 00:20:36 -08006394 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006396 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397 spin_unlock(&pers_lock);
6398 return 0;
6399}
6400
NeilBrowneea1bf32009-03-31 14:27:02 +11006401static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402{
6403 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006405 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406
6407 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006408 rcu_read_lock();
6409 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006411 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6412 (int)part_stat_read(&disk->part0, sectors[1]) -
6413 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006414 /* sync IO will cause sync_io to increase before the disk_stats
6415 * as sync_io is counted when a request starts, and
6416 * disk_stats is counted when it completes.
6417 * So resync activity will cause curr_events to be smaller than
6418 * when there was no such activity.
6419 * non-sync IO will cause disk_stat to increase without
6420 * increasing sync_io so curr_events will (eventually)
6421 * be larger than it was before. Once it becomes
6422 * substantially larger, the test below will cause
6423 * the array to appear non-idle, and resync will slow
6424 * down.
6425 * If there is a lot of outstanding resync activity when
6426 * we set last_event to curr_events, then all that activity
6427 * completing might cause the array to appear non-idle
6428 * and resync will be slowed down even though there might
6429 * not have been non-resync activity. This will only
6430 * happen once though. 'last_events' will soon reflect
6431 * the state where there is little or no outstanding
6432 * resync requests, and further resync activity will
6433 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006434 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006436 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437 rdev->last_events = curr_events;
6438 idle = 0;
6439 }
6440 }
NeilBrown4b809912008-07-21 17:05:25 +10006441 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006442 return idle;
6443}
6444
6445void md_done_sync(mddev_t *mddev, int blocks, int ok)
6446{
6447 /* another "blocks" (512byte) blocks have been synced */
6448 atomic_sub(blocks, &mddev->recovery_active);
6449 wake_up(&mddev->recovery_wait);
6450 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006451 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452 md_wakeup_thread(mddev->thread);
6453 // stop recovery, signal do_sync ....
6454 }
6455}
6456
6457
NeilBrown06d91a52005-06-21 17:17:12 -07006458/* md_write_start(mddev, bi)
6459 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006460 * in superblock) before writing, schedule a superblock update
6461 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006462 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006463void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464{
Neil Brown0fd62b82008-06-28 08:31:36 +10006465 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006466 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006467 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006468
NeilBrownf91de922005-11-08 21:39:36 -08006469 BUG_ON(mddev->ro == 1);
6470 if (mddev->ro == 2) {
6471 /* need to switch to read/write */
6472 mddev->ro = 0;
6473 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6474 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006475 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006476 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006477 }
NeilBrown06d91a52005-06-21 17:17:12 -07006478 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006479 if (mddev->safemode == 1)
6480 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006481 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006482 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006483 if (mddev->in_sync) {
6484 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006485 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006486 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006487 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006488 }
NeilBrowna9701a32005-11-08 21:39:34 -08006489 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006490 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006491 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006492 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006493 wait_event(mddev->sb_wait,
6494 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6495 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496}
6497
6498void md_write_end(mddev_t *mddev)
6499{
6500 if (atomic_dec_and_test(&mddev->writes_pending)) {
6501 if (mddev->safemode == 2)
6502 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006503 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6505 }
6506}
6507
NeilBrown2a2275d2007-01-26 00:57:11 -08006508/* md_allow_write(mddev)
6509 * Calling this ensures that the array is marked 'active' so that writes
6510 * may proceed without blocking. It is important to call this before
6511 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6512 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006513 *
6514 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6515 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006516 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006517int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006518{
6519 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006520 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006521 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006522 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006523 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006524 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006525
6526 spin_lock_irq(&mddev->write_lock);
6527 if (mddev->in_sync) {
6528 mddev->in_sync = 0;
6529 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6530 if (mddev->safemode_delay &&
6531 mddev->safemode == 0)
6532 mddev->safemode = 1;
6533 spin_unlock_irq(&mddev->write_lock);
6534 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006535 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006536 } else
6537 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006538
6539 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6540 return -EAGAIN;
6541 else
6542 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006543}
6544EXPORT_SYMBOL_GPL(md_allow_write);
6545
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546#define SYNC_MARKS 10
6547#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006548void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549{
6550 mddev_t *mddev2;
6551 unsigned int currspeed = 0,
6552 window;
NeilBrown57afd892005-06-21 17:17:13 -07006553 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554 unsigned long mark[SYNC_MARKS];
6555 sector_t mark_cnt[SYNC_MARKS];
6556 int last_mark,m;
6557 struct list_head *tmp;
6558 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006559 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006560 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006561 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562
6563 /* just incase thread restarts... */
6564 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6565 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006566 if (mddev->ro) /* never try to sync a read-only array */
6567 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568
NeilBrown61df9d92006-10-03 01:15:57 -07006569 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6570 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6571 desc = "data-check";
6572 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6573 desc = "requested-resync";
6574 else
6575 desc = "resync";
6576 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6577 desc = "reshape";
6578 else
6579 desc = "recovery";
6580
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581 /* we overload curr_resync somewhat here.
6582 * 0 == not engaged in resync at all
6583 * 2 == checking that there is no conflict with another sync
6584 * 1 == like 2, but have yielded to allow conflicting resync to
6585 * commense
6586 * other == active in resync - this many blocks
6587 *
6588 * Before starting a resync we must have set curr_resync to
6589 * 2, and then checked that every "conflicting" array has curr_resync
6590 * less than ours. When we find one that is the same or higher
6591 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6592 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6593 * This will mean we have to start checking from the beginning again.
6594 *
6595 */
6596
6597 do {
6598 mddev->curr_resync = 2;
6599
6600 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006601 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006602 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006603
6604 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006606 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607 if (mddev2 == mddev)
6608 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006609 if (!mddev->parallel_resync
6610 && mddev2->curr_resync
6611 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612 DEFINE_WAIT(wq);
6613 if (mddev < mddev2 && mddev->curr_resync == 2) {
6614 /* arbitrarily yield */
6615 mddev->curr_resync = 1;
6616 wake_up(&resync_wait);
6617 }
6618 if (mddev > mddev2 && mddev->curr_resync == 1)
6619 /* no need to wait here, we can wait the next
6620 * time 'round when curr_resync == 2
6621 */
6622 continue;
NeilBrown97441972008-09-19 11:49:54 +10006623 /* We need to wait 'interruptible' so as not to
6624 * contribute to the load average, and not to
6625 * be caught by 'softlockup'
6626 */
6627 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006628 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006629 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006630 printk(KERN_INFO "md: delaying %s of %s"
6631 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006633 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006634 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006635 if (signal_pending(current))
6636 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637 schedule();
6638 finish_wait(&resync_wait, &wq);
6639 goto try_again;
6640 }
6641 finish_wait(&resync_wait, &wq);
6642 }
6643 }
6644 } while (mddev->curr_resync < 2);
6645
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006646 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006647 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006649 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650 */
6651 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006652 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006653 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006654 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6655 j = mddev->resync_min;
6656 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006657 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006658
NeilBrownccfcc3c2006-03-27 01:18:09 -08006659 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006660 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006661 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006663 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006664 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006665 rcu_read_lock();
6666 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006667 if (rdev->raid_disk >= 0 &&
6668 !test_bit(Faulty, &rdev->flags) &&
6669 !test_bit(In_sync, &rdev->flags) &&
6670 rdev->recovery_offset < j)
6671 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006672 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674
NeilBrown61df9d92006-10-03 01:15:57 -07006675 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6676 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6677 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006678 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006679 "(but not more than %d KB/sec) for %s.\n",
6680 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681
NeilBrowneea1bf32009-03-31 14:27:02 +11006682 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006683
NeilBrown57afd892005-06-21 17:17:13 -07006684 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 for (m = 0; m < SYNC_MARKS; m++) {
6686 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006687 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688 }
6689 last_mark = 0;
6690 mddev->resync_mark = mark[last_mark];
6691 mddev->resync_mark_cnt = mark_cnt[last_mark];
6692
6693 /*
6694 * Tune reconstruction:
6695 */
6696 window = 32*(PAGE_SIZE/512);
6697 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6698 window/2,(unsigned long long) max_sectors/2);
6699
6700 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701 last_check = 0;
6702
6703 if (j>2) {
6704 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006705 "md: resuming %s of %s from checkpoint.\n",
6706 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707 mddev->curr_resync = j;
6708 }
NeilBrownefa59332009-12-14 12:49:49 +11006709 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006710
6711 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006712 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713
NeilBrown57afd892005-06-21 17:17:13 -07006714 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006715
NeilBrown7a91ee12009-05-26 12:57:21 +10006716 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6717 ((mddev->curr_resync > mddev->curr_resync_completed &&
6718 (mddev->curr_resync - mddev->curr_resync_completed)
6719 > (max_sectors >> 4)) ||
6720 (j - mddev->curr_resync_completed)*2
6721 >= mddev->resync_max - mddev->curr_resync_completed
6722 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006723 /* time to update curr_resync_completed */
6724 blk_unplug(mddev->queue);
6725 wait_event(mddev->recovery_wait,
6726 atomic_read(&mddev->recovery_active) == 0);
6727 mddev->curr_resync_completed =
6728 mddev->curr_resync;
6729 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006730 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006731 }
NeilBrownacb180b2009-04-14 16:28:34 +10006732
NeilBrowne62e58a2009-07-01 13:15:35 +10006733 while (j >= mddev->resync_max && !kthread_should_stop()) {
6734 /* As this condition is controlled by user-space,
6735 * we can block indefinitely, so use '_interruptible'
6736 * to avoid triggering warnings.
6737 */
6738 flush_signals(current); /* just in case */
6739 wait_event_interruptible(mddev->recovery_wait,
6740 mddev->resync_max > j
6741 || kthread_should_stop());
6742 }
NeilBrownacb180b2009-04-14 16:28:34 +10006743
6744 if (kthread_should_stop())
6745 goto interrupted;
6746
NeilBrown57afd892005-06-21 17:17:13 -07006747 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006748 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006749 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006750 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751 goto out;
6752 }
NeilBrown57afd892005-06-21 17:17:13 -07006753
6754 if (!skipped) { /* actual IO requested */
6755 io_sectors += sectors;
6756 atomic_add(sectors, &mddev->recovery_active);
6757 }
6758
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759 j += sectors;
6760 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006761 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006762 if (last_check == 0)
6763 /* this is the earliers that rebuilt will be
6764 * visible in /proc/mdstat
6765 */
6766 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006767
6768 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769 continue;
6770
NeilBrown57afd892005-06-21 17:17:13 -07006771 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772
NeilBrowndfc70642008-05-23 13:04:39 -07006773 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774 break;
6775
6776 repeat:
6777 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6778 /* step marks */
6779 int next = (last_mark+1) % SYNC_MARKS;
6780
6781 mddev->resync_mark = mark[next];
6782 mddev->resync_mark_cnt = mark_cnt[next];
6783 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006784 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785 last_mark = next;
6786 }
6787
6788
NeilBrownc6207272008-02-06 01:39:52 -08006789 if (kthread_should_stop())
6790 goto interrupted;
6791
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792
6793 /*
6794 * this loop exits only if either when we are slower than
6795 * the 'hard' speed limit, or the system was IO-idle for
6796 * a jiffy.
6797 * the system might be non-idle CPU-wise, but we only care
6798 * about not overloading the IO subsystem. (things like an
6799 * e2fsck being done on the RAID array should execute fast)
6800 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006801 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802 cond_resched();
6803
NeilBrown57afd892005-06-21 17:17:13 -07006804 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6805 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806
NeilBrown88202a02006-01-06 00:21:36 -08006807 if (currspeed > speed_min(mddev)) {
6808 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006809 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006810 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811 goto repeat;
6812 }
6813 }
6814 }
NeilBrown61df9d92006-10-03 01:15:57 -07006815 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816 /*
6817 * this also signals 'finished resyncing' to md_stop
6818 */
6819 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006820 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821
6822 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6823
6824 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006825 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826
NeilBrowndfc70642008-05-23 13:04:39 -07006827 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006828 mddev->curr_resync > 2) {
6829 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6830 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6831 if (mddev->curr_resync >= mddev->recovery_cp) {
6832 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006833 "md: checkpointing %s of %s.\n",
6834 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006835 mddev->recovery_cp = mddev->curr_resync;
6836 }
6837 } else
6838 mddev->recovery_cp = MaxSector;
6839 } else {
6840 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6841 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006842 rcu_read_lock();
6843 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006844 if (rdev->raid_disk >= 0 &&
6845 !test_bit(Faulty, &rdev->flags) &&
6846 !test_bit(In_sync, &rdev->flags) &&
6847 rdev->recovery_offset < mddev->curr_resync)
6848 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07006849 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006851 }
NeilBrown17571282006-12-10 02:20:52 -08006852 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006855 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6856 /* We completed so min/max setting can be forgotten if used. */
6857 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6858 mddev->resync_min = 0;
6859 mddev->resync_max = MaxSector;
6860 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6861 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006863 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6864 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006865 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866 wake_up(&resync_wait);
6867 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6868 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006869 return;
6870
6871 interrupted:
6872 /*
6873 * got a signal, exit.
6874 */
6875 printk(KERN_INFO
6876 "md: md_do_sync() got signal ... exiting\n");
6877 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6878 goto out;
6879
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880}
NeilBrown29269552006-03-27 01:18:10 -08006881EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882
6883
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006884static int remove_and_add_spares(mddev_t *mddev)
6885{
6886 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006887 int spares = 0;
6888
NeilBrown97e4f422009-03-31 14:33:13 +11006889 mddev->curr_resync_completed = 0;
6890
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006891 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006892 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006893 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006894 (test_bit(Faulty, &rdev->flags) ||
6895 ! test_bit(In_sync, &rdev->flags)) &&
6896 atomic_read(&rdev->nr_pending)==0) {
6897 if (mddev->pers->hot_remove_disk(
6898 mddev, rdev->raid_disk)==0) {
6899 char nm[20];
6900 sprintf(nm,"rd%d", rdev->raid_disk);
6901 sysfs_remove_link(&mddev->kobj, nm);
6902 rdev->raid_disk = -1;
6903 }
6904 }
6905
NeilBrown4044ba52009-01-09 08:31:11 +11006906 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006907 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006908 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006909 !test_bit(In_sync, &rdev->flags) &&
6910 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006911 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006912 if (rdev->raid_disk < 0
6913 && !test_bit(Faulty, &rdev->flags)) {
6914 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006915 if (mddev->pers->
6916 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006917 char nm[20];
6918 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006919 if (sysfs_create_link(&mddev->kobj,
6920 &rdev->kobj, nm))
6921 printk(KERN_WARNING
6922 "md: cannot register "
6923 "%s for %s\n",
6924 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006925 spares++;
6926 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11006927 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006928 } else
6929 break;
6930 }
NeilBrowndfc70642008-05-23 13:04:39 -07006931 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006932 }
6933 return spares;
6934}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935/*
6936 * This routine is regularly called by all per-raid-array threads to
6937 * deal with generic issues like resync and super-block update.
6938 * Raid personalities that don't have a thread (linear/raid0) do not
6939 * need this as they never do any recovery or update the superblock.
6940 *
6941 * It does not do any resync itself, but rather "forks" off other threads
6942 * to do that as needed.
6943 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6944 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006945 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946 * and wakeups up this thread which will reap the thread and finish up.
6947 * This thread also removes any faulty devices (with nr_pending == 0).
6948 *
6949 * The overall approach is:
6950 * 1/ if the superblock needs updating, update it.
6951 * 2/ If a recovery thread is running, don't do anything else.
6952 * 3/ If recovery has finished, clean up, possibly marking spares active.
6953 * 4/ If there are any faulty devices, remove them.
6954 * 5/ If array is degraded, try to add spares devices
6955 * 6/ If array has spares or is not in-sync, start a resync thread.
6956 */
6957void md_check_recovery(mddev_t *mddev)
6958{
6959 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960
6961
NeilBrown5f404022005-06-21 17:17:16 -07006962 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11006963 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964
6965 if (mddev->ro)
6966 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006967
6968 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006969 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006970 printk(KERN_INFO "md: %s in immediate safe mode\n",
6971 mdname(mddev));
6972 mddev->safemode = 2;
6973 }
6974 flush_signals(current);
6975 }
6976
NeilBrownc89a8ee2008-08-05 15:54:13 +10006977 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6978 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006980 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006982 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006983 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006984 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6985 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986 ))
6987 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006988
NeilBrowndf5b89b2006-03-27 01:18:20 -08006989 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006990 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006991
NeilBrownc89a8ee2008-08-05 15:54:13 +10006992 if (mddev->ro) {
6993 /* Only thing we do on a ro array is remove
6994 * failed devices.
6995 */
6996 remove_and_add_spares(mddev);
6997 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6998 goto unlock;
6999 }
7000
NeilBrown31a59e32008-04-30 00:52:30 -07007001 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007002 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007003 spin_lock_irq(&mddev->write_lock);
7004 if (mddev->safemode &&
7005 !atomic_read(&mddev->writes_pending) &&
7006 !mddev->in_sync &&
7007 mddev->recovery_cp == MaxSector) {
7008 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007009 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07007010 if (mddev->persistent)
7011 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7012 }
7013 if (mddev->safemode == 1)
7014 mddev->safemode = 0;
7015 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007016 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11007017 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007018 }
NeilBrownfca4d842005-06-21 17:17:11 -07007019
NeilBrown850b2b42006-10-03 01:15:46 -07007020 if (mddev->flags)
7021 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007022
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7024 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7025 /* resync/recovery still happening */
7026 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7027 goto unlock;
7028 }
7029 if (mddev->sync_thread) {
7030 /* resync has finished, collect result */
7031 md_unregister_thread(mddev->sync_thread);
7032 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07007033 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7034 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035 /* success...*/
7036 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10007037 if (mddev->pers->spare_active(mddev))
7038 sysfs_notify(&mddev->kobj, NULL,
7039 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040 }
NeilBrowncea9c222009-03-31 15:15:05 +11007041 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7042 mddev->pers->finish_reshape)
7043 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07007044 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07007045
7046 /* if array is no-longer degraded, then any saved_raid_disk
7047 * information must be scrapped
7048 */
7049 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007050 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07007051 rdev->saved_raid_disk = -1;
7052
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053 mddev->recovery = 0;
7054 /* flag recovery needed just to double check */
7055 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11007056 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007057 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058 goto unlock;
7059 }
Neil Brown72a23c22008-06-28 08:31:41 +10007060 /* Set RUNNING before clearing NEEDED to avoid
7061 * any transients in the value of "sync_action".
7062 */
7063 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7064 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007065 /* Clear some bits that don't mean anything, but
7066 * might be left set
7067 */
NeilBrown24dd4692005-11-08 21:39:26 -08007068 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7069 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007071 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7072 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073 /* no recovery is running.
7074 * remove any failed drives, then
7075 * add spares if possible.
7076 * Spare are also removed and re-added, to allow
7077 * the personality to fail the re-add.
7078 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007080 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007081 if (mddev->pers->check_reshape == NULL ||
7082 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007083 /* Cannot proceed */
7084 goto unlock;
7085 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007086 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007087 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007088 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7089 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007090 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007091 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007092 } else if (mddev->recovery_cp < MaxSector) {
7093 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007094 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007095 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7096 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007098
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007100 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7101 /* We are adding a device or devices to an array
7102 * which has the bitmap stored on all devices.
7103 * So make sure all bitmap pages get written
7104 */
7105 bitmap_write_all(mddev->bitmap);
7106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107 mddev->sync_thread = md_register_thread(md_do_sync,
7108 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007109 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110 if (!mddev->sync_thread) {
7111 printk(KERN_ERR "%s: could not start resync"
7112 " thread...\n",
7113 mdname(mddev));
7114 /* leave the spares where they are, it shouldn't hurt */
7115 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08007116 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007117 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11007118 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007119 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120 }
7121 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007122 if (!mddev->sync_thread) {
7123 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7124 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7125 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007126 if (mddev->sysfs_action)
7127 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129 mddev_unlock(mddev);
7130 }
7131}
7132
Dan Williams6bfe0b42008-04-30 00:52:32 -07007133void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7134{
NeilBrown3c0ee632008-10-21 13:25:28 +11007135 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007136 wait_event_timeout(rdev->blocked_wait,
7137 !test_bit(Blocked, &rdev->flags),
7138 msecs_to_jiffies(5000));
7139 rdev_dec_pending(rdev, mddev);
7140}
7141EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7142
Adrian Bunk75c96f82005-05-05 16:16:09 -07007143static int md_notify_reboot(struct notifier_block *this,
7144 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145{
7146 struct list_head *tmp;
7147 mddev_t *mddev;
7148
7149 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7150
7151 printk(KERN_INFO "md: stopping all md devices.\n");
7152
NeilBrown29ac4aa2008-02-06 01:39:58 -08007153 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007154 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007155 /* Force a switch to readonly even array
7156 * appears to still be in use. Hence
7157 * the '100'.
7158 */
NeilBrowna4bd82d2010-03-29 13:23:10 +11007159 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007160 mddev_unlock(mddev);
7161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162 /*
7163 * certain more exotic SCSI devices are known to be
7164 * volatile wrt too early system reboots. While the
7165 * right place to handle this issue is the given
7166 * driver, we do want to have a safe RAID driver ...
7167 */
7168 mdelay(1000*1);
7169 }
7170 return NOTIFY_DONE;
7171}
7172
Adrian Bunk75c96f82005-05-05 16:16:09 -07007173static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174 .notifier_call = md_notify_reboot,
7175 .next = NULL,
7176 .priority = INT_MAX, /* before any real devices */
7177};
7178
7179static void md_geninit(void)
7180{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7182
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07007183 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184}
7185
Adrian Bunk75c96f82005-05-05 16:16:09 -07007186static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007187{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007188 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189 return -1;
7190 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007191 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192 return -1;
7193 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007194 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007195 md_probe, NULL, NULL);
7196 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197 md_probe, NULL, NULL);
7198
Linus Torvalds1da177e2005-04-16 15:20:36 -07007199 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007200 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201
7202 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007203 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007204}
7205
7206
7207#ifndef MODULE
7208
7209/*
7210 * Searches all registered partitions for autorun RAID arrays
7211 * at boot time.
7212 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007213
7214static LIST_HEAD(all_detected_devices);
7215struct detected_devices_node {
7216 struct list_head list;
7217 dev_t dev;
7218};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219
7220void md_autodetect_dev(dev_t dev)
7221{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007222 struct detected_devices_node *node_detected_dev;
7223
7224 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7225 if (node_detected_dev) {
7226 node_detected_dev->dev = dev;
7227 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7228 } else {
7229 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7230 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007232}
7233
7234
7235static void autostart_arrays(int part)
7236{
7237 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007238 struct detected_devices_node *node_detected_dev;
7239 dev_t dev;
7240 int i_scanned, i_passed;
7241
7242 i_scanned = 0;
7243 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244
7245 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7246
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007247 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7248 i_scanned++;
7249 node_detected_dev = list_entry(all_detected_devices.next,
7250 struct detected_devices_node, list);
7251 list_del(&node_detected_dev->list);
7252 dev = node_detected_dev->dev;
7253 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007254 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007255 if (IS_ERR(rdev))
7256 continue;
7257
NeilBrownb2d444d2005-11-08 21:39:31 -08007258 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007259 MD_BUG();
7260 continue;
7261 }
NeilBrownd0fae182008-03-04 14:29:31 -08007262 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007263 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007264 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007265 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007266
7267 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7268 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007269
7270 autorun_devices(part);
7271}
7272
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007273#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007274
7275static __exit void md_exit(void)
7276{
7277 mddev_t *mddev;
7278 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007279
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007280 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007281 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007283 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284 unregister_blkdev(mdp_major, "mdp");
7285 unregister_reboot_notifier(&md_notifier);
7286 unregister_sysctl_table(raid_table_header);
7287 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007288 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007289 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007290 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291 }
7292}
7293
Dan Williams685784a2007-07-09 11:56:42 -07007294subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295module_exit(md_exit)
7296
NeilBrownf91de922005-11-08 21:39:36 -08007297static int get_ro(char *buffer, struct kernel_param *kp)
7298{
7299 return sprintf(buffer, "%d", start_readonly);
7300}
7301static int set_ro(const char *val, struct kernel_param *kp)
7302{
7303 char *e;
7304 int num = simple_strtoul(val, &e, 10);
7305 if (*val && (*e == '\0' || *e == '\n')) {
7306 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007307 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007308 }
7309 return -EINVAL;
7310}
7311
NeilBrown80ca3a42006-07-10 04:44:18 -07007312module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7313module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007314
NeilBrownefeb53c2009-01-09 08:31:10 +11007315module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007316
Linus Torvalds1da177e2005-04-16 15:20:36 -07007317EXPORT_SYMBOL(register_md_personality);
7318EXPORT_SYMBOL(unregister_md_personality);
7319EXPORT_SYMBOL(md_error);
7320EXPORT_SYMBOL(md_done_sync);
7321EXPORT_SYMBOL(md_write_start);
7322EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323EXPORT_SYMBOL(md_register_thread);
7324EXPORT_SYMBOL(md_unregister_thread);
7325EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007326EXPORT_SYMBOL(md_check_recovery);
7327MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007328MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007329MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007330MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);