blob: 1893af6787790a7d7919de4f478313fa4a2ba957 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090052#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110053#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110054#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56#define DEBUG 0
57#define dprintk(x...) ((void)(DEBUG && printk(x)))
58
59
60#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110061static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#endif
63
NeilBrown2604b702006-01-06 00:20:36 -080064static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static DEFINE_SPINLOCK(pers_lock);
66
Adrian Bunk5e563412006-06-26 00:27:42 -070067static void md_print_devices(void);
68
Bernd Schubert90b08712008-05-23 13:04:38 -070069static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
70
Adrian Bunk5e563412006-06-26 00:27:42 -070071#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/*
Robert Becker1e509152009-12-14 12:49:58 +110074 * Default number of read corrections we'll attempt on an rdev
75 * before ejecting it from the array. We divide the read error
76 * count by 2 for every hour elapsed between read errors.
77 */
78#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
79/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
81 * is 1000 KB/sec, so the extra system load does not show up that much.
82 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070083 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * subsystem is idle. There is also an 'absolute maximum' reconstruction
85 * speed limit - in case reconstruction slows down your system despite
86 * idle IO detection.
87 *
88 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080089 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 */
91
92static int sysctl_speed_limit_min = 1000;
93static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080094static inline int speed_min(mddev_t *mddev)
95{
96 return mddev->sync_speed_min ?
97 mddev->sync_speed_min : sysctl_speed_limit_min;
98}
99
100static inline int speed_max(mddev_t *mddev)
101{
102 return mddev->sync_speed_max ?
103 mddev->sync_speed_max : sysctl_speed_limit_max;
104}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106static struct ctl_table_header *raid_table_header;
107
108static ctl_table raid_table[] = {
109 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 .procname = "speed_limit_min",
111 .data = &sysctl_speed_limit_min,
112 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700113 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800114 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 },
116 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .procname = "speed_limit_max",
118 .data = &sysctl_speed_limit_max,
119 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700120 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800121 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800123 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124};
125
126static ctl_table raid_dir_table[] = {
127 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 .procname = "raid",
129 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700130 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 .child = raid_table,
132 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800133 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
136static ctl_table raid_root_table[] = {
137 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .procname = "dev",
139 .maxlen = 0,
140 .mode = 0555,
141 .child = raid_dir_table,
142 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800143 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700146static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
NeilBrownf91de922005-11-08 21:39:36 -0800148static int start_readonly;
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/*
NeilBrownd7603b72006-01-06 00:20:30 -0800151 * We have a system wide 'event count' that is incremented
152 * on any 'interesting' event, and readers of /proc/mdstat
153 * can use 'poll' or 'select' to find out when the event
154 * count increases.
155 *
156 * Events are:
157 * start array, stop array, error, add device, remove device,
158 * start build, activate spare
159 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800160static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800161static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800162void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800163{
164 atomic_inc(&md_event_count);
165 wake_up(&md_event_waiters);
166}
NeilBrown29269552006-03-27 01:18:10 -0800167EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800168
NeilBrownc331eb02006-05-30 21:27:13 -0700169/* Alternate version that can be called from interrupts
170 * when calling sysfs_notify isn't needed.
171 */
Adrian Bunk05381952006-06-26 00:28:01 -0700172static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700173{
174 atomic_inc(&md_event_count);
175 wake_up(&md_event_waiters);
176}
177
NeilBrownd7603b72006-01-06 00:20:30 -0800178/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 * Enables to iterate over all existing md arrays
180 * all_mddevs_lock protects this list.
181 */
182static LIST_HEAD(all_mddevs);
183static DEFINE_SPINLOCK(all_mddevs_lock);
184
185
186/*
187 * iterates through all used mddevs in the system.
188 * We take care to grab the all_mddevs_lock whenever navigating
189 * the list, and to always hold a refcount when unlocked.
190 * Any code which breaks out of this loop while own
191 * a reference to the current mddev and must mddev_put it.
192 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800193#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 \
195 for (({ spin_lock(&all_mddevs_lock); \
196 tmp = all_mddevs.next; \
197 mddev = NULL;}); \
198 ({ if (tmp != &all_mddevs) \
199 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
200 spin_unlock(&all_mddevs_lock); \
201 if (mddev) mddev_put(mddev); \
202 mddev = list_entry(tmp, mddev_t, all_mddevs); \
203 tmp != &all_mddevs;}); \
204 ({ spin_lock(&all_mddevs_lock); \
205 tmp = tmp->next;}) \
206 )
207
208
NeilBrown409c57f2009-03-31 14:39:39 +1100209/* Rather than calling directly into the personality make_request function,
210 * IO requests come here first so that we can check if the device is
211 * being suspended pending a reconfiguration.
212 * We hold a refcount over the call to ->make_request. By the time that
213 * call has finished, the bio has been linked into some internal structure
214 * and so is visible to ->quiesce(), so we don't need the refcount any more.
215 */
216static int md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
NeilBrown49077322010-03-25 16:20:56 +1100218 const int rw = bio_data_dir(bio);
NeilBrown409c57f2009-03-31 14:39:39 +1100219 mddev_t *mddev = q->queuedata;
220 int rv;
NeilBrown49077322010-03-25 16:20:56 +1100221 int cpu;
222
NeilBrown409c57f2009-03-31 14:39:39 +1100223 if (mddev == NULL || mddev->pers == NULL) {
224 bio_io_error(bio);
225 return 0;
226 }
227 rcu_read_lock();
NeilBrowna2826aa2009-12-14 12:49:49 +1100228 if (mddev->suspended || mddev->barrier) {
NeilBrown409c57f2009-03-31 14:39:39 +1100229 DEFINE_WAIT(__wait);
230 for (;;) {
231 prepare_to_wait(&mddev->sb_wait, &__wait,
232 TASK_UNINTERRUPTIBLE);
NeilBrowna2826aa2009-12-14 12:49:49 +1100233 if (!mddev->suspended && !mddev->barrier)
NeilBrown409c57f2009-03-31 14:39:39 +1100234 break;
235 rcu_read_unlock();
236 schedule();
237 rcu_read_lock();
238 }
239 finish_wait(&mddev->sb_wait, &__wait);
240 }
241 atomic_inc(&mddev->active_io);
242 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100243
NeilBrown21a52c62010-04-01 15:02:13 +1100244 rv = mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100245
246 cpu = part_stat_lock();
247 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
248 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
249 bio_sectors(bio));
250 part_stat_unlock();
251
NeilBrown409c57f2009-03-31 14:39:39 +1100252 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
253 wake_up(&mddev->sb_wait);
254
255 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
NeilBrown9e35b992010-04-06 14:23:02 +1000258/* mddev_suspend makes sure no new requests are submitted
259 * to the device, and that any requests that have been submitted
260 * are completely handled.
261 * Once ->stop is called and completes, the module will be completely
262 * unused.
263 */
NeilBrown409c57f2009-03-31 14:39:39 +1100264static void mddev_suspend(mddev_t *mddev)
265{
266 BUG_ON(mddev->suspended);
267 mddev->suspended = 1;
268 synchronize_rcu();
269 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
270 mddev->pers->quiesce(mddev, 1);
NeilBrown409c57f2009-03-31 14:39:39 +1100271}
272
273static void mddev_resume(mddev_t *mddev)
274{
275 mddev->suspended = 0;
276 wake_up(&mddev->sb_wait);
277 mddev->pers->quiesce(mddev, 0);
278}
279
NeilBrown3fa841d2009-09-23 18:10:29 +1000280int mddev_congested(mddev_t *mddev, int bits)
281{
NeilBrowna2826aa2009-12-14 12:49:49 +1100282 if (mddev->barrier)
283 return 1;
NeilBrown3fa841d2009-09-23 18:10:29 +1000284 return mddev->suspended;
285}
286EXPORT_SYMBOL(mddev_congested);
287
NeilBrowna2826aa2009-12-14 12:49:49 +1100288/*
289 * Generic barrier handling for md
290 */
291
292#define POST_REQUEST_BARRIER ((void*)1)
293
294static void md_end_barrier(struct bio *bio, int err)
295{
296 mdk_rdev_t *rdev = bio->bi_private;
297 mddev_t *mddev = rdev->mddev;
298 if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER)
299 set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags);
300
301 rdev_dec_pending(rdev, mddev);
302
303 if (atomic_dec_and_test(&mddev->flush_pending)) {
304 if (mddev->barrier == POST_REQUEST_BARRIER) {
305 /* This was a post-request barrier */
306 mddev->barrier = NULL;
307 wake_up(&mddev->sb_wait);
308 } else
309 /* The pre-request barrier has finished */
310 schedule_work(&mddev->barrier_work);
311 }
312 bio_put(bio);
313}
314
315static void submit_barriers(mddev_t *mddev)
316{
317 mdk_rdev_t *rdev;
318
319 rcu_read_lock();
320 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
321 if (rdev->raid_disk >= 0 &&
322 !test_bit(Faulty, &rdev->flags)) {
323 /* Take two references, one is dropped
324 * when request finishes, one after
325 * we reclaim rcu_read_lock
326 */
327 struct bio *bi;
328 atomic_inc(&rdev->nr_pending);
329 atomic_inc(&rdev->nr_pending);
330 rcu_read_unlock();
331 bi = bio_alloc(GFP_KERNEL, 0);
332 bi->bi_end_io = md_end_barrier;
333 bi->bi_private = rdev;
334 bi->bi_bdev = rdev->bdev;
335 atomic_inc(&mddev->flush_pending);
336 submit_bio(WRITE_BARRIER, bi);
337 rcu_read_lock();
338 rdev_dec_pending(rdev, mddev);
339 }
340 rcu_read_unlock();
341}
342
343static void md_submit_barrier(struct work_struct *ws)
344{
345 mddev_t *mddev = container_of(ws, mddev_t, barrier_work);
346 struct bio *bio = mddev->barrier;
347
348 atomic_set(&mddev->flush_pending, 1);
349
350 if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags))
351 bio_endio(bio, -EOPNOTSUPP);
352 else if (bio->bi_size == 0)
353 /* an empty barrier - all done */
354 bio_endio(bio, 0);
355 else {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200356 bio->bi_rw &= ~REQ_HARDBARRIER;
NeilBrown21a52c62010-04-01 15:02:13 +1100357 if (mddev->pers->make_request(mddev, bio))
NeilBrowna2826aa2009-12-14 12:49:49 +1100358 generic_make_request(bio);
359 mddev->barrier = POST_REQUEST_BARRIER;
360 submit_barriers(mddev);
361 }
362 if (atomic_dec_and_test(&mddev->flush_pending)) {
363 mddev->barrier = NULL;
364 wake_up(&mddev->sb_wait);
365 }
366}
367
368void md_barrier_request(mddev_t *mddev, struct bio *bio)
369{
370 spin_lock_irq(&mddev->write_lock);
371 wait_event_lock_irq(mddev->sb_wait,
372 !mddev->barrier,
373 mddev->write_lock, /*nothing*/);
374 mddev->barrier = bio;
375 spin_unlock_irq(&mddev->write_lock);
376
377 atomic_set(&mddev->flush_pending, 1);
378 INIT_WORK(&mddev->barrier_work, md_submit_barrier);
379
380 submit_barriers(mddev);
381
382 if (atomic_dec_and_test(&mddev->flush_pending))
383 schedule_work(&mddev->barrier_work);
384}
385EXPORT_SYMBOL(md_barrier_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387static inline mddev_t *mddev_get(mddev_t *mddev)
388{
389 atomic_inc(&mddev->active);
390 return mddev;
391}
392
Dan Williams5fd3a172009-03-04 00:57:25 -0700393static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395static void mddev_put(mddev_t *mddev)
396{
397 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
398 return;
NeilBrownd3374822009-01-09 08:31:10 +1100399 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100400 mddev->ctime == 0 && !mddev->hold_active) {
401 /* Array is not configured at all, and not held active,
402 * so destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 list_del(&mddev->all_mddevs);
NeilBrownd3374822009-01-09 08:31:10 +1100404 if (mddev->gendisk) {
405 /* we did a probe so need to clean up.
406 * Call schedule_work inside the spinlock
407 * so that flush_scheduled_work() after
408 * mddev_find will succeed in waiting for the
409 * work to be done.
410 */
411 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
412 schedule_work(&mddev->del_work);
413 } else
414 kfree(mddev);
415 }
416 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
NeilBrownfafd7fb2010-04-01 15:55:30 +1100419static void mddev_init(mddev_t *mddev)
420{
421 mutex_init(&mddev->open_mutex);
422 mutex_init(&mddev->reconfig_mutex);
423 mutex_init(&mddev->bitmap_info.mutex);
424 INIT_LIST_HEAD(&mddev->disks);
425 INIT_LIST_HEAD(&mddev->all_mddevs);
426 init_timer(&mddev->safemode_timer);
427 atomic_set(&mddev->active, 1);
428 atomic_set(&mddev->openers, 0);
429 atomic_set(&mddev->active_io, 0);
430 spin_lock_init(&mddev->write_lock);
431 atomic_set(&mddev->flush_pending, 0);
432 init_waitqueue_head(&mddev->sb_wait);
433 init_waitqueue_head(&mddev->recovery_wait);
434 mddev->reshape_position = MaxSector;
435 mddev->resync_min = 0;
436 mddev->resync_max = MaxSector;
437 mddev->level = LEVEL_NONE;
438}
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440static mddev_t * mddev_find(dev_t unit)
441{
442 mddev_t *mddev, *new = NULL;
443
444 retry:
445 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
NeilBrownefeb53c2009-01-09 08:31:10 +1100447 if (unit) {
448 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
449 if (mddev->unit == unit) {
450 mddev_get(mddev);
451 spin_unlock(&all_mddevs_lock);
452 kfree(new);
453 return mddev;
454 }
455
456 if (new) {
457 list_add(&new->all_mddevs, &all_mddevs);
458 spin_unlock(&all_mddevs_lock);
459 new->hold_active = UNTIL_IOCTL;
460 return new;
461 }
462 } else if (new) {
463 /* find an unused unit number */
464 static int next_minor = 512;
465 int start = next_minor;
466 int is_free = 0;
467 int dev = 0;
468 while (!is_free) {
469 dev = MKDEV(MD_MAJOR, next_minor);
470 next_minor++;
471 if (next_minor > MINORMASK)
472 next_minor = 0;
473 if (next_minor == start) {
474 /* Oh dear, all in use. */
475 spin_unlock(&all_mddevs_lock);
476 kfree(new);
477 return NULL;
478 }
479
480 is_free = 1;
481 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
482 if (mddev->unit == dev) {
483 is_free = 0;
484 break;
485 }
486 }
487 new->unit = dev;
488 new->md_minor = MINOR(dev);
489 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 list_add(&new->all_mddevs, &all_mddevs);
491 spin_unlock(&all_mddevs_lock);
492 return new;
493 }
494 spin_unlock(&all_mddevs_lock);
495
NeilBrown9ffae0c2006-01-06 00:20:32 -0800496 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if (!new)
498 return NULL;
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 new->unit = unit;
501 if (MAJOR(unit) == MD_MAJOR)
502 new->md_minor = MINOR(unit);
503 else
504 new->md_minor = MINOR(unit) >> MdpMinorShift;
505
NeilBrownfafd7fb2010-04-01 15:55:30 +1100506 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 goto retry;
509}
510
511static inline int mddev_lock(mddev_t * mddev)
512{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800513 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
Dan Williamsb522adc2009-03-31 15:00:31 +1100516static inline int mddev_is_locked(mddev_t *mddev)
517{
518 return mutex_is_locked(&mddev->reconfig_mutex);
519}
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521static inline int mddev_trylock(mddev_t * mddev)
522{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800523 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
NeilBrownb6eb1272010-04-15 10:13:47 +1000526static struct attribute_group md_redundancy_group;
527
NeilBrowna64c8762010-04-14 17:15:37 +1000528static void mddev_unlock(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
NeilBrowna64c8762010-04-14 17:15:37 +1000530 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000531 /* These cannot be removed under reconfig_mutex as
532 * an access to the files will try to take reconfig_mutex
533 * while holding the file unremovable, which leads to
534 * a deadlock.
535 * So hold open_mutex instead - we are allowed to take
536 * it while holding reconfig_mutex, and md_run can
537 * use it to wait for the remove to complete.
538 */
NeilBrowna64c8762010-04-14 17:15:37 +1000539 struct attribute_group *to_remove = mddev->to_remove;
540 mddev->to_remove = NULL;
NeilBrownb6eb1272010-04-15 10:13:47 +1000541 mutex_lock(&mddev->open_mutex);
542 mutex_unlock(&mddev->reconfig_mutex);
543
NeilBrowna64c8762010-04-14 17:15:37 +1000544 if (to_remove != &md_redundancy_group)
545 sysfs_remove_group(&mddev->kobj, to_remove);
546 if (mddev->pers == NULL ||
547 mddev->pers->sync_request == NULL) {
548 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
549 if (mddev->sysfs_action)
550 sysfs_put(mddev->sysfs_action);
551 mddev->sysfs_action = NULL;
552 }
NeilBrownb6eb1272010-04-15 10:13:47 +1000553 mutex_unlock(&mddev->open_mutex);
554 } else
555 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
NeilBrown005eca52005-08-22 13:11:08 -0700557 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
NeilBrown2989ddb2006-01-06 00:20:43 -0800560static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100562 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100564 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (rdev->desc_nr == nr)
566 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return NULL;
569}
570
571static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
572{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 mdk_rdev_t *rdev;
574
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100575 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (rdev->bdev->bd_dev == dev)
577 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return NULL;
580}
581
NeilBrownd9d166c2006-01-06 00:20:51 -0800582static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800583{
584 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800585 list_for_each_entry(pers, &pers_list, list) {
586 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800587 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800588 if (strcmp(pers->name, clevel)==0)
589 return pers;
590 }
NeilBrown2604b702006-01-06 00:20:36 -0800591 return NULL;
592}
593
Andre Nollb73df2d2008-07-11 22:02:23 +1000594/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700595static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Andre Nollb73df2d2008-07-11 22:02:23 +1000597 sector_t num_sectors = bdev->bd_inode->i_size / 512;
598 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601static int alloc_disk_sb(mdk_rdev_t * rdev)
602{
603 if (rdev->sb_page)
604 MD_BUG();
605
606 rdev->sb_page = alloc_page(GFP_KERNEL);
607 if (!rdev->sb_page) {
608 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000609 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
611
612 return 0;
613}
614
615static void free_disk_sb(mdk_rdev_t * rdev)
616{
617 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800618 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 rdev->sb_loaded = 0;
620 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000621 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100622 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
624}
625
626
NeilBrown6712ecf2007-09-27 12:47:43 +0200627static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700628{
629 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800630 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700631
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700632 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
633 printk("md: super_written gets error=%d, uptodate=%d\n",
634 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
635 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800636 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700637 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700638
NeilBrowna9701a32005-11-08 21:39:34 -0800639 if (atomic_dec_and_test(&mddev->pending_writes))
640 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700641 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700642}
643
NeilBrown6712ecf2007-09-27 12:47:43 +0200644static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800645{
646 struct bio *bio2 = bio->bi_private;
647 mdk_rdev_t *rdev = bio2->bi_private;
648 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800649
650 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
651 error == -EOPNOTSUPP) {
652 unsigned long flags;
653 /* barriers don't appear to be supported :-( */
654 set_bit(BarriersNotsupp, &rdev->flags);
655 mddev->barriers_work = 0;
656 spin_lock_irqsave(&mddev->write_lock, flags);
657 bio2->bi_next = mddev->biolist;
658 mddev->biolist = bio2;
659 spin_unlock_irqrestore(&mddev->write_lock, flags);
660 wake_up(&mddev->sb_wait);
661 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200662 } else {
663 bio_put(bio2);
664 bio->bi_private = rdev;
665 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800666 }
NeilBrowna9701a32005-11-08 21:39:34 -0800667}
668
NeilBrown7bfa19f2005-06-21 17:17:28 -0700669void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
670 sector_t sector, int size, struct page *page)
671{
672 /* write first size bytes of page to sector of rdev
673 * Increment mddev->pending_writes before returning
674 * and decrement it on completion, waking up sb_wait
675 * if zero is reached.
676 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800677 *
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200678 * As we might need to resubmit the request if REQ_HARDBARRIER
NeilBrowna9701a32005-11-08 21:39:34 -0800679 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700680 */
681 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200682 int rw = REQ_WRITE | REQ_SYNC | REQ_UNPLUG;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700683
684 bio->bi_bdev = rdev->bdev;
685 bio->bi_sector = sector;
686 bio_add_page(bio, page, size, 0);
687 bio->bi_private = rdev;
688 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800689 bio->bi_rw = rw;
690
NeilBrown7bfa19f2005-06-21 17:17:28 -0700691 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800692 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
693 struct bio *rbio;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200694 rw |= REQ_HARDBARRIER;
NeilBrowna9701a32005-11-08 21:39:34 -0800695 rbio = bio_clone(bio, GFP_NOIO);
696 rbio->bi_private = bio;
697 rbio->bi_end_io = super_written_barrier;
698 submit_bio(rw, rbio);
699 } else
700 submit_bio(rw, bio);
701}
702
703void md_super_wait(mddev_t *mddev)
704{
705 /* wait for all superblock writes that were scheduled to complete.
706 * if any had to be retried (due to BARRIER problems), retry them
707 */
708 DEFINE_WAIT(wq);
709 for(;;) {
710 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
711 if (atomic_read(&mddev->pending_writes)==0)
712 break;
713 while (mddev->biolist) {
714 struct bio *bio;
715 spin_lock_irq(&mddev->write_lock);
716 bio = mddev->biolist;
717 mddev->biolist = bio->bi_next ;
718 bio->bi_next = NULL;
719 spin_unlock_irq(&mddev->write_lock);
720 submit_bio(bio->bi_rw, bio);
721 }
722 schedule();
723 }
724 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700725}
726
NeilBrown6712ecf2007-09-27 12:47:43 +0200727static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
NeilBrowna654b9d82005-06-21 17:17:27 -0700732int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 struct page *page, int rw)
734{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700735 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 struct completion event;
737 int ret;
738
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200739 rw |= REQ_SYNC | REQ_UNPLUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 bio->bi_bdev = bdev;
742 bio->bi_sector = sector;
743 bio_add_page(bio, page, size, 0);
744 init_completion(&event);
745 bio->bi_private = &event;
746 bio->bi_end_io = bi_complete;
747 submit_bio(rw, bio);
748 wait_for_completion(&event);
749
750 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
751 bio_put(bio);
752 return ret;
753}
NeilBrowna8745db2006-01-06 00:20:34 -0800754EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
NeilBrown0002b272005-09-09 16:23:53 -0700756static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 char b[BDEVNAME_SIZE];
759 if (!rdev->sb_page) {
760 MD_BUG();
761 return -EINVAL;
762 }
763 if (rdev->sb_loaded)
764 return 0;
765
766
Andre Noll0f420352008-07-11 22:02:23 +1000767 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 goto fail;
769 rdev->sb_loaded = 1;
770 return 0;
771
772fail:
773 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
774 bdevname(rdev->bdev,b));
775 return -EINVAL;
776}
777
778static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
779{
Andre Noll05710462008-07-11 22:02:20 +1000780 return sb1->set_uuid0 == sb2->set_uuid0 &&
781 sb1->set_uuid1 == sb2->set_uuid1 &&
782 sb1->set_uuid2 == sb2->set_uuid2 &&
783 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
787{
788 int ret;
789 mdp_super_t *tmp1, *tmp2;
790
791 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
792 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
793
794 if (!tmp1 || !tmp2) {
795 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100796 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 goto abort;
798 }
799
800 *tmp1 = *sb1;
801 *tmp2 = *sb2;
802
803 /*
804 * nr_disks is not constant
805 */
806 tmp1->nr_disks = 0;
807 tmp2->nr_disks = 0;
808
Andre Nollce0c8e02008-07-11 22:02:20 +1000809 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700811 kfree(tmp1);
812 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return ret;
814}
815
NeilBrown4d167f02007-05-09 02:35:37 -0700816
817static u32 md_csum_fold(u32 csum)
818{
819 csum = (csum & 0xffff) + (csum >> 16);
820 return (csum & 0xffff) + (csum >> 16);
821}
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823static unsigned int calc_sb_csum(mdp_super_t * sb)
824{
NeilBrown4d167f02007-05-09 02:35:37 -0700825 u64 newcsum = 0;
826 u32 *sb32 = (u32*)sb;
827 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 unsigned int disk_csum, csum;
829
830 disk_csum = sb->sb_csum;
831 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700832
833 for (i = 0; i < MD_SB_BYTES/4 ; i++)
834 newcsum += sb32[i];
835 csum = (newcsum & 0xffffffff) + (newcsum>>32);
836
837
838#ifdef CONFIG_ALPHA
839 /* This used to use csum_partial, which was wrong for several
840 * reasons including that different results are returned on
841 * different architectures. It isn't critical that we get exactly
842 * the same return value as before (we always csum_fold before
843 * testing, and that removes any differences). However as we
844 * know that csum_partial always returned a 16bit value on
845 * alphas, do a fold to maximise conformity to previous behaviour.
846 */
847 sb->sb_csum = md_csum_fold(disk_csum);
848#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700850#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return csum;
852}
853
854
855/*
856 * Handle superblock details.
857 * We want to be able to handle multiple superblock formats
858 * so we have a common interface to them all, and an array of
859 * different handlers.
860 * We rely on user-space to write the initial superblock, and support
861 * reading and updating of superblocks.
862 * Interface methods are:
863 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
864 * loads and validates a superblock on dev.
865 * if refdev != NULL, compare superblocks on both devices
866 * Return:
867 * 0 - dev has a superblock that is compatible with refdev
868 * 1 - dev has a superblock that is compatible and newer than refdev
869 * so dev should be used as the refdev in future
870 * -EINVAL superblock incompatible or invalid
871 * -othererror e.g. -EIO
872 *
873 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
874 * Verify that dev is acceptable into mddev.
875 * The first time, mddev->raid_disks will be 0, and data from
876 * dev should be merged in. Subsequent calls check that dev
877 * is new enough. Return 0 or -EINVAL
878 *
879 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
880 * Update the superblock for rdev with data in mddev
881 * This does not write to disc.
882 *
883 */
884
885struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000886 char *name;
887 struct module *owner;
888 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
889 int minor_version);
890 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
891 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
892 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000893 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894};
895
896/*
Andre Noll0894cc32009-06-18 08:49:23 +1000897 * Check that the given mddev has no bitmap.
898 *
899 * This function is called from the run method of all personalities that do not
900 * support bitmaps. It prints an error message and returns non-zero if mddev
901 * has a bitmap. Otherwise, it returns 0.
902 *
903 */
904int md_check_no_bitmap(mddev_t *mddev)
905{
NeilBrownc3d97142009-12-14 12:49:52 +1100906 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000907 return 0;
908 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
909 mdname(mddev), mddev->pers->name);
910 return 1;
911}
912EXPORT_SYMBOL(md_check_no_bitmap);
913
914/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 * load_super for 0.90.0
916 */
917static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
918{
919 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
920 mdp_super_t *sb;
921 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 /*
Andre Noll0f420352008-07-11 22:02:23 +1000924 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 * it's at the end of the disk.
926 *
927 * It also happens to be a multiple of 4Kb.
928 */
Andre Noll0f420352008-07-11 22:02:23 +1000929 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
NeilBrown0002b272005-09-09 16:23:53 -0700931 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (ret) return ret;
933
934 ret = -EINVAL;
935
936 bdevname(rdev->bdev, b);
937 sb = (mdp_super_t*)page_address(rdev->sb_page);
938
939 if (sb->md_magic != MD_SB_MAGIC) {
940 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
941 b);
942 goto abort;
943 }
944
945 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800946 sb->minor_version < 90 ||
947 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
949 sb->major_version, sb->minor_version,
950 b);
951 goto abort;
952 }
953
954 if (sb->raid_disks <= 0)
955 goto abort;
956
NeilBrown4d167f02007-05-09 02:35:37 -0700957 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
959 b);
960 goto abort;
961 }
962
963 rdev->preferred_minor = sb->md_minor;
964 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700965 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 if (sb->level == LEVEL_MULTIPATH)
968 rdev->desc_nr = -1;
969 else
970 rdev->desc_nr = sb->this_disk.number;
971
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700972 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700974 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 __u64 ev1, ev2;
976 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
977 if (!uuid_equal(refsb, sb)) {
978 printk(KERN_WARNING "md: %s has different UUID to %s\n",
979 b, bdevname(refdev->bdev,b2));
980 goto abort;
981 }
982 if (!sb_equal(refsb, sb)) {
983 printk(KERN_WARNING "md: %s has same UUID"
984 " but different superblock to %s\n",
985 b, bdevname(refdev->bdev, b2));
986 goto abort;
987 }
988 ev1 = md_event(sb);
989 ev2 = md_event(refsb);
990 if (ev1 > ev2)
991 ret = 1;
992 else
993 ret = 0;
994 }
NeilBrown8190e752009-06-18 08:48:58 +1000995 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Andre Nolldd8ac332009-03-31 14:33:13 +1100997 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -0800998 /* "this cannot possibly happen" ... */
999 ret = -EINVAL;
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 abort:
1002 return ret;
1003}
1004
1005/*
1006 * validate_super for 0.90.0
1007 */
1008static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1009{
1010 mdp_disk_t *desc;
1011 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001012 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
NeilBrown41158c72005-06-21 17:17:25 -07001014 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001015 clear_bit(Faulty, &rdev->flags);
1016 clear_bit(In_sync, &rdev->flags);
1017 clear_bit(WriteMostly, &rdev->flags);
1018 clear_bit(BarriersNotsupp, &rdev->flags);
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (mddev->raid_disks == 0) {
1021 mddev->major_version = 0;
1022 mddev->minor_version = sb->minor_version;
1023 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001024 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001025 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 mddev->ctime = sb->ctime;
1027 mddev->utime = sb->utime;
1028 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001029 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 mddev->layout = sb->layout;
1031 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +11001032 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001033 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001034 mddev->bitmap_info.offset = 0;
1035 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
NeilBrownf6705572006-03-27 01:18:11 -08001037 if (mddev->minor_version >= 91) {
1038 mddev->reshape_position = sb->reshape_position;
1039 mddev->delta_disks = sb->delta_disks;
1040 mddev->new_level = sb->new_level;
1041 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001042 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001043 } else {
1044 mddev->reshape_position = MaxSector;
1045 mddev->delta_disks = 0;
1046 mddev->new_level = mddev->level;
1047 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001048 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001049 }
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (sb->state & (1<<MD_SB_CLEAN))
1052 mddev->recovery_cp = MaxSector;
1053 else {
1054 if (sb->events_hi == sb->cp_events_hi &&
1055 sb->events_lo == sb->cp_events_lo) {
1056 mddev->recovery_cp = sb->recovery_cp;
1057 } else
1058 mddev->recovery_cp = 0;
1059 }
1060
1061 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1062 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1063 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1064 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1065
1066 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001067
1068 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001069 mddev->bitmap_info.file == NULL)
1070 mddev->bitmap_info.offset =
1071 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001072
NeilBrown41158c72005-06-21 17:17:25 -07001073 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001074 /* Insist on good event counter while assembling, except
1075 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001077 if (sb->disks[rdev->desc_nr].state & (
1078 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1079 if (ev1 < mddev->events)
1080 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001081 } else if (mddev->bitmap) {
1082 /* if adding to array with a bitmap, then we can accept an
1083 * older device ... but not too old.
1084 */
NeilBrown41158c72005-06-21 17:17:25 -07001085 if (ev1 < mddev->bitmap->events_cleared)
1086 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001087 } else {
1088 if (ev1 < mddev->events)
1089 /* just a hot-add of a new device, leave raid_disk at -1 */
1090 return 0;
1091 }
NeilBrown41158c72005-06-21 17:17:25 -07001092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 desc = sb->disks + rdev->desc_nr;
1095
1096 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001097 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001098 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1099 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001100 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001102 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1103 /* active but not in sync implies recovery up to
1104 * reshape position. We don't know exactly where
1105 * that is, so set to zero for now */
1106 if (mddev->minor_version >= 91) {
1107 rdev->recovery_offset = 0;
1108 rdev->raid_disk = desc->raid_disk;
1109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001111 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1112 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001113 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001114 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 return 0;
1116}
1117
1118/*
1119 * sync_super for 0.90.0
1120 */
1121static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1122{
1123 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 mdk_rdev_t *rdev2;
1125 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 /* make rdev->sb match mddev data..
1129 *
1130 * 1/ zero out disks
1131 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1132 * 3/ any empty disks < next_spare become removed
1133 *
1134 * disks[0] gets initialised to REMOVED because
1135 * we cannot be sure from other fields if it has
1136 * been initialised or not.
1137 */
1138 int i;
1139 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1140
NeilBrown61181562005-09-09 16:24:02 -07001141 rdev->sb_size = MD_SB_BYTES;
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 sb = (mdp_super_t*)page_address(rdev->sb_page);
1144
1145 memset(sb, 0, sizeof(*sb));
1146
1147 sb->md_magic = MD_SB_MAGIC;
1148 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 sb->patch_version = mddev->patch_version;
1150 sb->gvalid_words = 0; /* ignored */
1151 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1152 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1153 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1154 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1155
1156 sb->ctime = mddev->ctime;
1157 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001158 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 sb->raid_disks = mddev->raid_disks;
1160 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001161 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 sb->utime = mddev->utime;
1163 sb->state = 0;
1164 sb->events_hi = (mddev->events>>32);
1165 sb->events_lo = (u32)mddev->events;
1166
NeilBrownf6705572006-03-27 01:18:11 -08001167 if (mddev->reshape_position == MaxSector)
1168 sb->minor_version = 90;
1169 else {
1170 sb->minor_version = 91;
1171 sb->reshape_position = mddev->reshape_position;
1172 sb->new_level = mddev->new_level;
1173 sb->delta_disks = mddev->delta_disks;
1174 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001175 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001176 }
1177 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (mddev->in_sync)
1179 {
1180 sb->recovery_cp = mddev->recovery_cp;
1181 sb->cp_events_hi = (mddev->events>>32);
1182 sb->cp_events_lo = (u32)mddev->events;
1183 if (mddev->recovery_cp == MaxSector)
1184 sb->state = (1<< MD_SB_CLEAN);
1185 } else
1186 sb->recovery_cp = 0;
1187
1188 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001189 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
NeilBrownc3d97142009-12-14 12:49:52 +11001191 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001192 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001195 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001197 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001198 int is_active = test_bit(In_sync, &rdev2->flags);
1199
1200 if (rdev2->raid_disk >= 0 &&
1201 sb->minor_version >= 91)
1202 /* we have nowhere to store the recovery_offset,
1203 * but if it is not below the reshape_position,
1204 * we can piggy-back on that.
1205 */
1206 is_active = 1;
1207 if (rdev2->raid_disk < 0 ||
1208 test_bit(Faulty, &rdev2->flags))
1209 is_active = 0;
1210 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001211 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001213 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001214 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 d = &sb->disks[rdev2->desc_nr];
1216 nr_disks++;
1217 d->number = rdev2->desc_nr;
1218 d->major = MAJOR(rdev2->bdev->bd_dev);
1219 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001220 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 d->raid_disk = rdev2->raid_disk;
1222 else
1223 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001224 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001226 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001228 if (test_bit(In_sync, &rdev2->flags))
1229 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 active++;
1231 working++;
1232 } else {
1233 d->state = 0;
1234 spare++;
1235 working++;
1236 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001237 if (test_bit(WriteMostly, &rdev2->flags))
1238 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 /* now set the "removed" and "faulty" bits on any missing devices */
1241 for (i=0 ; i < mddev->raid_disks ; i++) {
1242 mdp_disk_t *d = &sb->disks[i];
1243 if (d->state == 0 && d->number == 0) {
1244 d->number = i;
1245 d->raid_disk = i;
1246 d->state = (1<<MD_DISK_REMOVED);
1247 d->state |= (1<<MD_DISK_FAULTY);
1248 failed++;
1249 }
1250 }
1251 sb->nr_disks = nr_disks;
1252 sb->active_disks = active;
1253 sb->working_disks = working;
1254 sb->failed_disks = failed;
1255 sb->spare_disks = spare;
1256
1257 sb->this_disk = sb->disks[rdev->desc_nr];
1258 sb->sb_csum = calc_sb_csum(sb);
1259}
1260
1261/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001262 * rdev_size_change for 0.90.0
1263 */
1264static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001265super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001266{
Andre Noll58c0fed2009-03-31 14:33:13 +11001267 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001268 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001269 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001270 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001271 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001272 if (!num_sectors || num_sectors > rdev->sb_start)
1273 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001274 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001275 rdev->sb_page);
1276 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001277 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001278}
1279
1280
1281/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 * version 1 superblock
1283 */
1284
NeilBrown1c05b4b2006-10-21 10:24:08 -07001285static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001287 __le32 disk_csum;
1288 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 unsigned long long newcsum;
1290 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001291 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 int i;
1293
1294 disk_csum = sb->sb_csum;
1295 sb->sb_csum = 0;
1296 newcsum = 0;
1297 for (i=0; size>=4; size -= 4 )
1298 newcsum += le32_to_cpu(*isuper++);
1299
1300 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001301 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1304 sb->sb_csum = disk_csum;
1305 return cpu_to_le32(csum);
1306}
1307
1308static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1309{
1310 struct mdp_superblock_1 *sb;
1311 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001312 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001314 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 /*
Andre Noll0f420352008-07-11 22:02:23 +10001317 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 * It is always aligned to a 4K boundary and
1319 * depeding on minor_version, it can be:
1320 * 0: At least 8K, but less than 12K, from end of device
1321 * 1: At start of device
1322 * 2: 4K from start of device.
1323 */
1324 switch(minor_version) {
1325 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001326 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1327 sb_start -= 8*2;
1328 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 break;
1330 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001331 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 break;
1333 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001334 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 break;
1336 default:
1337 return -EINVAL;
1338 }
Andre Noll0f420352008-07-11 22:02:23 +10001339 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
NeilBrown0002b272005-09-09 16:23:53 -07001341 /* superblock is rarely larger than 1K, but it can be larger,
1342 * and it is safe to read 4k, so we do that
1343 */
1344 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (ret) return ret;
1346
1347
1348 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1349
1350 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1351 sb->major_version != cpu_to_le32(1) ||
1352 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001353 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001354 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return -EINVAL;
1356
1357 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1358 printk("md: invalid superblock checksum on %s\n",
1359 bdevname(rdev->bdev,b));
1360 return -EINVAL;
1361 }
1362 if (le64_to_cpu(sb->data_size) < 10) {
1363 printk("md: data_size too small on %s\n",
1364 bdevname(rdev->bdev,b));
1365 return -EINVAL;
1366 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 rdev->preferred_minor = 0xffff;
1369 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001370 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
NeilBrown0002b272005-09-09 16:23:53 -07001372 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001373 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001374 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001375 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1376
1377 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001378 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001379 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001380
NeilBrown31b65a02006-07-10 04:44:14 -07001381 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1382 rdev->desc_nr = -1;
1383 else
1384 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1385
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001386 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001387 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001388 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 __u64 ev1, ev2;
1390 struct mdp_superblock_1 *refsb =
1391 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1392
1393 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1394 sb->level != refsb->level ||
1395 sb->layout != refsb->layout ||
1396 sb->chunksize != refsb->chunksize) {
1397 printk(KERN_WARNING "md: %s has strangely different"
1398 " superblock to %s\n",
1399 bdevname(rdev->bdev,b),
1400 bdevname(refdev->bdev,b2));
1401 return -EINVAL;
1402 }
1403 ev1 = le64_to_cpu(sb->events);
1404 ev2 = le64_to_cpu(refsb->events);
1405
1406 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001407 ret = 1;
1408 else
1409 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
NeilBrowna1801f82008-03-04 14:29:31 -08001411 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001412 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1413 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001415 rdev->sectors = rdev->sb_start;
1416 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001418 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001419 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001420 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001421 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
1424static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1425{
1426 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001427 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
NeilBrown41158c72005-06-21 17:17:25 -07001429 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001430 clear_bit(Faulty, &rdev->flags);
1431 clear_bit(In_sync, &rdev->flags);
1432 clear_bit(WriteMostly, &rdev->flags);
1433 clear_bit(BarriersNotsupp, &rdev->flags);
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (mddev->raid_disks == 0) {
1436 mddev->major_version = 1;
1437 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001438 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001439 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1441 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1442 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001443 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 mddev->layout = le32_to_cpu(sb->layout);
1445 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001446 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001447 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001448 mddev->bitmap_info.offset = 0;
1449 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1452 memcpy(mddev->uuid, sb->set_uuid, 16);
1453
1454 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001455
NeilBrown71c08052005-09-09 16:23:51 -07001456 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001457 mddev->bitmap_info.file == NULL )
1458 mddev->bitmap_info.offset =
1459 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001460
NeilBrownf6705572006-03-27 01:18:11 -08001461 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1462 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1463 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1464 mddev->new_level = le32_to_cpu(sb->new_level);
1465 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001466 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001467 } else {
1468 mddev->reshape_position = MaxSector;
1469 mddev->delta_disks = 0;
1470 mddev->new_level = mddev->level;
1471 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001472 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001473 }
1474
NeilBrown41158c72005-06-21 17:17:25 -07001475 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001476 /* Insist of good event counter while assembling, except for
1477 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001479 if (rdev->desc_nr >= 0 &&
1480 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1481 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1482 if (ev1 < mddev->events)
1483 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001484 } else if (mddev->bitmap) {
1485 /* If adding to array with a bitmap, then we can accept an
1486 * older device, but not too old.
1487 */
NeilBrown41158c72005-06-21 17:17:25 -07001488 if (ev1 < mddev->bitmap->events_cleared)
1489 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001490 } else {
1491 if (ev1 < mddev->events)
1492 /* just a hot-add of a new device, leave raid_disk at -1 */
1493 return 0;
1494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (mddev->level != LEVEL_MULTIPATH) {
1496 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001497 if (rdev->desc_nr < 0 ||
1498 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1499 role = 0xffff;
1500 rdev->desc_nr = -1;
1501 } else
1502 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 switch(role) {
1504 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 break;
1506 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001507 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 break;
1509 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001510 if ((le32_to_cpu(sb->feature_map) &
1511 MD_FEATURE_RECOVERY_OFFSET))
1512 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1513 else
1514 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 rdev->raid_disk = role;
1516 break;
1517 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001518 if (sb->devflags & WriteMostly1)
1519 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001520 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001521 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return 0;
1524}
1525
1526static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1527{
1528 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 mdk_rdev_t *rdev2;
1530 int max_dev, i;
1531 /* make rdev->sb match mddev and rdev data. */
1532
1533 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1534
1535 sb->feature_map = 0;
1536 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001537 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 memset(sb->pad1, 0, sizeof(sb->pad1));
1539 memset(sb->pad2, 0, sizeof(sb->pad2));
1540 memset(sb->pad3, 0, sizeof(sb->pad3));
1541
1542 sb->utime = cpu_to_le64((__u64)mddev->utime);
1543 sb->events = cpu_to_le64(mddev->events);
1544 if (mddev->in_sync)
1545 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1546 else
1547 sb->resync_offset = cpu_to_le64(0);
1548
NeilBrown1c05b4b2006-10-21 10:24:08 -07001549 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001550
NeilBrownf0ca3402006-02-02 14:28:04 -08001551 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001552 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001553 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001554 sb->level = cpu_to_le32(mddev->level);
1555 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001556
NeilBrownc3d97142009-12-14 12:49:52 +11001557 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1558 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001559 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001560 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001561
1562 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001563 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001564 sb->feature_map |=
1565 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1566 sb->recovery_offset =
1567 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001568 }
1569
NeilBrownf6705572006-03-27 01:18:11 -08001570 if (mddev->reshape_position != MaxSector) {
1571 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1572 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1573 sb->new_layout = cpu_to_le32(mddev->new_layout);
1574 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1575 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001576 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001577 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001580 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 if (rdev2->desc_nr+1 > max_dev)
1582 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001583
NeilBrown70471da2009-08-03 10:59:57 +10001584 if (max_dev > le32_to_cpu(sb->max_dev)) {
1585 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001586 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001587 rdev->sb_size = max_dev * 2 + 256;
1588 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1589 if (rdev->sb_size & bmask)
1590 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 for (i=0; i<max_dev;i++)
1593 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1594
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001595 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001597 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001599 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001601 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001602 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 else
1604 sb->dev_roles[i] = cpu_to_le16(0xffff);
1605 }
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 sb->sb_csum = calc_sb_1_csum(sb);
1608}
1609
Chris Webb0cd17fe2008-06-28 08:31:46 +10001610static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001611super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001612{
1613 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001614 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001615 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001616 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001617 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001618 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001619 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1620 max_sectors -= rdev->data_offset;
1621 if (!num_sectors || num_sectors > max_sectors)
1622 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001623 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001624 /* minor version 0 with bitmap we can't move */
1625 return 0;
1626 } else {
1627 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001628 sector_t sb_start;
1629 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1630 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001631 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001632 if (!num_sectors || num_sectors > max_sectors)
1633 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001634 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001635 }
1636 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001637 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001638 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001639 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001640 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001641 rdev->sb_page);
1642 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001643 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001644}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Adrian Bunk75c96f82005-05-05 16:16:09 -07001646static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 [0] = {
1648 .name = "0.90.0",
1649 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001650 .load_super = super_90_load,
1651 .validate_super = super_90_validate,
1652 .sync_super = super_90_sync,
1653 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 },
1655 [1] = {
1656 .name = "md-1",
1657 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001658 .load_super = super_1_load,
1659 .validate_super = super_1_validate,
1660 .sync_super = super_1_sync,
1661 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 },
1663};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1666{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001667 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
NeilBrown4b809912008-07-21 17:05:25 +10001669 rcu_read_lock();
1670 rdev_for_each_rcu(rdev, mddev1)
1671 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001672 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001673 rdev2->bdev->bd_contains) {
1674 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001675 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001676 }
1677 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 return 0;
1679}
1680
1681static LIST_HEAD(pending_raid_disks);
1682
Andre Nollac5e7112009-08-03 10:59:47 +10001683/*
1684 * Try to register data integrity profile for an mddev
1685 *
1686 * This is called when an array is started and after a disk has been kicked
1687 * from the array. It only succeeds if all working and active component devices
1688 * are integrity capable with matching profiles.
1689 */
1690int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001691{
Andre Nollac5e7112009-08-03 10:59:47 +10001692 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001693
Andre Nollac5e7112009-08-03 10:59:47 +10001694 if (list_empty(&mddev->disks))
1695 return 0; /* nothing to do */
1696 if (blk_get_integrity(mddev->gendisk))
1697 return 0; /* already registered */
1698 list_for_each_entry(rdev, &mddev->disks, same_set) {
1699 /* skip spares and non-functional disks */
1700 if (test_bit(Faulty, &rdev->flags))
1701 continue;
1702 if (rdev->raid_disk < 0)
1703 continue;
1704 /*
1705 * If at least one rdev is not integrity capable, we can not
1706 * enable data integrity for the md device.
1707 */
1708 if (!bdev_get_integrity(rdev->bdev))
1709 return -EINVAL;
1710 if (!reference) {
1711 /* Use the first rdev as the reference */
1712 reference = rdev;
1713 continue;
1714 }
1715 /* does this rdev's profile match the reference profile? */
1716 if (blk_integrity_compare(reference->bdev->bd_disk,
1717 rdev->bdev->bd_disk) < 0)
1718 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001719 }
Andre Nollac5e7112009-08-03 10:59:47 +10001720 /*
1721 * All component devices are integrity capable and have matching
1722 * profiles, register the common profile for the md device.
1723 */
1724 if (blk_integrity_register(mddev->gendisk,
1725 bdev_get_integrity(reference->bdev)) != 0) {
1726 printk(KERN_ERR "md: failed to register integrity for %s\n",
1727 mdname(mddev));
1728 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001729 }
Andre Nollac5e7112009-08-03 10:59:47 +10001730 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1731 mdname(mddev));
1732 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001733}
Andre Nollac5e7112009-08-03 10:59:47 +10001734EXPORT_SYMBOL(md_integrity_register);
1735
1736/* Disable data integrity if non-capable/non-matching disk is being added */
1737void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1738{
1739 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1740 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1741
1742 if (!bi_mddev) /* nothing to do */
1743 return;
1744 if (rdev->raid_disk < 0) /* skip spares */
1745 return;
1746 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1747 rdev->bdev->bd_disk) >= 0)
1748 return;
1749 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1750 blk_integrity_unregister(mddev->gendisk);
1751}
1752EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1755{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001756 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001757 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001758 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001759 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 if (rdev->mddev) {
1762 MD_BUG();
1763 return -EINVAL;
1764 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001765
1766 /* prevent duplicates */
1767 if (find_rdev(mddev, rdev->bdev->bd_dev))
1768 return -EEXIST;
1769
Andre Nolldd8ac332009-03-31 14:33:13 +11001770 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1771 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1772 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001773 if (mddev->pers) {
1774 /* Cannot change size, so fail
1775 * If mddev->level <= 0, then we don't care
1776 * about aligning sizes (e.g. linear)
1777 */
1778 if (mddev->level > 0)
1779 return -ENOSPC;
1780 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001781 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784 /* Verify rdev->desc_nr is unique.
1785 * If it is -1, assign a free number, else
1786 * check number is not in use
1787 */
1788 if (rdev->desc_nr < 0) {
1789 int choice = 0;
1790 if (mddev->pers) choice = mddev->raid_disks;
1791 while (find_rdev_nr(mddev, choice))
1792 choice++;
1793 rdev->desc_nr = choice;
1794 } else {
1795 if (find_rdev_nr(mddev, rdev->desc_nr))
1796 return -EBUSY;
1797 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001798 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1799 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1800 mdname(mddev), mddev->max_disks);
1801 return -EBUSY;
1802 }
NeilBrown19133a42005-11-08 21:39:35 -08001803 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001804 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001805 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001808 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001809
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001810 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001811 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001812
Tejun Heo0762b8b2008-08-25 19:56:12 +09001813 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001814 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1815 kobject_del(&rdev->kobj);
1816 goto fail;
1817 }
Eric W. Biederman3ff195b2010-03-30 11:31:26 -07001818 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, NULL, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11001819
NeilBrown4b809912008-07-21 17:05:25 +10001820 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001821 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001822
1823 /* May as well allow recovery to be retried once */
1824 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001827
1828 fail:
1829 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1830 b, mdname(mddev));
1831 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832}
1833
NeilBrown177a99b2008-02-06 01:39:56 -08001834static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001835{
1836 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1837 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001838 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001839}
1840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1842{
1843 char b[BDEVNAME_SIZE];
1844 if (!rdev->mddev) {
1845 MD_BUG();
1846 return;
1847 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001848 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001849 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1851 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001852 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001853 sysfs_put(rdev->sysfs_state);
1854 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001855 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001856 * writing to 'remove' to "dev/state". We also need
1857 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001858 */
NeilBrown4b809912008-07-21 17:05:25 +10001859 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001860 INIT_WORK(&rdev->del_work, md_delayed_delete);
1861 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001862 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863}
1864
1865/*
1866 * prevent the device from being mounted, repartitioned or
1867 * otherwise reused by a RAID array (or any other kernel
1868 * subsystem), by bd_claiming the device.
1869 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001870static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
1872 int err = 0;
1873 struct block_device *bdev;
1874 char b[BDEVNAME_SIZE];
1875
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001876 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 if (IS_ERR(bdev)) {
1878 printk(KERN_ERR "md: could not open %s.\n",
1879 __bdevname(dev, b));
1880 return PTR_ERR(bdev);
1881 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001882 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 if (err) {
1884 printk(KERN_ERR "md: could not bd_claim %s.\n",
1885 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001886 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 return err;
1888 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001889 if (!shared)
1890 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 rdev->bdev = bdev;
1892 return err;
1893}
1894
1895static void unlock_rdev(mdk_rdev_t *rdev)
1896{
1897 struct block_device *bdev = rdev->bdev;
1898 rdev->bdev = NULL;
1899 if (!bdev)
1900 MD_BUG();
1901 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001902 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903}
1904
1905void md_autodetect_dev(dev_t dev);
1906
1907static void export_rdev(mdk_rdev_t * rdev)
1908{
1909 char b[BDEVNAME_SIZE];
1910 printk(KERN_INFO "md: export_rdev(%s)\n",
1911 bdevname(rdev->bdev,b));
1912 if (rdev->mddev)
1913 MD_BUG();
1914 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001916 if (test_bit(AutoDetected, &rdev->flags))
1917 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918#endif
1919 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001920 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921}
1922
1923static void kick_rdev_from_array(mdk_rdev_t * rdev)
1924{
1925 unbind_rdev_from_array(rdev);
1926 export_rdev(rdev);
1927}
1928
1929static void export_array(mddev_t *mddev)
1930{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001931 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
NeilBrownd089c6a2008-02-06 01:39:59 -08001933 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (!rdev->mddev) {
1935 MD_BUG();
1936 continue;
1937 }
1938 kick_rdev_from_array(rdev);
1939 }
1940 if (!list_empty(&mddev->disks))
1941 MD_BUG();
1942 mddev->raid_disks = 0;
1943 mddev->major_version = 0;
1944}
1945
1946static void print_desc(mdp_disk_t *desc)
1947{
1948 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1949 desc->major,desc->minor,desc->raid_disk,desc->state);
1950}
1951
Cheng Renquancd2ac932009-01-09 08:31:08 +11001952static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
1954 int i;
1955
1956 printk(KERN_INFO
1957 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1958 sb->major_version, sb->minor_version, sb->patch_version,
1959 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1960 sb->ctime);
1961 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1962 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1963 sb->md_minor, sb->layout, sb->chunk_size);
1964 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1965 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1966 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1967 sb->failed_disks, sb->spare_disks,
1968 sb->sb_csum, (unsigned long)sb->events_lo);
1969
1970 printk(KERN_INFO);
1971 for (i = 0; i < MD_SB_DISKS; i++) {
1972 mdp_disk_t *desc;
1973
1974 desc = sb->disks + i;
1975 if (desc->number || desc->major || desc->minor ||
1976 desc->raid_disk || (desc->state && (desc->state != 4))) {
1977 printk(" D %2d: ", i);
1978 print_desc(desc);
1979 }
1980 }
1981 printk(KERN_INFO "md: THIS: ");
1982 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
1984
Cheng Renquancd2ac932009-01-09 08:31:08 +11001985static void print_sb_1(struct mdp_superblock_1 *sb)
1986{
1987 __u8 *uuid;
1988
1989 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001990 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08001991 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001992 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001993 le32_to_cpu(sb->major_version),
1994 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08001995 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11001996 sb->set_name,
1997 (unsigned long long)le64_to_cpu(sb->ctime)
1998 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1999
2000 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002001 printk(KERN_INFO
2002 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002003 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002004 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002005 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2006 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002007 le32_to_cpu(sb->level),
2008 (unsigned long long)le64_to_cpu(sb->size),
2009 le32_to_cpu(sb->raid_disks),
2010 le32_to_cpu(sb->layout),
2011 le32_to_cpu(sb->chunksize),
2012 (unsigned long long)le64_to_cpu(sb->data_offset),
2013 (unsigned long long)le64_to_cpu(sb->data_size),
2014 (unsigned long long)le64_to_cpu(sb->super_offset),
2015 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2016 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002017 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002018 sb->devflags,
2019 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2020 (unsigned long long)le64_to_cpu(sb->events),
2021 (unsigned long long)le64_to_cpu(sb->resync_offset),
2022 le32_to_cpu(sb->sb_csum),
2023 le32_to_cpu(sb->max_dev)
2024 );
2025}
2026
2027static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
2029 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002030 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2031 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002032 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2033 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002035 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2036 switch (major_version) {
2037 case 0:
2038 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
2039 break;
2040 case 1:
2041 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
2042 break;
2043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 } else
2045 printk(KERN_INFO "md: no rdev superblock!\n");
2046}
2047
Adrian Bunk5e563412006-06-26 00:27:42 -07002048static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002050 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 mdk_rdev_t *rdev;
2052 mddev_t *mddev;
2053 char b[BDEVNAME_SIZE];
2054
2055 printk("\n");
2056 printk("md: **********************************\n");
2057 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2058 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002059 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
NeilBrown32a76272005-06-21 17:17:14 -07002061 if (mddev->bitmap)
2062 bitmap_print_sb(mddev->bitmap);
2063 else
2064 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002065 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 printk("<%s>", bdevname(rdev->bdev,b));
2067 printk("\n");
2068
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002069 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002070 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
2072 printk("md: **********************************\n");
2073 printk("\n");
2074}
2075
2076
NeilBrown42543762006-06-26 00:27:57 -07002077static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
NeilBrown42543762006-06-26 00:27:57 -07002079 /* Update each superblock (in-memory image), but
2080 * if we are allowed to, skip spares which already
2081 * have the right event counter, or have one earlier
2082 * (which would mean they aren't being marked as dirty
2083 * with the rest of the array)
2084 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
NeilBrown5e865102009-11-12 12:08:04 +11002087 /* First make sure individual recovery_offsets are correct */
2088 list_for_each_entry(rdev, &mddev->disks, same_set) {
2089 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10002090 mddev->delta_disks >= 0 &&
NeilBrown5e865102009-11-12 12:08:04 +11002091 !test_bit(In_sync, &rdev->flags) &&
2092 mddev->curr_resync_completed > rdev->recovery_offset)
2093 rdev->recovery_offset = mddev->curr_resync_completed;
2094
2095 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002096 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002097 if (rdev->sb_events == mddev->events ||
2098 (nospares &&
2099 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002100 rdev->sb_events+1 == mddev->events)) {
2101 /* Don't update this superblock */
2102 rdev->sb_loaded = 2;
2103 } else {
2104 super_types[mddev->major_version].
2105 sync_super(mddev, rdev);
2106 rdev->sb_loaded = 1;
2107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 }
2109}
2110
NeilBrown850b2b422006-10-03 01:15:46 -07002111static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002114 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002115 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
NeilBrown1b57f132009-06-18 08:48:19 +10002117 mddev->utime = get_seconds();
NeilBrown8377bc82008-04-30 00:52:28 -07002118 if (mddev->external)
2119 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08002121 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002122
NeilBrown850b2b422006-10-03 01:15:46 -07002123 set_bit(MD_CHANGE_PENDING, &mddev->flags);
2124 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2125 force_change = 1;
2126 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2127 /* just a clean<-> dirty transition, possibly leave spares alone,
2128 * though if events isn't the right even/odd, we will have to do
2129 * spares after all
2130 */
2131 nospares = 1;
2132 if (force_change)
2133 nospares = 0;
2134 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002135 /* If the array is degraded, then skipping spares is both
2136 * dangerous and fairly pointless.
2137 * Dangerous because a device that was removed from the array
2138 * might have a event_count that still looks up-to-date,
2139 * so it can be re-added without a resync.
2140 * Pointless because if there are any spares to skip,
2141 * then a recovery will happen and soon that array won't
2142 * be degraded any more and the spare can go back to sleep then.
2143 */
NeilBrown850b2b422006-10-03 01:15:46 -07002144 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002145
NeilBrown06d91a52005-06-21 17:17:12 -07002146 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002147
2148 /* If this is just a dirty<->clean transition, and the array is clean
2149 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002150 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002151 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002152 && mddev->can_decrease_events
2153 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002154 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002155 mddev->can_decrease_events = 0;
2156 } else {
NeilBrown42543762006-06-26 00:27:57 -07002157 /* otherwise we have to go forward and ... */
2158 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002159 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002160 }
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}
Emese Revfy52cf25d2010-01-19 02:58:23 +01002688static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002689 .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{
Dan Williamsf2859af2010-05-02 10:04:16 -07002937 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002938 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002939 struct mdk_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07002940 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11002941 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002942 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002943
2944 if (mddev->pers == NULL) {
2945 if (len == 0)
2946 return 0;
2947 if (len >= sizeof(mddev->clevel))
2948 return -ENOSPC;
2949 strncpy(mddev->clevel, buf, len);
2950 if (mddev->clevel[len-1] == '\n')
2951 len--;
2952 mddev->clevel[len] = 0;
2953 mddev->level = LEVEL_NONE;
2954 return rv;
2955 }
2956
2957 /* request to change the personality. Need to ensure:
2958 * - array is not engaged in resync/recovery/reshape
2959 * - old personality can be suspended
2960 * - new personality will access other array.
2961 */
2962
2963 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownd9d166c2006-01-06 00:20:51 -08002964 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002965
2966 if (!mddev->pers->quiesce) {
2967 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2968 mdname(mddev), mddev->pers->name);
2969 return -EINVAL;
2970 }
2971
2972 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07002973 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11002974 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07002975 strncpy(clevel, buf, len);
2976 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002977 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07002978 clevel[len] = 0;
2979 if (strict_strtol(clevel, 10, &level))
2980 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11002981
Dan Williamsf2859af2010-05-02 10:04:16 -07002982 if (request_module("md-%s", clevel) != 0)
2983 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11002984 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07002985 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11002986 if (!pers || !try_module_get(pers->owner)) {
2987 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07002988 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11002989 return -EINVAL;
2990 }
2991 spin_unlock(&pers_lock);
2992
2993 if (pers == mddev->pers) {
2994 /* Nothing to do! */
2995 module_put(pers->owner);
2996 return rv;
2997 }
2998 if (!pers->takeover) {
2999 module_put(pers->owner);
3000 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003001 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003002 return -EINVAL;
3003 }
3004
NeilBrowne93f68a2010-06-15 09:36:03 +01003005 list_for_each_entry(rdev, &mddev->disks, same_set)
3006 rdev->new_raid_disk = rdev->raid_disk;
3007
NeilBrown245f46c2009-03-31 14:39:39 +11003008 /* ->takeover must set new_* and/or delta_disks
3009 * if it succeeds, and may set them when it fails.
3010 */
3011 priv = pers->takeover(mddev);
3012 if (IS_ERR(priv)) {
3013 mddev->new_level = mddev->level;
3014 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003015 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003016 mddev->raid_disks -= mddev->delta_disks;
3017 mddev->delta_disks = 0;
3018 module_put(pers->owner);
3019 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003020 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003021 return PTR_ERR(priv);
3022 }
3023
3024 /* Looks like we have a winner */
3025 mddev_suspend(mddev);
3026 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003027
3028 if (mddev->pers->sync_request == NULL &&
3029 pers->sync_request != NULL) {
3030 /* need to add the md_redundancy_group */
3031 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3032 printk(KERN_WARNING
3033 "md: cannot register extra attributes for %s\n",
3034 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003035 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003036 }
3037 if (mddev->pers->sync_request != NULL &&
3038 pers->sync_request == NULL) {
3039 /* need to remove the md_redundancy_group */
3040 if (mddev->to_remove == NULL)
3041 mddev->to_remove = &md_redundancy_group;
3042 }
3043
Trela Maciej54071b32010-03-08 16:02:42 +11003044 if (mddev->pers->sync_request == NULL &&
3045 mddev->external) {
3046 /* We are converting from a no-redundancy array
3047 * to a redundancy array and metadata is managed
3048 * externally so we need to be sure that writes
3049 * won't block due to a need to transition
3050 * clean->dirty
3051 * until external management is started.
3052 */
3053 mddev->in_sync = 0;
3054 mddev->safemode_delay = 0;
3055 mddev->safemode = 0;
3056 }
3057
NeilBrowne93f68a2010-06-15 09:36:03 +01003058 list_for_each_entry(rdev, &mddev->disks, same_set) {
3059 char nm[20];
3060 if (rdev->raid_disk < 0)
3061 continue;
3062 if (rdev->new_raid_disk > mddev->raid_disks)
3063 rdev->new_raid_disk = -1;
3064 if (rdev->new_raid_disk == rdev->raid_disk)
3065 continue;
3066 sprintf(nm, "rd%d", rdev->raid_disk);
3067 sysfs_remove_link(&mddev->kobj, nm);
3068 }
3069 list_for_each_entry(rdev, &mddev->disks, same_set) {
3070 if (rdev->raid_disk < 0)
3071 continue;
3072 if (rdev->new_raid_disk == rdev->raid_disk)
3073 continue;
3074 rdev->raid_disk = rdev->new_raid_disk;
3075 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003076 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003077 else {
3078 char nm[20];
3079 sprintf(nm, "rd%d", rdev->raid_disk);
3080 if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3081 printk("md: cannot register %s for %s after level change\n",
3082 nm, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003083 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003084 }
3085
3086 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003087 mddev->pers = pers;
3088 mddev->private = priv;
3089 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3090 mddev->level = mddev->new_level;
3091 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003092 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003093 mddev->delta_disks = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003094 if (mddev->pers->sync_request == NULL) {
3095 /* this is now an array without redundancy, so
3096 * it must always be in_sync
3097 */
3098 mddev->in_sync = 1;
3099 del_timer_sync(&mddev->safemode_timer);
3100 }
NeilBrown245f46c2009-03-31 14:39:39 +11003101 pers->run(mddev);
3102 mddev_resume(mddev);
3103 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3104 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3105 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003106 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003107 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003108 return rv;
3109}
3110
3111static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003112__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003113
NeilBrownd4dbd022006-06-26 00:27:59 -07003114
3115static ssize_t
3116layout_show(mddev_t *mddev, char *page)
3117{
3118 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003119 if (mddev->reshape_position != MaxSector &&
3120 mddev->layout != mddev->new_layout)
3121 return sprintf(page, "%d (%d)\n",
3122 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003123 return sprintf(page, "%d\n", mddev->layout);
3124}
3125
3126static ssize_t
3127layout_store(mddev_t *mddev, const char *buf, size_t len)
3128{
3129 char *e;
3130 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003131
3132 if (!*buf || (*e && *e != '\n'))
3133 return -EINVAL;
3134
NeilBrownb3546032009-03-31 14:56:41 +11003135 if (mddev->pers) {
3136 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003137 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003138 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003139 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003140 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003141 if (err) {
3142 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003143 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003144 }
NeilBrownb3546032009-03-31 14:56:41 +11003145 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003146 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003147 if (mddev->reshape_position == MaxSector)
3148 mddev->layout = n;
3149 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003150 return len;
3151}
3152static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003153__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003154
3155
NeilBrowneae17012005-11-08 21:39:23 -08003156static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003157raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003158{
NeilBrownbb636542005-11-08 21:39:45 -08003159 if (mddev->raid_disks == 0)
3160 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003161 if (mddev->reshape_position != MaxSector &&
3162 mddev->delta_disks != 0)
3163 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3164 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003165 return sprintf(page, "%d\n", mddev->raid_disks);
3166}
3167
NeilBrownda943b992006-01-06 00:20:54 -08003168static int update_raid_disks(mddev_t *mddev, int raid_disks);
3169
3170static ssize_t
3171raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3172{
NeilBrownda943b992006-01-06 00:20:54 -08003173 char *e;
3174 int rv = 0;
3175 unsigned long n = simple_strtoul(buf, &e, 10);
3176
3177 if (!*buf || (*e && *e != '\n'))
3178 return -EINVAL;
3179
3180 if (mddev->pers)
3181 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003182 else if (mddev->reshape_position != MaxSector) {
3183 int olddisks = mddev->raid_disks - mddev->delta_disks;
3184 mddev->delta_disks = n - olddisks;
3185 mddev->raid_disks = n;
3186 } else
NeilBrownda943b992006-01-06 00:20:54 -08003187 mddev->raid_disks = n;
3188 return rv ? rv : len;
3189}
3190static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003191__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003192
NeilBrown24dd4692005-11-08 21:39:26 -08003193static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003194chunk_size_show(mddev_t *mddev, char *page)
3195{
NeilBrown08a02ec2007-05-09 02:35:38 -07003196 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003197 mddev->chunk_sectors != mddev->new_chunk_sectors)
3198 return sprintf(page, "%d (%d)\n",
3199 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003200 mddev->chunk_sectors << 9);
3201 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003202}
3203
3204static ssize_t
3205chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3206{
NeilBrown3b343802006-01-06 00:20:47 -08003207 char *e;
3208 unsigned long n = simple_strtoul(buf, &e, 10);
3209
NeilBrown3b343802006-01-06 00:20:47 -08003210 if (!*buf || (*e && *e != '\n'))
3211 return -EINVAL;
3212
NeilBrownb3546032009-03-31 14:56:41 +11003213 if (mddev->pers) {
3214 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003215 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003216 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003217 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003218 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003219 if (err) {
3220 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003221 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003222 }
NeilBrownb3546032009-03-31 14:56:41 +11003223 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003224 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003225 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003226 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003227 }
NeilBrown3b343802006-01-06 00:20:47 -08003228 return len;
3229}
3230static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003231__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003232
NeilBrowna94213b2006-06-26 00:28:00 -07003233static ssize_t
3234resync_start_show(mddev_t *mddev, char *page)
3235{
NeilBrownd1a7c502009-03-31 15:24:32 +11003236 if (mddev->recovery_cp == MaxSector)
3237 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003238 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3239}
3240
3241static ssize_t
3242resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3243{
NeilBrowna94213b2006-06-26 00:28:00 -07003244 char *e;
3245 unsigned long long n = simple_strtoull(buf, &e, 10);
3246
3247 if (mddev->pers)
3248 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003249 if (cmd_match(buf, "none"))
3250 n = MaxSector;
3251 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003252 return -EINVAL;
3253
3254 mddev->recovery_cp = n;
3255 return len;
3256}
3257static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003258__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003259
NeilBrown9e653b62006-06-26 00:27:58 -07003260/*
3261 * The array state can be:
3262 *
3263 * clear
3264 * No devices, no size, no level
3265 * Equivalent to STOP_ARRAY ioctl
3266 * inactive
3267 * May have some settings, but array is not active
3268 * all IO results in error
3269 * When written, doesn't tear down array, but just stops it
3270 * suspended (not supported yet)
3271 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003272 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003273 * readonly
3274 * no resync can happen. no superblocks get written.
3275 * write requests fail
3276 * read-auto
3277 * like readonly, but behaves like 'clean' on a write request.
3278 *
3279 * clean - no pending writes, but otherwise active.
3280 * When written to inactive array, starts without resync
3281 * If a write request arrives then
3282 * if metadata is known, mark 'dirty' and switch to 'active'.
3283 * if not known, block and switch to write-pending
3284 * If written to an active array that has pending writes, then fails.
3285 * active
3286 * fully active: IO and resync can be happening.
3287 * When written to inactive array, starts with resync
3288 *
3289 * write-pending
3290 * clean, but writes are blocked waiting for 'active' to be written.
3291 *
3292 * active-idle
3293 * like active, but no writes have been seen for a while (100msec).
3294 *
3295 */
3296enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3297 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003298static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003299 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3300 "write-pending", "active-idle", NULL };
3301
3302static int match_word(const char *word, char **list)
3303{
3304 int n;
3305 for (n=0; list[n]; n++)
3306 if (cmd_match(word, list[n]))
3307 break;
3308 return n;
3309}
3310
3311static ssize_t
3312array_state_show(mddev_t *mddev, char *page)
3313{
3314 enum array_state st = inactive;
3315
3316 if (mddev->pers)
3317 switch(mddev->ro) {
3318 case 1:
3319 st = readonly;
3320 break;
3321 case 2:
3322 st = read_auto;
3323 break;
3324 case 0:
3325 if (mddev->in_sync)
3326 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08003327 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3328 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003329 else if (mddev->safemode)
3330 st = active_idle;
3331 else
3332 st = active;
3333 }
3334 else {
3335 if (list_empty(&mddev->disks) &&
3336 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003337 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003338 st = clear;
3339 else
3340 st = inactive;
3341 }
3342 return sprintf(page, "%s\n", array_states[st]);
3343}
3344
Neil Browndf5b20c2008-07-11 22:02:22 +10003345static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrowna4bd82d2010-03-29 13:23:10 +11003346static int md_set_readonly(mddev_t * mddev, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003347static int do_md_run(mddev_t * mddev);
3348static int restart_array(mddev_t *mddev);
3349
3350static ssize_t
3351array_state_store(mddev_t *mddev, const char *buf, size_t len)
3352{
3353 int err = -EINVAL;
3354 enum array_state st = match_word(buf, array_states);
3355 switch(st) {
3356 case bad_word:
3357 break;
3358 case clear:
3359 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003360 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003361 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003362 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003363 break;
3364 case inactive:
3365 /* stopping an active array */
3366 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003367 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003368 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003369 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003370 } else
3371 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003372 break;
3373 case suspended:
3374 break; /* not supported yet */
3375 case readonly:
3376 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003377 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003378 else {
3379 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003380 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003381 err = do_md_run(mddev);
3382 }
3383 break;
3384 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003385 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003386 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003387 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003388 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003389 err = restart_array(mddev);
3390 if (err == 0) {
3391 mddev->ro = 2;
3392 set_disk_ro(mddev->gendisk, 0);
3393 }
NeilBrown9e653b62006-06-26 00:27:58 -07003394 } else {
3395 mddev->ro = 2;
3396 err = do_md_run(mddev);
3397 }
3398 break;
3399 case clean:
3400 if (mddev->pers) {
3401 restart_array(mddev);
3402 spin_lock_irq(&mddev->write_lock);
3403 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003404 if (mddev->in_sync == 0) {
3405 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003406 if (mddev->safemode == 1)
3407 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003408 if (mddev->persistent)
3409 set_bit(MD_CHANGE_CLEAN,
3410 &mddev->flags);
3411 }
3412 err = 0;
3413 } else
3414 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003415 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003416 } else
3417 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003418 break;
3419 case active:
3420 if (mddev->pers) {
3421 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003422 if (mddev->external)
3423 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003424 wake_up(&mddev->sb_wait);
3425 err = 0;
3426 } else {
3427 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003428 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003429 err = do_md_run(mddev);
3430 }
3431 break;
3432 case write_pending:
3433 case active_idle:
3434 /* these cannot be set */
3435 break;
3436 }
3437 if (err)
3438 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003439 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003440 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003441 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003442 }
NeilBrown9e653b62006-06-26 00:27:58 -07003443}
NeilBrown80ca3a42006-07-10 04:44:18 -07003444static struct md_sysfs_entry md_array_state =
3445__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003446
NeilBrown6d7ff7382006-01-06 00:21:16 -08003447static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003448max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3449 return sprintf(page, "%d\n",
3450 atomic_read(&mddev->max_corr_read_errors));
3451}
3452
3453static ssize_t
3454max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3455{
3456 char *e;
3457 unsigned long n = simple_strtoul(buf, &e, 10);
3458
3459 if (*buf && (*e == 0 || *e == '\n')) {
3460 atomic_set(&mddev->max_corr_read_errors, n);
3461 return len;
3462 }
3463 return -EINVAL;
3464}
3465
3466static struct md_sysfs_entry max_corr_read_errors =
3467__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3468 max_corrected_read_errors_store);
3469
3470static ssize_t
NeilBrown6d7ff7382006-01-06 00:21:16 -08003471null_show(mddev_t *mddev, char *page)
3472{
3473 return -EINVAL;
3474}
3475
3476static ssize_t
3477new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3478{
3479 /* buf must be %d:%d\n? giving major and minor numbers */
3480 /* The new device is added to the array.
3481 * If the array has a persistent superblock, we read the
3482 * superblock to initialise info and check validity.
3483 * Otherwise, only checking done is that in bind_rdev_to_array,
3484 * which mainly checks size.
3485 */
3486 char *e;
3487 int major = simple_strtoul(buf, &e, 10);
3488 int minor;
3489 dev_t dev;
3490 mdk_rdev_t *rdev;
3491 int err;
3492
3493 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3494 return -EINVAL;
3495 minor = simple_strtoul(e+1, &e, 10);
3496 if (*e && *e != '\n')
3497 return -EINVAL;
3498 dev = MKDEV(major, minor);
3499 if (major != MAJOR(dev) ||
3500 minor != MINOR(dev))
3501 return -EOVERFLOW;
3502
3503
3504 if (mddev->persistent) {
3505 rdev = md_import_device(dev, mddev->major_version,
3506 mddev->minor_version);
3507 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3508 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3509 mdk_rdev_t, same_set);
3510 err = super_types[mddev->major_version]
3511 .load_super(rdev, rdev0, mddev->minor_version);
3512 if (err < 0)
3513 goto out;
3514 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003515 } else if (mddev->external)
3516 rdev = md_import_device(dev, -2, -1);
3517 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003518 rdev = md_import_device(dev, -1, -1);
3519
3520 if (IS_ERR(rdev))
3521 return PTR_ERR(rdev);
3522 err = bind_rdev_to_array(rdev, mddev);
3523 out:
3524 if (err)
3525 export_rdev(rdev);
3526 return err ? err : len;
3527}
3528
3529static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003530__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003531
3532static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003533bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3534{
3535 char *end;
3536 unsigned long chunk, end_chunk;
3537
3538 if (!mddev->bitmap)
3539 goto out;
3540 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3541 while (*buf) {
3542 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3543 if (buf == end) break;
3544 if (*end == '-') { /* range */
3545 buf = end + 1;
3546 end_chunk = simple_strtoul(buf, &end, 0);
3547 if (buf == end) break;
3548 }
3549 if (*end && !isspace(*end)) break;
3550 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003551 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003552 }
3553 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3554out:
3555 return len;
3556}
3557
3558static struct md_sysfs_entry md_bitmap =
3559__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3560
3561static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003562size_show(mddev_t *mddev, char *page)
3563{
Andre Noll58c0fed2009-03-31 14:33:13 +11003564 return sprintf(page, "%llu\n",
3565 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003566}
3567
Andre Nolld71f9f82008-07-11 22:02:22 +10003568static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003569
3570static ssize_t
3571size_store(mddev_t *mddev, const char *buf, size_t len)
3572{
3573 /* If array is inactive, we can reduce the component size, but
3574 * not increase it (except from 0).
3575 * If array is active, we can try an on-line resize
3576 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003577 sector_t sectors;
3578 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003579
Andre Noll58c0fed2009-03-31 14:33:13 +11003580 if (err < 0)
3581 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003582 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003583 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07003584 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003585 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003586 if (mddev->dev_sectors == 0 ||
3587 mddev->dev_sectors > sectors)
3588 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003589 else
3590 err = -ENOSPC;
3591 }
3592 return err ? err : len;
3593}
3594
3595static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003596__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003597
NeilBrown8bb93aa2006-01-06 00:20:50 -08003598
3599/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003600 * This is one of
3601 * 'none' for arrays with no metadata (good luck...)
3602 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003603 * or N.M for internally known formats
3604 */
3605static ssize_t
3606metadata_show(mddev_t *mddev, char *page)
3607{
3608 if (mddev->persistent)
3609 return sprintf(page, "%d.%d\n",
3610 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003611 else if (mddev->external)
3612 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003613 else
3614 return sprintf(page, "none\n");
3615}
3616
3617static ssize_t
3618metadata_store(mddev_t *mddev, const char *buf, size_t len)
3619{
3620 int major, minor;
3621 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003622 /* Changing the details of 'external' metadata is
3623 * always permitted. Otherwise there must be
3624 * no devices attached to the array.
3625 */
3626 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3627 ;
3628 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003629 return -EBUSY;
3630
3631 if (cmd_match(buf, "none")) {
3632 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003633 mddev->external = 0;
3634 mddev->major_version = 0;
3635 mddev->minor_version = 90;
3636 return len;
3637 }
3638 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003639 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003640 if (namelen >= sizeof(mddev->metadata_type))
3641 namelen = sizeof(mddev->metadata_type)-1;
3642 strncpy(mddev->metadata_type, buf+9, namelen);
3643 mddev->metadata_type[namelen] = 0;
3644 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3645 mddev->metadata_type[--namelen] = 0;
3646 mddev->persistent = 0;
3647 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003648 mddev->major_version = 0;
3649 mddev->minor_version = 90;
3650 return len;
3651 }
3652 major = simple_strtoul(buf, &e, 10);
3653 if (e==buf || *e != '.')
3654 return -EINVAL;
3655 buf = e+1;
3656 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003657 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003658 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003659 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003660 return -ENOENT;
3661 mddev->major_version = major;
3662 mddev->minor_version = minor;
3663 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003664 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003665 return len;
3666}
3667
3668static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003669__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003670
NeilBrowna35b0d62006-01-06 00:20:49 -08003671static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003672action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003673{
NeilBrown7eec3142005-11-08 21:39:44 -08003674 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003675 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3676 type = "frozen";
3677 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003678 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003679 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3680 type = "reshape";
3681 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003682 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3683 type = "resync";
3684 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3685 type = "check";
3686 else
3687 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003688 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003689 type = "recover";
3690 }
3691 return sprintf(page, "%s\n", type);
3692}
3693
3694static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003695action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003696{
NeilBrown7eec3142005-11-08 21:39:44 -08003697 if (!mddev->pers || !mddev->pers->sync_request)
3698 return -EINVAL;
3699
NeilBrownb6a9ce62009-05-26 09:41:17 +10003700 if (cmd_match(page, "frozen"))
3701 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3702 else
3703 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3704
3705 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003706 if (mddev->sync_thread) {
3707 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3708 md_unregister_thread(mddev->sync_thread);
3709 mddev->sync_thread = NULL;
3710 mddev->recovery = 0;
3711 }
NeilBrown03c902e2006-01-06 00:20:46 -08003712 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3713 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003714 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003715 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003716 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003717 else if (cmd_match(page, "recover")) {
3718 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3719 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3720 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003721 int err;
3722 if (mddev->pers->start_reshape == NULL)
3723 return -EINVAL;
3724 err = mddev->pers->start_reshape(mddev);
3725 if (err)
3726 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003727 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003728 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003729 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003730 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003731 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003732 return -EINVAL;
3733 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3734 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003735 }
NeilBrown03c902e2006-01-06 00:20:46 -08003736 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003737 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003738 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003739 return len;
3740}
3741
NeilBrown9d888832005-11-08 21:39:26 -08003742static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003743mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003744{
3745 return sprintf(page, "%llu\n",
3746 (unsigned long long) mddev->resync_mismatches);
3747}
3748
NeilBrown80ca3a42006-07-10 04:44:18 -07003749static struct md_sysfs_entry md_scan_mode =
3750__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003751
NeilBrown96de1e62005-11-08 21:39:39 -08003752
NeilBrown80ca3a42006-07-10 04:44:18 -07003753static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003754
NeilBrown88202a02006-01-06 00:21:36 -08003755static ssize_t
3756sync_min_show(mddev_t *mddev, char *page)
3757{
3758 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3759 mddev->sync_speed_min ? "local": "system");
3760}
3761
3762static ssize_t
3763sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3764{
3765 int min;
3766 char *e;
3767 if (strncmp(buf, "system", 6)==0) {
3768 mddev->sync_speed_min = 0;
3769 return len;
3770 }
3771 min = simple_strtoul(buf, &e, 10);
3772 if (buf == e || (*e && *e != '\n') || min <= 0)
3773 return -EINVAL;
3774 mddev->sync_speed_min = min;
3775 return len;
3776}
3777
3778static struct md_sysfs_entry md_sync_min =
3779__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3780
3781static ssize_t
3782sync_max_show(mddev_t *mddev, char *page)
3783{
3784 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3785 mddev->sync_speed_max ? "local": "system");
3786}
3787
3788static ssize_t
3789sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3790{
3791 int max;
3792 char *e;
3793 if (strncmp(buf, "system", 6)==0) {
3794 mddev->sync_speed_max = 0;
3795 return len;
3796 }
3797 max = simple_strtoul(buf, &e, 10);
3798 if (buf == e || (*e && *e != '\n') || max <= 0)
3799 return -EINVAL;
3800 mddev->sync_speed_max = max;
3801 return len;
3802}
3803
3804static struct md_sysfs_entry md_sync_max =
3805__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3806
Iustin Popd7f3d292007-10-16 23:30:54 -07003807static ssize_t
3808degraded_show(mddev_t *mddev, char *page)
3809{
3810 return sprintf(page, "%d\n", mddev->degraded);
3811}
3812static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003813
3814static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003815sync_force_parallel_show(mddev_t *mddev, char *page)
3816{
3817 return sprintf(page, "%d\n", mddev->parallel_resync);
3818}
3819
3820static ssize_t
3821sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3822{
3823 long n;
3824
3825 if (strict_strtol(buf, 10, &n))
3826 return -EINVAL;
3827
3828 if (n != 0 && n != 1)
3829 return -EINVAL;
3830
3831 mddev->parallel_resync = n;
3832
3833 if (mddev->sync_thread)
3834 wake_up(&resync_wait);
3835
3836 return len;
3837}
3838
3839/* force parallel resync, even with shared block devices */
3840static struct md_sysfs_entry md_sync_force_parallel =
3841__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3842 sync_force_parallel_show, sync_force_parallel_store);
3843
3844static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003845sync_speed_show(mddev_t *mddev, char *page)
3846{
3847 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003848 if (mddev->curr_resync == 0)
3849 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003850 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3851 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003852 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003853 db = resync - mddev->resync_mark_cnt;
3854 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003855}
3856
NeilBrown80ca3a42006-07-10 04:44:18 -07003857static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003858
3859static ssize_t
3860sync_completed_show(mddev_t *mddev, char *page)
3861{
Andre Noll58c0fed2009-03-31 14:33:13 +11003862 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003863
NeilBrownacb180b2009-04-14 16:28:34 +10003864 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3865 return sprintf(page, "none\n");
3866
NeilBrown88202a02006-01-06 00:21:36 -08003867 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003868 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003869 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003870 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003871
NeilBrownacb180b2009-04-14 16:28:34 +10003872 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003873 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003874}
3875
NeilBrown80ca3a42006-07-10 04:44:18 -07003876static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003877
NeilBrowne464eaf2006-03-27 01:18:14 -08003878static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003879min_sync_show(mddev_t *mddev, char *page)
3880{
3881 return sprintf(page, "%llu\n",
3882 (unsigned long long)mddev->resync_min);
3883}
3884static ssize_t
3885min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3886{
3887 unsigned long long min;
3888 if (strict_strtoull(buf, 10, &min))
3889 return -EINVAL;
3890 if (min > mddev->resync_max)
3891 return -EINVAL;
3892 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3893 return -EBUSY;
3894
3895 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003896 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003897 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003898 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003899 return -EINVAL;
3900 }
3901 mddev->resync_min = min;
3902
3903 return len;
3904}
3905
3906static struct md_sysfs_entry md_min_sync =
3907__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3908
3909static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003910max_sync_show(mddev_t *mddev, char *page)
3911{
3912 if (mddev->resync_max == MaxSector)
3913 return sprintf(page, "max\n");
3914 else
3915 return sprintf(page, "%llu\n",
3916 (unsigned long long)mddev->resync_max);
3917}
3918static ssize_t
3919max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3920{
3921 if (strncmp(buf, "max", 3) == 0)
3922 mddev->resync_max = MaxSector;
3923 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003924 unsigned long long max;
3925 if (strict_strtoull(buf, 10, &max))
3926 return -EINVAL;
3927 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003928 return -EINVAL;
3929 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003930 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003931 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3932 return -EBUSY;
3933
3934 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003935 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003936 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003937 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003938 return -EINVAL;
3939 }
3940 mddev->resync_max = max;
3941 }
3942 wake_up(&mddev->recovery_wait);
3943 return len;
3944}
3945
3946static struct md_sysfs_entry md_max_sync =
3947__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3948
3949static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003950suspend_lo_show(mddev_t *mddev, char *page)
3951{
3952 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3953}
3954
3955static ssize_t
3956suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3957{
3958 char *e;
3959 unsigned long long new = simple_strtoull(buf, &e, 10);
3960
NeilBrownb8d966e2009-07-01 11:14:04 +10003961 if (mddev->pers == NULL ||
3962 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003963 return -EINVAL;
3964 if (buf == e || (*e && *e != '\n'))
3965 return -EINVAL;
3966 if (new >= mddev->suspend_hi ||
3967 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3968 mddev->suspend_lo = new;
3969 mddev->pers->quiesce(mddev, 2);
3970 return len;
3971 } else
3972 return -EINVAL;
3973}
3974static struct md_sysfs_entry md_suspend_lo =
3975__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3976
3977
3978static ssize_t
3979suspend_hi_show(mddev_t *mddev, char *page)
3980{
3981 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3982}
3983
3984static ssize_t
3985suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3986{
3987 char *e;
3988 unsigned long long new = simple_strtoull(buf, &e, 10);
3989
NeilBrownb8d966e2009-07-01 11:14:04 +10003990 if (mddev->pers == NULL ||
3991 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003992 return -EINVAL;
3993 if (buf == e || (*e && *e != '\n'))
3994 return -EINVAL;
3995 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3996 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3997 mddev->suspend_hi = new;
3998 mddev->pers->quiesce(mddev, 1);
3999 mddev->pers->quiesce(mddev, 0);
4000 return len;
4001 } else
4002 return -EINVAL;
4003}
4004static struct md_sysfs_entry md_suspend_hi =
4005__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4006
NeilBrown08a02ec2007-05-09 02:35:38 -07004007static ssize_t
4008reshape_position_show(mddev_t *mddev, char *page)
4009{
4010 if (mddev->reshape_position != MaxSector)
4011 return sprintf(page, "%llu\n",
4012 (unsigned long long)mddev->reshape_position);
4013 strcpy(page, "none\n");
4014 return 5;
4015}
4016
4017static ssize_t
4018reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
4019{
4020 char *e;
4021 unsigned long long new = simple_strtoull(buf, &e, 10);
4022 if (mddev->pers)
4023 return -EBUSY;
4024 if (buf == e || (*e && *e != '\n'))
4025 return -EINVAL;
4026 mddev->reshape_position = new;
4027 mddev->delta_disks = 0;
4028 mddev->new_level = mddev->level;
4029 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004030 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004031 return len;
4032}
4033
4034static struct md_sysfs_entry md_reshape_position =
4035__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4036 reshape_position_store);
4037
Dan Williamsb522adc2009-03-31 15:00:31 +11004038static ssize_t
4039array_size_show(mddev_t *mddev, char *page)
4040{
4041 if (mddev->external_size)
4042 return sprintf(page, "%llu\n",
4043 (unsigned long long)mddev->array_sectors/2);
4044 else
4045 return sprintf(page, "default\n");
4046}
4047
4048static ssize_t
4049array_size_store(mddev_t *mddev, const char *buf, size_t len)
4050{
4051 sector_t sectors;
4052
4053 if (strncmp(buf, "default", 7) == 0) {
4054 if (mddev->pers)
4055 sectors = mddev->pers->size(mddev, 0, 0);
4056 else
4057 sectors = mddev->array_sectors;
4058
4059 mddev->external_size = 0;
4060 } else {
4061 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4062 return -EINVAL;
4063 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004064 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004065
4066 mddev->external_size = 1;
4067 }
4068
4069 mddev->array_sectors = sectors;
4070 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004071 if (mddev->pers)
4072 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11004073
4074 return len;
4075}
4076
4077static struct md_sysfs_entry md_array_size =
4078__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4079 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004080
NeilBrowneae17012005-11-08 21:39:23 -08004081static struct attribute *md_default_attrs[] = {
4082 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004083 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004084 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004085 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004086 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004087 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004088 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004089 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004090 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004091 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004092 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004093 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004094 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004095 NULL,
4096};
4097
4098static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004099 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004100 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004101 &md_sync_min.attr,
4102 &md_sync_max.attr,
4103 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004104 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004105 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004106 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004107 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004108 &md_suspend_lo.attr,
4109 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004110 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004111 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004112 NULL,
4113};
NeilBrown411036f2005-11-08 21:39:40 -08004114static struct attribute_group md_redundancy_group = {
4115 .name = NULL,
4116 .attrs = md_redundancy_attrs,
4117};
4118
NeilBrowneae17012005-11-08 21:39:23 -08004119
4120static ssize_t
4121md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4122{
4123 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4124 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004125 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004126
4127 if (!entry->show)
4128 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004129 rv = mddev_lock(mddev);
4130 if (!rv) {
4131 rv = entry->show(mddev, page);
4132 mddev_unlock(mddev);
4133 }
NeilBrown96de1e62005-11-08 21:39:39 -08004134 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004135}
4136
4137static ssize_t
4138md_attr_store(struct kobject *kobj, struct attribute *attr,
4139 const char *page, size_t length)
4140{
4141 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4142 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004143 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004144
4145 if (!entry->store)
4146 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004147 if (!capable(CAP_SYS_ADMIN))
4148 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004149 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004150 if (mddev->hold_active == UNTIL_IOCTL)
4151 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004152 if (!rv) {
4153 rv = entry->store(mddev, page, length);
4154 mddev_unlock(mddev);
4155 }
NeilBrown96de1e62005-11-08 21:39:39 -08004156 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004157}
4158
4159static void md_free(struct kobject *ko)
4160{
4161 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004162
4163 if (mddev->sysfs_state)
4164 sysfs_put(mddev->sysfs_state);
4165
4166 if (mddev->gendisk) {
4167 del_gendisk(mddev->gendisk);
4168 put_disk(mddev->gendisk);
4169 }
4170 if (mddev->queue)
4171 blk_cleanup_queue(mddev->queue);
4172
NeilBrowneae17012005-11-08 21:39:23 -08004173 kfree(mddev);
4174}
4175
Emese Revfy52cf25d2010-01-19 02:58:23 +01004176static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004177 .show = md_attr_show,
4178 .store = md_attr_store,
4179};
4180static struct kobj_type md_ktype = {
4181 .release = md_free,
4182 .sysfs_ops = &md_sysfs_ops,
4183 .default_attrs = md_default_attrs,
4184};
4185
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186int mdp_major = 0;
4187
Dan Williams5fd3a172009-03-04 00:57:25 -07004188static void mddev_delayed_delete(struct work_struct *ws)
4189{
4190 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4191
NeilBrown43a70502009-12-14 12:49:55 +11004192 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004193 kobject_del(&mddev->kobj);
4194 kobject_put(&mddev->kobj);
4195}
4196
NeilBrownefeb53c2009-01-09 08:31:10 +11004197static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004199 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 mddev_t *mddev = mddev_find(dev);
4201 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004202 int partitioned;
4203 int shift;
4204 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004205 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206
4207 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004208 return -ENODEV;
4209
4210 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4211 shift = partitioned ? MdpMinorShift : 0;
4212 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
NeilBrownd3374822009-01-09 08:31:10 +11004214 /* wait for any previous instance if this device
4215 * to be completed removed (mddev_delayed_delete).
4216 */
4217 flush_scheduled_work();
4218
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004219 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004220 error = -EEXIST;
4221 if (mddev->gendisk)
4222 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004223
4224 if (name) {
4225 /* Need to ensure that 'name' is not a duplicate.
4226 */
4227 mddev_t *mddev2;
4228 spin_lock(&all_mddevs_lock);
4229
4230 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4231 if (mddev2->gendisk &&
4232 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4233 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004234 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004235 }
4236 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 }
NeilBrown8b765392009-01-09 08:31:08 +11004238
NeilBrown0909dc42009-07-01 12:27:21 +10004239 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004240 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004241 if (!mddev->queue)
4242 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004243 mddev->queue->queuedata = mddev;
4244
NeilBrown8b765392009-01-09 08:31:08 +11004245 /* Can be unlocked because the queue is new: no concurrency */
4246 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4247
NeilBrown409c57f2009-03-31 14:39:39 +11004248 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004249
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 disk = alloc_disk(1 << shift);
4251 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004252 blk_cleanup_queue(mddev->queue);
4253 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004254 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004256 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004258 if (name)
4259 strcpy(disk->disk_name, name);
4260 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004262 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 disk->fops = &md_fops;
4265 disk->private_data = mddev;
4266 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004267 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004268 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004269 * remove it now.
4270 */
4271 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 add_disk(disk);
4273 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004274 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4275 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004276 if (error) {
4277 /* This isn't possible, but as kobject_init_and_add is marked
4278 * __must_check, we must do something with the result
4279 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004280 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4281 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004282 error = 0;
4283 }
NeilBrown43a70502009-12-14 12:49:55 +11004284 if (sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4285 printk(KERN_DEBUG "pointless warning\n");
NeilBrown0909dc42009-07-01 12:27:21 +10004286 abort:
4287 mutex_unlock(&disks_mutex);
4288 if (!error) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004289 kobject_uevent(&mddev->kobj, KOBJ_ADD);
Eric W. Biederman3ff195b2010-03-30 11:31:26 -07004290 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, NULL, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004291 }
NeilBrownd3374822009-01-09 08:31:10 +11004292 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004293 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004294}
4295
4296static struct kobject *md_probe(dev_t dev, int *part, void *data)
4297{
4298 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 return NULL;
4300}
4301
NeilBrownefeb53c2009-01-09 08:31:10 +11004302static int add_named_array(const char *val, struct kernel_param *kp)
4303{
4304 /* val must be "md_*" where * is not all digits.
4305 * We allocate an array with a large free minor number, and
4306 * set the name to val. val must not already be an active name.
4307 */
4308 int len = strlen(val);
4309 char buf[DISK_NAME_LEN];
4310
4311 while (len && val[len-1] == '\n')
4312 len--;
4313 if (len >= DISK_NAME_LEN)
4314 return -E2BIG;
4315 strlcpy(buf, val, len+1);
4316 if (strncmp(buf, "md_", 3) != 0)
4317 return -EINVAL;
4318 return md_alloc(0, buf);
4319}
4320
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321static void md_safemode_timeout(unsigned long data)
4322{
4323 mddev_t *mddev = (mddev_t *) data;
4324
Neil Brown0fd62b82008-06-28 08:31:36 +10004325 if (!atomic_read(&mddev->writes_pending)) {
4326 mddev->safemode = 1;
4327 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11004328 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 md_wakeup_thread(mddev->thread);
4331}
4332
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004333static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
NeilBrownfe60b012010-03-29 11:10:42 +11004335static int md_run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336{
NeilBrown2604b702006-01-06 00:20:36 -08004337 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 mdk_rdev_t *rdev;
NeilBrown2604b702006-01-06 00:20:36 -08004339 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340
NeilBrowna757e642005-04-16 15:26:42 -07004341 if (list_empty(&mddev->disks))
4342 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344
4345 if (mddev->pers)
4346 return -EBUSY;
4347
NeilBrownb6eb1272010-04-15 10:13:47 +10004348 /* These two calls synchronise us with the
4349 * sysfs_remove_group calls in mddev_unlock,
4350 * so they must have completed.
4351 */
4352 mutex_lock(&mddev->open_mutex);
4353 mutex_unlock(&mddev->open_mutex);
4354
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 /*
4356 * Analyze all RAID superblock(s)
4357 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004358 if (!mddev->raid_disks) {
4359 if (!mddev->persistent)
4360 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004361 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
NeilBrownd9d166c2006-01-06 00:20:51 -08004364 if (mddev->level != LEVEL_NONE)
4365 request_module("md-level-%d", mddev->level);
4366 else if (mddev->clevel[0])
4367 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368
4369 /*
4370 * Drop all container device buffers, from now on
4371 * the only valid external interface is through the md
4372 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004374 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004375 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 continue;
4377 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004378 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004379
4380 /* perform some consistency tests on the device.
4381 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004382 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004383 */
Andre Noll0f420352008-07-11 22:02:23 +10004384 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004385 if (mddev->dev_sectors &&
4386 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004387 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004388 printk("md: %s: data overlaps metadata\n",
4389 mdname(mddev));
4390 return -EINVAL;
4391 }
4392 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004393 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004394 > rdev->data_offset) {
4395 printk("md: %s: metadata overlaps data\n",
4396 mdname(mddev));
4397 return -EINVAL;
4398 }
4399 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004400 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 }
4402
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004404 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004405 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004407 if (mddev->level != LEVEL_NONE)
4408 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4409 mddev->level);
4410 else
4411 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4412 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 return -EINVAL;
4414 }
NeilBrown2604b702006-01-06 00:20:36 -08004415 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004417 if (mddev->level != pers->level) {
4418 mddev->level = pers->level;
4419 mddev->new_level = pers->level;
4420 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004421 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
NeilBrownf6705572006-03-27 01:18:11 -08004423 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004424 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004425 /* This personality cannot handle reshaping... */
4426 mddev->pers = NULL;
4427 module_put(pers->owner);
4428 return -EINVAL;
4429 }
4430
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004431 if (pers->sync_request) {
4432 /* Warn if this is a potentially silly
4433 * configuration.
4434 */
4435 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4436 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004437 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004438
4439 list_for_each_entry(rdev, &mddev->disks, same_set)
4440 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004441 if (rdev < rdev2 &&
4442 rdev->bdev->bd_contains ==
4443 rdev2->bdev->bd_contains) {
4444 printk(KERN_WARNING
4445 "%s: WARNING: %s appears to be"
4446 " on the same physical disk as"
4447 " %s.\n",
4448 mdname(mddev),
4449 bdevname(rdev->bdev,b),
4450 bdevname(rdev2->bdev,b2));
4451 warned = 1;
4452 }
4453 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004454
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004455 if (warned)
4456 printk(KERN_WARNING
4457 "True protection against single-disk"
4458 " failure might be compromised.\n");
4459 }
4460
NeilBrown657390d2005-08-26 18:34:16 -07004461 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004462 /* may be over-ridden by personality */
4463 mddev->resync_max_sectors = mddev->dev_sectors;
4464
NeilBrowna9701a32005-11-08 21:39:34 -08004465 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004466 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467
NeilBrown0f9552b52009-12-30 12:08:50 +11004468 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004469 mddev->ro = 2; /* read-only, but switch on first write */
4470
NeilBrownb15c2e52006-01-06 00:20:16 -08004471 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004472 if (err)
4473 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004474 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4475 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4476 " but 'external_size' not in effect?\n", __func__);
4477 printk(KERN_ERR
4478 "md: invalid array_size %llu > default size %llu\n",
4479 (unsigned long long)mddev->array_sectors / 2,
4480 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4481 err = -EINVAL;
4482 mddev->pers->stop(mddev);
4483 }
4484 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004485 err = bitmap_create(mddev);
4486 if (err) {
4487 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4488 mdname(mddev), err);
4489 mddev->pers->stop(mddev);
4490 }
4491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 module_put(mddev->pers->owner);
4494 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004495 bitmap_destroy(mddev);
4496 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004498 if (mddev->pers->sync_request) {
4499 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4500 printk(KERN_WARNING
4501 "md: cannot register extra attributes for %s\n",
4502 mdname(mddev));
Eric W. Biederman3ff195b2010-03-30 11:31:26 -07004503 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004504 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004505 mddev->ro = 0;
4506
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004508 atomic_set(&mddev->max_corr_read_errors,
4509 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 mddev->safemode = 0;
4511 mddev->safemode_timer.function = md_safemode_timeout;
4512 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004513 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004515
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004516 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004517 if (rdev->raid_disk >= 0) {
4518 char nm[20];
4519 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004520 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4521 printk("md: cannot register %s for %s\n",
4522 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524
4525 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4526
NeilBrown850b2b422006-10-03 01:15:46 -07004527 if (mddev->flags)
4528 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
NeilBrown0b8c9de2006-07-10 04:44:15 -07004530 md_wakeup_thread(mddev->thread);
4531 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004532
NeilBrownd7603b72006-01-06 00:20:30 -08004533 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004534 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004535 if (mddev->sysfs_action)
4536 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004537 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 return 0;
4539}
4540
NeilBrownfe60b012010-03-29 11:10:42 +11004541static int do_md_run(mddev_t *mddev)
4542{
4543 int err;
4544
4545 err = md_run(mddev);
4546 if (err)
4547 goto out;
4548
4549 set_capacity(mddev->gendisk, mddev->array_sectors);
4550 revalidate_disk(mddev->gendisk);
4551 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4552out:
4553 return err;
4554}
4555
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556static int restart_array(mddev_t *mddev)
4557{
4558 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
Andre Noll80fab1d2008-07-11 22:02:21 +10004560 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004562 return -ENXIO;
4563 if (!mddev->pers)
4564 return -EINVAL;
4565 if (!mddev->ro)
4566 return -EBUSY;
4567 mddev->safemode = 0;
4568 mddev->ro = 0;
4569 set_disk_ro(disk, 0);
4570 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4571 mdname(mddev));
4572 /* Kick recovery or resync if necessary */
4573 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4574 md_wakeup_thread(mddev->thread);
4575 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004576 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004577 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578}
4579
NeilBrownacc55e22006-06-26 00:27:47 -07004580/* similar to deny_write_access, but accounts for our holding a reference
4581 * to the file ourselves */
4582static int deny_bitmap_write_access(struct file * file)
4583{
4584 struct inode *inode = file->f_mapping->host;
4585
4586 spin_lock(&inode->i_lock);
4587 if (atomic_read(&inode->i_writecount) > 1) {
4588 spin_unlock(&inode->i_lock);
4589 return -ETXTBSY;
4590 }
4591 atomic_set(&inode->i_writecount, -1);
4592 spin_unlock(&inode->i_lock);
4593
4594 return 0;
4595}
4596
NeilBrown43a70502009-12-14 12:49:55 +11004597void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004598{
4599 struct inode *inode = file->f_mapping->host;
4600
4601 spin_lock(&inode->i_lock);
4602 atomic_set(&inode->i_writecount, 1);
4603 spin_unlock(&inode->i_lock);
4604}
4605
NeilBrown6177b472010-03-29 11:37:13 +11004606static void md_clean(mddev_t *mddev)
4607{
4608 mddev->array_sectors = 0;
4609 mddev->external_size = 0;
4610 mddev->dev_sectors = 0;
4611 mddev->raid_disks = 0;
4612 mddev->recovery_cp = 0;
4613 mddev->resync_min = 0;
4614 mddev->resync_max = MaxSector;
4615 mddev->reshape_position = MaxSector;
4616 mddev->external = 0;
4617 mddev->persistent = 0;
4618 mddev->level = LEVEL_NONE;
4619 mddev->clevel[0] = 0;
4620 mddev->flags = 0;
4621 mddev->ro = 0;
4622 mddev->metadata_type[0] = 0;
4623 mddev->chunk_sectors = 0;
4624 mddev->ctime = mddev->utime = 0;
4625 mddev->layout = 0;
4626 mddev->max_disks = 0;
4627 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10004628 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004629 mddev->delta_disks = 0;
4630 mddev->new_level = LEVEL_NONE;
4631 mddev->new_layout = 0;
4632 mddev->new_chunk_sectors = 0;
4633 mddev->curr_resync = 0;
4634 mddev->resync_mismatches = 0;
4635 mddev->suspend_lo = mddev->suspend_hi = 0;
4636 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4637 mddev->recovery = 0;
4638 mddev->in_sync = 0;
4639 mddev->degraded = 0;
4640 mddev->barriers_work = 0;
4641 mddev->safemode = 0;
4642 mddev->bitmap_info.offset = 0;
4643 mddev->bitmap_info.default_offset = 0;
4644 mddev->bitmap_info.chunksize = 0;
4645 mddev->bitmap_info.daemon_sleep = 0;
4646 mddev->bitmap_info.max_write_behind = 0;
4647}
4648
NeilBrowna047e122010-03-29 12:07:53 +11004649static void md_stop_writes(mddev_t *mddev)
4650{
4651 if (mddev->sync_thread) {
4652 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4653 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4654 md_unregister_thread(mddev->sync_thread);
4655 mddev->sync_thread = NULL;
4656 }
4657
4658 del_timer_sync(&mddev->safemode_timer);
4659
4660 bitmap_flush(mddev);
4661 md_super_wait(mddev);
4662
4663 if (!mddev->in_sync || mddev->flags) {
4664 /* mark array as shutdown cleanly */
4665 mddev->in_sync = 1;
4666 md_update_sb(mddev, 1);
4667 }
4668}
4669
NeilBrown6177b472010-03-29 11:37:13 +11004670static void md_stop(mddev_t *mddev)
4671{
NeilBrowncca9cf92010-04-01 12:08:16 +11004672 md_stop_writes(mddev);
4673
NeilBrown6177b472010-03-29 11:37:13 +11004674 mddev->pers->stop(mddev);
4675 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4676 mddev->to_remove = &md_redundancy_group;
4677 module_put(mddev->pers->owner);
4678 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11004679 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11004680}
4681
NeilBrowna4bd82d2010-03-29 13:23:10 +11004682static int md_set_readonly(mddev_t *mddev, int is_open)
4683{
4684 int err = 0;
4685 mutex_lock(&mddev->open_mutex);
4686 if (atomic_read(&mddev->openers) > is_open) {
4687 printk("md: %s still in use.\n",mdname(mddev));
4688 err = -EBUSY;
4689 goto out;
4690 }
4691 if (mddev->pers) {
4692 md_stop_writes(mddev);
4693
4694 err = -ENXIO;
4695 if (mddev->ro==1)
4696 goto out;
4697 mddev->ro = 1;
4698 set_disk_ro(mddev->gendisk, 1);
4699 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4700 sysfs_notify_dirent(mddev->sysfs_state);
4701 err = 0;
4702 }
4703out:
4704 mutex_unlock(&mddev->open_mutex);
4705 return err;
4706}
4707
NeilBrown9e653b62006-06-26 00:27:58 -07004708/* mode:
4709 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07004710 * 2 - stop but do not disassemble array
4711 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004712static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713{
4714 int err = 0;
4715 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004716 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717
NeilBrownc8c00a62009-08-10 12:50:52 +10004718 mutex_lock(&mddev->open_mutex);
NeilBrownf2ea68c2008-07-21 17:05:25 +10004719 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004720 printk("md: %s still in use.\n",mdname(mddev));
NeilBrownc8c00a62009-08-10 12:50:52 +10004721 err = -EBUSY;
4722 } else if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723
NeilBrowna4bd82d2010-03-29 13:23:10 +11004724 if (mddev->ro)
4725 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004726
NeilBrowna4bd82d2010-03-29 13:23:10 +11004727 md_stop(mddev);
4728 mddev->queue->merge_bvec_fn = NULL;
4729 mddev->queue->unplug_fn = NULL;
4730 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004731
NeilBrowna4bd82d2010-03-29 13:23:10 +11004732 /* tell userspace to handle 'inactive' */
4733 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004734
NeilBrowna4bd82d2010-03-29 13:23:10 +11004735 list_for_each_entry(rdev, &mddev->disks, same_set)
4736 if (rdev->raid_disk >= 0) {
4737 char nm[20];
4738 sprintf(nm, "rd%d", rdev->raid_disk);
4739 sysfs_remove_link(&mddev->kobj, nm);
4740 }
NeilBrownc4647292009-05-07 12:51:06 +10004741
NeilBrowna4bd82d2010-03-29 13:23:10 +11004742 set_capacity(disk, 0);
4743 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08004744
NeilBrowna4bd82d2010-03-29 13:23:10 +11004745 if (mddev->ro)
4746 mddev->ro = 0;
4747
NeilBrown80ffb3c2009-08-18 10:35:26 +10004748 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 }
NeilBrownc8c00a62009-08-10 12:50:52 +10004750 mutex_unlock(&mddev->open_mutex);
4751 if (err)
4752 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 /*
4754 * Free resources if final stop
4755 */
NeilBrown9e653b62006-06-26 00:27:58 -07004756 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004757
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4759
NeilBrown978f9462006-02-02 14:28:05 -08004760 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004761 if (mddev->bitmap_info.file) {
4762 restore_bitmap_write_access(mddev->bitmap_info.file);
4763 fput(mddev->bitmap_info.file);
4764 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004765 }
NeilBrownc3d97142009-12-14 12:49:52 +11004766 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004767
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 export_array(mddev);
4769
NeilBrown6177b472010-03-29 11:37:13 +11004770 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11004771 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004772 if (mddev->hold_active == UNTIL_STOP)
4773 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004774
NeilBrowna4bd82d2010-03-29 13:23:10 +11004775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004777 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004778 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004779 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 return err;
4781}
4782
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004783#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784static void autorun_array(mddev_t *mddev)
4785{
4786 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 int err;
4788
NeilBrowna757e642005-04-16 15:26:42 -07004789 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791
4792 printk(KERN_INFO "md: running: ");
4793
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004794 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 char b[BDEVNAME_SIZE];
4796 printk("<%s>", bdevname(rdev->bdev,b));
4797 }
4798 printk("\n");
4799
NeilBrownd710e132008-10-13 11:55:12 +11004800 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 if (err) {
4802 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004803 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 }
4805}
4806
4807/*
4808 * lets try to run arrays based on all disks that have arrived
4809 * until now. (those are in pending_raid_disks)
4810 *
4811 * the method: pick the first pending disk, collect all disks with
4812 * the same UUID, remove all from the pending list and put them into
4813 * the 'same_array' list. Then order this list based on superblock
4814 * update time (freshest comes first), kick out 'old' disks and
4815 * compare superblocks. If everything's fine then run it.
4816 *
4817 * If "unit" is allocated, then bump its reference count
4818 */
4819static void autorun_devices(int part)
4820{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004821 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 mddev_t *mddev;
4823 char b[BDEVNAME_SIZE];
4824
4825 printk(KERN_INFO "md: autorun ...\n");
4826 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004827 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004829 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 rdev0 = list_entry(pending_raid_disks.next,
4831 mdk_rdev_t, same_set);
4832
4833 printk(KERN_INFO "md: considering %s ...\n",
4834 bdevname(rdev0->bdev,b));
4835 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004836 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 if (super_90_load(rdev, rdev0, 0) >= 0) {
4838 printk(KERN_INFO "md: adding %s ...\n",
4839 bdevname(rdev->bdev,b));
4840 list_move(&rdev->same_set, &candidates);
4841 }
4842 /*
4843 * now we have a set of devices, with all of them having
4844 * mostly sane superblocks. It's time to allocate the
4845 * mddev.
4846 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004847 if (part) {
4848 dev = MKDEV(mdp_major,
4849 rdev0->preferred_minor << MdpMinorShift);
4850 unit = MINOR(dev) >> MdpMinorShift;
4851 } else {
4852 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4853 unit = MINOR(dev);
4854 }
4855 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4857 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4858 break;
4859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860
4861 md_probe(dev, NULL, NULL);
4862 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004863 if (!mddev || !mddev->gendisk) {
4864 if (mddev)
4865 mddev_put(mddev);
4866 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 "md: cannot allocate memory for md drive.\n");
4868 break;
4869 }
4870 if (mddev_lock(mddev))
4871 printk(KERN_WARNING "md: %s locked, cannot run\n",
4872 mdname(mddev));
4873 else if (mddev->raid_disks || mddev->major_version
4874 || !list_empty(&mddev->disks)) {
4875 printk(KERN_WARNING
4876 "md: %s already running, cannot run %s\n",
4877 mdname(mddev), bdevname(rdev0->bdev,b));
4878 mddev_unlock(mddev);
4879 } else {
4880 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004881 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004882 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 list_del_init(&rdev->same_set);
4884 if (bind_rdev_to_array(rdev, mddev))
4885 export_rdev(rdev);
4886 }
4887 autorun_array(mddev);
4888 mddev_unlock(mddev);
4889 }
4890 /* on success, candidates will be empty, on error
4891 * it won't...
4892 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004893 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004894 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 mddev_put(mddev);
4898 }
4899 printk(KERN_INFO "md: ... autorun DONE.\n");
4900}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004901#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903static int get_version(void __user * arg)
4904{
4905 mdu_version_t ver;
4906
4907 ver.major = MD_MAJOR_VERSION;
4908 ver.minor = MD_MINOR_VERSION;
4909 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4910
4911 if (copy_to_user(arg, &ver, sizeof(ver)))
4912 return -EFAULT;
4913
4914 return 0;
4915}
4916
4917static int get_array_info(mddev_t * mddev, void __user * arg)
4918{
4919 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004920 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
NeilBrowna9f326e2009-09-23 18:06:41 +10004923 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004924 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004926 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 failed++;
4928 else {
4929 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004930 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004931 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 else
4933 spare++;
4934 }
4935 }
4936
4937 info.major_version = mddev->major_version;
4938 info.minor_version = mddev->minor_version;
4939 info.patch_version = MD_PATCHLEVEL_VERSION;
4940 info.ctime = mddev->ctime;
4941 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004942 info.size = mddev->dev_sectors / 2;
4943 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004944 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 info.nr_disks = nr;
4946 info.raid_disks = mddev->raid_disks;
4947 info.md_minor = mddev->md_minor;
4948 info.not_persistent= !mddev->persistent;
4949
4950 info.utime = mddev->utime;
4951 info.state = 0;
4952 if (mddev->in_sync)
4953 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11004954 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07004955 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004956 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 info.working_disks = working;
4958 info.failed_disks = failed;
4959 info.spare_disks = spare;
4960
4961 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004962 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963
4964 if (copy_to_user(arg, &info, sizeof(info)))
4965 return -EFAULT;
4966
4967 return 0;
4968}
4969
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004970static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004971{
4972 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4973 char *ptr, *buf = NULL;
4974 int err = -ENOMEM;
4975
Dan Williamsb5470dc2008-06-27 21:44:04 -07004976 if (md_allow_write(mddev))
4977 file = kmalloc(sizeof(*file), GFP_NOIO);
4978 else
4979 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004980
NeilBrown32a76272005-06-21 17:17:14 -07004981 if (!file)
4982 goto out;
4983
4984 /* bitmap disabled, zero the first byte and copy out */
4985 if (!mddev->bitmap || !mddev->bitmap->file) {
4986 file->pathname[0] = '\0';
4987 goto copy_out;
4988 }
4989
4990 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4991 if (!buf)
4992 goto out;
4993
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004994 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4995 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004996 goto out;
4997
4998 strcpy(file->pathname, ptr);
4999
5000copy_out:
5001 err = 0;
5002 if (copy_to_user(arg, file, sizeof(*file)))
5003 err = -EFAULT;
5004out:
5005 kfree(buf);
5006 kfree(file);
5007 return err;
5008}
5009
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010static int get_disk_info(mddev_t * mddev, void __user * arg)
5011{
5012 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 mdk_rdev_t *rdev;
5014
5015 if (copy_from_user(&info, arg, sizeof(info)))
5016 return -EFAULT;
5017
Andre Noll26ef3792008-07-11 22:02:21 +10005018 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 if (rdev) {
5020 info.major = MAJOR(rdev->bdev->bd_dev);
5021 info.minor = MINOR(rdev->bdev->bd_dev);
5022 info.raid_disk = rdev->raid_disk;
5023 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005024 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005026 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 info.state |= (1<<MD_DISK_ACTIVE);
5028 info.state |= (1<<MD_DISK_SYNC);
5029 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005030 if (test_bit(WriteMostly, &rdev->flags))
5031 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 } else {
5033 info.major = info.minor = 0;
5034 info.raid_disk = -1;
5035 info.state = (1<<MD_DISK_REMOVED);
5036 }
5037
5038 if (copy_to_user(arg, &info, sizeof(info)))
5039 return -EFAULT;
5040
5041 return 0;
5042}
5043
5044static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5045{
5046 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5047 mdk_rdev_t *rdev;
5048 dev_t dev = MKDEV(info->major,info->minor);
5049
5050 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5051 return -EOVERFLOW;
5052
5053 if (!mddev->raid_disks) {
5054 int err;
5055 /* expecting a device which has a superblock */
5056 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5057 if (IS_ERR(rdev)) {
5058 printk(KERN_WARNING
5059 "md: md_import_device returned %ld\n",
5060 PTR_ERR(rdev));
5061 return PTR_ERR(rdev);
5062 }
5063 if (!list_empty(&mddev->disks)) {
5064 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5065 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005066 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 .load_super(rdev, rdev0, mddev->minor_version);
5068 if (err < 0) {
5069 printk(KERN_WARNING
5070 "md: %s has different UUID to %s\n",
5071 bdevname(rdev->bdev,b),
5072 bdevname(rdev0->bdev,b2));
5073 export_rdev(rdev);
5074 return -EINVAL;
5075 }
5076 }
5077 err = bind_rdev_to_array(rdev, mddev);
5078 if (err)
5079 export_rdev(rdev);
5080 return err;
5081 }
5082
5083 /*
5084 * add_new_disk can be used once the array is assembled
5085 * to add "hot spares". They must already have a superblock
5086 * written
5087 */
5088 if (mddev->pers) {
5089 int err;
5090 if (!mddev->pers->hot_add_disk) {
5091 printk(KERN_WARNING
5092 "%s: personality does not support diskops!\n",
5093 mdname(mddev));
5094 return -EINVAL;
5095 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005096 if (mddev->persistent)
5097 rdev = md_import_device(dev, mddev->major_version,
5098 mddev->minor_version);
5099 else
5100 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 if (IS_ERR(rdev)) {
5102 printk(KERN_WARNING
5103 "md: md_import_device returned %ld\n",
5104 PTR_ERR(rdev));
5105 return PTR_ERR(rdev);
5106 }
NeilBrown41158c72005-06-21 17:17:25 -07005107 /* set save_raid_disk if appropriate */
5108 if (!mddev->persistent) {
5109 if (info->state & (1<<MD_DISK_SYNC) &&
5110 info->raid_disk < mddev->raid_disks)
5111 rdev->raid_disk = info->raid_disk;
5112 else
5113 rdev->raid_disk = -1;
5114 } else
5115 super_types[mddev->major_version].
5116 validate_super(mddev, rdev);
5117 rdev->saved_raid_disk = rdev->raid_disk;
5118
NeilBrownb2d444d2005-11-08 21:39:31 -08005119 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005120 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5121 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005122 else
5123 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005124
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 rdev->raid_disk = -1;
5126 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005127 if (!err && !mddev->pers->hot_remove_disk) {
5128 /* If there is hot_add_disk but no hot_remove_disk
5129 * then added disks for geometry changes,
5130 * and should be added immediately.
5131 */
5132 super_types[mddev->major_version].
5133 validate_super(mddev, rdev);
5134 err = mddev->pers->hot_add_disk(mddev, rdev);
5135 if (err)
5136 unbind_rdev_from_array(rdev);
5137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 if (err)
5139 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005140 else
NeilBrown3c0ee632008-10-21 13:25:28 +11005141 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005142
NeilBrown17571282006-12-10 02:20:52 -08005143 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005144 if (mddev->degraded)
5145 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005146 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07005147 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 return err;
5149 }
5150
5151 /* otherwise, add_new_disk is only allowed
5152 * for major_version==0 superblocks
5153 */
5154 if (mddev->major_version != 0) {
5155 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5156 mdname(mddev));
5157 return -EINVAL;
5158 }
5159
5160 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5161 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005162 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 if (IS_ERR(rdev)) {
5164 printk(KERN_WARNING
5165 "md: error, md_import_device() returned %ld\n",
5166 PTR_ERR(rdev));
5167 return PTR_ERR(rdev);
5168 }
5169 rdev->desc_nr = info->number;
5170 if (info->raid_disk < mddev->raid_disks)
5171 rdev->raid_disk = info->raid_disk;
5172 else
5173 rdev->raid_disk = -1;
5174
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005176 if (info->state & (1<<MD_DISK_SYNC))
5177 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005179 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5180 set_bit(WriteMostly, &rdev->flags);
5181
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 if (!mddev->persistent) {
5183 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10005184 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 } else
Andre Noll0f420352008-07-11 22:02:23 +10005186 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10005187 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188
NeilBrown2bf071b2006-01-06 00:20:55 -08005189 err = bind_rdev_to_array(rdev, mddev);
5190 if (err) {
5191 export_rdev(rdev);
5192 return err;
5193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 }
5195
5196 return 0;
5197}
5198
5199static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5200{
5201 char b[BDEVNAME_SIZE];
5202 mdk_rdev_t *rdev;
5203
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 rdev = find_rdev(mddev, dev);
5205 if (!rdev)
5206 return -ENXIO;
5207
5208 if (rdev->raid_disk >= 0)
5209 goto busy;
5210
5211 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07005212 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005213 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214
5215 return 0;
5216busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005217 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 bdevname(rdev->bdev,b), mdname(mddev));
5219 return -EBUSY;
5220}
5221
5222static int hot_add_disk(mddev_t * mddev, dev_t dev)
5223{
5224 char b[BDEVNAME_SIZE];
5225 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 mdk_rdev_t *rdev;
5227
5228 if (!mddev->pers)
5229 return -ENODEV;
5230
5231 if (mddev->major_version != 0) {
5232 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5233 " version-0 superblocks.\n",
5234 mdname(mddev));
5235 return -EINVAL;
5236 }
5237 if (!mddev->pers->hot_add_disk) {
5238 printk(KERN_WARNING
5239 "%s: personality does not support diskops!\n",
5240 mdname(mddev));
5241 return -EINVAL;
5242 }
5243
NeilBrownd710e132008-10-13 11:55:12 +11005244 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 if (IS_ERR(rdev)) {
5246 printk(KERN_WARNING
5247 "md: error, md_import_device() returned %ld\n",
5248 PTR_ERR(rdev));
5249 return -EINVAL;
5250 }
5251
5252 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10005253 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 else
Andre Noll0f420352008-07-11 22:02:23 +10005255 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256
NeilBrown8190e752009-06-18 08:48:58 +10005257 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258
NeilBrownb2d444d2005-11-08 21:39:31 -08005259 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 printk(KERN_WARNING
5261 "md: can not hot-add faulty %s disk to %s!\n",
5262 bdevname(rdev->bdev,b), mdname(mddev));
5263 err = -EINVAL;
5264 goto abort_export;
5265 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005266 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005268 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005269 err = bind_rdev_to_array(rdev, mddev);
5270 if (err)
5271 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272
5273 /*
5274 * The rest should better be atomic, we can have disk failures
5275 * noticed in interrupt contexts ...
5276 */
5277
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 rdev->raid_disk = -1;
5279
NeilBrown850b2b422006-10-03 01:15:46 -07005280 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281
5282 /*
5283 * Kick recovery, maybe this spare has to be added to the
5284 * array immediately.
5285 */
5286 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5287 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005288 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 return 0;
5290
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291abort_export:
5292 export_rdev(rdev);
5293 return err;
5294}
5295
NeilBrown32a76272005-06-21 17:17:14 -07005296static int set_bitmap_file(mddev_t *mddev, int fd)
5297{
5298 int err;
5299
NeilBrown36fa3062005-09-09 16:23:45 -07005300 if (mddev->pers) {
5301 if (!mddev->pers->quiesce)
5302 return -EBUSY;
5303 if (mddev->recovery || mddev->sync_thread)
5304 return -EBUSY;
5305 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005306 }
5307
NeilBrown36fa3062005-09-09 16:23:45 -07005308
5309 if (fd >= 0) {
5310 if (mddev->bitmap)
5311 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005312 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005313
NeilBrownc3d97142009-12-14 12:49:52 +11005314 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005315 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5316 mdname(mddev));
5317 return -EBADF;
5318 }
5319
NeilBrownc3d97142009-12-14 12:49:52 +11005320 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005321 if (err) {
5322 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5323 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005324 fput(mddev->bitmap_info.file);
5325 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005326 return err;
5327 }
NeilBrownc3d97142009-12-14 12:49:52 +11005328 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005329 } else if (mddev->bitmap == NULL)
5330 return -ENOENT; /* cannot remove what isn't there */
5331 err = 0;
5332 if (mddev->pers) {
5333 mddev->pers->quiesce(mddev, 1);
5334 if (fd >= 0)
5335 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005336 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005337 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005338 fd = -1; /* make sure to put the file */
5339 }
NeilBrown36fa3062005-09-09 16:23:45 -07005340 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005341 }
5342 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005343 if (mddev->bitmap_info.file) {
5344 restore_bitmap_write_access(mddev->bitmap_info.file);
5345 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005346 }
NeilBrownc3d97142009-12-14 12:49:52 +11005347 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005348 }
5349
NeilBrown32a76272005-06-21 17:17:14 -07005350 return err;
5351}
5352
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353/*
5354 * set_array_info is used two different ways
5355 * The original usage is when creating a new array.
5356 * In this usage, raid_disks is > 0 and it together with
5357 * level, size, not_persistent,layout,chunksize determine the
5358 * shape of the array.
5359 * This will always create an array with a type-0.90.0 superblock.
5360 * The newer usage is when assembling an array.
5361 * In this case raid_disks will be 0, and the major_version field is
5362 * use to determine which style super-blocks are to be found on the devices.
5363 * The minor and patch _version numbers are also kept incase the
5364 * super_block handler wishes to interpret them.
5365 */
5366static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5367{
5368
5369 if (info->raid_disks == 0) {
5370 /* just setting version number for superblock loading */
5371 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005372 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 super_types[info->major_version].name == NULL) {
5374 /* maybe try to auto-load a module? */
5375 printk(KERN_INFO
5376 "md: superblock version %d not known\n",
5377 info->major_version);
5378 return -EINVAL;
5379 }
5380 mddev->major_version = info->major_version;
5381 mddev->minor_version = info->minor_version;
5382 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005383 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005384 /* ensure mddev_put doesn't delete this now that there
5385 * is some minimal configuration.
5386 */
5387 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 return 0;
5389 }
5390 mddev->major_version = MD_MAJOR_VERSION;
5391 mddev->minor_version = MD_MINOR_VERSION;
5392 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5393 mddev->ctime = get_seconds();
5394
5395 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005396 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005397 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 mddev->raid_disks = info->raid_disks;
5399 /* don't set md_minor, it is determined by which /dev/md* was
5400 * openned
5401 */
5402 if (info->state & (1<<MD_SB_CLEAN))
5403 mddev->recovery_cp = MaxSector;
5404 else
5405 mddev->recovery_cp = 0;
5406 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005407 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408
5409 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005410 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411
5412 mddev->max_disks = MD_SB_DISKS;
5413
NeilBrowne6910632008-02-06 01:39:51 -08005414 if (mddev->persistent)
5415 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07005416 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417
NeilBrownc3d97142009-12-14 12:49:52 +11005418 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5419 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005420
NeilBrownf6705572006-03-27 01:18:11 -08005421 mddev->reshape_position = MaxSector;
5422
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 /*
5424 * Generate a 128 bit UUID
5425 */
5426 get_random_bytes(mddev->uuid, 16);
5427
NeilBrownf6705572006-03-27 01:18:11 -08005428 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005429 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005430 mddev->new_layout = mddev->layout;
5431 mddev->delta_disks = 0;
5432
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 return 0;
5434}
5435
Dan Williams1f403622009-03-31 14:59:03 +11005436void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5437{
Dan Williamsb522adc2009-03-31 15:00:31 +11005438 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5439
5440 if (mddev->external_size)
5441 return;
5442
Dan Williams1f403622009-03-31 14:59:03 +11005443 mddev->array_sectors = array_sectors;
5444}
5445EXPORT_SYMBOL(md_set_array_sectors);
5446
Andre Nolld71f9f82008-07-11 22:02:22 +10005447static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005448{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005449 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005450 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005451 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005452
5453 if (mddev->pers->resize == NULL)
5454 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005455 /* The "num_sectors" is the number of sectors of each device that
5456 * is used. This can only make sense for arrays with redundancy.
5457 * linear and raid0 always use whatever space is available. We can only
5458 * consider changing this number if no resync or reconstruction is
5459 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005460 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005461 * of each device. If num_sectors is zero, we find the largest size
5462 * that fits.
5463
NeilBrowna35b0d62006-01-06 00:20:49 -08005464 */
5465 if (mddev->sync_thread)
5466 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005467 if (mddev->bitmap)
5468 /* Sorry, cannot grow a bitmap yet, just remove it,
5469 * grow, and re-add.
5470 */
5471 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005472 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005473 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005474
Andre Nolld71f9f82008-07-11 22:02:22 +10005475 if (fit && (num_sectors == 0 || num_sectors > avail))
5476 num_sectors = avail;
5477 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005478 return -ENOSPC;
5479 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005480 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005481 if (!rv)
5482 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005483 return rv;
5484}
5485
NeilBrownda943b992006-01-06 00:20:54 -08005486static int update_raid_disks(mddev_t *mddev, int raid_disks)
5487{
5488 int rv;
5489 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005490 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005491 return -EINVAL;
5492 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005493 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08005494 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005495 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005496 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005497 mddev->delta_disks = raid_disks - mddev->raid_disks;
5498
5499 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005500 return rv;
5501}
5502
5503
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504/*
5505 * update_array_info is used to change the configuration of an
5506 * on-line array.
5507 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5508 * fields in the info are checked against the array.
5509 * Any differences that cannot be handled will cause an error.
5510 * Normally, only one change can be managed at a time.
5511 */
5512static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5513{
5514 int rv = 0;
5515 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005516 int state = 0;
5517
5518 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005519 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005520 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521
5522 if (mddev->major_version != info->major_version ||
5523 mddev->minor_version != info->minor_version ||
5524/* mddev->patch_version != info->patch_version || */
5525 mddev->ctime != info->ctime ||
5526 mddev->level != info->level ||
5527/* mddev->layout != info->layout || */
5528 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005529 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005530 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5531 ((state^info->state) & 0xfffffe00)
5532 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 return -EINVAL;
5534 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005535 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5536 cnt++;
5537 if (mddev->raid_disks != info->raid_disks)
5538 cnt++;
5539 if (mddev->layout != info->layout)
5540 cnt++;
5541 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5542 cnt++;
5543 if (cnt == 0)
5544 return 0;
5545 if (cnt > 1)
5546 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547
5548 if (mddev->layout != info->layout) {
5549 /* Change layout
5550 * we don't need to do anything at the md level, the
5551 * personality will take care of it all.
5552 */
NeilBrown50ac1682009-06-18 08:47:55 +10005553 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005555 else {
5556 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005557 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005558 if (rv)
5559 mddev->new_layout = mddev->layout;
5560 return rv;
5561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005563 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005564 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565
NeilBrownda943b992006-01-06 00:20:54 -08005566 if (mddev->raid_disks != info->raid_disks)
5567 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568
NeilBrown36fa3062005-09-09 16:23:45 -07005569 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5570 if (mddev->pers->quiesce == NULL)
5571 return -EINVAL;
5572 if (mddev->recovery || mddev->sync_thread)
5573 return -EBUSY;
5574 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5575 /* add the bitmap */
5576 if (mddev->bitmap)
5577 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005578 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005579 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005580 mddev->bitmap_info.offset =
5581 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005582 mddev->pers->quiesce(mddev, 1);
5583 rv = bitmap_create(mddev);
5584 if (rv)
5585 bitmap_destroy(mddev);
5586 mddev->pers->quiesce(mddev, 0);
5587 } else {
5588 /* remove the bitmap */
5589 if (!mddev->bitmap)
5590 return -ENOENT;
5591 if (mddev->bitmap->file)
5592 return -EINVAL;
5593 mddev->pers->quiesce(mddev, 1);
5594 bitmap_destroy(mddev);
5595 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005596 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005597 }
5598 }
NeilBrown850b2b422006-10-03 01:15:46 -07005599 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 return rv;
5601}
5602
5603static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5604{
5605 mdk_rdev_t *rdev;
5606
5607 if (mddev->pers == NULL)
5608 return -ENODEV;
5609
5610 rdev = find_rdev(mddev, dev);
5611 if (!rdev)
5612 return -ENODEV;
5613
5614 md_error(mddev, rdev);
5615 return 0;
5616}
5617
Andre Noll2f9618c2008-04-25 18:57:58 +02005618/*
5619 * We have a problem here : there is no easy way to give a CHS
5620 * virtual geometry. We currently pretend that we have a 2 heads
5621 * 4 sectors (with a BIG number of cylinders...). This drives
5622 * dosfs just mad... ;-)
5623 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005624static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5625{
5626 mddev_t *mddev = bdev->bd_disk->private_data;
5627
5628 geo->heads = 2;
5629 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11005630 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005631 return 0;
5632}
5633
Al Viroa39907f2008-03-02 10:31:15 -05005634static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 unsigned int cmd, unsigned long arg)
5636{
5637 int err = 0;
5638 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10005640 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641
5642 if (!capable(CAP_SYS_ADMIN))
5643 return -EACCES;
5644
5645 /*
5646 * Commands dealing with the RAID driver but not any
5647 * particular array:
5648 */
5649 switch (cmd)
5650 {
5651 case RAID_VERSION:
5652 err = get_version(argp);
5653 goto done;
5654
5655 case PRINT_RAID_DEBUG:
5656 err = 0;
5657 md_print_devices();
5658 goto done;
5659
5660#ifndef MODULE
5661 case RAID_AUTORUN:
5662 err = 0;
5663 autostart_arrays(arg);
5664 goto done;
5665#endif
5666 default:;
5667 }
5668
5669 /*
5670 * Commands creating/starting a new array:
5671 */
5672
Al Viroa39907f2008-03-02 10:31:15 -05005673 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674
5675 if (!mddev) {
5676 BUG();
5677 goto abort;
5678 }
5679
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 err = mddev_lock(mddev);
5681 if (err) {
5682 printk(KERN_INFO
5683 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5684 err, cmd);
5685 goto abort;
5686 }
5687
5688 switch (cmd)
5689 {
5690 case SET_ARRAY_INFO:
5691 {
5692 mdu_array_info_t info;
5693 if (!arg)
5694 memset(&info, 0, sizeof(info));
5695 else if (copy_from_user(&info, argp, sizeof(info))) {
5696 err = -EFAULT;
5697 goto abort_unlock;
5698 }
5699 if (mddev->pers) {
5700 err = update_array_info(mddev, &info);
5701 if (err) {
5702 printk(KERN_WARNING "md: couldn't update"
5703 " array info. %d\n", err);
5704 goto abort_unlock;
5705 }
5706 goto done_unlock;
5707 }
5708 if (!list_empty(&mddev->disks)) {
5709 printk(KERN_WARNING
5710 "md: array %s already has disks!\n",
5711 mdname(mddev));
5712 err = -EBUSY;
5713 goto abort_unlock;
5714 }
5715 if (mddev->raid_disks) {
5716 printk(KERN_WARNING
5717 "md: array %s already initialised!\n",
5718 mdname(mddev));
5719 err = -EBUSY;
5720 goto abort_unlock;
5721 }
5722 err = set_array_info(mddev, &info);
5723 if (err) {
5724 printk(KERN_WARNING "md: couldn't set"
5725 " array info. %d\n", err);
5726 goto abort_unlock;
5727 }
5728 }
5729 goto done_unlock;
5730
5731 default:;
5732 }
5733
5734 /*
5735 * Commands querying/configuring an existing array:
5736 */
NeilBrown32a76272005-06-21 17:17:14 -07005737 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005738 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005739 if ((!mddev->raid_disks && !mddev->external)
5740 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5741 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5742 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 err = -ENODEV;
5744 goto abort_unlock;
5745 }
5746
5747 /*
5748 * Commands even a read-only array can execute:
5749 */
5750 switch (cmd)
5751 {
5752 case GET_ARRAY_INFO:
5753 err = get_array_info(mddev, argp);
5754 goto done_unlock;
5755
NeilBrown32a76272005-06-21 17:17:14 -07005756 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005757 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005758 goto done_unlock;
5759
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 case GET_DISK_INFO:
5761 err = get_disk_info(mddev, argp);
5762 goto done_unlock;
5763
5764 case RESTART_ARRAY_RW:
5765 err = restart_array(mddev);
5766 goto done_unlock;
5767
5768 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005769 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 goto done_unlock;
5771
5772 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11005773 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 goto done_unlock;
5775
Dan Williamse2218352010-05-12 08:25:37 +10005776 case BLKROSET:
5777 if (get_user(ro, (int __user *)(arg))) {
5778 err = -EFAULT;
5779 goto done_unlock;
5780 }
5781 err = -EINVAL;
5782
5783 /* if the bdev is going readonly the value of mddev->ro
5784 * does not matter, no writes are coming
5785 */
5786 if (ro)
5787 goto done_unlock;
5788
5789 /* are we are already prepared for writes? */
5790 if (mddev->ro != 1)
5791 goto done_unlock;
5792
5793 /* transitioning to readauto need only happen for
5794 * arrays that call md_write_start
5795 */
5796 if (mddev->pers) {
5797 err = restart_array(mddev);
5798 if (err == 0) {
5799 mddev->ro = 2;
5800 set_disk_ro(mddev->gendisk, 0);
5801 }
5802 }
5803 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 }
5805
5806 /*
5807 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005808 * superblock, so we do not allow them on read-only arrays.
5809 * However non-MD ioctls (e.g. get-size) will still come through
5810 * here and hit the 'default' below, so only disallow
5811 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005813 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005814 if (mddev->ro == 2) {
5815 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005816 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005817 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5818 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005819 } else {
5820 err = -EROFS;
5821 goto abort_unlock;
5822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 }
5824
5825 switch (cmd)
5826 {
5827 case ADD_NEW_DISK:
5828 {
5829 mdu_disk_info_t info;
5830 if (copy_from_user(&info, argp, sizeof(info)))
5831 err = -EFAULT;
5832 else
5833 err = add_new_disk(mddev, &info);
5834 goto done_unlock;
5835 }
5836
5837 case HOT_REMOVE_DISK:
5838 err = hot_remove_disk(mddev, new_decode_dev(arg));
5839 goto done_unlock;
5840
5841 case HOT_ADD_DISK:
5842 err = hot_add_disk(mddev, new_decode_dev(arg));
5843 goto done_unlock;
5844
5845 case SET_DISK_FAULTY:
5846 err = set_disk_faulty(mddev, new_decode_dev(arg));
5847 goto done_unlock;
5848
5849 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005850 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 goto done_unlock;
5852
NeilBrown32a76272005-06-21 17:17:14 -07005853 case SET_BITMAP_FILE:
5854 err = set_bitmap_file(mddev, (int)arg);
5855 goto done_unlock;
5856
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 err = -EINVAL;
5859 goto abort_unlock;
5860 }
5861
5862done_unlock:
5863abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005864 if (mddev->hold_active == UNTIL_IOCTL &&
5865 err != -EINVAL)
5866 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 mddev_unlock(mddev);
5868
5869 return err;
5870done:
5871 if (err)
5872 MD_BUG();
5873abort:
5874 return err;
5875}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005876#ifdef CONFIG_COMPAT
5877static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5878 unsigned int cmd, unsigned long arg)
5879{
5880 switch (cmd) {
5881 case HOT_REMOVE_DISK:
5882 case HOT_ADD_DISK:
5883 case SET_DISK_FAULTY:
5884 case SET_BITMAP_FILE:
5885 /* These take in integer arg, do not convert */
5886 break;
5887 default:
5888 arg = (unsigned long)compat_ptr(arg);
5889 break;
5890 }
5891
5892 return md_ioctl(bdev, mode, cmd, arg);
5893}
5894#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895
Al Viroa39907f2008-03-02 10:31:15 -05005896static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897{
5898 /*
5899 * Succeed if we can lock the mddev, which confirms that
5900 * it isn't being stopped right now.
5901 */
NeilBrownd3374822009-01-09 08:31:10 +11005902 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 int err;
5904
NeilBrownd3374822009-01-09 08:31:10 +11005905 if (mddev->gendisk != bdev->bd_disk) {
5906 /* we are racing with mddev_put which is discarding this
5907 * bd_disk.
5908 */
5909 mddev_put(mddev);
5910 /* Wait until bdev->bd_disk is definitely gone */
5911 flush_scheduled_work();
5912 /* Then retry the open from the top */
5913 return -ERESTARTSYS;
5914 }
5915 BUG_ON(mddev != bdev->bd_disk->private_data);
5916
NeilBrownc8c00a62009-08-10 12:50:52 +10005917 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 goto out;
5919
5920 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005921 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005922 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923
NeilBrownf3b99be2010-06-24 13:31:03 +10005924 check_disk_size_change(mddev->gendisk, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 out:
5926 return err;
5927}
5928
Al Viroa39907f2008-03-02 10:31:15 -05005929static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930{
Al Viroa39907f2008-03-02 10:31:15 -05005931 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005933 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005934 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 mddev_put(mddev);
5936
5937 return 0;
5938}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005939static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940{
5941 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005942 .open = md_open,
5943 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005944 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005945#ifdef CONFIG_COMPAT
5946 .compat_ioctl = md_compat_ioctl,
5947#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005948 .getgeo = md_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949};
5950
Adrian Bunk75c96f82005-05-05 16:16:09 -07005951static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952{
5953 mdk_thread_t *thread = arg;
5954
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 /*
5956 * md_thread is a 'system-thread', it's priority should be very
5957 * high. We avoid resource deadlocks individually in each
5958 * raid personality. (RAID5 does preallocation) We also use RR and
5959 * the very same RT priority as kswapd, thus we will never get
5960 * into a priority inversion deadlock.
5961 *
5962 * we definitely have to have equal or higher priority than
5963 * bdflush, otherwise bdflush will deadlock if there are too
5964 * many dirty RAID5 blocks.
5965 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966
NeilBrown6985c432005-10-19 21:23:47 -07005967 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005968 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969
NeilBrown93588e22005-11-15 00:09:12 -08005970 /* We need to wait INTERRUPTIBLE so that
5971 * we don't add to the load-average.
5972 * That means we need to be sure no signals are
5973 * pending
5974 */
5975 if (signal_pending(current))
5976 flush_signals(current);
5977
5978 wait_event_interruptible_timeout
5979 (thread->wqueue,
5980 test_bit(THREAD_WAKEUP, &thread->flags)
5981 || kthread_should_stop(),
5982 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983
5984 clear_bit(THREAD_WAKEUP, &thread->flags);
5985
NeilBrown787453c2005-11-08 21:39:43 -08005986 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005988
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 return 0;
5990}
5991
5992void md_wakeup_thread(mdk_thread_t *thread)
5993{
5994 if (thread) {
5995 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5996 set_bit(THREAD_WAKEUP, &thread->flags);
5997 wake_up(&thread->wqueue);
5998 }
5999}
6000
6001mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
6002 const char *name)
6003{
6004 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005
NeilBrown9ffae0c2006-01-06 00:20:32 -08006006 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 if (!thread)
6008 return NULL;
6009
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 init_waitqueue_head(&thread->wqueue);
6011
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 thread->run = run;
6013 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006014 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006015 thread->tsk = kthread_run(md_thread, thread,
6016 "%s_%s",
6017 mdname(thread->mddev),
6018 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006019 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020 kfree(thread);
6021 return NULL;
6022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 return thread;
6024}
6025
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026void md_unregister_thread(mdk_thread_t *thread)
6027{
NeilBrowne0cf8f02009-03-31 14:39:39 +11006028 if (!thread)
6029 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07006030 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07006031
6032 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 kfree(thread);
6034}
6035
6036void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6037{
6038 if (!mddev) {
6039 MD_BUG();
6040 return;
6041 }
6042
NeilBrownb2d444d2005-11-08 21:39:31 -08006043 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006045
6046 if (mddev->external)
6047 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07006048/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6050 mdname(mddev),
6051 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6052 __builtin_return_address(0),__builtin_return_address(1),
6053 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07006054*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07006055 if (!mddev->pers)
6056 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 if (!mddev->pers->error_handler)
6058 return;
6059 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006060 if (mddev->degraded)
6061 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc0cc75f2010-03-22 10:28:51 +11006062 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6064 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6065 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07006066 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067}
6068
6069/* seq_file implementation /proc/mdstat */
6070
6071static void status_unused(struct seq_file *seq)
6072{
6073 int i = 0;
6074 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075
6076 seq_printf(seq, "unused devices: ");
6077
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006078 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 char b[BDEVNAME_SIZE];
6080 i++;
6081 seq_printf(seq, "%s ",
6082 bdevname(rdev->bdev,b));
6083 }
6084 if (!i)
6085 seq_printf(seq, "<none>");
6086
6087 seq_printf(seq, "\n");
6088}
6089
6090
6091static void status_resync(struct seq_file *seq, mddev_t * mddev)
6092{
NeilBrowndd71cf62009-05-07 12:49:35 +10006093 sector_t max_sectors, resync, res;
6094 unsigned long dt, db;
6095 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006096 int scale;
6097 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098
NeilBrowndd71cf62009-05-07 12:49:35 +10006099 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100
6101 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006102 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006104 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105
6106 /*
6107 * Should not happen.
6108 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006109 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110 MD_BUG();
6111 return;
6112 }
NeilBrown4588b422006-03-27 01:18:04 -08006113 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006114 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006115 * u32, as those are the requirements for sector_div.
6116 * Thus 'scale' must be at least 10
6117 */
6118 scale = 10;
6119 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006120 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006121 scale++;
6122 }
6123 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006124 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006125
6126 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 {
NeilBrown4588b422006-03-27 01:18:04 -08006128 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 seq_printf(seq, "[");
6130 for (i = 0; i < x; i++)
6131 seq_printf(seq, "=");
6132 seq_printf(seq, ">");
6133 for (i = 0; i < y; i++)
6134 seq_printf(seq, ".");
6135 seq_printf(seq, "] ");
6136 }
NeilBrown4588b422006-03-27 01:18:04 -08006137 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006138 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6139 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006140 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6141 "check" :
6142 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6143 "resync" : "recovery"))),
6144 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006145 (unsigned long long) resync/2,
6146 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147
6148 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 * dt: time from mark until now
6150 * db: blocks written from mark until now
6151 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006152 *
6153 * rt is a sector_t, so could be 32bit or 64bit.
6154 * So we divide before multiply in case it is 32bit and close
6155 * to the limit.
6156 * We scale the divisor (db) by 32 to avoid loosing precision
6157 * near the end of resync when the number of remaining sectors
6158 * is close to 'db'.
6159 * We then divide rt by 32 after multiplying by db to compensate.
6160 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 */
6162 dt = ((jiffies - mddev->resync_mark) / HZ);
6163 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006164 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6165 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166
NeilBrowndd71cf62009-05-07 12:49:35 +10006167 rt = max_sectors - resync; /* number of remaining sectors */
6168 sector_div(rt, db/32+1);
6169 rt *= dt;
6170 rt >>= 5;
6171
6172 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6173 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174
NeilBrownff4e8d92006-07-10 04:44:16 -07006175 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176}
6177
6178static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6179{
6180 struct list_head *tmp;
6181 loff_t l = *pos;
6182 mddev_t *mddev;
6183
6184 if (l >= 0x10000)
6185 return NULL;
6186 if (!l--)
6187 /* header */
6188 return (void*)1;
6189
6190 spin_lock(&all_mddevs_lock);
6191 list_for_each(tmp,&all_mddevs)
6192 if (!l--) {
6193 mddev = list_entry(tmp, mddev_t, all_mddevs);
6194 mddev_get(mddev);
6195 spin_unlock(&all_mddevs_lock);
6196 return mddev;
6197 }
6198 spin_unlock(&all_mddevs_lock);
6199 if (!l--)
6200 return (void*)2;/* tail */
6201 return NULL;
6202}
6203
6204static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6205{
6206 struct list_head *tmp;
6207 mddev_t *next_mddev, *mddev = v;
6208
6209 ++*pos;
6210 if (v == (void*)2)
6211 return NULL;
6212
6213 spin_lock(&all_mddevs_lock);
6214 if (v == (void*)1)
6215 tmp = all_mddevs.next;
6216 else
6217 tmp = mddev->all_mddevs.next;
6218 if (tmp != &all_mddevs)
6219 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6220 else {
6221 next_mddev = (void*)2;
6222 *pos = 0x10000;
6223 }
6224 spin_unlock(&all_mddevs_lock);
6225
6226 if (v != (void*)1)
6227 mddev_put(mddev);
6228 return next_mddev;
6229
6230}
6231
6232static void md_seq_stop(struct seq_file *seq, void *v)
6233{
6234 mddev_t *mddev = v;
6235
6236 if (mddev && v != (void*)1 && v != (void*)2)
6237 mddev_put(mddev);
6238}
6239
NeilBrownd7603b72006-01-06 00:20:30 -08006240struct mdstat_info {
6241 int event;
6242};
6243
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244static int md_seq_show(struct seq_file *seq, void *v)
6245{
6246 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006247 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006249 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006250 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251
6252 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006253 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254 seq_printf(seq, "Personalities : ");
6255 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006256 list_for_each_entry(pers, &pers_list, list)
6257 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258
6259 spin_unlock(&pers_lock);
6260 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006261 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 return 0;
6263 }
6264 if (v == (void*)2) {
6265 status_unused(seq);
6266 return 0;
6267 }
6268
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006269 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006271
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6273 seq_printf(seq, "%s : %sactive", mdname(mddev),
6274 mddev->pers ? "" : "in");
6275 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006276 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006278 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006279 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 seq_printf(seq, " %s", mddev->pers->name);
6281 }
6282
Andre Nolldd8ac332009-03-31 14:33:13 +11006283 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006284 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285 char b[BDEVNAME_SIZE];
6286 seq_printf(seq, " %s[%d]",
6287 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006288 if (test_bit(WriteMostly, &rdev->flags))
6289 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006290 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 seq_printf(seq, "(F)");
6292 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006293 } else if (rdev->raid_disk < 0)
6294 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006295 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 }
6297
6298 if (!list_empty(&mddev->disks)) {
6299 if (mddev->pers)
6300 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006301 (unsigned long long)
6302 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303 else
6304 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006305 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006307 if (mddev->persistent) {
6308 if (mddev->major_version != 0 ||
6309 mddev->minor_version != 90) {
6310 seq_printf(seq," super %d.%d",
6311 mddev->major_version,
6312 mddev->minor_version);
6313 }
NeilBrowne6910632008-02-06 01:39:51 -08006314 } else if (mddev->external)
6315 seq_printf(seq, " super external:%s",
6316 mddev->metadata_type);
6317 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006318 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319
6320 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006321 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006323 if (mddev->pers->sync_request) {
6324 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006325 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006326 seq_printf(seq, "\n ");
6327 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6328 seq_printf(seq, "\tresync=DELAYED\n ");
6329 else if (mddev->recovery_cp < MaxSector)
6330 seq_printf(seq, "\tresync=PENDING\n ");
6331 }
NeilBrown32a76272005-06-21 17:17:14 -07006332 } else
6333 seq_printf(seq, "\n ");
6334
6335 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006336 unsigned long chunk_kb;
6337 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006338 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006339 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006340 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6341 "%lu%s chunk",
6342 bitmap->pages - bitmap->missing_pages,
6343 bitmap->pages,
6344 (bitmap->pages - bitmap->missing_pages)
6345 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006346 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006347 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006348 if (bitmap->file) {
6349 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006350 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006351 }
NeilBrown78d742d2005-06-21 17:17:15 -07006352
NeilBrown32a76272005-06-21 17:17:14 -07006353 seq_printf(seq, "\n");
6354 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355 }
6356
6357 seq_printf(seq, "\n");
6358 }
6359 mddev_unlock(mddev);
6360
6361 return 0;
6362}
6363
Jan Engelhardt110518b2009-05-07 12:49:37 +10006364static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365 .start = md_seq_start,
6366 .next = md_seq_next,
6367 .stop = md_seq_stop,
6368 .show = md_seq_show,
6369};
6370
6371static int md_seq_open(struct inode *inode, struct file *file)
6372{
6373 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006374 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6375 if (mi == NULL)
6376 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377
6378 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006379 if (error)
6380 kfree(mi);
6381 else {
6382 struct seq_file *p = file->private_data;
6383 p->private = mi;
6384 mi->event = atomic_read(&md_event_count);
6385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006386 return error;
6387}
6388
NeilBrownd7603b72006-01-06 00:20:30 -08006389static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6390{
6391 struct seq_file *m = filp->private_data;
6392 struct mdstat_info *mi = m->private;
6393 int mask;
6394
6395 poll_wait(filp, &md_event_waiters, wait);
6396
6397 /* always allow read */
6398 mask = POLLIN | POLLRDNORM;
6399
6400 if (mi->event != atomic_read(&md_event_count))
6401 mask |= POLLERR | POLLPRI;
6402 return mask;
6403}
6404
Arjan van de Venfa027c22007-02-12 00:55:33 -08006405static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006406 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407 .open = md_seq_open,
6408 .read = seq_read,
6409 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006410 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006411 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412};
6413
NeilBrown2604b702006-01-06 00:20:36 -08006414int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006417 list_add_tail(&p->list, &pers_list);
6418 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 spin_unlock(&pers_lock);
6420 return 0;
6421}
6422
NeilBrown2604b702006-01-06 00:20:36 -08006423int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424{
NeilBrown2604b702006-01-06 00:20:36 -08006425 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006427 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 spin_unlock(&pers_lock);
6429 return 0;
6430}
6431
NeilBrowneea1bf32009-03-31 14:27:02 +11006432static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433{
6434 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006436 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006437
6438 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006439 rcu_read_lock();
6440 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006442 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6443 (int)part_stat_read(&disk->part0, sectors[1]) -
6444 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006445 /* sync IO will cause sync_io to increase before the disk_stats
6446 * as sync_io is counted when a request starts, and
6447 * disk_stats is counted when it completes.
6448 * So resync activity will cause curr_events to be smaller than
6449 * when there was no such activity.
6450 * non-sync IO will cause disk_stat to increase without
6451 * increasing sync_io so curr_events will (eventually)
6452 * be larger than it was before. Once it becomes
6453 * substantially larger, the test below will cause
6454 * the array to appear non-idle, and resync will slow
6455 * down.
6456 * If there is a lot of outstanding resync activity when
6457 * we set last_event to curr_events, then all that activity
6458 * completing might cause the array to appear non-idle
6459 * and resync will be slowed down even though there might
6460 * not have been non-resync activity. This will only
6461 * happen once though. 'last_events' will soon reflect
6462 * the state where there is little or no outstanding
6463 * resync requests, and further resync activity will
6464 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006465 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006467 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468 rdev->last_events = curr_events;
6469 idle = 0;
6470 }
6471 }
NeilBrown4b809912008-07-21 17:05:25 +10006472 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473 return idle;
6474}
6475
6476void md_done_sync(mddev_t *mddev, int blocks, int ok)
6477{
6478 /* another "blocks" (512byte) blocks have been synced */
6479 atomic_sub(blocks, &mddev->recovery_active);
6480 wake_up(&mddev->recovery_wait);
6481 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006482 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483 md_wakeup_thread(mddev->thread);
6484 // stop recovery, signal do_sync ....
6485 }
6486}
6487
6488
NeilBrown06d91a52005-06-21 17:17:12 -07006489/* md_write_start(mddev, bi)
6490 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006491 * in superblock) before writing, schedule a superblock update
6492 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006493 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006494void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495{
Neil Brown0fd62b82008-06-28 08:31:36 +10006496 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006497 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006498 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006499
NeilBrownf91de922005-11-08 21:39:36 -08006500 BUG_ON(mddev->ro == 1);
6501 if (mddev->ro == 2) {
6502 /* need to switch to read/write */
6503 mddev->ro = 0;
6504 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6505 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006506 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006507 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006508 }
NeilBrown06d91a52005-06-21 17:17:12 -07006509 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006510 if (mddev->safemode == 1)
6511 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006512 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006513 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006514 if (mddev->in_sync) {
6515 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006516 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006517 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006518 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006519 }
NeilBrowna9701a32005-11-08 21:39:34 -08006520 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006521 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006522 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006523 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006524 wait_event(mddev->sb_wait,
6525 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6526 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527}
6528
6529void md_write_end(mddev_t *mddev)
6530{
6531 if (atomic_dec_and_test(&mddev->writes_pending)) {
6532 if (mddev->safemode == 2)
6533 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006534 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6536 }
6537}
6538
NeilBrown2a2275d2007-01-26 00:57:11 -08006539/* md_allow_write(mddev)
6540 * Calling this ensures that the array is marked 'active' so that writes
6541 * may proceed without blocking. It is important to call this before
6542 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6543 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006544 *
6545 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6546 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006547 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006548int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006549{
6550 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006551 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006552 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006553 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006554 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006555 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006556
6557 spin_lock_irq(&mddev->write_lock);
6558 if (mddev->in_sync) {
6559 mddev->in_sync = 0;
6560 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6561 if (mddev->safemode_delay &&
6562 mddev->safemode == 0)
6563 mddev->safemode = 1;
6564 spin_unlock_irq(&mddev->write_lock);
6565 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006566 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006567 } else
6568 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006569
6570 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6571 return -EAGAIN;
6572 else
6573 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006574}
6575EXPORT_SYMBOL_GPL(md_allow_write);
6576
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577#define SYNC_MARKS 10
6578#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006579void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580{
6581 mddev_t *mddev2;
6582 unsigned int currspeed = 0,
6583 window;
NeilBrown57afd892005-06-21 17:17:13 -07006584 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585 unsigned long mark[SYNC_MARKS];
6586 sector_t mark_cnt[SYNC_MARKS];
6587 int last_mark,m;
6588 struct list_head *tmp;
6589 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006590 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006591 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006592 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593
6594 /* just incase thread restarts... */
6595 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6596 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006597 if (mddev->ro) /* never try to sync a read-only array */
6598 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599
NeilBrown61df9d92006-10-03 01:15:57 -07006600 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6601 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6602 desc = "data-check";
6603 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6604 desc = "requested-resync";
6605 else
6606 desc = "resync";
6607 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6608 desc = "reshape";
6609 else
6610 desc = "recovery";
6611
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612 /* we overload curr_resync somewhat here.
6613 * 0 == not engaged in resync at all
6614 * 2 == checking that there is no conflict with another sync
6615 * 1 == like 2, but have yielded to allow conflicting resync to
6616 * commense
6617 * other == active in resync - this many blocks
6618 *
6619 * Before starting a resync we must have set curr_resync to
6620 * 2, and then checked that every "conflicting" array has curr_resync
6621 * less than ours. When we find one that is the same or higher
6622 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6623 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6624 * This will mean we have to start checking from the beginning again.
6625 *
6626 */
6627
6628 do {
6629 mddev->curr_resync = 2;
6630
6631 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006632 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006633 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006634
6635 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006637 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638 if (mddev2 == mddev)
6639 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006640 if (!mddev->parallel_resync
6641 && mddev2->curr_resync
6642 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643 DEFINE_WAIT(wq);
6644 if (mddev < mddev2 && mddev->curr_resync == 2) {
6645 /* arbitrarily yield */
6646 mddev->curr_resync = 1;
6647 wake_up(&resync_wait);
6648 }
6649 if (mddev > mddev2 && mddev->curr_resync == 1)
6650 /* no need to wait here, we can wait the next
6651 * time 'round when curr_resync == 2
6652 */
6653 continue;
NeilBrown97441972008-09-19 11:49:54 +10006654 /* We need to wait 'interruptible' so as not to
6655 * contribute to the load average, and not to
6656 * be caught by 'softlockup'
6657 */
6658 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006659 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006660 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006661 printk(KERN_INFO "md: delaying %s of %s"
6662 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006664 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006666 if (signal_pending(current))
6667 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668 schedule();
6669 finish_wait(&resync_wait, &wq);
6670 goto try_again;
6671 }
6672 finish_wait(&resync_wait, &wq);
6673 }
6674 }
6675 } while (mddev->curr_resync < 2);
6676
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006677 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006678 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006680 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681 */
6682 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006683 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006684 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006685 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6686 j = mddev->resync_min;
6687 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006688 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006689
NeilBrownccfcc3c2006-03-27 01:18:09 -08006690 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006691 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006692 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006694 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006695 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006696 rcu_read_lock();
6697 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006698 if (rdev->raid_disk >= 0 &&
6699 !test_bit(Faulty, &rdev->flags) &&
6700 !test_bit(In_sync, &rdev->flags) &&
6701 rdev->recovery_offset < j)
6702 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006703 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705
NeilBrown61df9d92006-10-03 01:15:57 -07006706 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6707 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6708 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006709 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006710 "(but not more than %d KB/sec) for %s.\n",
6711 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712
NeilBrowneea1bf32009-03-31 14:27:02 +11006713 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006714
NeilBrown57afd892005-06-21 17:17:13 -07006715 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716 for (m = 0; m < SYNC_MARKS; m++) {
6717 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006718 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719 }
6720 last_mark = 0;
6721 mddev->resync_mark = mark[last_mark];
6722 mddev->resync_mark_cnt = mark_cnt[last_mark];
6723
6724 /*
6725 * Tune reconstruction:
6726 */
6727 window = 32*(PAGE_SIZE/512);
6728 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6729 window/2,(unsigned long long) max_sectors/2);
6730
6731 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732 last_check = 0;
6733
6734 if (j>2) {
6735 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006736 "md: resuming %s of %s from checkpoint.\n",
6737 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738 mddev->curr_resync = j;
6739 }
NeilBrownefa59332009-12-14 12:49:49 +11006740 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741
6742 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006743 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744
NeilBrown57afd892005-06-21 17:17:13 -07006745 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006746
NeilBrown7a91ee12009-05-26 12:57:21 +10006747 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6748 ((mddev->curr_resync > mddev->curr_resync_completed &&
6749 (mddev->curr_resync - mddev->curr_resync_completed)
6750 > (max_sectors >> 4)) ||
6751 (j - mddev->curr_resync_completed)*2
6752 >= mddev->resync_max - mddev->curr_resync_completed
6753 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006754 /* time to update curr_resync_completed */
6755 blk_unplug(mddev->queue);
6756 wait_event(mddev->recovery_wait,
6757 atomic_read(&mddev->recovery_active) == 0);
6758 mddev->curr_resync_completed =
6759 mddev->curr_resync;
6760 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006761 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006762 }
NeilBrownacb180b2009-04-14 16:28:34 +10006763
NeilBrowne62e58a2009-07-01 13:15:35 +10006764 while (j >= mddev->resync_max && !kthread_should_stop()) {
6765 /* As this condition is controlled by user-space,
6766 * we can block indefinitely, so use '_interruptible'
6767 * to avoid triggering warnings.
6768 */
6769 flush_signals(current); /* just in case */
6770 wait_event_interruptible(mddev->recovery_wait,
6771 mddev->resync_max > j
6772 || kthread_should_stop());
6773 }
NeilBrownacb180b2009-04-14 16:28:34 +10006774
6775 if (kthread_should_stop())
6776 goto interrupted;
6777
NeilBrown57afd892005-06-21 17:17:13 -07006778 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006779 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006780 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006781 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 goto out;
6783 }
NeilBrown57afd892005-06-21 17:17:13 -07006784
6785 if (!skipped) { /* actual IO requested */
6786 io_sectors += sectors;
6787 atomic_add(sectors, &mddev->recovery_active);
6788 }
6789
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790 j += sectors;
6791 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006792 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006793 if (last_check == 0)
6794 /* this is the earliers that rebuilt will be
6795 * visible in /proc/mdstat
6796 */
6797 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006798
6799 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800 continue;
6801
NeilBrown57afd892005-06-21 17:17:13 -07006802 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803
NeilBrowndfc70642008-05-23 13:04:39 -07006804 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 break;
6806
6807 repeat:
6808 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6809 /* step marks */
6810 int next = (last_mark+1) % SYNC_MARKS;
6811
6812 mddev->resync_mark = mark[next];
6813 mddev->resync_mark_cnt = mark_cnt[next];
6814 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006815 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816 last_mark = next;
6817 }
6818
6819
NeilBrownc6207272008-02-06 01:39:52 -08006820 if (kthread_should_stop())
6821 goto interrupted;
6822
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823
6824 /*
6825 * this loop exits only if either when we are slower than
6826 * the 'hard' speed limit, or the system was IO-idle for
6827 * a jiffy.
6828 * the system might be non-idle CPU-wise, but we only care
6829 * about not overloading the IO subsystem. (things like an
6830 * e2fsck being done on the RAID array should execute fast)
6831 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006832 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833 cond_resched();
6834
NeilBrown57afd892005-06-21 17:17:13 -07006835 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6836 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837
NeilBrown88202a02006-01-06 00:21:36 -08006838 if (currspeed > speed_min(mddev)) {
6839 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006840 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006841 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842 goto repeat;
6843 }
6844 }
6845 }
NeilBrown61df9d92006-10-03 01:15:57 -07006846 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847 /*
6848 * this also signals 'finished resyncing' to md_stop
6849 */
6850 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006851 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852
6853 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6854
6855 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006856 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857
NeilBrowndfc70642008-05-23 13:04:39 -07006858 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006859 mddev->curr_resync > 2) {
6860 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6861 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6862 if (mddev->curr_resync >= mddev->recovery_cp) {
6863 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006864 "md: checkpointing %s of %s.\n",
6865 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006866 mddev->recovery_cp = mddev->curr_resync;
6867 }
6868 } else
6869 mddev->recovery_cp = MaxSector;
6870 } else {
6871 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6872 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006873 rcu_read_lock();
6874 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006875 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10006876 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006877 !test_bit(Faulty, &rdev->flags) &&
6878 !test_bit(In_sync, &rdev->flags) &&
6879 rdev->recovery_offset < mddev->curr_resync)
6880 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07006881 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883 }
NeilBrown17571282006-12-10 02:20:52 -08006884 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006887 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6888 /* We completed so min/max setting can be forgotten if used. */
6889 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6890 mddev->resync_min = 0;
6891 mddev->resync_max = MaxSector;
6892 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6893 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006895 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6896 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006897 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898 wake_up(&resync_wait);
6899 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6900 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006901 return;
6902
6903 interrupted:
6904 /*
6905 * got a signal, exit.
6906 */
6907 printk(KERN_INFO
6908 "md: md_do_sync() got signal ... exiting\n");
6909 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6910 goto out;
6911
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912}
NeilBrown29269552006-03-27 01:18:10 -08006913EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914
6915
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006916static int remove_and_add_spares(mddev_t *mddev)
6917{
6918 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006919 int spares = 0;
6920
NeilBrown97e4f422009-03-31 14:33:13 +11006921 mddev->curr_resync_completed = 0;
6922
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006923 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006924 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006925 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006926 (test_bit(Faulty, &rdev->flags) ||
6927 ! test_bit(In_sync, &rdev->flags)) &&
6928 atomic_read(&rdev->nr_pending)==0) {
6929 if (mddev->pers->hot_remove_disk(
6930 mddev, rdev->raid_disk)==0) {
6931 char nm[20];
6932 sprintf(nm,"rd%d", rdev->raid_disk);
6933 sysfs_remove_link(&mddev->kobj, nm);
6934 rdev->raid_disk = -1;
6935 }
6936 }
6937
NeilBrown4044ba52009-01-09 08:31:11 +11006938 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006939 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006940 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006941 !test_bit(In_sync, &rdev->flags) &&
6942 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006943 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006944 if (rdev->raid_disk < 0
6945 && !test_bit(Faulty, &rdev->flags)) {
6946 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006947 if (mddev->pers->
6948 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006949 char nm[20];
6950 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006951 if (sysfs_create_link(&mddev->kobj,
6952 &rdev->kobj, nm))
6953 printk(KERN_WARNING
6954 "md: cannot register "
6955 "%s for %s\n",
6956 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006957 spares++;
6958 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11006959 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006960 } else
6961 break;
6962 }
NeilBrowndfc70642008-05-23 13:04:39 -07006963 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006964 }
6965 return spares;
6966}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967/*
6968 * This routine is regularly called by all per-raid-array threads to
6969 * deal with generic issues like resync and super-block update.
6970 * Raid personalities that don't have a thread (linear/raid0) do not
6971 * need this as they never do any recovery or update the superblock.
6972 *
6973 * It does not do any resync itself, but rather "forks" off other threads
6974 * to do that as needed.
6975 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6976 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006977 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978 * and wakeups up this thread which will reap the thread and finish up.
6979 * This thread also removes any faulty devices (with nr_pending == 0).
6980 *
6981 * The overall approach is:
6982 * 1/ if the superblock needs updating, update it.
6983 * 2/ If a recovery thread is running, don't do anything else.
6984 * 3/ If recovery has finished, clean up, possibly marking spares active.
6985 * 4/ If there are any faulty devices, remove them.
6986 * 5/ If array is degraded, try to add spares devices
6987 * 6/ If array has spares or is not in-sync, start a resync thread.
6988 */
6989void md_check_recovery(mddev_t *mddev)
6990{
6991 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992
6993
NeilBrown5f404022005-06-21 17:17:16 -07006994 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11006995 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996
6997 if (mddev->ro)
6998 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006999
7000 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007001 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007002 printk(KERN_INFO "md: %s in immediate safe mode\n",
7003 mdname(mddev));
7004 mddev->safemode = 2;
7005 }
7006 flush_signals(current);
7007 }
7008
NeilBrownc89a8ee2008-08-05 15:54:13 +10007009 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7010 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08007012 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007014 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007015 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007016 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7017 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018 ))
7019 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007020
NeilBrowndf5b89b2006-03-27 01:18:20 -08007021 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007022 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007023
NeilBrownc89a8ee2008-08-05 15:54:13 +10007024 if (mddev->ro) {
7025 /* Only thing we do on a ro array is remove
7026 * failed devices.
7027 */
7028 remove_and_add_spares(mddev);
7029 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7030 goto unlock;
7031 }
7032
NeilBrown31a59e32008-04-30 00:52:30 -07007033 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007034 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007035 spin_lock_irq(&mddev->write_lock);
7036 if (mddev->safemode &&
7037 !atomic_read(&mddev->writes_pending) &&
7038 !mddev->in_sync &&
7039 mddev->recovery_cp == MaxSector) {
7040 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007041 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07007042 if (mddev->persistent)
7043 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7044 }
7045 if (mddev->safemode == 1)
7046 mddev->safemode = 0;
7047 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007048 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11007049 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007050 }
NeilBrownfca4d842005-06-21 17:17:11 -07007051
NeilBrown850b2b422006-10-03 01:15:46 -07007052 if (mddev->flags)
7053 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007054
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7056 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7057 /* resync/recovery still happening */
7058 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7059 goto unlock;
7060 }
7061 if (mddev->sync_thread) {
7062 /* resync has finished, collect result */
7063 md_unregister_thread(mddev->sync_thread);
7064 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07007065 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7066 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067 /* success...*/
7068 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10007069 if (mddev->pers->spare_active(mddev))
7070 sysfs_notify(&mddev->kobj, NULL,
7071 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007072 }
NeilBrowncea9c222009-03-31 15:15:05 +11007073 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7074 mddev->pers->finish_reshape)
7075 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07007076 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07007077
7078 /* if array is no-longer degraded, then any saved_raid_disk
7079 * information must be scrapped
7080 */
7081 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007082 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07007083 rdev->saved_raid_disk = -1;
7084
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085 mddev->recovery = 0;
7086 /* flag recovery needed just to double check */
7087 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11007088 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007089 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007090 goto unlock;
7091 }
Neil Brown72a23c22008-06-28 08:31:41 +10007092 /* Set RUNNING before clearing NEEDED to avoid
7093 * any transients in the value of "sync_action".
7094 */
7095 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7096 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007097 /* Clear some bits that don't mean anything, but
7098 * might be left set
7099 */
NeilBrown24dd4692005-11-08 21:39:26 -08007100 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7101 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007103 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7104 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007105 /* no recovery is running.
7106 * remove any failed drives, then
7107 * add spares if possible.
7108 * Spare are also removed and re-added, to allow
7109 * the personality to fail the re-add.
7110 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007112 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007113 if (mddev->pers->check_reshape == NULL ||
7114 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007115 /* Cannot proceed */
7116 goto unlock;
7117 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007118 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007119 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007120 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7121 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007122 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007123 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007124 } else if (mddev->recovery_cp < MaxSector) {
7125 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007126 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007127 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7128 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007130
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007132 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7133 /* We are adding a device or devices to an array
7134 * which has the bitmap stored on all devices.
7135 * So make sure all bitmap pages get written
7136 */
7137 bitmap_write_all(mddev->bitmap);
7138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139 mddev->sync_thread = md_register_thread(md_do_sync,
7140 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007141 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142 if (!mddev->sync_thread) {
7143 printk(KERN_ERR "%s: could not start resync"
7144 " thread...\n",
7145 mdname(mddev));
7146 /* leave the spares where they are, it shouldn't hurt */
7147 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08007148 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11007150 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007151 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152 }
7153 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007154 if (!mddev->sync_thread) {
7155 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7156 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7157 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007158 if (mddev->sysfs_action)
7159 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161 mddev_unlock(mddev);
7162 }
7163}
7164
Dan Williams6bfe0b42008-04-30 00:52:32 -07007165void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7166{
NeilBrown3c0ee632008-10-21 13:25:28 +11007167 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007168 wait_event_timeout(rdev->blocked_wait,
7169 !test_bit(Blocked, &rdev->flags),
7170 msecs_to_jiffies(5000));
7171 rdev_dec_pending(rdev, mddev);
7172}
7173EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7174
Adrian Bunk75c96f82005-05-05 16:16:09 -07007175static int md_notify_reboot(struct notifier_block *this,
7176 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177{
7178 struct list_head *tmp;
7179 mddev_t *mddev;
7180
7181 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7182
7183 printk(KERN_INFO "md: stopping all md devices.\n");
7184
NeilBrown29ac4aa2008-02-06 01:39:58 -08007185 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007186 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007187 /* Force a switch to readonly even array
7188 * appears to still be in use. Hence
7189 * the '100'.
7190 */
NeilBrowna4bd82d2010-03-29 13:23:10 +11007191 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007192 mddev_unlock(mddev);
7193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007194 /*
7195 * certain more exotic SCSI devices are known to be
7196 * volatile wrt too early system reboots. While the
7197 * right place to handle this issue is the given
7198 * driver, we do want to have a safe RAID driver ...
7199 */
7200 mdelay(1000*1);
7201 }
7202 return NOTIFY_DONE;
7203}
7204
Adrian Bunk75c96f82005-05-05 16:16:09 -07007205static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206 .notifier_call = md_notify_reboot,
7207 .next = NULL,
7208 .priority = INT_MAX, /* before any real devices */
7209};
7210
7211static void md_geninit(void)
7212{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7214
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07007215 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216}
7217
Adrian Bunk75c96f82005-05-05 16:16:09 -07007218static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007220 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221 return -1;
7222 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007223 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224 return -1;
7225 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007226 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007227 md_probe, NULL, NULL);
7228 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007229 md_probe, NULL, NULL);
7230
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007232 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233
7234 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007235 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236}
7237
7238
7239#ifndef MODULE
7240
7241/*
7242 * Searches all registered partitions for autorun RAID arrays
7243 * at boot time.
7244 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007245
7246static LIST_HEAD(all_detected_devices);
7247struct detected_devices_node {
7248 struct list_head list;
7249 dev_t dev;
7250};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251
7252void md_autodetect_dev(dev_t dev)
7253{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007254 struct detected_devices_node *node_detected_dev;
7255
7256 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7257 if (node_detected_dev) {
7258 node_detected_dev->dev = dev;
7259 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7260 } else {
7261 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7262 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007264}
7265
7266
7267static void autostart_arrays(int part)
7268{
7269 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007270 struct detected_devices_node *node_detected_dev;
7271 dev_t dev;
7272 int i_scanned, i_passed;
7273
7274 i_scanned = 0;
7275 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007276
7277 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7278
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007279 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7280 i_scanned++;
7281 node_detected_dev = list_entry(all_detected_devices.next,
7282 struct detected_devices_node, list);
7283 list_del(&node_detected_dev->list);
7284 dev = node_detected_dev->dev;
7285 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007286 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287 if (IS_ERR(rdev))
7288 continue;
7289
NeilBrownb2d444d2005-11-08 21:39:31 -08007290 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291 MD_BUG();
7292 continue;
7293 }
NeilBrownd0fae182008-03-04 14:29:31 -08007294 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007296 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007297 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007298
7299 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7300 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007301
7302 autorun_devices(part);
7303}
7304
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007305#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306
7307static __exit void md_exit(void)
7308{
7309 mddev_t *mddev;
7310 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007311
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007312 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007313 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007314
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007315 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316 unregister_blkdev(mdp_major, "mdp");
7317 unregister_reboot_notifier(&md_notifier);
7318 unregister_sysctl_table(raid_table_header);
7319 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007320 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007321 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007322 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323 }
7324}
7325
Dan Williams685784a2007-07-09 11:56:42 -07007326subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007327module_exit(md_exit)
7328
NeilBrownf91de922005-11-08 21:39:36 -08007329static int get_ro(char *buffer, struct kernel_param *kp)
7330{
7331 return sprintf(buffer, "%d", start_readonly);
7332}
7333static int set_ro(const char *val, struct kernel_param *kp)
7334{
7335 char *e;
7336 int num = simple_strtoul(val, &e, 10);
7337 if (*val && (*e == '\0' || *e == '\n')) {
7338 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007339 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007340 }
7341 return -EINVAL;
7342}
7343
NeilBrown80ca3a42006-07-10 04:44:18 -07007344module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7345module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007346
NeilBrownefeb53c2009-01-09 08:31:10 +11007347module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007348
Linus Torvalds1da177e2005-04-16 15:20:36 -07007349EXPORT_SYMBOL(register_md_personality);
7350EXPORT_SYMBOL(unregister_md_personality);
7351EXPORT_SYMBOL(md_error);
7352EXPORT_SYMBOL(md_done_sync);
7353EXPORT_SYMBOL(md_write_start);
7354EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355EXPORT_SYMBOL(md_register_thread);
7356EXPORT_SYMBOL(md_unregister_thread);
7357EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007358EXPORT_SYMBOL(md_check_recovery);
7359MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007360MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007361MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007362MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);