blob: af0780ae56b572a8a49c882ca112eabb62f5ffd7 [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
NeilBrown850b2b422006-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
NeilBrown850b2b422006-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 */
NeilBrown850b2b422006-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 */
NeilBrown850b2b422006-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);
NeilBrown850b2b422006-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);
NeilBrown850b2b422006-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 }
NeilBrown850b2b422006-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");
NeilBrownd9d166c2006-01-06 00:20:51 -08003078 return rv;
3079}
3080
3081static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003082__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003083
NeilBrownd4dbd022006-06-26 00:27:59 -07003084
3085static ssize_t
3086layout_show(mddev_t *mddev, char *page)
3087{
3088 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003089 if (mddev->reshape_position != MaxSector &&
3090 mddev->layout != mddev->new_layout)
3091 return sprintf(page, "%d (%d)\n",
3092 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003093 return sprintf(page, "%d\n", mddev->layout);
3094}
3095
3096static ssize_t
3097layout_store(mddev_t *mddev, const char *buf, size_t len)
3098{
3099 char *e;
3100 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003101
3102 if (!*buf || (*e && *e != '\n'))
3103 return -EINVAL;
3104
NeilBrownb3546032009-03-31 14:56:41 +11003105 if (mddev->pers) {
3106 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003107 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003108 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003109 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003110 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003111 if (err) {
3112 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003113 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003114 }
NeilBrownb3546032009-03-31 14:56:41 +11003115 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003116 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003117 if (mddev->reshape_position == MaxSector)
3118 mddev->layout = n;
3119 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003120 return len;
3121}
3122static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003123__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003124
3125
NeilBrowneae17012005-11-08 21:39:23 -08003126static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003127raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003128{
NeilBrownbb636542005-11-08 21:39:45 -08003129 if (mddev->raid_disks == 0)
3130 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003131 if (mddev->reshape_position != MaxSector &&
3132 mddev->delta_disks != 0)
3133 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3134 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003135 return sprintf(page, "%d\n", mddev->raid_disks);
3136}
3137
NeilBrownda943b992006-01-06 00:20:54 -08003138static int update_raid_disks(mddev_t *mddev, int raid_disks);
3139
3140static ssize_t
3141raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3142{
NeilBrownda943b992006-01-06 00:20:54 -08003143 char *e;
3144 int rv = 0;
3145 unsigned long n = simple_strtoul(buf, &e, 10);
3146
3147 if (!*buf || (*e && *e != '\n'))
3148 return -EINVAL;
3149
3150 if (mddev->pers)
3151 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003152 else if (mddev->reshape_position != MaxSector) {
3153 int olddisks = mddev->raid_disks - mddev->delta_disks;
3154 mddev->delta_disks = n - olddisks;
3155 mddev->raid_disks = n;
3156 } else
NeilBrownda943b992006-01-06 00:20:54 -08003157 mddev->raid_disks = n;
3158 return rv ? rv : len;
3159}
3160static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003161__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003162
NeilBrown24dd4692005-11-08 21:39:26 -08003163static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003164chunk_size_show(mddev_t *mddev, char *page)
3165{
NeilBrown08a02ec2007-05-09 02:35:38 -07003166 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003167 mddev->chunk_sectors != mddev->new_chunk_sectors)
3168 return sprintf(page, "%d (%d)\n",
3169 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003170 mddev->chunk_sectors << 9);
3171 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003172}
3173
3174static ssize_t
3175chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3176{
NeilBrown3b343802006-01-06 00:20:47 -08003177 char *e;
3178 unsigned long n = simple_strtoul(buf, &e, 10);
3179
NeilBrown3b343802006-01-06 00:20:47 -08003180 if (!*buf || (*e && *e != '\n'))
3181 return -EINVAL;
3182
NeilBrownb3546032009-03-31 14:56:41 +11003183 if (mddev->pers) {
3184 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003185 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003186 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003187 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003188 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003189 if (err) {
3190 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003191 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003192 }
NeilBrownb3546032009-03-31 14:56:41 +11003193 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003194 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003195 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003196 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003197 }
NeilBrown3b343802006-01-06 00:20:47 -08003198 return len;
3199}
3200static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003201__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003202
NeilBrowna94213b2006-06-26 00:28:00 -07003203static ssize_t
3204resync_start_show(mddev_t *mddev, char *page)
3205{
NeilBrownd1a7c502009-03-31 15:24:32 +11003206 if (mddev->recovery_cp == MaxSector)
3207 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003208 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3209}
3210
3211static ssize_t
3212resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3213{
NeilBrowna94213b2006-06-26 00:28:00 -07003214 char *e;
3215 unsigned long long n = simple_strtoull(buf, &e, 10);
3216
3217 if (mddev->pers)
3218 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003219 if (cmd_match(buf, "none"))
3220 n = MaxSector;
3221 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003222 return -EINVAL;
3223
3224 mddev->recovery_cp = n;
3225 return len;
3226}
3227static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003228__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003229
NeilBrown9e653b62006-06-26 00:27:58 -07003230/*
3231 * The array state can be:
3232 *
3233 * clear
3234 * No devices, no size, no level
3235 * Equivalent to STOP_ARRAY ioctl
3236 * inactive
3237 * May have some settings, but array is not active
3238 * all IO results in error
3239 * When written, doesn't tear down array, but just stops it
3240 * suspended (not supported yet)
3241 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003242 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003243 * readonly
3244 * no resync can happen. no superblocks get written.
3245 * write requests fail
3246 * read-auto
3247 * like readonly, but behaves like 'clean' on a write request.
3248 *
3249 * clean - no pending writes, but otherwise active.
3250 * When written to inactive array, starts without resync
3251 * If a write request arrives then
3252 * if metadata is known, mark 'dirty' and switch to 'active'.
3253 * if not known, block and switch to write-pending
3254 * If written to an active array that has pending writes, then fails.
3255 * active
3256 * fully active: IO and resync can be happening.
3257 * When written to inactive array, starts with resync
3258 *
3259 * write-pending
3260 * clean, but writes are blocked waiting for 'active' to be written.
3261 *
3262 * active-idle
3263 * like active, but no writes have been seen for a while (100msec).
3264 *
3265 */
3266enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3267 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003268static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003269 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3270 "write-pending", "active-idle", NULL };
3271
3272static int match_word(const char *word, char **list)
3273{
3274 int n;
3275 for (n=0; list[n]; n++)
3276 if (cmd_match(word, list[n]))
3277 break;
3278 return n;
3279}
3280
3281static ssize_t
3282array_state_show(mddev_t *mddev, char *page)
3283{
3284 enum array_state st = inactive;
3285
3286 if (mddev->pers)
3287 switch(mddev->ro) {
3288 case 1:
3289 st = readonly;
3290 break;
3291 case 2:
3292 st = read_auto;
3293 break;
3294 case 0:
3295 if (mddev->in_sync)
3296 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08003297 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3298 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003299 else if (mddev->safemode)
3300 st = active_idle;
3301 else
3302 st = active;
3303 }
3304 else {
3305 if (list_empty(&mddev->disks) &&
3306 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003307 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003308 st = clear;
3309 else
3310 st = inactive;
3311 }
3312 return sprintf(page, "%s\n", array_states[st]);
3313}
3314
Neil Browndf5b20c2008-07-11 22:02:22 +10003315static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrowna4bd82d2010-03-29 13:23:10 +11003316static int md_set_readonly(mddev_t * mddev, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003317static int do_md_run(mddev_t * mddev);
3318static int restart_array(mddev_t *mddev);
3319
3320static ssize_t
3321array_state_store(mddev_t *mddev, const char *buf, size_t len)
3322{
3323 int err = -EINVAL;
3324 enum array_state st = match_word(buf, array_states);
3325 switch(st) {
3326 case bad_word:
3327 break;
3328 case clear:
3329 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003330 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003331 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003332 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003333 break;
3334 case inactive:
3335 /* stopping an active array */
3336 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003337 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003338 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003339 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003340 } else
3341 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003342 break;
3343 case suspended:
3344 break; /* not supported yet */
3345 case readonly:
3346 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003347 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003348 else {
3349 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003350 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003351 err = do_md_run(mddev);
3352 }
3353 break;
3354 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003355 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003356 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003357 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003358 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003359 err = restart_array(mddev);
3360 if (err == 0) {
3361 mddev->ro = 2;
3362 set_disk_ro(mddev->gendisk, 0);
3363 }
NeilBrown9e653b62006-06-26 00:27:58 -07003364 } else {
3365 mddev->ro = 2;
3366 err = do_md_run(mddev);
3367 }
3368 break;
3369 case clean:
3370 if (mddev->pers) {
3371 restart_array(mddev);
3372 spin_lock_irq(&mddev->write_lock);
3373 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003374 if (mddev->in_sync == 0) {
3375 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003376 if (mddev->safemode == 1)
3377 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003378 if (mddev->persistent)
3379 set_bit(MD_CHANGE_CLEAN,
3380 &mddev->flags);
3381 }
3382 err = 0;
3383 } else
3384 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003385 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003386 } else
3387 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003388 break;
3389 case active:
3390 if (mddev->pers) {
3391 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003392 if (mddev->external)
3393 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003394 wake_up(&mddev->sb_wait);
3395 err = 0;
3396 } else {
3397 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003398 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003399 err = do_md_run(mddev);
3400 }
3401 break;
3402 case write_pending:
3403 case active_idle:
3404 /* these cannot be set */
3405 break;
3406 }
3407 if (err)
3408 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003409 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003410 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003411 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003412 }
NeilBrown9e653b62006-06-26 00:27:58 -07003413}
NeilBrown80ca3a42006-07-10 04:44:18 -07003414static struct md_sysfs_entry md_array_state =
3415__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003416
NeilBrown6d7ff7382006-01-06 00:21:16 -08003417static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003418max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3419 return sprintf(page, "%d\n",
3420 atomic_read(&mddev->max_corr_read_errors));
3421}
3422
3423static ssize_t
3424max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3425{
3426 char *e;
3427 unsigned long n = simple_strtoul(buf, &e, 10);
3428
3429 if (*buf && (*e == 0 || *e == '\n')) {
3430 atomic_set(&mddev->max_corr_read_errors, n);
3431 return len;
3432 }
3433 return -EINVAL;
3434}
3435
3436static struct md_sysfs_entry max_corr_read_errors =
3437__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3438 max_corrected_read_errors_store);
3439
3440static ssize_t
NeilBrown6d7ff7382006-01-06 00:21:16 -08003441null_show(mddev_t *mddev, char *page)
3442{
3443 return -EINVAL;
3444}
3445
3446static ssize_t
3447new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3448{
3449 /* buf must be %d:%d\n? giving major and minor numbers */
3450 /* The new device is added to the array.
3451 * If the array has a persistent superblock, we read the
3452 * superblock to initialise info and check validity.
3453 * Otherwise, only checking done is that in bind_rdev_to_array,
3454 * which mainly checks size.
3455 */
3456 char *e;
3457 int major = simple_strtoul(buf, &e, 10);
3458 int minor;
3459 dev_t dev;
3460 mdk_rdev_t *rdev;
3461 int err;
3462
3463 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3464 return -EINVAL;
3465 minor = simple_strtoul(e+1, &e, 10);
3466 if (*e && *e != '\n')
3467 return -EINVAL;
3468 dev = MKDEV(major, minor);
3469 if (major != MAJOR(dev) ||
3470 minor != MINOR(dev))
3471 return -EOVERFLOW;
3472
3473
3474 if (mddev->persistent) {
3475 rdev = md_import_device(dev, mddev->major_version,
3476 mddev->minor_version);
3477 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3478 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3479 mdk_rdev_t, same_set);
3480 err = super_types[mddev->major_version]
3481 .load_super(rdev, rdev0, mddev->minor_version);
3482 if (err < 0)
3483 goto out;
3484 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003485 } else if (mddev->external)
3486 rdev = md_import_device(dev, -2, -1);
3487 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003488 rdev = md_import_device(dev, -1, -1);
3489
3490 if (IS_ERR(rdev))
3491 return PTR_ERR(rdev);
3492 err = bind_rdev_to_array(rdev, mddev);
3493 out:
3494 if (err)
3495 export_rdev(rdev);
3496 return err ? err : len;
3497}
3498
3499static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003500__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003501
3502static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003503bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3504{
3505 char *end;
3506 unsigned long chunk, end_chunk;
3507
3508 if (!mddev->bitmap)
3509 goto out;
3510 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3511 while (*buf) {
3512 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3513 if (buf == end) break;
3514 if (*end == '-') { /* range */
3515 buf = end + 1;
3516 end_chunk = simple_strtoul(buf, &end, 0);
3517 if (buf == end) break;
3518 }
3519 if (*end && !isspace(*end)) break;
3520 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003521 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003522 }
3523 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3524out:
3525 return len;
3526}
3527
3528static struct md_sysfs_entry md_bitmap =
3529__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3530
3531static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003532size_show(mddev_t *mddev, char *page)
3533{
Andre Noll58c0fed2009-03-31 14:33:13 +11003534 return sprintf(page, "%llu\n",
3535 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003536}
3537
Andre Nolld71f9f82008-07-11 22:02:22 +10003538static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003539
3540static ssize_t
3541size_store(mddev_t *mddev, const char *buf, size_t len)
3542{
3543 /* If array is inactive, we can reduce the component size, but
3544 * not increase it (except from 0).
3545 * If array is active, we can try an on-line resize
3546 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003547 sector_t sectors;
3548 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003549
Andre Noll58c0fed2009-03-31 14:33:13 +11003550 if (err < 0)
3551 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003552 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003553 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07003554 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003555 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003556 if (mddev->dev_sectors == 0 ||
3557 mddev->dev_sectors > sectors)
3558 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003559 else
3560 err = -ENOSPC;
3561 }
3562 return err ? err : len;
3563}
3564
3565static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003566__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003567
NeilBrown8bb93aa2006-01-06 00:20:50 -08003568
3569/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003570 * This is one of
3571 * 'none' for arrays with no metadata (good luck...)
3572 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003573 * or N.M for internally known formats
3574 */
3575static ssize_t
3576metadata_show(mddev_t *mddev, char *page)
3577{
3578 if (mddev->persistent)
3579 return sprintf(page, "%d.%d\n",
3580 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003581 else if (mddev->external)
3582 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003583 else
3584 return sprintf(page, "none\n");
3585}
3586
3587static ssize_t
3588metadata_store(mddev_t *mddev, const char *buf, size_t len)
3589{
3590 int major, minor;
3591 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003592 /* Changing the details of 'external' metadata is
3593 * always permitted. Otherwise there must be
3594 * no devices attached to the array.
3595 */
3596 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3597 ;
3598 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003599 return -EBUSY;
3600
3601 if (cmd_match(buf, "none")) {
3602 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003603 mddev->external = 0;
3604 mddev->major_version = 0;
3605 mddev->minor_version = 90;
3606 return len;
3607 }
3608 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003609 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003610 if (namelen >= sizeof(mddev->metadata_type))
3611 namelen = sizeof(mddev->metadata_type)-1;
3612 strncpy(mddev->metadata_type, buf+9, namelen);
3613 mddev->metadata_type[namelen] = 0;
3614 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3615 mddev->metadata_type[--namelen] = 0;
3616 mddev->persistent = 0;
3617 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003618 mddev->major_version = 0;
3619 mddev->minor_version = 90;
3620 return len;
3621 }
3622 major = simple_strtoul(buf, &e, 10);
3623 if (e==buf || *e != '.')
3624 return -EINVAL;
3625 buf = e+1;
3626 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003627 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003628 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003629 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003630 return -ENOENT;
3631 mddev->major_version = major;
3632 mddev->minor_version = minor;
3633 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003634 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003635 return len;
3636}
3637
3638static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003639__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003640
NeilBrowna35b0d62006-01-06 00:20:49 -08003641static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003642action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003643{
NeilBrown7eec3142005-11-08 21:39:44 -08003644 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003645 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3646 type = "frozen";
3647 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003648 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003649 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3650 type = "reshape";
3651 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003652 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3653 type = "resync";
3654 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3655 type = "check";
3656 else
3657 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003658 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003659 type = "recover";
3660 }
3661 return sprintf(page, "%s\n", type);
3662}
3663
3664static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003665action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003666{
NeilBrown7eec3142005-11-08 21:39:44 -08003667 if (!mddev->pers || !mddev->pers->sync_request)
3668 return -EINVAL;
3669
NeilBrownb6a9ce62009-05-26 09:41:17 +10003670 if (cmd_match(page, "frozen"))
3671 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3672 else
3673 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3674
3675 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003676 if (mddev->sync_thread) {
3677 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3678 md_unregister_thread(mddev->sync_thread);
3679 mddev->sync_thread = NULL;
3680 mddev->recovery = 0;
3681 }
NeilBrown03c902e2006-01-06 00:20:46 -08003682 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3683 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003684 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003685 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003686 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003687 else if (cmd_match(page, "recover")) {
3688 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3689 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3690 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003691 int err;
3692 if (mddev->pers->start_reshape == NULL)
3693 return -EINVAL;
3694 err = mddev->pers->start_reshape(mddev);
3695 if (err)
3696 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003697 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003698 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003699 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003700 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003701 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003702 return -EINVAL;
3703 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3704 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003705 }
NeilBrown03c902e2006-01-06 00:20:46 -08003706 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003707 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003708 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003709 return len;
3710}
3711
NeilBrown9d888832005-11-08 21:39:26 -08003712static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003713mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003714{
3715 return sprintf(page, "%llu\n",
3716 (unsigned long long) mddev->resync_mismatches);
3717}
3718
NeilBrown80ca3a42006-07-10 04:44:18 -07003719static struct md_sysfs_entry md_scan_mode =
3720__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003721
NeilBrown96de1e62005-11-08 21:39:39 -08003722
NeilBrown80ca3a42006-07-10 04:44:18 -07003723static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003724
NeilBrown88202a02006-01-06 00:21:36 -08003725static ssize_t
3726sync_min_show(mddev_t *mddev, char *page)
3727{
3728 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3729 mddev->sync_speed_min ? "local": "system");
3730}
3731
3732static ssize_t
3733sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3734{
3735 int min;
3736 char *e;
3737 if (strncmp(buf, "system", 6)==0) {
3738 mddev->sync_speed_min = 0;
3739 return len;
3740 }
3741 min = simple_strtoul(buf, &e, 10);
3742 if (buf == e || (*e && *e != '\n') || min <= 0)
3743 return -EINVAL;
3744 mddev->sync_speed_min = min;
3745 return len;
3746}
3747
3748static struct md_sysfs_entry md_sync_min =
3749__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3750
3751static ssize_t
3752sync_max_show(mddev_t *mddev, char *page)
3753{
3754 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3755 mddev->sync_speed_max ? "local": "system");
3756}
3757
3758static ssize_t
3759sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3760{
3761 int max;
3762 char *e;
3763 if (strncmp(buf, "system", 6)==0) {
3764 mddev->sync_speed_max = 0;
3765 return len;
3766 }
3767 max = simple_strtoul(buf, &e, 10);
3768 if (buf == e || (*e && *e != '\n') || max <= 0)
3769 return -EINVAL;
3770 mddev->sync_speed_max = max;
3771 return len;
3772}
3773
3774static struct md_sysfs_entry md_sync_max =
3775__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3776
Iustin Popd7f3d292007-10-16 23:30:54 -07003777static ssize_t
3778degraded_show(mddev_t *mddev, char *page)
3779{
3780 return sprintf(page, "%d\n", mddev->degraded);
3781}
3782static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003783
3784static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003785sync_force_parallel_show(mddev_t *mddev, char *page)
3786{
3787 return sprintf(page, "%d\n", mddev->parallel_resync);
3788}
3789
3790static ssize_t
3791sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3792{
3793 long n;
3794
3795 if (strict_strtol(buf, 10, &n))
3796 return -EINVAL;
3797
3798 if (n != 0 && n != 1)
3799 return -EINVAL;
3800
3801 mddev->parallel_resync = n;
3802
3803 if (mddev->sync_thread)
3804 wake_up(&resync_wait);
3805
3806 return len;
3807}
3808
3809/* force parallel resync, even with shared block devices */
3810static struct md_sysfs_entry md_sync_force_parallel =
3811__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3812 sync_force_parallel_show, sync_force_parallel_store);
3813
3814static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003815sync_speed_show(mddev_t *mddev, char *page)
3816{
3817 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003818 if (mddev->curr_resync == 0)
3819 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003820 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3821 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003822 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003823 db = resync - mddev->resync_mark_cnt;
3824 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003825}
3826
NeilBrown80ca3a42006-07-10 04:44:18 -07003827static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003828
3829static ssize_t
3830sync_completed_show(mddev_t *mddev, char *page)
3831{
Andre Noll58c0fed2009-03-31 14:33:13 +11003832 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003833
NeilBrownacb180b2009-04-14 16:28:34 +10003834 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3835 return sprintf(page, "none\n");
3836
NeilBrown88202a02006-01-06 00:21:36 -08003837 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003838 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003839 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003840 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003841
NeilBrownacb180b2009-04-14 16:28:34 +10003842 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003843 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003844}
3845
NeilBrown80ca3a42006-07-10 04:44:18 -07003846static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003847
NeilBrowne464eaf2006-03-27 01:18:14 -08003848static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003849min_sync_show(mddev_t *mddev, char *page)
3850{
3851 return sprintf(page, "%llu\n",
3852 (unsigned long long)mddev->resync_min);
3853}
3854static ssize_t
3855min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3856{
3857 unsigned long long min;
3858 if (strict_strtoull(buf, 10, &min))
3859 return -EINVAL;
3860 if (min > mddev->resync_max)
3861 return -EINVAL;
3862 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3863 return -EBUSY;
3864
3865 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003866 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003867 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003868 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003869 return -EINVAL;
3870 }
3871 mddev->resync_min = min;
3872
3873 return len;
3874}
3875
3876static struct md_sysfs_entry md_min_sync =
3877__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3878
3879static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003880max_sync_show(mddev_t *mddev, char *page)
3881{
3882 if (mddev->resync_max == MaxSector)
3883 return sprintf(page, "max\n");
3884 else
3885 return sprintf(page, "%llu\n",
3886 (unsigned long long)mddev->resync_max);
3887}
3888static ssize_t
3889max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3890{
3891 if (strncmp(buf, "max", 3) == 0)
3892 mddev->resync_max = MaxSector;
3893 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003894 unsigned long long max;
3895 if (strict_strtoull(buf, 10, &max))
3896 return -EINVAL;
3897 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003898 return -EINVAL;
3899 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003900 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003901 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3902 return -EBUSY;
3903
3904 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003905 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003906 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003907 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003908 return -EINVAL;
3909 }
3910 mddev->resync_max = max;
3911 }
3912 wake_up(&mddev->recovery_wait);
3913 return len;
3914}
3915
3916static struct md_sysfs_entry md_max_sync =
3917__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3918
3919static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003920suspend_lo_show(mddev_t *mddev, char *page)
3921{
3922 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3923}
3924
3925static ssize_t
3926suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3927{
3928 char *e;
3929 unsigned long long new = simple_strtoull(buf, &e, 10);
3930
NeilBrownb8d966e2009-07-01 11:14:04 +10003931 if (mddev->pers == NULL ||
3932 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003933 return -EINVAL;
3934 if (buf == e || (*e && *e != '\n'))
3935 return -EINVAL;
3936 if (new >= mddev->suspend_hi ||
3937 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3938 mddev->suspend_lo = new;
3939 mddev->pers->quiesce(mddev, 2);
3940 return len;
3941 } else
3942 return -EINVAL;
3943}
3944static struct md_sysfs_entry md_suspend_lo =
3945__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3946
3947
3948static ssize_t
3949suspend_hi_show(mddev_t *mddev, char *page)
3950{
3951 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3952}
3953
3954static ssize_t
3955suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3956{
3957 char *e;
3958 unsigned long long new = simple_strtoull(buf, &e, 10);
3959
NeilBrownb8d966e2009-07-01 11:14:04 +10003960 if (mddev->pers == NULL ||
3961 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003962 return -EINVAL;
3963 if (buf == e || (*e && *e != '\n'))
3964 return -EINVAL;
3965 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3966 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3967 mddev->suspend_hi = new;
3968 mddev->pers->quiesce(mddev, 1);
3969 mddev->pers->quiesce(mddev, 0);
3970 return len;
3971 } else
3972 return -EINVAL;
3973}
3974static struct md_sysfs_entry md_suspend_hi =
3975__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3976
NeilBrown08a02ec2007-05-09 02:35:38 -07003977static ssize_t
3978reshape_position_show(mddev_t *mddev, char *page)
3979{
3980 if (mddev->reshape_position != MaxSector)
3981 return sprintf(page, "%llu\n",
3982 (unsigned long long)mddev->reshape_position);
3983 strcpy(page, "none\n");
3984 return 5;
3985}
3986
3987static ssize_t
3988reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3989{
3990 char *e;
3991 unsigned long long new = simple_strtoull(buf, &e, 10);
3992 if (mddev->pers)
3993 return -EBUSY;
3994 if (buf == e || (*e && *e != '\n'))
3995 return -EINVAL;
3996 mddev->reshape_position = new;
3997 mddev->delta_disks = 0;
3998 mddev->new_level = mddev->level;
3999 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004000 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004001 return len;
4002}
4003
4004static struct md_sysfs_entry md_reshape_position =
4005__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4006 reshape_position_store);
4007
Dan Williamsb522adc2009-03-31 15:00:31 +11004008static ssize_t
4009array_size_show(mddev_t *mddev, char *page)
4010{
4011 if (mddev->external_size)
4012 return sprintf(page, "%llu\n",
4013 (unsigned long long)mddev->array_sectors/2);
4014 else
4015 return sprintf(page, "default\n");
4016}
4017
4018static ssize_t
4019array_size_store(mddev_t *mddev, const char *buf, size_t len)
4020{
4021 sector_t sectors;
4022
4023 if (strncmp(buf, "default", 7) == 0) {
4024 if (mddev->pers)
4025 sectors = mddev->pers->size(mddev, 0, 0);
4026 else
4027 sectors = mddev->array_sectors;
4028
4029 mddev->external_size = 0;
4030 } else {
4031 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4032 return -EINVAL;
4033 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004034 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004035
4036 mddev->external_size = 1;
4037 }
4038
4039 mddev->array_sectors = sectors;
4040 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004041 if (mddev->pers)
4042 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11004043
4044 return len;
4045}
4046
4047static struct md_sysfs_entry md_array_size =
4048__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4049 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004050
NeilBrowneae17012005-11-08 21:39:23 -08004051static struct attribute *md_default_attrs[] = {
4052 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004053 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004054 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004055 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004056 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004057 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004058 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004059 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004060 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004061 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004062 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004063 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004064 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004065 NULL,
4066};
4067
4068static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004069 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004070 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004071 &md_sync_min.attr,
4072 &md_sync_max.attr,
4073 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004074 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004075 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004076 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004077 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004078 &md_suspend_lo.attr,
4079 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004080 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004081 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004082 NULL,
4083};
NeilBrown411036f2005-11-08 21:39:40 -08004084static struct attribute_group md_redundancy_group = {
4085 .name = NULL,
4086 .attrs = md_redundancy_attrs,
4087};
4088
NeilBrowneae17012005-11-08 21:39:23 -08004089
4090static ssize_t
4091md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4092{
4093 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4094 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004095 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004096
4097 if (!entry->show)
4098 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004099 rv = mddev_lock(mddev);
4100 if (!rv) {
4101 rv = entry->show(mddev, page);
4102 mddev_unlock(mddev);
4103 }
NeilBrown96de1e62005-11-08 21:39:39 -08004104 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004105}
4106
4107static ssize_t
4108md_attr_store(struct kobject *kobj, struct attribute *attr,
4109 const char *page, size_t length)
4110{
4111 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4112 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004113 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004114
4115 if (!entry->store)
4116 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004117 if (!capable(CAP_SYS_ADMIN))
4118 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004119 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004120 if (mddev->hold_active == UNTIL_IOCTL)
4121 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004122 if (!rv) {
4123 rv = entry->store(mddev, page, length);
4124 mddev_unlock(mddev);
4125 }
NeilBrown96de1e62005-11-08 21:39:39 -08004126 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004127}
4128
4129static void md_free(struct kobject *ko)
4130{
4131 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004132
4133 if (mddev->sysfs_state)
4134 sysfs_put(mddev->sysfs_state);
4135
4136 if (mddev->gendisk) {
4137 del_gendisk(mddev->gendisk);
4138 put_disk(mddev->gendisk);
4139 }
4140 if (mddev->queue)
4141 blk_cleanup_queue(mddev->queue);
4142
NeilBrowneae17012005-11-08 21:39:23 -08004143 kfree(mddev);
4144}
4145
4146static struct sysfs_ops md_sysfs_ops = {
4147 .show = md_attr_show,
4148 .store = md_attr_store,
4149};
4150static struct kobj_type md_ktype = {
4151 .release = md_free,
4152 .sysfs_ops = &md_sysfs_ops,
4153 .default_attrs = md_default_attrs,
4154};
4155
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156int mdp_major = 0;
4157
Dan Williams5fd3a172009-03-04 00:57:25 -07004158static void mddev_delayed_delete(struct work_struct *ws)
4159{
4160 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4161
NeilBrown43a70502009-12-14 12:49:55 +11004162 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004163 kobject_del(&mddev->kobj);
4164 kobject_put(&mddev->kobj);
4165}
4166
NeilBrownefeb53c2009-01-09 08:31:10 +11004167static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004169 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 mddev_t *mddev = mddev_find(dev);
4171 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004172 int partitioned;
4173 int shift;
4174 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004175 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
4177 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004178 return -ENODEV;
4179
4180 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4181 shift = partitioned ? MdpMinorShift : 0;
4182 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183
NeilBrownd3374822009-01-09 08:31:10 +11004184 /* wait for any previous instance if this device
4185 * to be completed removed (mddev_delayed_delete).
4186 */
4187 flush_scheduled_work();
4188
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004189 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004190 error = -EEXIST;
4191 if (mddev->gendisk)
4192 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004193
4194 if (name) {
4195 /* Need to ensure that 'name' is not a duplicate.
4196 */
4197 mddev_t *mddev2;
4198 spin_lock(&all_mddevs_lock);
4199
4200 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4201 if (mddev2->gendisk &&
4202 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4203 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004204 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004205 }
4206 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 }
NeilBrown8b765392009-01-09 08:31:08 +11004208
NeilBrown0909dc42009-07-01 12:27:21 +10004209 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004210 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004211 if (!mddev->queue)
4212 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004213 mddev->queue->queuedata = mddev;
4214
NeilBrown8b765392009-01-09 08:31:08 +11004215 /* Can be unlocked because the queue is new: no concurrency */
4216 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4217
NeilBrown409c57f2009-03-31 14:39:39 +11004218 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004219
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 disk = alloc_disk(1 << shift);
4221 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004222 blk_cleanup_queue(mddev->queue);
4223 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004224 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004226 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004228 if (name)
4229 strcpy(disk->disk_name, name);
4230 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004232 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 disk->fops = &md_fops;
4235 disk->private_data = mddev;
4236 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004237 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004238 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004239 * remove it now.
4240 */
4241 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 add_disk(disk);
4243 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004244 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4245 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004246 if (error) {
4247 /* This isn't possible, but as kobject_init_and_add is marked
4248 * __must_check, we must do something with the result
4249 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004250 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4251 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004252 error = 0;
4253 }
NeilBrown43a70502009-12-14 12:49:55 +11004254 if (sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4255 printk(KERN_DEBUG "pointless warning\n");
NeilBrown0909dc42009-07-01 12:27:21 +10004256 abort:
4257 mutex_unlock(&disks_mutex);
4258 if (!error) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004259 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11004260 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
4261 }
NeilBrownd3374822009-01-09 08:31:10 +11004262 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004263 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004264}
4265
4266static struct kobject *md_probe(dev_t dev, int *part, void *data)
4267{
4268 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 return NULL;
4270}
4271
NeilBrownefeb53c2009-01-09 08:31:10 +11004272static int add_named_array(const char *val, struct kernel_param *kp)
4273{
4274 /* val must be "md_*" where * is not all digits.
4275 * We allocate an array with a large free minor number, and
4276 * set the name to val. val must not already be an active name.
4277 */
4278 int len = strlen(val);
4279 char buf[DISK_NAME_LEN];
4280
4281 while (len && val[len-1] == '\n')
4282 len--;
4283 if (len >= DISK_NAME_LEN)
4284 return -E2BIG;
4285 strlcpy(buf, val, len+1);
4286 if (strncmp(buf, "md_", 3) != 0)
4287 return -EINVAL;
4288 return md_alloc(0, buf);
4289}
4290
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291static void md_safemode_timeout(unsigned long data)
4292{
4293 mddev_t *mddev = (mddev_t *) data;
4294
Neil Brown0fd62b82008-06-28 08:31:36 +10004295 if (!atomic_read(&mddev->writes_pending)) {
4296 mddev->safemode = 1;
4297 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11004298 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 md_wakeup_thread(mddev->thread);
4301}
4302
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004303static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304
NeilBrownfe60b012010-03-29 11:10:42 +11004305static int md_run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306{
NeilBrown2604b702006-01-06 00:20:36 -08004307 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 mdk_rdev_t *rdev;
NeilBrown2604b702006-01-06 00:20:36 -08004309 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310
NeilBrowna757e642005-04-16 15:26:42 -07004311 if (list_empty(&mddev->disks))
4312 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314
4315 if (mddev->pers)
4316 return -EBUSY;
4317
NeilBrownb6eb1272010-04-15 10:13:47 +10004318 /* These two calls synchronise us with the
4319 * sysfs_remove_group calls in mddev_unlock,
4320 * so they must have completed.
4321 */
4322 mutex_lock(&mddev->open_mutex);
4323 mutex_unlock(&mddev->open_mutex);
4324
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 /*
4326 * Analyze all RAID superblock(s)
4327 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004328 if (!mddev->raid_disks) {
4329 if (!mddev->persistent)
4330 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004331 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333
NeilBrownd9d166c2006-01-06 00:20:51 -08004334 if (mddev->level != LEVEL_NONE)
4335 request_module("md-level-%d", mddev->level);
4336 else if (mddev->clevel[0])
4337 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338
4339 /*
4340 * Drop all container device buffers, from now on
4341 * the only valid external interface is through the md
4342 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004344 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004345 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 continue;
4347 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004348 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004349
4350 /* perform some consistency tests on the device.
4351 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004352 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004353 */
Andre Noll0f420352008-07-11 22:02:23 +10004354 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004355 if (mddev->dev_sectors &&
4356 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004357 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004358 printk("md: %s: data overlaps metadata\n",
4359 mdname(mddev));
4360 return -EINVAL;
4361 }
4362 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004363 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004364 > rdev->data_offset) {
4365 printk("md: %s: metadata overlaps data\n",
4366 mdname(mddev));
4367 return -EINVAL;
4368 }
4369 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004370 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 }
4372
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004374 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004375 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004377 if (mddev->level != LEVEL_NONE)
4378 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4379 mddev->level);
4380 else
4381 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4382 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 return -EINVAL;
4384 }
NeilBrown2604b702006-01-06 00:20:36 -08004385 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004387 if (mddev->level != pers->level) {
4388 mddev->level = pers->level;
4389 mddev->new_level = pers->level;
4390 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004391 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392
NeilBrownf6705572006-03-27 01:18:11 -08004393 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004394 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004395 /* This personality cannot handle reshaping... */
4396 mddev->pers = NULL;
4397 module_put(pers->owner);
4398 return -EINVAL;
4399 }
4400
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004401 if (pers->sync_request) {
4402 /* Warn if this is a potentially silly
4403 * configuration.
4404 */
4405 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4406 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004407 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004408
4409 list_for_each_entry(rdev, &mddev->disks, same_set)
4410 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004411 if (rdev < rdev2 &&
4412 rdev->bdev->bd_contains ==
4413 rdev2->bdev->bd_contains) {
4414 printk(KERN_WARNING
4415 "%s: WARNING: %s appears to be"
4416 " on the same physical disk as"
4417 " %s.\n",
4418 mdname(mddev),
4419 bdevname(rdev->bdev,b),
4420 bdevname(rdev2->bdev,b2));
4421 warned = 1;
4422 }
4423 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004424
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004425 if (warned)
4426 printk(KERN_WARNING
4427 "True protection against single-disk"
4428 " failure might be compromised.\n");
4429 }
4430
NeilBrown657390d2005-08-26 18:34:16 -07004431 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004432 /* may be over-ridden by personality */
4433 mddev->resync_max_sectors = mddev->dev_sectors;
4434
NeilBrowna9701a32005-11-08 21:39:34 -08004435 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004436 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
NeilBrown0f9552b52009-12-30 12:08:50 +11004438 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004439 mddev->ro = 2; /* read-only, but switch on first write */
4440
NeilBrownb15c2e52006-01-06 00:20:16 -08004441 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004442 if (err)
4443 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004444 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4445 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4446 " but 'external_size' not in effect?\n", __func__);
4447 printk(KERN_ERR
4448 "md: invalid array_size %llu > default size %llu\n",
4449 (unsigned long long)mddev->array_sectors / 2,
4450 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4451 err = -EINVAL;
4452 mddev->pers->stop(mddev);
4453 }
4454 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004455 err = bitmap_create(mddev);
4456 if (err) {
4457 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4458 mdname(mddev), err);
4459 mddev->pers->stop(mddev);
4460 }
4461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 module_put(mddev->pers->owner);
4464 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004465 bitmap_destroy(mddev);
4466 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004468 if (mddev->pers->sync_request) {
4469 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4470 printk(KERN_WARNING
4471 "md: cannot register extra attributes for %s\n",
4472 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004473 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004474 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004475 mddev->ro = 0;
4476
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004478 atomic_set(&mddev->max_corr_read_errors,
4479 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 mddev->safemode = 0;
4481 mddev->safemode_timer.function = md_safemode_timeout;
4482 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004483 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004485
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004486 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004487 if (rdev->raid_disk >= 0) {
4488 char nm[20];
4489 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004490 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4491 printk("md: cannot register %s for %s\n",
4492 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494
4495 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4496
NeilBrown850b2b422006-10-03 01:15:46 -07004497 if (mddev->flags)
4498 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499
NeilBrown0b8c9de2006-07-10 04:44:15 -07004500 md_wakeup_thread(mddev->thread);
4501 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004502
NeilBrownd7603b72006-01-06 00:20:30 -08004503 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004504 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004505 if (mddev->sysfs_action)
4506 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004507 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 return 0;
4509}
4510
NeilBrownfe60b012010-03-29 11:10:42 +11004511static int do_md_run(mddev_t *mddev)
4512{
4513 int err;
4514
4515 err = md_run(mddev);
4516 if (err)
4517 goto out;
4518
4519 set_capacity(mddev->gendisk, mddev->array_sectors);
4520 revalidate_disk(mddev->gendisk);
4521 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4522out:
4523 return err;
4524}
4525
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526static int restart_array(mddev_t *mddev)
4527{
4528 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
Andre Noll80fab1d2008-07-11 22:02:21 +10004530 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004532 return -ENXIO;
4533 if (!mddev->pers)
4534 return -EINVAL;
4535 if (!mddev->ro)
4536 return -EBUSY;
4537 mddev->safemode = 0;
4538 mddev->ro = 0;
4539 set_disk_ro(disk, 0);
4540 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4541 mdname(mddev));
4542 /* Kick recovery or resync if necessary */
4543 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4544 md_wakeup_thread(mddev->thread);
4545 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004546 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004547 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548}
4549
NeilBrownacc55e22006-06-26 00:27:47 -07004550/* similar to deny_write_access, but accounts for our holding a reference
4551 * to the file ourselves */
4552static int deny_bitmap_write_access(struct file * file)
4553{
4554 struct inode *inode = file->f_mapping->host;
4555
4556 spin_lock(&inode->i_lock);
4557 if (atomic_read(&inode->i_writecount) > 1) {
4558 spin_unlock(&inode->i_lock);
4559 return -ETXTBSY;
4560 }
4561 atomic_set(&inode->i_writecount, -1);
4562 spin_unlock(&inode->i_lock);
4563
4564 return 0;
4565}
4566
NeilBrown43a70502009-12-14 12:49:55 +11004567void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004568{
4569 struct inode *inode = file->f_mapping->host;
4570
4571 spin_lock(&inode->i_lock);
4572 atomic_set(&inode->i_writecount, 1);
4573 spin_unlock(&inode->i_lock);
4574}
4575
NeilBrown6177b472010-03-29 11:37:13 +11004576static void md_clean(mddev_t *mddev)
4577{
4578 mddev->array_sectors = 0;
4579 mddev->external_size = 0;
4580 mddev->dev_sectors = 0;
4581 mddev->raid_disks = 0;
4582 mddev->recovery_cp = 0;
4583 mddev->resync_min = 0;
4584 mddev->resync_max = MaxSector;
4585 mddev->reshape_position = MaxSector;
4586 mddev->external = 0;
4587 mddev->persistent = 0;
4588 mddev->level = LEVEL_NONE;
4589 mddev->clevel[0] = 0;
4590 mddev->flags = 0;
4591 mddev->ro = 0;
4592 mddev->metadata_type[0] = 0;
4593 mddev->chunk_sectors = 0;
4594 mddev->ctime = mddev->utime = 0;
4595 mddev->layout = 0;
4596 mddev->max_disks = 0;
4597 mddev->events = 0;
4598 mddev->delta_disks = 0;
4599 mddev->new_level = LEVEL_NONE;
4600 mddev->new_layout = 0;
4601 mddev->new_chunk_sectors = 0;
4602 mddev->curr_resync = 0;
4603 mddev->resync_mismatches = 0;
4604 mddev->suspend_lo = mddev->suspend_hi = 0;
4605 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4606 mddev->recovery = 0;
4607 mddev->in_sync = 0;
4608 mddev->degraded = 0;
4609 mddev->barriers_work = 0;
4610 mddev->safemode = 0;
4611 mddev->bitmap_info.offset = 0;
4612 mddev->bitmap_info.default_offset = 0;
4613 mddev->bitmap_info.chunksize = 0;
4614 mddev->bitmap_info.daemon_sleep = 0;
4615 mddev->bitmap_info.max_write_behind = 0;
4616}
4617
NeilBrowna047e122010-03-29 12:07:53 +11004618static void md_stop_writes(mddev_t *mddev)
4619{
4620 if (mddev->sync_thread) {
4621 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4622 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4623 md_unregister_thread(mddev->sync_thread);
4624 mddev->sync_thread = NULL;
4625 }
4626
4627 del_timer_sync(&mddev->safemode_timer);
4628
4629 bitmap_flush(mddev);
4630 md_super_wait(mddev);
4631
4632 if (!mddev->in_sync || mddev->flags) {
4633 /* mark array as shutdown cleanly */
4634 mddev->in_sync = 1;
4635 md_update_sb(mddev, 1);
4636 }
4637}
4638
NeilBrown6177b472010-03-29 11:37:13 +11004639static void md_stop(mddev_t *mddev)
4640{
NeilBrowncca9cf92010-04-01 12:08:16 +11004641 md_stop_writes(mddev);
4642
NeilBrown6177b472010-03-29 11:37:13 +11004643 mddev->pers->stop(mddev);
4644 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4645 mddev->to_remove = &md_redundancy_group;
4646 module_put(mddev->pers->owner);
4647 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11004648 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11004649}
4650
NeilBrowna4bd82d2010-03-29 13:23:10 +11004651static int md_set_readonly(mddev_t *mddev, int is_open)
4652{
4653 int err = 0;
4654 mutex_lock(&mddev->open_mutex);
4655 if (atomic_read(&mddev->openers) > is_open) {
4656 printk("md: %s still in use.\n",mdname(mddev));
4657 err = -EBUSY;
4658 goto out;
4659 }
4660 if (mddev->pers) {
4661 md_stop_writes(mddev);
4662
4663 err = -ENXIO;
4664 if (mddev->ro==1)
4665 goto out;
4666 mddev->ro = 1;
4667 set_disk_ro(mddev->gendisk, 1);
4668 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4669 sysfs_notify_dirent(mddev->sysfs_state);
4670 err = 0;
4671 }
4672out:
4673 mutex_unlock(&mddev->open_mutex);
4674 return err;
4675}
4676
NeilBrown9e653b62006-06-26 00:27:58 -07004677/* mode:
4678 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07004679 * 2 - stop but do not disassemble array
4680 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004681static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682{
4683 int err = 0;
4684 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004685 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686
NeilBrownc8c00a62009-08-10 12:50:52 +10004687 mutex_lock(&mddev->open_mutex);
NeilBrownf2ea68c2008-07-21 17:05:25 +10004688 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004689 printk("md: %s still in use.\n",mdname(mddev));
NeilBrownc8c00a62009-08-10 12:50:52 +10004690 err = -EBUSY;
4691 } else if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692
NeilBrowna4bd82d2010-03-29 13:23:10 +11004693 if (mddev->ro)
4694 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004695
NeilBrowna4bd82d2010-03-29 13:23:10 +11004696 md_stop(mddev);
4697 mddev->queue->merge_bvec_fn = NULL;
4698 mddev->queue->unplug_fn = NULL;
4699 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004700
NeilBrowna4bd82d2010-03-29 13:23:10 +11004701 /* tell userspace to handle 'inactive' */
4702 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004703
NeilBrowna4bd82d2010-03-29 13:23:10 +11004704 list_for_each_entry(rdev, &mddev->disks, same_set)
4705 if (rdev->raid_disk >= 0) {
4706 char nm[20];
4707 sprintf(nm, "rd%d", rdev->raid_disk);
4708 sysfs_remove_link(&mddev->kobj, nm);
4709 }
NeilBrownc4647292009-05-07 12:51:06 +10004710
NeilBrowna4bd82d2010-03-29 13:23:10 +11004711 set_capacity(disk, 0);
4712 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08004713
NeilBrowna4bd82d2010-03-29 13:23:10 +11004714 if (mddev->ro)
4715 mddev->ro = 0;
4716
NeilBrown80ffb3c2009-08-18 10:35:26 +10004717 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 }
NeilBrownc8c00a62009-08-10 12:50:52 +10004719 mutex_unlock(&mddev->open_mutex);
4720 if (err)
4721 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 /*
4723 * Free resources if final stop
4724 */
NeilBrown9e653b62006-06-26 00:27:58 -07004725 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004726
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4728
NeilBrown978f9462006-02-02 14:28:05 -08004729 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004730 if (mddev->bitmap_info.file) {
4731 restore_bitmap_write_access(mddev->bitmap_info.file);
4732 fput(mddev->bitmap_info.file);
4733 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004734 }
NeilBrownc3d97142009-12-14 12:49:52 +11004735 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004736
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 export_array(mddev);
4738
NeilBrown6177b472010-03-29 11:37:13 +11004739 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11004740 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004741 if (mddev->hold_active == UNTIL_STOP)
4742 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004743
NeilBrowna4bd82d2010-03-29 13:23:10 +11004744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004746 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004747 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004748 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 return err;
4750}
4751
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004752#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753static void autorun_array(mddev_t *mddev)
4754{
4755 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 int err;
4757
NeilBrowna757e642005-04-16 15:26:42 -07004758 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760
4761 printk(KERN_INFO "md: running: ");
4762
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004763 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 char b[BDEVNAME_SIZE];
4765 printk("<%s>", bdevname(rdev->bdev,b));
4766 }
4767 printk("\n");
4768
NeilBrownd710e132008-10-13 11:55:12 +11004769 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 if (err) {
4771 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004772 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 }
4774}
4775
4776/*
4777 * lets try to run arrays based on all disks that have arrived
4778 * until now. (those are in pending_raid_disks)
4779 *
4780 * the method: pick the first pending disk, collect all disks with
4781 * the same UUID, remove all from the pending list and put them into
4782 * the 'same_array' list. Then order this list based on superblock
4783 * update time (freshest comes first), kick out 'old' disks and
4784 * compare superblocks. If everything's fine then run it.
4785 *
4786 * If "unit" is allocated, then bump its reference count
4787 */
4788static void autorun_devices(int part)
4789{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004790 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 mddev_t *mddev;
4792 char b[BDEVNAME_SIZE];
4793
4794 printk(KERN_INFO "md: autorun ...\n");
4795 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004796 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004798 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 rdev0 = list_entry(pending_raid_disks.next,
4800 mdk_rdev_t, same_set);
4801
4802 printk(KERN_INFO "md: considering %s ...\n",
4803 bdevname(rdev0->bdev,b));
4804 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004805 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 if (super_90_load(rdev, rdev0, 0) >= 0) {
4807 printk(KERN_INFO "md: adding %s ...\n",
4808 bdevname(rdev->bdev,b));
4809 list_move(&rdev->same_set, &candidates);
4810 }
4811 /*
4812 * now we have a set of devices, with all of them having
4813 * mostly sane superblocks. It's time to allocate the
4814 * mddev.
4815 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004816 if (part) {
4817 dev = MKDEV(mdp_major,
4818 rdev0->preferred_minor << MdpMinorShift);
4819 unit = MINOR(dev) >> MdpMinorShift;
4820 } else {
4821 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4822 unit = MINOR(dev);
4823 }
4824 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4826 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4827 break;
4828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829
4830 md_probe(dev, NULL, NULL);
4831 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004832 if (!mddev || !mddev->gendisk) {
4833 if (mddev)
4834 mddev_put(mddev);
4835 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 "md: cannot allocate memory for md drive.\n");
4837 break;
4838 }
4839 if (mddev_lock(mddev))
4840 printk(KERN_WARNING "md: %s locked, cannot run\n",
4841 mdname(mddev));
4842 else if (mddev->raid_disks || mddev->major_version
4843 || !list_empty(&mddev->disks)) {
4844 printk(KERN_WARNING
4845 "md: %s already running, cannot run %s\n",
4846 mdname(mddev), bdevname(rdev0->bdev,b));
4847 mddev_unlock(mddev);
4848 } else {
4849 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004850 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004851 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 list_del_init(&rdev->same_set);
4853 if (bind_rdev_to_array(rdev, mddev))
4854 export_rdev(rdev);
4855 }
4856 autorun_array(mddev);
4857 mddev_unlock(mddev);
4858 }
4859 /* on success, candidates will be empty, on error
4860 * it won't...
4861 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004862 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004863 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 mddev_put(mddev);
4867 }
4868 printk(KERN_INFO "md: ... autorun DONE.\n");
4869}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004870#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872static int get_version(void __user * arg)
4873{
4874 mdu_version_t ver;
4875
4876 ver.major = MD_MAJOR_VERSION;
4877 ver.minor = MD_MINOR_VERSION;
4878 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4879
4880 if (copy_to_user(arg, &ver, sizeof(ver)))
4881 return -EFAULT;
4882
4883 return 0;
4884}
4885
4886static int get_array_info(mddev_t * mddev, void __user * arg)
4887{
4888 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004889 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891
NeilBrowna9f326e2009-09-23 18:06:41 +10004892 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004893 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004895 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 failed++;
4897 else {
4898 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004899 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004900 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 else
4902 spare++;
4903 }
4904 }
4905
4906 info.major_version = mddev->major_version;
4907 info.minor_version = mddev->minor_version;
4908 info.patch_version = MD_PATCHLEVEL_VERSION;
4909 info.ctime = mddev->ctime;
4910 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004911 info.size = mddev->dev_sectors / 2;
4912 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004913 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 info.nr_disks = nr;
4915 info.raid_disks = mddev->raid_disks;
4916 info.md_minor = mddev->md_minor;
4917 info.not_persistent= !mddev->persistent;
4918
4919 info.utime = mddev->utime;
4920 info.state = 0;
4921 if (mddev->in_sync)
4922 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11004923 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07004924 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004925 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 info.working_disks = working;
4927 info.failed_disks = failed;
4928 info.spare_disks = spare;
4929
4930 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004931 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
4933 if (copy_to_user(arg, &info, sizeof(info)))
4934 return -EFAULT;
4935
4936 return 0;
4937}
4938
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004939static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004940{
4941 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4942 char *ptr, *buf = NULL;
4943 int err = -ENOMEM;
4944
Dan Williamsb5470dc2008-06-27 21:44:04 -07004945 if (md_allow_write(mddev))
4946 file = kmalloc(sizeof(*file), GFP_NOIO);
4947 else
4948 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004949
NeilBrown32a76272005-06-21 17:17:14 -07004950 if (!file)
4951 goto out;
4952
4953 /* bitmap disabled, zero the first byte and copy out */
4954 if (!mddev->bitmap || !mddev->bitmap->file) {
4955 file->pathname[0] = '\0';
4956 goto copy_out;
4957 }
4958
4959 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4960 if (!buf)
4961 goto out;
4962
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004963 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4964 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004965 goto out;
4966
4967 strcpy(file->pathname, ptr);
4968
4969copy_out:
4970 err = 0;
4971 if (copy_to_user(arg, file, sizeof(*file)))
4972 err = -EFAULT;
4973out:
4974 kfree(buf);
4975 kfree(file);
4976 return err;
4977}
4978
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979static int get_disk_info(mddev_t * mddev, void __user * arg)
4980{
4981 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 mdk_rdev_t *rdev;
4983
4984 if (copy_from_user(&info, arg, sizeof(info)))
4985 return -EFAULT;
4986
Andre Noll26ef3792008-07-11 22:02:21 +10004987 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 if (rdev) {
4989 info.major = MAJOR(rdev->bdev->bd_dev);
4990 info.minor = MINOR(rdev->bdev->bd_dev);
4991 info.raid_disk = rdev->raid_disk;
4992 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004993 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004995 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 info.state |= (1<<MD_DISK_ACTIVE);
4997 info.state |= (1<<MD_DISK_SYNC);
4998 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004999 if (test_bit(WriteMostly, &rdev->flags))
5000 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 } else {
5002 info.major = info.minor = 0;
5003 info.raid_disk = -1;
5004 info.state = (1<<MD_DISK_REMOVED);
5005 }
5006
5007 if (copy_to_user(arg, &info, sizeof(info)))
5008 return -EFAULT;
5009
5010 return 0;
5011}
5012
5013static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5014{
5015 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5016 mdk_rdev_t *rdev;
5017 dev_t dev = MKDEV(info->major,info->minor);
5018
5019 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5020 return -EOVERFLOW;
5021
5022 if (!mddev->raid_disks) {
5023 int err;
5024 /* expecting a device which has a superblock */
5025 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5026 if (IS_ERR(rdev)) {
5027 printk(KERN_WARNING
5028 "md: md_import_device returned %ld\n",
5029 PTR_ERR(rdev));
5030 return PTR_ERR(rdev);
5031 }
5032 if (!list_empty(&mddev->disks)) {
5033 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5034 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005035 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 .load_super(rdev, rdev0, mddev->minor_version);
5037 if (err < 0) {
5038 printk(KERN_WARNING
5039 "md: %s has different UUID to %s\n",
5040 bdevname(rdev->bdev,b),
5041 bdevname(rdev0->bdev,b2));
5042 export_rdev(rdev);
5043 return -EINVAL;
5044 }
5045 }
5046 err = bind_rdev_to_array(rdev, mddev);
5047 if (err)
5048 export_rdev(rdev);
5049 return err;
5050 }
5051
5052 /*
5053 * add_new_disk can be used once the array is assembled
5054 * to add "hot spares". They must already have a superblock
5055 * written
5056 */
5057 if (mddev->pers) {
5058 int err;
5059 if (!mddev->pers->hot_add_disk) {
5060 printk(KERN_WARNING
5061 "%s: personality does not support diskops!\n",
5062 mdname(mddev));
5063 return -EINVAL;
5064 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005065 if (mddev->persistent)
5066 rdev = md_import_device(dev, mddev->major_version,
5067 mddev->minor_version);
5068 else
5069 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 if (IS_ERR(rdev)) {
5071 printk(KERN_WARNING
5072 "md: md_import_device returned %ld\n",
5073 PTR_ERR(rdev));
5074 return PTR_ERR(rdev);
5075 }
NeilBrown41158c72005-06-21 17:17:25 -07005076 /* set save_raid_disk if appropriate */
5077 if (!mddev->persistent) {
5078 if (info->state & (1<<MD_DISK_SYNC) &&
5079 info->raid_disk < mddev->raid_disks)
5080 rdev->raid_disk = info->raid_disk;
5081 else
5082 rdev->raid_disk = -1;
5083 } else
5084 super_types[mddev->major_version].
5085 validate_super(mddev, rdev);
5086 rdev->saved_raid_disk = rdev->raid_disk;
5087
NeilBrownb2d444d2005-11-08 21:39:31 -08005088 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005089 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5090 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005091 else
5092 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005093
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 rdev->raid_disk = -1;
5095 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005096 if (!err && !mddev->pers->hot_remove_disk) {
5097 /* If there is hot_add_disk but no hot_remove_disk
5098 * then added disks for geometry changes,
5099 * and should be added immediately.
5100 */
5101 super_types[mddev->major_version].
5102 validate_super(mddev, rdev);
5103 err = mddev->pers->hot_add_disk(mddev, rdev);
5104 if (err)
5105 unbind_rdev_from_array(rdev);
5106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 if (err)
5108 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005109 else
NeilBrown3c0ee632008-10-21 13:25:28 +11005110 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005111
NeilBrown17571282006-12-10 02:20:52 -08005112 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005113 if (mddev->degraded)
5114 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005115 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07005116 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 return err;
5118 }
5119
5120 /* otherwise, add_new_disk is only allowed
5121 * for major_version==0 superblocks
5122 */
5123 if (mddev->major_version != 0) {
5124 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5125 mdname(mddev));
5126 return -EINVAL;
5127 }
5128
5129 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5130 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005131 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 if (IS_ERR(rdev)) {
5133 printk(KERN_WARNING
5134 "md: error, md_import_device() returned %ld\n",
5135 PTR_ERR(rdev));
5136 return PTR_ERR(rdev);
5137 }
5138 rdev->desc_nr = info->number;
5139 if (info->raid_disk < mddev->raid_disks)
5140 rdev->raid_disk = info->raid_disk;
5141 else
5142 rdev->raid_disk = -1;
5143
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005145 if (info->state & (1<<MD_DISK_SYNC))
5146 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005148 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5149 set_bit(WriteMostly, &rdev->flags);
5150
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 if (!mddev->persistent) {
5152 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10005153 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 } else
Andre Noll0f420352008-07-11 22:02:23 +10005155 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10005156 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157
NeilBrown2bf071b2006-01-06 00:20:55 -08005158 err = bind_rdev_to_array(rdev, mddev);
5159 if (err) {
5160 export_rdev(rdev);
5161 return err;
5162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 }
5164
5165 return 0;
5166}
5167
5168static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5169{
5170 char b[BDEVNAME_SIZE];
5171 mdk_rdev_t *rdev;
5172
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 rdev = find_rdev(mddev, dev);
5174 if (!rdev)
5175 return -ENXIO;
5176
5177 if (rdev->raid_disk >= 0)
5178 goto busy;
5179
5180 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07005181 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005182 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183
5184 return 0;
5185busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005186 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 bdevname(rdev->bdev,b), mdname(mddev));
5188 return -EBUSY;
5189}
5190
5191static int hot_add_disk(mddev_t * mddev, dev_t dev)
5192{
5193 char b[BDEVNAME_SIZE];
5194 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 mdk_rdev_t *rdev;
5196
5197 if (!mddev->pers)
5198 return -ENODEV;
5199
5200 if (mddev->major_version != 0) {
5201 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5202 " version-0 superblocks.\n",
5203 mdname(mddev));
5204 return -EINVAL;
5205 }
5206 if (!mddev->pers->hot_add_disk) {
5207 printk(KERN_WARNING
5208 "%s: personality does not support diskops!\n",
5209 mdname(mddev));
5210 return -EINVAL;
5211 }
5212
NeilBrownd710e132008-10-13 11:55:12 +11005213 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 if (IS_ERR(rdev)) {
5215 printk(KERN_WARNING
5216 "md: error, md_import_device() returned %ld\n",
5217 PTR_ERR(rdev));
5218 return -EINVAL;
5219 }
5220
5221 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10005222 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 else
Andre Noll0f420352008-07-11 22:02:23 +10005224 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225
NeilBrown8190e752009-06-18 08:48:58 +10005226 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227
NeilBrownb2d444d2005-11-08 21:39:31 -08005228 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 printk(KERN_WARNING
5230 "md: can not hot-add faulty %s disk to %s!\n",
5231 bdevname(rdev->bdev,b), mdname(mddev));
5232 err = -EINVAL;
5233 goto abort_export;
5234 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005235 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005237 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005238 err = bind_rdev_to_array(rdev, mddev);
5239 if (err)
5240 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241
5242 /*
5243 * The rest should better be atomic, we can have disk failures
5244 * noticed in interrupt contexts ...
5245 */
5246
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 rdev->raid_disk = -1;
5248
NeilBrown850b2b422006-10-03 01:15:46 -07005249 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250
5251 /*
5252 * Kick recovery, maybe this spare has to be added to the
5253 * array immediately.
5254 */
5255 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5256 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005257 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 return 0;
5259
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260abort_export:
5261 export_rdev(rdev);
5262 return err;
5263}
5264
NeilBrown32a76272005-06-21 17:17:14 -07005265static int set_bitmap_file(mddev_t *mddev, int fd)
5266{
5267 int err;
5268
NeilBrown36fa3062005-09-09 16:23:45 -07005269 if (mddev->pers) {
5270 if (!mddev->pers->quiesce)
5271 return -EBUSY;
5272 if (mddev->recovery || mddev->sync_thread)
5273 return -EBUSY;
5274 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005275 }
5276
NeilBrown36fa3062005-09-09 16:23:45 -07005277
5278 if (fd >= 0) {
5279 if (mddev->bitmap)
5280 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005281 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005282
NeilBrownc3d97142009-12-14 12:49:52 +11005283 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005284 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5285 mdname(mddev));
5286 return -EBADF;
5287 }
5288
NeilBrownc3d97142009-12-14 12:49:52 +11005289 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005290 if (err) {
5291 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5292 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005293 fput(mddev->bitmap_info.file);
5294 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005295 return err;
5296 }
NeilBrownc3d97142009-12-14 12:49:52 +11005297 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005298 } else if (mddev->bitmap == NULL)
5299 return -ENOENT; /* cannot remove what isn't there */
5300 err = 0;
5301 if (mddev->pers) {
5302 mddev->pers->quiesce(mddev, 1);
5303 if (fd >= 0)
5304 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005305 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005306 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005307 fd = -1; /* make sure to put the file */
5308 }
NeilBrown36fa3062005-09-09 16:23:45 -07005309 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005310 }
5311 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005312 if (mddev->bitmap_info.file) {
5313 restore_bitmap_write_access(mddev->bitmap_info.file);
5314 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005315 }
NeilBrownc3d97142009-12-14 12:49:52 +11005316 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005317 }
5318
NeilBrown32a76272005-06-21 17:17:14 -07005319 return err;
5320}
5321
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322/*
5323 * set_array_info is used two different ways
5324 * The original usage is when creating a new array.
5325 * In this usage, raid_disks is > 0 and it together with
5326 * level, size, not_persistent,layout,chunksize determine the
5327 * shape of the array.
5328 * This will always create an array with a type-0.90.0 superblock.
5329 * The newer usage is when assembling an array.
5330 * In this case raid_disks will be 0, and the major_version field is
5331 * use to determine which style super-blocks are to be found on the devices.
5332 * The minor and patch _version numbers are also kept incase the
5333 * super_block handler wishes to interpret them.
5334 */
5335static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5336{
5337
5338 if (info->raid_disks == 0) {
5339 /* just setting version number for superblock loading */
5340 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005341 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 super_types[info->major_version].name == NULL) {
5343 /* maybe try to auto-load a module? */
5344 printk(KERN_INFO
5345 "md: superblock version %d not known\n",
5346 info->major_version);
5347 return -EINVAL;
5348 }
5349 mddev->major_version = info->major_version;
5350 mddev->minor_version = info->minor_version;
5351 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005352 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005353 /* ensure mddev_put doesn't delete this now that there
5354 * is some minimal configuration.
5355 */
5356 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 return 0;
5358 }
5359 mddev->major_version = MD_MAJOR_VERSION;
5360 mddev->minor_version = MD_MINOR_VERSION;
5361 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5362 mddev->ctime = get_seconds();
5363
5364 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005365 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005366 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 mddev->raid_disks = info->raid_disks;
5368 /* don't set md_minor, it is determined by which /dev/md* was
5369 * openned
5370 */
5371 if (info->state & (1<<MD_SB_CLEAN))
5372 mddev->recovery_cp = MaxSector;
5373 else
5374 mddev->recovery_cp = 0;
5375 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005376 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377
5378 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005379 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380
5381 mddev->max_disks = MD_SB_DISKS;
5382
NeilBrowne6910632008-02-06 01:39:51 -08005383 if (mddev->persistent)
5384 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07005385 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386
NeilBrownc3d97142009-12-14 12:49:52 +11005387 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5388 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005389
NeilBrownf6705572006-03-27 01:18:11 -08005390 mddev->reshape_position = MaxSector;
5391
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 /*
5393 * Generate a 128 bit UUID
5394 */
5395 get_random_bytes(mddev->uuid, 16);
5396
NeilBrownf6705572006-03-27 01:18:11 -08005397 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005398 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005399 mddev->new_layout = mddev->layout;
5400 mddev->delta_disks = 0;
5401
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 return 0;
5403}
5404
Dan Williams1f403622009-03-31 14:59:03 +11005405void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5406{
Dan Williamsb522adc2009-03-31 15:00:31 +11005407 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5408
5409 if (mddev->external_size)
5410 return;
5411
Dan Williams1f403622009-03-31 14:59:03 +11005412 mddev->array_sectors = array_sectors;
5413}
5414EXPORT_SYMBOL(md_set_array_sectors);
5415
Andre Nolld71f9f82008-07-11 22:02:22 +10005416static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005417{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005418 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005419 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005420 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005421
5422 if (mddev->pers->resize == NULL)
5423 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005424 /* The "num_sectors" is the number of sectors of each device that
5425 * is used. This can only make sense for arrays with redundancy.
5426 * linear and raid0 always use whatever space is available. We can only
5427 * consider changing this number if no resync or reconstruction is
5428 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005429 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005430 * of each device. If num_sectors is zero, we find the largest size
5431 * that fits.
5432
NeilBrowna35b0d62006-01-06 00:20:49 -08005433 */
5434 if (mddev->sync_thread)
5435 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005436 if (mddev->bitmap)
5437 /* Sorry, cannot grow a bitmap yet, just remove it,
5438 * grow, and re-add.
5439 */
5440 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005441 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005442 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005443
Andre Nolld71f9f82008-07-11 22:02:22 +10005444 if (fit && (num_sectors == 0 || num_sectors > avail))
5445 num_sectors = avail;
5446 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005447 return -ENOSPC;
5448 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005449 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005450 if (!rv)
5451 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005452 return rv;
5453}
5454
NeilBrownda943b992006-01-06 00:20:54 -08005455static int update_raid_disks(mddev_t *mddev, int raid_disks)
5456{
5457 int rv;
5458 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005459 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005460 return -EINVAL;
5461 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005462 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08005463 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005464 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005465 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005466 mddev->delta_disks = raid_disks - mddev->raid_disks;
5467
5468 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005469 return rv;
5470}
5471
5472
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473/*
5474 * update_array_info is used to change the configuration of an
5475 * on-line array.
5476 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5477 * fields in the info are checked against the array.
5478 * Any differences that cannot be handled will cause an error.
5479 * Normally, only one change can be managed at a time.
5480 */
5481static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5482{
5483 int rv = 0;
5484 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005485 int state = 0;
5486
5487 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005488 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005489 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490
5491 if (mddev->major_version != info->major_version ||
5492 mddev->minor_version != info->minor_version ||
5493/* mddev->patch_version != info->patch_version || */
5494 mddev->ctime != info->ctime ||
5495 mddev->level != info->level ||
5496/* mddev->layout != info->layout || */
5497 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005498 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005499 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5500 ((state^info->state) & 0xfffffe00)
5501 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 return -EINVAL;
5503 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005504 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5505 cnt++;
5506 if (mddev->raid_disks != info->raid_disks)
5507 cnt++;
5508 if (mddev->layout != info->layout)
5509 cnt++;
5510 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5511 cnt++;
5512 if (cnt == 0)
5513 return 0;
5514 if (cnt > 1)
5515 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516
5517 if (mddev->layout != info->layout) {
5518 /* Change layout
5519 * we don't need to do anything at the md level, the
5520 * personality will take care of it all.
5521 */
NeilBrown50ac1682009-06-18 08:47:55 +10005522 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005524 else {
5525 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005526 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005527 if (rv)
5528 mddev->new_layout = mddev->layout;
5529 return rv;
5530 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005532 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005533 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534
NeilBrownda943b992006-01-06 00:20:54 -08005535 if (mddev->raid_disks != info->raid_disks)
5536 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537
NeilBrown36fa3062005-09-09 16:23:45 -07005538 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5539 if (mddev->pers->quiesce == NULL)
5540 return -EINVAL;
5541 if (mddev->recovery || mddev->sync_thread)
5542 return -EBUSY;
5543 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5544 /* add the bitmap */
5545 if (mddev->bitmap)
5546 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005547 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005548 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005549 mddev->bitmap_info.offset =
5550 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005551 mddev->pers->quiesce(mddev, 1);
5552 rv = bitmap_create(mddev);
5553 if (rv)
5554 bitmap_destroy(mddev);
5555 mddev->pers->quiesce(mddev, 0);
5556 } else {
5557 /* remove the bitmap */
5558 if (!mddev->bitmap)
5559 return -ENOENT;
5560 if (mddev->bitmap->file)
5561 return -EINVAL;
5562 mddev->pers->quiesce(mddev, 1);
5563 bitmap_destroy(mddev);
5564 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005565 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005566 }
5567 }
NeilBrown850b2b422006-10-03 01:15:46 -07005568 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 return rv;
5570}
5571
5572static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5573{
5574 mdk_rdev_t *rdev;
5575
5576 if (mddev->pers == NULL)
5577 return -ENODEV;
5578
5579 rdev = find_rdev(mddev, dev);
5580 if (!rdev)
5581 return -ENODEV;
5582
5583 md_error(mddev, rdev);
5584 return 0;
5585}
5586
Andre Noll2f9618c2008-04-25 18:57:58 +02005587/*
5588 * We have a problem here : there is no easy way to give a CHS
5589 * virtual geometry. We currently pretend that we have a 2 heads
5590 * 4 sectors (with a BIG number of cylinders...). This drives
5591 * dosfs just mad... ;-)
5592 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005593static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5594{
5595 mddev_t *mddev = bdev->bd_disk->private_data;
5596
5597 geo->heads = 2;
5598 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11005599 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005600 return 0;
5601}
5602
Al Viroa39907f2008-03-02 10:31:15 -05005603static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 unsigned int cmd, unsigned long arg)
5605{
5606 int err = 0;
5607 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10005609 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
5611 if (!capable(CAP_SYS_ADMIN))
5612 return -EACCES;
5613
5614 /*
5615 * Commands dealing with the RAID driver but not any
5616 * particular array:
5617 */
5618 switch (cmd)
5619 {
5620 case RAID_VERSION:
5621 err = get_version(argp);
5622 goto done;
5623
5624 case PRINT_RAID_DEBUG:
5625 err = 0;
5626 md_print_devices();
5627 goto done;
5628
5629#ifndef MODULE
5630 case RAID_AUTORUN:
5631 err = 0;
5632 autostart_arrays(arg);
5633 goto done;
5634#endif
5635 default:;
5636 }
5637
5638 /*
5639 * Commands creating/starting a new array:
5640 */
5641
Al Viroa39907f2008-03-02 10:31:15 -05005642 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643
5644 if (!mddev) {
5645 BUG();
5646 goto abort;
5647 }
5648
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 err = mddev_lock(mddev);
5650 if (err) {
5651 printk(KERN_INFO
5652 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5653 err, cmd);
5654 goto abort;
5655 }
5656
5657 switch (cmd)
5658 {
5659 case SET_ARRAY_INFO:
5660 {
5661 mdu_array_info_t info;
5662 if (!arg)
5663 memset(&info, 0, sizeof(info));
5664 else if (copy_from_user(&info, argp, sizeof(info))) {
5665 err = -EFAULT;
5666 goto abort_unlock;
5667 }
5668 if (mddev->pers) {
5669 err = update_array_info(mddev, &info);
5670 if (err) {
5671 printk(KERN_WARNING "md: couldn't update"
5672 " array info. %d\n", err);
5673 goto abort_unlock;
5674 }
5675 goto done_unlock;
5676 }
5677 if (!list_empty(&mddev->disks)) {
5678 printk(KERN_WARNING
5679 "md: array %s already has disks!\n",
5680 mdname(mddev));
5681 err = -EBUSY;
5682 goto abort_unlock;
5683 }
5684 if (mddev->raid_disks) {
5685 printk(KERN_WARNING
5686 "md: array %s already initialised!\n",
5687 mdname(mddev));
5688 err = -EBUSY;
5689 goto abort_unlock;
5690 }
5691 err = set_array_info(mddev, &info);
5692 if (err) {
5693 printk(KERN_WARNING "md: couldn't set"
5694 " array info. %d\n", err);
5695 goto abort_unlock;
5696 }
5697 }
5698 goto done_unlock;
5699
5700 default:;
5701 }
5702
5703 /*
5704 * Commands querying/configuring an existing array:
5705 */
NeilBrown32a76272005-06-21 17:17:14 -07005706 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005707 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005708 if ((!mddev->raid_disks && !mddev->external)
5709 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5710 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5711 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 err = -ENODEV;
5713 goto abort_unlock;
5714 }
5715
5716 /*
5717 * Commands even a read-only array can execute:
5718 */
5719 switch (cmd)
5720 {
5721 case GET_ARRAY_INFO:
5722 err = get_array_info(mddev, argp);
5723 goto done_unlock;
5724
NeilBrown32a76272005-06-21 17:17:14 -07005725 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005726 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005727 goto done_unlock;
5728
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 case GET_DISK_INFO:
5730 err = get_disk_info(mddev, argp);
5731 goto done_unlock;
5732
5733 case RESTART_ARRAY_RW:
5734 err = restart_array(mddev);
5735 goto done_unlock;
5736
5737 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005738 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 goto done_unlock;
5740
5741 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11005742 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 goto done_unlock;
5744
Dan Williamse2218352010-05-12 08:25:37 +10005745 case BLKROSET:
5746 if (get_user(ro, (int __user *)(arg))) {
5747 err = -EFAULT;
5748 goto done_unlock;
5749 }
5750 err = -EINVAL;
5751
5752 /* if the bdev is going readonly the value of mddev->ro
5753 * does not matter, no writes are coming
5754 */
5755 if (ro)
5756 goto done_unlock;
5757
5758 /* are we are already prepared for writes? */
5759 if (mddev->ro != 1)
5760 goto done_unlock;
5761
5762 /* transitioning to readauto need only happen for
5763 * arrays that call md_write_start
5764 */
5765 if (mddev->pers) {
5766 err = restart_array(mddev);
5767 if (err == 0) {
5768 mddev->ro = 2;
5769 set_disk_ro(mddev->gendisk, 0);
5770 }
5771 }
5772 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 }
5774
5775 /*
5776 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005777 * superblock, so we do not allow them on read-only arrays.
5778 * However non-MD ioctls (e.g. get-size) will still come through
5779 * here and hit the 'default' below, so only disallow
5780 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005782 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005783 if (mddev->ro == 2) {
5784 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005785 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005786 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5787 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005788 } else {
5789 err = -EROFS;
5790 goto abort_unlock;
5791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 }
5793
5794 switch (cmd)
5795 {
5796 case ADD_NEW_DISK:
5797 {
5798 mdu_disk_info_t info;
5799 if (copy_from_user(&info, argp, sizeof(info)))
5800 err = -EFAULT;
5801 else
5802 err = add_new_disk(mddev, &info);
5803 goto done_unlock;
5804 }
5805
5806 case HOT_REMOVE_DISK:
5807 err = hot_remove_disk(mddev, new_decode_dev(arg));
5808 goto done_unlock;
5809
5810 case HOT_ADD_DISK:
5811 err = hot_add_disk(mddev, new_decode_dev(arg));
5812 goto done_unlock;
5813
5814 case SET_DISK_FAULTY:
5815 err = set_disk_faulty(mddev, new_decode_dev(arg));
5816 goto done_unlock;
5817
5818 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005819 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 goto done_unlock;
5821
NeilBrown32a76272005-06-21 17:17:14 -07005822 case SET_BITMAP_FILE:
5823 err = set_bitmap_file(mddev, (int)arg);
5824 goto done_unlock;
5825
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 err = -EINVAL;
5828 goto abort_unlock;
5829 }
5830
5831done_unlock:
5832abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005833 if (mddev->hold_active == UNTIL_IOCTL &&
5834 err != -EINVAL)
5835 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 mddev_unlock(mddev);
5837
5838 return err;
5839done:
5840 if (err)
5841 MD_BUG();
5842abort:
5843 return err;
5844}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005845#ifdef CONFIG_COMPAT
5846static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5847 unsigned int cmd, unsigned long arg)
5848{
5849 switch (cmd) {
5850 case HOT_REMOVE_DISK:
5851 case HOT_ADD_DISK:
5852 case SET_DISK_FAULTY:
5853 case SET_BITMAP_FILE:
5854 /* These take in integer arg, do not convert */
5855 break;
5856 default:
5857 arg = (unsigned long)compat_ptr(arg);
5858 break;
5859 }
5860
5861 return md_ioctl(bdev, mode, cmd, arg);
5862}
5863#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864
Al Viroa39907f2008-03-02 10:31:15 -05005865static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866{
5867 /*
5868 * Succeed if we can lock the mddev, which confirms that
5869 * it isn't being stopped right now.
5870 */
NeilBrownd3374822009-01-09 08:31:10 +11005871 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 int err;
5873
NeilBrownd3374822009-01-09 08:31:10 +11005874 if (mddev->gendisk != bdev->bd_disk) {
5875 /* we are racing with mddev_put which is discarding this
5876 * bd_disk.
5877 */
5878 mddev_put(mddev);
5879 /* Wait until bdev->bd_disk is definitely gone */
5880 flush_scheduled_work();
5881 /* Then retry the open from the top */
5882 return -ERESTARTSYS;
5883 }
5884 BUG_ON(mddev != bdev->bd_disk->private_data);
5885
NeilBrownc8c00a62009-08-10 12:50:52 +10005886 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887 goto out;
5888
5889 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005890 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005891 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 out:
5894 return err;
5895}
5896
Al Viroa39907f2008-03-02 10:31:15 -05005897static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898{
Al Viroa39907f2008-03-02 10:31:15 -05005899 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005901 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005902 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 mddev_put(mddev);
5904
5905 return 0;
5906}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005907static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908{
5909 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005910 .open = md_open,
5911 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005912 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005913#ifdef CONFIG_COMPAT
5914 .compat_ioctl = md_compat_ioctl,
5915#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005916 .getgeo = md_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917};
5918
Adrian Bunk75c96f82005-05-05 16:16:09 -07005919static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920{
5921 mdk_thread_t *thread = arg;
5922
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 /*
5924 * md_thread is a 'system-thread', it's priority should be very
5925 * high. We avoid resource deadlocks individually in each
5926 * raid personality. (RAID5 does preallocation) We also use RR and
5927 * the very same RT priority as kswapd, thus we will never get
5928 * into a priority inversion deadlock.
5929 *
5930 * we definitely have to have equal or higher priority than
5931 * bdflush, otherwise bdflush will deadlock if there are too
5932 * many dirty RAID5 blocks.
5933 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934
NeilBrown6985c432005-10-19 21:23:47 -07005935 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005936 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937
NeilBrown93588e22005-11-15 00:09:12 -08005938 /* We need to wait INTERRUPTIBLE so that
5939 * we don't add to the load-average.
5940 * That means we need to be sure no signals are
5941 * pending
5942 */
5943 if (signal_pending(current))
5944 flush_signals(current);
5945
5946 wait_event_interruptible_timeout
5947 (thread->wqueue,
5948 test_bit(THREAD_WAKEUP, &thread->flags)
5949 || kthread_should_stop(),
5950 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951
5952 clear_bit(THREAD_WAKEUP, &thread->flags);
5953
NeilBrown787453c2005-11-08 21:39:43 -08005954 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005956
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 return 0;
5958}
5959
5960void md_wakeup_thread(mdk_thread_t *thread)
5961{
5962 if (thread) {
5963 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5964 set_bit(THREAD_WAKEUP, &thread->flags);
5965 wake_up(&thread->wqueue);
5966 }
5967}
5968
5969mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5970 const char *name)
5971{
5972 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973
NeilBrown9ffae0c2006-01-06 00:20:32 -08005974 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 if (!thread)
5976 return NULL;
5977
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 init_waitqueue_head(&thread->wqueue);
5979
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 thread->run = run;
5981 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005982 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10005983 thread->tsk = kthread_run(md_thread, thread,
5984 "%s_%s",
5985 mdname(thread->mddev),
5986 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07005987 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 kfree(thread);
5989 return NULL;
5990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 return thread;
5992}
5993
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994void md_unregister_thread(mdk_thread_t *thread)
5995{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005996 if (!thread)
5997 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005998 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005999
6000 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 kfree(thread);
6002}
6003
6004void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6005{
6006 if (!mddev) {
6007 MD_BUG();
6008 return;
6009 }
6010
NeilBrownb2d444d2005-11-08 21:39:31 -08006011 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006013
6014 if (mddev->external)
6015 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07006016/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6018 mdname(mddev),
6019 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6020 __builtin_return_address(0),__builtin_return_address(1),
6021 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07006022*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07006023 if (!mddev->pers)
6024 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 if (!mddev->pers->error_handler)
6026 return;
6027 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006028 if (mddev->degraded)
6029 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc0cc75f2010-03-22 10:28:51 +11006030 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6032 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6033 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07006034 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035}
6036
6037/* seq_file implementation /proc/mdstat */
6038
6039static void status_unused(struct seq_file *seq)
6040{
6041 int i = 0;
6042 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043
6044 seq_printf(seq, "unused devices: ");
6045
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006046 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 char b[BDEVNAME_SIZE];
6048 i++;
6049 seq_printf(seq, "%s ",
6050 bdevname(rdev->bdev,b));
6051 }
6052 if (!i)
6053 seq_printf(seq, "<none>");
6054
6055 seq_printf(seq, "\n");
6056}
6057
6058
6059static void status_resync(struct seq_file *seq, mddev_t * mddev)
6060{
NeilBrowndd71cf62009-05-07 12:49:35 +10006061 sector_t max_sectors, resync, res;
6062 unsigned long dt, db;
6063 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006064 int scale;
6065 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066
NeilBrowndd71cf62009-05-07 12:49:35 +10006067 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068
6069 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006070 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006072 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073
6074 /*
6075 * Should not happen.
6076 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006077 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 MD_BUG();
6079 return;
6080 }
NeilBrown4588b422006-03-27 01:18:04 -08006081 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006082 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006083 * u32, as those are the requirements for sector_div.
6084 * Thus 'scale' must be at least 10
6085 */
6086 scale = 10;
6087 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006088 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006089 scale++;
6090 }
6091 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006092 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006093
6094 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 {
NeilBrown4588b422006-03-27 01:18:04 -08006096 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 seq_printf(seq, "[");
6098 for (i = 0; i < x; i++)
6099 seq_printf(seq, "=");
6100 seq_printf(seq, ">");
6101 for (i = 0; i < y; i++)
6102 seq_printf(seq, ".");
6103 seq_printf(seq, "] ");
6104 }
NeilBrown4588b422006-03-27 01:18:04 -08006105 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006106 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6107 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006108 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6109 "check" :
6110 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6111 "resync" : "recovery"))),
6112 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006113 (unsigned long long) resync/2,
6114 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115
6116 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117 * dt: time from mark until now
6118 * db: blocks written from mark until now
6119 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006120 *
6121 * rt is a sector_t, so could be 32bit or 64bit.
6122 * So we divide before multiply in case it is 32bit and close
6123 * to the limit.
6124 * We scale the divisor (db) by 32 to avoid loosing precision
6125 * near the end of resync when the number of remaining sectors
6126 * is close to 'db'.
6127 * We then divide rt by 32 after multiplying by db to compensate.
6128 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 */
6130 dt = ((jiffies - mddev->resync_mark) / HZ);
6131 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006132 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6133 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134
NeilBrowndd71cf62009-05-07 12:49:35 +10006135 rt = max_sectors - resync; /* number of remaining sectors */
6136 sector_div(rt, db/32+1);
6137 rt *= dt;
6138 rt >>= 5;
6139
6140 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6141 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142
NeilBrownff4e8d92006-07-10 04:44:16 -07006143 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144}
6145
6146static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6147{
6148 struct list_head *tmp;
6149 loff_t l = *pos;
6150 mddev_t *mddev;
6151
6152 if (l >= 0x10000)
6153 return NULL;
6154 if (!l--)
6155 /* header */
6156 return (void*)1;
6157
6158 spin_lock(&all_mddevs_lock);
6159 list_for_each(tmp,&all_mddevs)
6160 if (!l--) {
6161 mddev = list_entry(tmp, mddev_t, all_mddevs);
6162 mddev_get(mddev);
6163 spin_unlock(&all_mddevs_lock);
6164 return mddev;
6165 }
6166 spin_unlock(&all_mddevs_lock);
6167 if (!l--)
6168 return (void*)2;/* tail */
6169 return NULL;
6170}
6171
6172static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6173{
6174 struct list_head *tmp;
6175 mddev_t *next_mddev, *mddev = v;
6176
6177 ++*pos;
6178 if (v == (void*)2)
6179 return NULL;
6180
6181 spin_lock(&all_mddevs_lock);
6182 if (v == (void*)1)
6183 tmp = all_mddevs.next;
6184 else
6185 tmp = mddev->all_mddevs.next;
6186 if (tmp != &all_mddevs)
6187 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6188 else {
6189 next_mddev = (void*)2;
6190 *pos = 0x10000;
6191 }
6192 spin_unlock(&all_mddevs_lock);
6193
6194 if (v != (void*)1)
6195 mddev_put(mddev);
6196 return next_mddev;
6197
6198}
6199
6200static void md_seq_stop(struct seq_file *seq, void *v)
6201{
6202 mddev_t *mddev = v;
6203
6204 if (mddev && v != (void*)1 && v != (void*)2)
6205 mddev_put(mddev);
6206}
6207
NeilBrownd7603b72006-01-06 00:20:30 -08006208struct mdstat_info {
6209 int event;
6210};
6211
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212static int md_seq_show(struct seq_file *seq, void *v)
6213{
6214 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006215 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006217 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006218 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219
6220 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006221 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 seq_printf(seq, "Personalities : ");
6223 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006224 list_for_each_entry(pers, &pers_list, list)
6225 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226
6227 spin_unlock(&pers_lock);
6228 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006229 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230 return 0;
6231 }
6232 if (v == (void*)2) {
6233 status_unused(seq);
6234 return 0;
6235 }
6236
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006237 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006239
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6241 seq_printf(seq, "%s : %sactive", mdname(mddev),
6242 mddev->pers ? "" : "in");
6243 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006244 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006246 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006247 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 seq_printf(seq, " %s", mddev->pers->name);
6249 }
6250
Andre Nolldd8ac332009-03-31 14:33:13 +11006251 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006252 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253 char b[BDEVNAME_SIZE];
6254 seq_printf(seq, " %s[%d]",
6255 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006256 if (test_bit(WriteMostly, &rdev->flags))
6257 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006258 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 seq_printf(seq, "(F)");
6260 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006261 } else if (rdev->raid_disk < 0)
6262 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006263 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264 }
6265
6266 if (!list_empty(&mddev->disks)) {
6267 if (mddev->pers)
6268 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006269 (unsigned long long)
6270 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 else
6272 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006273 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006275 if (mddev->persistent) {
6276 if (mddev->major_version != 0 ||
6277 mddev->minor_version != 90) {
6278 seq_printf(seq," super %d.%d",
6279 mddev->major_version,
6280 mddev->minor_version);
6281 }
NeilBrowne6910632008-02-06 01:39:51 -08006282 } else if (mddev->external)
6283 seq_printf(seq, " super external:%s",
6284 mddev->metadata_type);
6285 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006286 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287
6288 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006289 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006291 if (mddev->pers->sync_request) {
6292 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006293 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006294 seq_printf(seq, "\n ");
6295 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6296 seq_printf(seq, "\tresync=DELAYED\n ");
6297 else if (mddev->recovery_cp < MaxSector)
6298 seq_printf(seq, "\tresync=PENDING\n ");
6299 }
NeilBrown32a76272005-06-21 17:17:14 -07006300 } else
6301 seq_printf(seq, "\n ");
6302
6303 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006304 unsigned long chunk_kb;
6305 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006306 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006307 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006308 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6309 "%lu%s chunk",
6310 bitmap->pages - bitmap->missing_pages,
6311 bitmap->pages,
6312 (bitmap->pages - bitmap->missing_pages)
6313 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006314 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006315 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006316 if (bitmap->file) {
6317 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006318 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006319 }
NeilBrown78d742d2005-06-21 17:17:15 -07006320
NeilBrown32a76272005-06-21 17:17:14 -07006321 seq_printf(seq, "\n");
6322 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323 }
6324
6325 seq_printf(seq, "\n");
6326 }
6327 mddev_unlock(mddev);
6328
6329 return 0;
6330}
6331
Jan Engelhardt110518b2009-05-07 12:49:37 +10006332static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333 .start = md_seq_start,
6334 .next = md_seq_next,
6335 .stop = md_seq_stop,
6336 .show = md_seq_show,
6337};
6338
6339static int md_seq_open(struct inode *inode, struct file *file)
6340{
6341 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006342 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6343 if (mi == NULL)
6344 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345
6346 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006347 if (error)
6348 kfree(mi);
6349 else {
6350 struct seq_file *p = file->private_data;
6351 p->private = mi;
6352 mi->event = atomic_read(&md_event_count);
6353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354 return error;
6355}
6356
NeilBrownd7603b72006-01-06 00:20:30 -08006357static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6358{
6359 struct seq_file *m = filp->private_data;
6360 struct mdstat_info *mi = m->private;
6361 int mask;
6362
6363 poll_wait(filp, &md_event_waiters, wait);
6364
6365 /* always allow read */
6366 mask = POLLIN | POLLRDNORM;
6367
6368 if (mi->event != atomic_read(&md_event_count))
6369 mask |= POLLERR | POLLPRI;
6370 return mask;
6371}
6372
Arjan van de Venfa027c22007-02-12 00:55:33 -08006373static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006374 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 .open = md_seq_open,
6376 .read = seq_read,
6377 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006378 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006379 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380};
6381
NeilBrown2604b702006-01-06 00:20:36 -08006382int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006385 list_add_tail(&p->list, &pers_list);
6386 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387 spin_unlock(&pers_lock);
6388 return 0;
6389}
6390
NeilBrown2604b702006-01-06 00:20:36 -08006391int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392{
NeilBrown2604b702006-01-06 00:20:36 -08006393 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006395 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396 spin_unlock(&pers_lock);
6397 return 0;
6398}
6399
NeilBrowneea1bf32009-03-31 14:27:02 +11006400static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401{
6402 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006404 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405
6406 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006407 rcu_read_lock();
6408 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006410 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6411 (int)part_stat_read(&disk->part0, sectors[1]) -
6412 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006413 /* sync IO will cause sync_io to increase before the disk_stats
6414 * as sync_io is counted when a request starts, and
6415 * disk_stats is counted when it completes.
6416 * So resync activity will cause curr_events to be smaller than
6417 * when there was no such activity.
6418 * non-sync IO will cause disk_stat to increase without
6419 * increasing sync_io so curr_events will (eventually)
6420 * be larger than it was before. Once it becomes
6421 * substantially larger, the test below will cause
6422 * the array to appear non-idle, and resync will slow
6423 * down.
6424 * If there is a lot of outstanding resync activity when
6425 * we set last_event to curr_events, then all that activity
6426 * completing might cause the array to appear non-idle
6427 * and resync will be slowed down even though there might
6428 * not have been non-resync activity. This will only
6429 * happen once though. 'last_events' will soon reflect
6430 * the state where there is little or no outstanding
6431 * resync requests, and further resync activity will
6432 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006433 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006435 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436 rdev->last_events = curr_events;
6437 idle = 0;
6438 }
6439 }
NeilBrown4b809912008-07-21 17:05:25 +10006440 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441 return idle;
6442}
6443
6444void md_done_sync(mddev_t *mddev, int blocks, int ok)
6445{
6446 /* another "blocks" (512byte) blocks have been synced */
6447 atomic_sub(blocks, &mddev->recovery_active);
6448 wake_up(&mddev->recovery_wait);
6449 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006450 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 md_wakeup_thread(mddev->thread);
6452 // stop recovery, signal do_sync ....
6453 }
6454}
6455
6456
NeilBrown06d91a52005-06-21 17:17:12 -07006457/* md_write_start(mddev, bi)
6458 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006459 * in superblock) before writing, schedule a superblock update
6460 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006461 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006462void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463{
Neil Brown0fd62b82008-06-28 08:31:36 +10006464 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006465 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006466 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006467
NeilBrownf91de922005-11-08 21:39:36 -08006468 BUG_ON(mddev->ro == 1);
6469 if (mddev->ro == 2) {
6470 /* need to switch to read/write */
6471 mddev->ro = 0;
6472 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6473 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006474 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006475 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006476 }
NeilBrown06d91a52005-06-21 17:17:12 -07006477 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006478 if (mddev->safemode == 1)
6479 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006480 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006481 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006482 if (mddev->in_sync) {
6483 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006484 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006485 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006486 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006487 }
NeilBrowna9701a32005-11-08 21:39:34 -08006488 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006489 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006490 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006491 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006492 wait_event(mddev->sb_wait,
6493 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6494 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495}
6496
6497void md_write_end(mddev_t *mddev)
6498{
6499 if (atomic_dec_and_test(&mddev->writes_pending)) {
6500 if (mddev->safemode == 2)
6501 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006502 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6504 }
6505}
6506
NeilBrown2a2275d2007-01-26 00:57:11 -08006507/* md_allow_write(mddev)
6508 * Calling this ensures that the array is marked 'active' so that writes
6509 * may proceed without blocking. It is important to call this before
6510 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6511 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006512 *
6513 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6514 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006515 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006516int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006517{
6518 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006519 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006520 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006521 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006522 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006523 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006524
6525 spin_lock_irq(&mddev->write_lock);
6526 if (mddev->in_sync) {
6527 mddev->in_sync = 0;
6528 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6529 if (mddev->safemode_delay &&
6530 mddev->safemode == 0)
6531 mddev->safemode = 1;
6532 spin_unlock_irq(&mddev->write_lock);
6533 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006534 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006535 } else
6536 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006537
6538 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6539 return -EAGAIN;
6540 else
6541 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006542}
6543EXPORT_SYMBOL_GPL(md_allow_write);
6544
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545#define SYNC_MARKS 10
6546#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006547void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548{
6549 mddev_t *mddev2;
6550 unsigned int currspeed = 0,
6551 window;
NeilBrown57afd892005-06-21 17:17:13 -07006552 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553 unsigned long mark[SYNC_MARKS];
6554 sector_t mark_cnt[SYNC_MARKS];
6555 int last_mark,m;
6556 struct list_head *tmp;
6557 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006558 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006559 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006560 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561
6562 /* just incase thread restarts... */
6563 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6564 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006565 if (mddev->ro) /* never try to sync a read-only array */
6566 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006567
NeilBrown61df9d92006-10-03 01:15:57 -07006568 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6569 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6570 desc = "data-check";
6571 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6572 desc = "requested-resync";
6573 else
6574 desc = "resync";
6575 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6576 desc = "reshape";
6577 else
6578 desc = "recovery";
6579
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 /* we overload curr_resync somewhat here.
6581 * 0 == not engaged in resync at all
6582 * 2 == checking that there is no conflict with another sync
6583 * 1 == like 2, but have yielded to allow conflicting resync to
6584 * commense
6585 * other == active in resync - this many blocks
6586 *
6587 * Before starting a resync we must have set curr_resync to
6588 * 2, and then checked that every "conflicting" array has curr_resync
6589 * less than ours. When we find one that is the same or higher
6590 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6591 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6592 * This will mean we have to start checking from the beginning again.
6593 *
6594 */
6595
6596 do {
6597 mddev->curr_resync = 2;
6598
6599 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006600 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006601 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006602
6603 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006605 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606 if (mddev2 == mddev)
6607 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006608 if (!mddev->parallel_resync
6609 && mddev2->curr_resync
6610 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611 DEFINE_WAIT(wq);
6612 if (mddev < mddev2 && mddev->curr_resync == 2) {
6613 /* arbitrarily yield */
6614 mddev->curr_resync = 1;
6615 wake_up(&resync_wait);
6616 }
6617 if (mddev > mddev2 && mddev->curr_resync == 1)
6618 /* no need to wait here, we can wait the next
6619 * time 'round when curr_resync == 2
6620 */
6621 continue;
NeilBrown97441972008-09-19 11:49:54 +10006622 /* We need to wait 'interruptible' so as not to
6623 * contribute to the load average, and not to
6624 * be caught by 'softlockup'
6625 */
6626 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006627 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006628 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006629 printk(KERN_INFO "md: delaying %s of %s"
6630 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006632 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006634 if (signal_pending(current))
6635 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 schedule();
6637 finish_wait(&resync_wait, &wq);
6638 goto try_again;
6639 }
6640 finish_wait(&resync_wait, &wq);
6641 }
6642 }
6643 } while (mddev->curr_resync < 2);
6644
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006645 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006646 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006648 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006649 */
6650 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006651 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006652 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006653 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6654 j = mddev->resync_min;
6655 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006656 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006657
NeilBrownccfcc3c2006-03-27 01:18:09 -08006658 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006659 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006660 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006661 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006662 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006663 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006664 rcu_read_lock();
6665 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006666 if (rdev->raid_disk >= 0 &&
6667 !test_bit(Faulty, &rdev->flags) &&
6668 !test_bit(In_sync, &rdev->flags) &&
6669 rdev->recovery_offset < j)
6670 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006671 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673
NeilBrown61df9d92006-10-03 01:15:57 -07006674 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6675 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6676 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006677 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006678 "(but not more than %d KB/sec) for %s.\n",
6679 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680
NeilBrowneea1bf32009-03-31 14:27:02 +11006681 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006682
NeilBrown57afd892005-06-21 17:17:13 -07006683 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684 for (m = 0; m < SYNC_MARKS; m++) {
6685 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006686 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687 }
6688 last_mark = 0;
6689 mddev->resync_mark = mark[last_mark];
6690 mddev->resync_mark_cnt = mark_cnt[last_mark];
6691
6692 /*
6693 * Tune reconstruction:
6694 */
6695 window = 32*(PAGE_SIZE/512);
6696 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6697 window/2,(unsigned long long) max_sectors/2);
6698
6699 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700 last_check = 0;
6701
6702 if (j>2) {
6703 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006704 "md: resuming %s of %s from checkpoint.\n",
6705 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706 mddev->curr_resync = j;
6707 }
NeilBrownefa59332009-12-14 12:49:49 +11006708 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709
6710 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006711 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712
NeilBrown57afd892005-06-21 17:17:13 -07006713 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006714
NeilBrown7a91ee12009-05-26 12:57:21 +10006715 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6716 ((mddev->curr_resync > mddev->curr_resync_completed &&
6717 (mddev->curr_resync - mddev->curr_resync_completed)
6718 > (max_sectors >> 4)) ||
6719 (j - mddev->curr_resync_completed)*2
6720 >= mddev->resync_max - mddev->curr_resync_completed
6721 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006722 /* time to update curr_resync_completed */
6723 blk_unplug(mddev->queue);
6724 wait_event(mddev->recovery_wait,
6725 atomic_read(&mddev->recovery_active) == 0);
6726 mddev->curr_resync_completed =
6727 mddev->curr_resync;
6728 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006729 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006730 }
NeilBrownacb180b2009-04-14 16:28:34 +10006731
NeilBrowne62e58a2009-07-01 13:15:35 +10006732 while (j >= mddev->resync_max && !kthread_should_stop()) {
6733 /* As this condition is controlled by user-space,
6734 * we can block indefinitely, so use '_interruptible'
6735 * to avoid triggering warnings.
6736 */
6737 flush_signals(current); /* just in case */
6738 wait_event_interruptible(mddev->recovery_wait,
6739 mddev->resync_max > j
6740 || kthread_should_stop());
6741 }
NeilBrownacb180b2009-04-14 16:28:34 +10006742
6743 if (kthread_should_stop())
6744 goto interrupted;
6745
NeilBrown57afd892005-06-21 17:17:13 -07006746 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006747 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006748 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006749 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750 goto out;
6751 }
NeilBrown57afd892005-06-21 17:17:13 -07006752
6753 if (!skipped) { /* actual IO requested */
6754 io_sectors += sectors;
6755 atomic_add(sectors, &mddev->recovery_active);
6756 }
6757
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758 j += sectors;
6759 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006760 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006761 if (last_check == 0)
6762 /* this is the earliers that rebuilt will be
6763 * visible in /proc/mdstat
6764 */
6765 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006766
6767 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 continue;
6769
NeilBrown57afd892005-06-21 17:17:13 -07006770 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771
NeilBrowndfc70642008-05-23 13:04:39 -07006772 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773 break;
6774
6775 repeat:
6776 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6777 /* step marks */
6778 int next = (last_mark+1) % SYNC_MARKS;
6779
6780 mddev->resync_mark = mark[next];
6781 mddev->resync_mark_cnt = mark_cnt[next];
6782 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006783 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784 last_mark = next;
6785 }
6786
6787
NeilBrownc6207272008-02-06 01:39:52 -08006788 if (kthread_should_stop())
6789 goto interrupted;
6790
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791
6792 /*
6793 * this loop exits only if either when we are slower than
6794 * the 'hard' speed limit, or the system was IO-idle for
6795 * a jiffy.
6796 * the system might be non-idle CPU-wise, but we only care
6797 * about not overloading the IO subsystem. (things like an
6798 * e2fsck being done on the RAID array should execute fast)
6799 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006800 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801 cond_resched();
6802
NeilBrown57afd892005-06-21 17:17:13 -07006803 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6804 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805
NeilBrown88202a02006-01-06 00:21:36 -08006806 if (currspeed > speed_min(mddev)) {
6807 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006808 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006809 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810 goto repeat;
6811 }
6812 }
6813 }
NeilBrown61df9d92006-10-03 01:15:57 -07006814 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815 /*
6816 * this also signals 'finished resyncing' to md_stop
6817 */
6818 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006819 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820
6821 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6822
6823 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006824 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825
NeilBrowndfc70642008-05-23 13:04:39 -07006826 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006827 mddev->curr_resync > 2) {
6828 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6829 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6830 if (mddev->curr_resync >= mddev->recovery_cp) {
6831 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006832 "md: checkpointing %s of %s.\n",
6833 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006834 mddev->recovery_cp = mddev->curr_resync;
6835 }
6836 } else
6837 mddev->recovery_cp = MaxSector;
6838 } else {
6839 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6840 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006841 rcu_read_lock();
6842 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006843 if (rdev->raid_disk >= 0 &&
6844 !test_bit(Faulty, &rdev->flags) &&
6845 !test_bit(In_sync, &rdev->flags) &&
6846 rdev->recovery_offset < mddev->curr_resync)
6847 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07006848 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006850 }
NeilBrown17571282006-12-10 02:20:52 -08006851 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006854 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6855 /* We completed so min/max setting can be forgotten if used. */
6856 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6857 mddev->resync_min = 0;
6858 mddev->resync_max = MaxSector;
6859 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6860 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006862 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6863 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006864 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865 wake_up(&resync_wait);
6866 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6867 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006868 return;
6869
6870 interrupted:
6871 /*
6872 * got a signal, exit.
6873 */
6874 printk(KERN_INFO
6875 "md: md_do_sync() got signal ... exiting\n");
6876 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6877 goto out;
6878
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879}
NeilBrown29269552006-03-27 01:18:10 -08006880EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881
6882
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006883static int remove_and_add_spares(mddev_t *mddev)
6884{
6885 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006886 int spares = 0;
6887
NeilBrown97e4f422009-03-31 14:33:13 +11006888 mddev->curr_resync_completed = 0;
6889
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006890 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006891 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006892 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006893 (test_bit(Faulty, &rdev->flags) ||
6894 ! test_bit(In_sync, &rdev->flags)) &&
6895 atomic_read(&rdev->nr_pending)==0) {
6896 if (mddev->pers->hot_remove_disk(
6897 mddev, rdev->raid_disk)==0) {
6898 char nm[20];
6899 sprintf(nm,"rd%d", rdev->raid_disk);
6900 sysfs_remove_link(&mddev->kobj, nm);
6901 rdev->raid_disk = -1;
6902 }
6903 }
6904
NeilBrown4044ba52009-01-09 08:31:11 +11006905 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006906 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006907 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006908 !test_bit(In_sync, &rdev->flags) &&
6909 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006910 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006911 if (rdev->raid_disk < 0
6912 && !test_bit(Faulty, &rdev->flags)) {
6913 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006914 if (mddev->pers->
6915 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006916 char nm[20];
6917 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006918 if (sysfs_create_link(&mddev->kobj,
6919 &rdev->kobj, nm))
6920 printk(KERN_WARNING
6921 "md: cannot register "
6922 "%s for %s\n",
6923 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006924 spares++;
6925 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11006926 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006927 } else
6928 break;
6929 }
NeilBrowndfc70642008-05-23 13:04:39 -07006930 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006931 }
6932 return spares;
6933}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006934/*
6935 * This routine is regularly called by all per-raid-array threads to
6936 * deal with generic issues like resync and super-block update.
6937 * Raid personalities that don't have a thread (linear/raid0) do not
6938 * need this as they never do any recovery or update the superblock.
6939 *
6940 * It does not do any resync itself, but rather "forks" off other threads
6941 * to do that as needed.
6942 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6943 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006944 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945 * and wakeups up this thread which will reap the thread and finish up.
6946 * This thread also removes any faulty devices (with nr_pending == 0).
6947 *
6948 * The overall approach is:
6949 * 1/ if the superblock needs updating, update it.
6950 * 2/ If a recovery thread is running, don't do anything else.
6951 * 3/ If recovery has finished, clean up, possibly marking spares active.
6952 * 4/ If there are any faulty devices, remove them.
6953 * 5/ If array is degraded, try to add spares devices
6954 * 6/ If array has spares or is not in-sync, start a resync thread.
6955 */
6956void md_check_recovery(mddev_t *mddev)
6957{
6958 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959
6960
NeilBrown5f404022005-06-21 17:17:16 -07006961 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11006962 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963
6964 if (mddev->ro)
6965 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006966
6967 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006968 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006969 printk(KERN_INFO "md: %s in immediate safe mode\n",
6970 mdname(mddev));
6971 mddev->safemode = 2;
6972 }
6973 flush_signals(current);
6974 }
6975
NeilBrownc89a8ee2008-08-05 15:54:13 +10006976 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6977 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006979 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006981 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006982 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006983 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6984 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985 ))
6986 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006987
NeilBrowndf5b89b2006-03-27 01:18:20 -08006988 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006989 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006990
NeilBrownc89a8ee2008-08-05 15:54:13 +10006991 if (mddev->ro) {
6992 /* Only thing we do on a ro array is remove
6993 * failed devices.
6994 */
6995 remove_and_add_spares(mddev);
6996 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6997 goto unlock;
6998 }
6999
NeilBrown31a59e32008-04-30 00:52:30 -07007000 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007001 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007002 spin_lock_irq(&mddev->write_lock);
7003 if (mddev->safemode &&
7004 !atomic_read(&mddev->writes_pending) &&
7005 !mddev->in_sync &&
7006 mddev->recovery_cp == MaxSector) {
7007 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007008 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07007009 if (mddev->persistent)
7010 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7011 }
7012 if (mddev->safemode == 1)
7013 mddev->safemode = 0;
7014 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007015 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11007016 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007017 }
NeilBrownfca4d842005-06-21 17:17:11 -07007018
NeilBrown850b2b422006-10-03 01:15:46 -07007019 if (mddev->flags)
7020 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007021
Linus Torvalds1da177e2005-04-16 15:20:36 -07007022 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7023 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7024 /* resync/recovery still happening */
7025 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7026 goto unlock;
7027 }
7028 if (mddev->sync_thread) {
7029 /* resync has finished, collect result */
7030 md_unregister_thread(mddev->sync_thread);
7031 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07007032 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7033 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007034 /* success...*/
7035 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10007036 if (mddev->pers->spare_active(mddev))
7037 sysfs_notify(&mddev->kobj, NULL,
7038 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039 }
NeilBrowncea9c222009-03-31 15:15:05 +11007040 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7041 mddev->pers->finish_reshape)
7042 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07007043 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07007044
7045 /* if array is no-longer degraded, then any saved_raid_disk
7046 * information must be scrapped
7047 */
7048 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007049 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07007050 rdev->saved_raid_disk = -1;
7051
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052 mddev->recovery = 0;
7053 /* flag recovery needed just to double check */
7054 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11007055 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007056 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057 goto unlock;
7058 }
Neil Brown72a23c22008-06-28 08:31:41 +10007059 /* Set RUNNING before clearing NEEDED to avoid
7060 * any transients in the value of "sync_action".
7061 */
7062 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7063 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007064 /* Clear some bits that don't mean anything, but
7065 * might be left set
7066 */
NeilBrown24dd4692005-11-08 21:39:26 -08007067 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7068 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007070 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7071 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007072 /* no recovery is running.
7073 * remove any failed drives, then
7074 * add spares if possible.
7075 * Spare are also removed and re-added, to allow
7076 * the personality to fail the re-add.
7077 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007078
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007079 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007080 if (mddev->pers->check_reshape == NULL ||
7081 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007082 /* Cannot proceed */
7083 goto unlock;
7084 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007085 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007086 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007087 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7088 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007089 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007090 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007091 } else if (mddev->recovery_cp < MaxSector) {
7092 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007093 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007094 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7095 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007097
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007099 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7100 /* We are adding a device or devices to an array
7101 * which has the bitmap stored on all devices.
7102 * So make sure all bitmap pages get written
7103 */
7104 bitmap_write_all(mddev->bitmap);
7105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007106 mddev->sync_thread = md_register_thread(md_do_sync,
7107 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007108 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109 if (!mddev->sync_thread) {
7110 printk(KERN_ERR "%s: could not start resync"
7111 " thread...\n",
7112 mdname(mddev));
7113 /* leave the spares where they are, it shouldn't hurt */
7114 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08007115 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11007117 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007118 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119 }
7120 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007121 if (!mddev->sync_thread) {
7122 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7123 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7124 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007125 if (mddev->sysfs_action)
7126 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128 mddev_unlock(mddev);
7129 }
7130}
7131
Dan Williams6bfe0b42008-04-30 00:52:32 -07007132void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7133{
NeilBrown3c0ee632008-10-21 13:25:28 +11007134 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007135 wait_event_timeout(rdev->blocked_wait,
7136 !test_bit(Blocked, &rdev->flags),
7137 msecs_to_jiffies(5000));
7138 rdev_dec_pending(rdev, mddev);
7139}
7140EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7141
Adrian Bunk75c96f82005-05-05 16:16:09 -07007142static int md_notify_reboot(struct notifier_block *this,
7143 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144{
7145 struct list_head *tmp;
7146 mddev_t *mddev;
7147
7148 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7149
7150 printk(KERN_INFO "md: stopping all md devices.\n");
7151
NeilBrown29ac4aa2008-02-06 01:39:58 -08007152 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007153 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007154 /* Force a switch to readonly even array
7155 * appears to still be in use. Hence
7156 * the '100'.
7157 */
NeilBrowna4bd82d2010-03-29 13:23:10 +11007158 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007159 mddev_unlock(mddev);
7160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161 /*
7162 * certain more exotic SCSI devices are known to be
7163 * volatile wrt too early system reboots. While the
7164 * right place to handle this issue is the given
7165 * driver, we do want to have a safe RAID driver ...
7166 */
7167 mdelay(1000*1);
7168 }
7169 return NOTIFY_DONE;
7170}
7171
Adrian Bunk75c96f82005-05-05 16:16:09 -07007172static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007173 .notifier_call = md_notify_reboot,
7174 .next = NULL,
7175 .priority = INT_MAX, /* before any real devices */
7176};
7177
7178static void md_geninit(void)
7179{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7181
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07007182 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007183}
7184
Adrian Bunk75c96f82005-05-05 16:16:09 -07007185static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007187 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188 return -1;
7189 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007190 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191 return -1;
7192 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007193 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007194 md_probe, NULL, NULL);
7195 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196 md_probe, NULL, NULL);
7197
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007199 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200
7201 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203}
7204
7205
7206#ifndef MODULE
7207
7208/*
7209 * Searches all registered partitions for autorun RAID arrays
7210 * at boot time.
7211 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007212
7213static LIST_HEAD(all_detected_devices);
7214struct detected_devices_node {
7215 struct list_head list;
7216 dev_t dev;
7217};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218
7219void md_autodetect_dev(dev_t dev)
7220{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007221 struct detected_devices_node *node_detected_dev;
7222
7223 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7224 if (node_detected_dev) {
7225 node_detected_dev->dev = dev;
7226 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7227 } else {
7228 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7229 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231}
7232
7233
7234static void autostart_arrays(int part)
7235{
7236 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007237 struct detected_devices_node *node_detected_dev;
7238 dev_t dev;
7239 int i_scanned, i_passed;
7240
7241 i_scanned = 0;
7242 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007243
7244 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7245
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007246 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7247 i_scanned++;
7248 node_detected_dev = list_entry(all_detected_devices.next,
7249 struct detected_devices_node, list);
7250 list_del(&node_detected_dev->list);
7251 dev = node_detected_dev->dev;
7252 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007253 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007254 if (IS_ERR(rdev))
7255 continue;
7256
NeilBrownb2d444d2005-11-08 21:39:31 -08007257 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007258 MD_BUG();
7259 continue;
7260 }
NeilBrownd0fae182008-03-04 14:29:31 -08007261 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007262 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007263 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007264 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007265
7266 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7267 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007268
7269 autorun_devices(part);
7270}
7271
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007272#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273
7274static __exit void md_exit(void)
7275{
7276 mddev_t *mddev;
7277 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007278
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007279 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007280 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007282 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283 unregister_blkdev(mdp_major, "mdp");
7284 unregister_reboot_notifier(&md_notifier);
7285 unregister_sysctl_table(raid_table_header);
7286 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007287 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007289 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290 }
7291}
7292
Dan Williams685784a2007-07-09 11:56:42 -07007293subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294module_exit(md_exit)
7295
NeilBrownf91de922005-11-08 21:39:36 -08007296static int get_ro(char *buffer, struct kernel_param *kp)
7297{
7298 return sprintf(buffer, "%d", start_readonly);
7299}
7300static int set_ro(const char *val, struct kernel_param *kp)
7301{
7302 char *e;
7303 int num = simple_strtoul(val, &e, 10);
7304 if (*val && (*e == '\0' || *e == '\n')) {
7305 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007306 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007307 }
7308 return -EINVAL;
7309}
7310
NeilBrown80ca3a42006-07-10 04:44:18 -07007311module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7312module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007313
NeilBrownefeb53c2009-01-09 08:31:10 +11007314module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007315
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316EXPORT_SYMBOL(register_md_personality);
7317EXPORT_SYMBOL(unregister_md_personality);
7318EXPORT_SYMBOL(md_error);
7319EXPORT_SYMBOL(md_done_sync);
7320EXPORT_SYMBOL(md_write_start);
7321EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007322EXPORT_SYMBOL(md_register_thread);
7323EXPORT_SYMBOL(md_unregister_thread);
7324EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007325EXPORT_SYMBOL(md_check_recovery);
7326MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007327MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007328MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007329MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);