blob: 9712b2e97be48b709575be868585da13e447a58a [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{
NeilBrown409c57f2009-03-31 14:39:39 +1100218 mddev_t *mddev = q->queuedata;
219 int rv;
220 if (mddev == NULL || mddev->pers == NULL) {
221 bio_io_error(bio);
222 return 0;
223 }
224 rcu_read_lock();
NeilBrowna2826aa2009-12-14 12:49:49 +1100225 if (mddev->suspended || mddev->barrier) {
NeilBrown409c57f2009-03-31 14:39:39 +1100226 DEFINE_WAIT(__wait);
227 for (;;) {
228 prepare_to_wait(&mddev->sb_wait, &__wait,
229 TASK_UNINTERRUPTIBLE);
NeilBrowna2826aa2009-12-14 12:49:49 +1100230 if (!mddev->suspended && !mddev->barrier)
NeilBrown409c57f2009-03-31 14:39:39 +1100231 break;
232 rcu_read_unlock();
233 schedule();
234 rcu_read_lock();
235 }
236 finish_wait(&mddev->sb_wait, &__wait);
237 }
238 atomic_inc(&mddev->active_io);
239 rcu_read_unlock();
240 rv = mddev->pers->make_request(q, bio);
241 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
242 wake_up(&mddev->sb_wait);
243
244 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
NeilBrown409c57f2009-03-31 14:39:39 +1100247static void mddev_suspend(mddev_t *mddev)
248{
249 BUG_ON(mddev->suspended);
250 mddev->suspended = 1;
251 synchronize_rcu();
252 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
253 mddev->pers->quiesce(mddev, 1);
254 md_unregister_thread(mddev->thread);
255 mddev->thread = NULL;
256 /* we now know that no code is executing in the personality module,
257 * except possibly the tail end of a ->bi_end_io function, but that
258 * is certain to complete before the module has a chance to get
259 * unloaded
260 */
261}
262
263static void mddev_resume(mddev_t *mddev)
264{
265 mddev->suspended = 0;
266 wake_up(&mddev->sb_wait);
267 mddev->pers->quiesce(mddev, 0);
268}
269
NeilBrown3fa841d2009-09-23 18:10:29 +1000270int mddev_congested(mddev_t *mddev, int bits)
271{
NeilBrowna2826aa2009-12-14 12:49:49 +1100272 if (mddev->barrier)
273 return 1;
NeilBrown3fa841d2009-09-23 18:10:29 +1000274 return mddev->suspended;
275}
276EXPORT_SYMBOL(mddev_congested);
277
NeilBrowna2826aa2009-12-14 12:49:49 +1100278/*
279 * Generic barrier handling for md
280 */
281
282#define POST_REQUEST_BARRIER ((void*)1)
283
284static void md_end_barrier(struct bio *bio, int err)
285{
286 mdk_rdev_t *rdev = bio->bi_private;
287 mddev_t *mddev = rdev->mddev;
288 if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER)
289 set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags);
290
291 rdev_dec_pending(rdev, mddev);
292
293 if (atomic_dec_and_test(&mddev->flush_pending)) {
294 if (mddev->barrier == POST_REQUEST_BARRIER) {
295 /* This was a post-request barrier */
296 mddev->barrier = NULL;
297 wake_up(&mddev->sb_wait);
298 } else
299 /* The pre-request barrier has finished */
300 schedule_work(&mddev->barrier_work);
301 }
302 bio_put(bio);
303}
304
305static void submit_barriers(mddev_t *mddev)
306{
307 mdk_rdev_t *rdev;
308
309 rcu_read_lock();
310 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
311 if (rdev->raid_disk >= 0 &&
312 !test_bit(Faulty, &rdev->flags)) {
313 /* Take two references, one is dropped
314 * when request finishes, one after
315 * we reclaim rcu_read_lock
316 */
317 struct bio *bi;
318 atomic_inc(&rdev->nr_pending);
319 atomic_inc(&rdev->nr_pending);
320 rcu_read_unlock();
321 bi = bio_alloc(GFP_KERNEL, 0);
322 bi->bi_end_io = md_end_barrier;
323 bi->bi_private = rdev;
324 bi->bi_bdev = rdev->bdev;
325 atomic_inc(&mddev->flush_pending);
326 submit_bio(WRITE_BARRIER, bi);
327 rcu_read_lock();
328 rdev_dec_pending(rdev, mddev);
329 }
330 rcu_read_unlock();
331}
332
333static void md_submit_barrier(struct work_struct *ws)
334{
335 mddev_t *mddev = container_of(ws, mddev_t, barrier_work);
336 struct bio *bio = mddev->barrier;
337
338 atomic_set(&mddev->flush_pending, 1);
339
340 if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags))
341 bio_endio(bio, -EOPNOTSUPP);
342 else if (bio->bi_size == 0)
343 /* an empty barrier - all done */
344 bio_endio(bio, 0);
345 else {
346 bio->bi_rw &= ~(1<<BIO_RW_BARRIER);
347 if (mddev->pers->make_request(mddev->queue, bio))
348 generic_make_request(bio);
349 mddev->barrier = POST_REQUEST_BARRIER;
350 submit_barriers(mddev);
351 }
352 if (atomic_dec_and_test(&mddev->flush_pending)) {
353 mddev->barrier = NULL;
354 wake_up(&mddev->sb_wait);
355 }
356}
357
358void md_barrier_request(mddev_t *mddev, struct bio *bio)
359{
360 spin_lock_irq(&mddev->write_lock);
361 wait_event_lock_irq(mddev->sb_wait,
362 !mddev->barrier,
363 mddev->write_lock, /*nothing*/);
364 mddev->barrier = bio;
365 spin_unlock_irq(&mddev->write_lock);
366
367 atomic_set(&mddev->flush_pending, 1);
368 INIT_WORK(&mddev->barrier_work, md_submit_barrier);
369
370 submit_barriers(mddev);
371
372 if (atomic_dec_and_test(&mddev->flush_pending))
373 schedule_work(&mddev->barrier_work);
374}
375EXPORT_SYMBOL(md_barrier_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377static inline mddev_t *mddev_get(mddev_t *mddev)
378{
379 atomic_inc(&mddev->active);
380 return mddev;
381}
382
Dan Williams5fd3a172009-03-04 00:57:25 -0700383static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385static void mddev_put(mddev_t *mddev)
386{
387 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
388 return;
NeilBrownd3374822009-01-09 08:31:10 +1100389 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100390 mddev->ctime == 0 && !mddev->hold_active) {
391 /* Array is not configured at all, and not held active,
392 * so destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 list_del(&mddev->all_mddevs);
NeilBrownd3374822009-01-09 08:31:10 +1100394 if (mddev->gendisk) {
395 /* we did a probe so need to clean up.
396 * Call schedule_work inside the spinlock
397 * so that flush_scheduled_work() after
398 * mddev_find will succeed in waiting for the
399 * work to be done.
400 */
401 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
402 schedule_work(&mddev->del_work);
403 } else
404 kfree(mddev);
405 }
406 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409static mddev_t * mddev_find(dev_t unit)
410{
411 mddev_t *mddev, *new = NULL;
412
413 retry:
414 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
NeilBrownefeb53c2009-01-09 08:31:10 +1100416 if (unit) {
417 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
418 if (mddev->unit == unit) {
419 mddev_get(mddev);
420 spin_unlock(&all_mddevs_lock);
421 kfree(new);
422 return mddev;
423 }
424
425 if (new) {
426 list_add(&new->all_mddevs, &all_mddevs);
427 spin_unlock(&all_mddevs_lock);
428 new->hold_active = UNTIL_IOCTL;
429 return new;
430 }
431 } else if (new) {
432 /* find an unused unit number */
433 static int next_minor = 512;
434 int start = next_minor;
435 int is_free = 0;
436 int dev = 0;
437 while (!is_free) {
438 dev = MKDEV(MD_MAJOR, next_minor);
439 next_minor++;
440 if (next_minor > MINORMASK)
441 next_minor = 0;
442 if (next_minor == start) {
443 /* Oh dear, all in use. */
444 spin_unlock(&all_mddevs_lock);
445 kfree(new);
446 return NULL;
447 }
448
449 is_free = 1;
450 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
451 if (mddev->unit == dev) {
452 is_free = 0;
453 break;
454 }
455 }
456 new->unit = dev;
457 new->md_minor = MINOR(dev);
458 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 list_add(&new->all_mddevs, &all_mddevs);
460 spin_unlock(&all_mddevs_lock);
461 return new;
462 }
463 spin_unlock(&all_mddevs_lock);
464
NeilBrown9ffae0c2006-01-06 00:20:32 -0800465 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (!new)
467 return NULL;
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 new->unit = unit;
470 if (MAJOR(unit) == MD_MAJOR)
471 new->md_minor = MINOR(unit);
472 else
473 new->md_minor = MINOR(unit) >> MdpMinorShift;
474
NeilBrownc8c00a62009-08-10 12:50:52 +1000475 mutex_init(&new->open_mutex);
NeilBrowndf5b89b2006-03-27 01:18:20 -0800476 mutex_init(&new->reconfig_mutex);
NeilBrownc3d97142009-12-14 12:49:52 +1100477 mutex_init(&new->bitmap_info.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 INIT_LIST_HEAD(&new->disks);
479 INIT_LIST_HEAD(&new->all_mddevs);
480 init_timer(&new->safemode_timer);
481 atomic_set(&new->active, 1);
NeilBrownf2ea68c2008-07-21 17:05:25 +1000482 atomic_set(&new->openers, 0);
NeilBrown409c57f2009-03-31 14:39:39 +1100483 atomic_set(&new->active_io, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700484 spin_lock_init(&new->write_lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100485 atomic_set(&new->flush_pending, 0);
NeilBrown3d310eb2005-06-21 17:17:26 -0700486 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700487 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700488 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000489 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800490 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700491 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 goto retry;
494}
495
496static inline int mddev_lock(mddev_t * mddev)
497{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800498 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
Dan Williamsb522adc2009-03-31 15:00:31 +1100501static inline int mddev_is_locked(mddev_t *mddev)
502{
503 return mutex_is_locked(&mddev->reconfig_mutex);
504}
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506static inline int mddev_trylock(mddev_t * mddev)
507{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800508 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
511static inline void mddev_unlock(mddev_t * mddev)
512{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800513 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
NeilBrown005eca52005-08-22 13:11:08 -0700515 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
NeilBrown2989ddb2006-01-06 00:20:43 -0800518static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100520 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100522 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (rdev->desc_nr == nr)
524 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return NULL;
527}
528
529static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
530{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 mdk_rdev_t *rdev;
532
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100533 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (rdev->bdev->bd_dev == dev)
535 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return NULL;
538}
539
NeilBrownd9d166c2006-01-06 00:20:51 -0800540static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800541{
542 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800543 list_for_each_entry(pers, &pers_list, list) {
544 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800545 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800546 if (strcmp(pers->name, clevel)==0)
547 return pers;
548 }
NeilBrown2604b702006-01-06 00:20:36 -0800549 return NULL;
550}
551
Andre Nollb73df2d2008-07-11 22:02:23 +1000552/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700553static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Andre Nollb73df2d2008-07-11 22:02:23 +1000555 sector_t num_sectors = bdev->bd_inode->i_size / 512;
556 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559static int alloc_disk_sb(mdk_rdev_t * rdev)
560{
561 if (rdev->sb_page)
562 MD_BUG();
563
564 rdev->sb_page = alloc_page(GFP_KERNEL);
565 if (!rdev->sb_page) {
566 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000567 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569
570 return 0;
571}
572
573static void free_disk_sb(mdk_rdev_t * rdev)
574{
575 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800576 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 rdev->sb_loaded = 0;
578 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000579 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100580 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
582}
583
584
NeilBrown6712ecf2007-09-27 12:47:43 +0200585static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700586{
587 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800588 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700589
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700590 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
591 printk("md: super_written gets error=%d, uptodate=%d\n",
592 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
593 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800594 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700595 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700596
NeilBrowna9701a32005-11-08 21:39:34 -0800597 if (atomic_dec_and_test(&mddev->pending_writes))
598 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700599 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700600}
601
NeilBrown6712ecf2007-09-27 12:47:43 +0200602static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800603{
604 struct bio *bio2 = bio->bi_private;
605 mdk_rdev_t *rdev = bio2->bi_private;
606 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800607
608 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
609 error == -EOPNOTSUPP) {
610 unsigned long flags;
611 /* barriers don't appear to be supported :-( */
612 set_bit(BarriersNotsupp, &rdev->flags);
613 mddev->barriers_work = 0;
614 spin_lock_irqsave(&mddev->write_lock, flags);
615 bio2->bi_next = mddev->biolist;
616 mddev->biolist = bio2;
617 spin_unlock_irqrestore(&mddev->write_lock, flags);
618 wake_up(&mddev->sb_wait);
619 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200620 } else {
621 bio_put(bio2);
622 bio->bi_private = rdev;
623 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800624 }
NeilBrowna9701a32005-11-08 21:39:34 -0800625}
626
NeilBrown7bfa19f2005-06-21 17:17:28 -0700627void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
628 sector_t sector, int size, struct page *page)
629{
630 /* write first size bytes of page to sector of rdev
631 * Increment mddev->pending_writes before returning
632 * and decrement it on completion, waking up sb_wait
633 * if zero is reached.
634 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800635 *
636 * As we might need to resubmit the request if BIO_RW_BARRIER
637 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700638 */
639 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Jens Axboe93dbb392009-02-16 10:25:40 +0100640 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700641
642 bio->bi_bdev = rdev->bdev;
643 bio->bi_sector = sector;
644 bio_add_page(bio, page, size, 0);
645 bio->bi_private = rdev;
646 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800647 bio->bi_rw = rw;
648
NeilBrown7bfa19f2005-06-21 17:17:28 -0700649 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800650 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
651 struct bio *rbio;
652 rw |= (1<<BIO_RW_BARRIER);
653 rbio = bio_clone(bio, GFP_NOIO);
654 rbio->bi_private = bio;
655 rbio->bi_end_io = super_written_barrier;
656 submit_bio(rw, rbio);
657 } else
658 submit_bio(rw, bio);
659}
660
661void md_super_wait(mddev_t *mddev)
662{
663 /* wait for all superblock writes that were scheduled to complete.
664 * if any had to be retried (due to BARRIER problems), retry them
665 */
666 DEFINE_WAIT(wq);
667 for(;;) {
668 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
669 if (atomic_read(&mddev->pending_writes)==0)
670 break;
671 while (mddev->biolist) {
672 struct bio *bio;
673 spin_lock_irq(&mddev->write_lock);
674 bio = mddev->biolist;
675 mddev->biolist = bio->bi_next ;
676 bio->bi_next = NULL;
677 spin_unlock_irq(&mddev->write_lock);
678 submit_bio(bio->bi_rw, bio);
679 }
680 schedule();
681 }
682 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700683}
684
NeilBrown6712ecf2007-09-27 12:47:43 +0200685static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
NeilBrowna654b9d82005-06-21 17:17:27 -0700690int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 struct page *page, int rw)
692{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700693 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 struct completion event;
695 int ret;
696
Jens Axboe93dbb392009-02-16 10:25:40 +0100697 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 bio->bi_bdev = bdev;
700 bio->bi_sector = sector;
701 bio_add_page(bio, page, size, 0);
702 init_completion(&event);
703 bio->bi_private = &event;
704 bio->bi_end_io = bi_complete;
705 submit_bio(rw, bio);
706 wait_for_completion(&event);
707
708 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
709 bio_put(bio);
710 return ret;
711}
NeilBrowna8745db2006-01-06 00:20:34 -0800712EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
NeilBrown0002b272005-09-09 16:23:53 -0700714static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 char b[BDEVNAME_SIZE];
717 if (!rdev->sb_page) {
718 MD_BUG();
719 return -EINVAL;
720 }
721 if (rdev->sb_loaded)
722 return 0;
723
724
Andre Noll0f420352008-07-11 22:02:23 +1000725 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 goto fail;
727 rdev->sb_loaded = 1;
728 return 0;
729
730fail:
731 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
732 bdevname(rdev->bdev,b));
733 return -EINVAL;
734}
735
736static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
737{
Andre Noll05710462008-07-11 22:02:20 +1000738 return sb1->set_uuid0 == sb2->set_uuid0 &&
739 sb1->set_uuid1 == sb2->set_uuid1 &&
740 sb1->set_uuid2 == sb2->set_uuid2 &&
741 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
745{
746 int ret;
747 mdp_super_t *tmp1, *tmp2;
748
749 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
750 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
751
752 if (!tmp1 || !tmp2) {
753 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100754 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 goto abort;
756 }
757
758 *tmp1 = *sb1;
759 *tmp2 = *sb2;
760
761 /*
762 * nr_disks is not constant
763 */
764 tmp1->nr_disks = 0;
765 tmp2->nr_disks = 0;
766
Andre Nollce0c8e02008-07-11 22:02:20 +1000767 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700769 kfree(tmp1);
770 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return ret;
772}
773
NeilBrown4d167f02007-05-09 02:35:37 -0700774
775static u32 md_csum_fold(u32 csum)
776{
777 csum = (csum & 0xffff) + (csum >> 16);
778 return (csum & 0xffff) + (csum >> 16);
779}
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781static unsigned int calc_sb_csum(mdp_super_t * sb)
782{
NeilBrown4d167f02007-05-09 02:35:37 -0700783 u64 newcsum = 0;
784 u32 *sb32 = (u32*)sb;
785 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 unsigned int disk_csum, csum;
787
788 disk_csum = sb->sb_csum;
789 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700790
791 for (i = 0; i < MD_SB_BYTES/4 ; i++)
792 newcsum += sb32[i];
793 csum = (newcsum & 0xffffffff) + (newcsum>>32);
794
795
796#ifdef CONFIG_ALPHA
797 /* This used to use csum_partial, which was wrong for several
798 * reasons including that different results are returned on
799 * different architectures. It isn't critical that we get exactly
800 * the same return value as before (we always csum_fold before
801 * testing, and that removes any differences). However as we
802 * know that csum_partial always returned a 16bit value on
803 * alphas, do a fold to maximise conformity to previous behaviour.
804 */
805 sb->sb_csum = md_csum_fold(disk_csum);
806#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700808#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 return csum;
810}
811
812
813/*
814 * Handle superblock details.
815 * We want to be able to handle multiple superblock formats
816 * so we have a common interface to them all, and an array of
817 * different handlers.
818 * We rely on user-space to write the initial superblock, and support
819 * reading and updating of superblocks.
820 * Interface methods are:
821 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
822 * loads and validates a superblock on dev.
823 * if refdev != NULL, compare superblocks on both devices
824 * Return:
825 * 0 - dev has a superblock that is compatible with refdev
826 * 1 - dev has a superblock that is compatible and newer than refdev
827 * so dev should be used as the refdev in future
828 * -EINVAL superblock incompatible or invalid
829 * -othererror e.g. -EIO
830 *
831 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
832 * Verify that dev is acceptable into mddev.
833 * The first time, mddev->raid_disks will be 0, and data from
834 * dev should be merged in. Subsequent calls check that dev
835 * is new enough. Return 0 or -EINVAL
836 *
837 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
838 * Update the superblock for rdev with data in mddev
839 * This does not write to disc.
840 *
841 */
842
843struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000844 char *name;
845 struct module *owner;
846 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
847 int minor_version);
848 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
849 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
850 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000851 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852};
853
854/*
Andre Noll0894cc32009-06-18 08:49:23 +1000855 * Check that the given mddev has no bitmap.
856 *
857 * This function is called from the run method of all personalities that do not
858 * support bitmaps. It prints an error message and returns non-zero if mddev
859 * has a bitmap. Otherwise, it returns 0.
860 *
861 */
862int md_check_no_bitmap(mddev_t *mddev)
863{
NeilBrownc3d97142009-12-14 12:49:52 +1100864 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000865 return 0;
866 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
867 mdname(mddev), mddev->pers->name);
868 return 1;
869}
870EXPORT_SYMBOL(md_check_no_bitmap);
871
872/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 * load_super for 0.90.0
874 */
875static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
876{
877 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
878 mdp_super_t *sb;
879 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 /*
Andre Noll0f420352008-07-11 22:02:23 +1000882 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 * it's at the end of the disk.
884 *
885 * It also happens to be a multiple of 4Kb.
886 */
Andre Noll0f420352008-07-11 22:02:23 +1000887 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
NeilBrown0002b272005-09-09 16:23:53 -0700889 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (ret) return ret;
891
892 ret = -EINVAL;
893
894 bdevname(rdev->bdev, b);
895 sb = (mdp_super_t*)page_address(rdev->sb_page);
896
897 if (sb->md_magic != MD_SB_MAGIC) {
898 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
899 b);
900 goto abort;
901 }
902
903 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800904 sb->minor_version < 90 ||
905 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
907 sb->major_version, sb->minor_version,
908 b);
909 goto abort;
910 }
911
912 if (sb->raid_disks <= 0)
913 goto abort;
914
NeilBrown4d167f02007-05-09 02:35:37 -0700915 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
917 b);
918 goto abort;
919 }
920
921 rdev->preferred_minor = sb->md_minor;
922 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700923 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 if (sb->level == LEVEL_MULTIPATH)
926 rdev->desc_nr = -1;
927 else
928 rdev->desc_nr = sb->this_disk.number;
929
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700930 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700932 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 __u64 ev1, ev2;
934 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
935 if (!uuid_equal(refsb, sb)) {
936 printk(KERN_WARNING "md: %s has different UUID to %s\n",
937 b, bdevname(refdev->bdev,b2));
938 goto abort;
939 }
940 if (!sb_equal(refsb, sb)) {
941 printk(KERN_WARNING "md: %s has same UUID"
942 " but different superblock to %s\n",
943 b, bdevname(refdev->bdev, b2));
944 goto abort;
945 }
946 ev1 = md_event(sb);
947 ev2 = md_event(refsb);
948 if (ev1 > ev2)
949 ret = 1;
950 else
951 ret = 0;
952 }
NeilBrown8190e752009-06-18 08:48:58 +1000953 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Andre Nolldd8ac332009-03-31 14:33:13 +1100955 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -0800956 /* "this cannot possibly happen" ... */
957 ret = -EINVAL;
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 abort:
960 return ret;
961}
962
963/*
964 * validate_super for 0.90.0
965 */
966static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
967{
968 mdp_disk_t *desc;
969 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700970 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
NeilBrown41158c72005-06-21 17:17:25 -0700972 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800973 clear_bit(Faulty, &rdev->flags);
974 clear_bit(In_sync, &rdev->flags);
975 clear_bit(WriteMostly, &rdev->flags);
976 clear_bit(BarriersNotsupp, &rdev->flags);
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (mddev->raid_disks == 0) {
979 mddev->major_version = 0;
980 mddev->minor_version = sb->minor_version;
981 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800982 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +1000983 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 mddev->ctime = sb->ctime;
985 mddev->utime = sb->utime;
986 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800987 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 mddev->layout = sb->layout;
989 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +1100990 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -0700991 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +1100992 mddev->bitmap_info.offset = 0;
993 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
NeilBrownf6705572006-03-27 01:18:11 -0800995 if (mddev->minor_version >= 91) {
996 mddev->reshape_position = sb->reshape_position;
997 mddev->delta_disks = sb->delta_disks;
998 mddev->new_level = sb->new_level;
999 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001000 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001001 } else {
1002 mddev->reshape_position = MaxSector;
1003 mddev->delta_disks = 0;
1004 mddev->new_level = mddev->level;
1005 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001006 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001007 }
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (sb->state & (1<<MD_SB_CLEAN))
1010 mddev->recovery_cp = MaxSector;
1011 else {
1012 if (sb->events_hi == sb->cp_events_hi &&
1013 sb->events_lo == sb->cp_events_lo) {
1014 mddev->recovery_cp = sb->recovery_cp;
1015 } else
1016 mddev->recovery_cp = 0;
1017 }
1018
1019 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1020 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1021 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1022 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1023
1024 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001025
1026 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001027 mddev->bitmap_info.file == NULL)
1028 mddev->bitmap_info.offset =
1029 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001030
NeilBrown41158c72005-06-21 17:17:25 -07001031 } else if (mddev->pers == NULL) {
1032 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 ++ev1;
1034 if (ev1 < mddev->events)
1035 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001036 } else if (mddev->bitmap) {
1037 /* if adding to array with a bitmap, then we can accept an
1038 * older device ... but not too old.
1039 */
NeilBrown41158c72005-06-21 17:17:25 -07001040 if (ev1 < mddev->bitmap->events_cleared)
1041 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001042 } else {
1043 if (ev1 < mddev->events)
1044 /* just a hot-add of a new device, leave raid_disk at -1 */
1045 return 0;
1046 }
NeilBrown41158c72005-06-21 17:17:25 -07001047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 desc = sb->disks + rdev->desc_nr;
1050
1051 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001052 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001053 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1054 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001055 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd92009-11-13 17:40:48 +11001057 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1058 /* active but not in sync implies recovery up to
1059 * reshape position. We don't know exactly where
1060 * that is, so set to zero for now */
1061 if (mddev->minor_version >= 91) {
1062 rdev->recovery_offset = 0;
1063 rdev->raid_disk = desc->raid_disk;
1064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001066 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1067 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001068 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001069 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 return 0;
1071}
1072
1073/*
1074 * sync_super for 0.90.0
1075 */
1076static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1077{
1078 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 mdk_rdev_t *rdev2;
1080 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 /* make rdev->sb match mddev data..
1084 *
1085 * 1/ zero out disks
1086 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1087 * 3/ any empty disks < next_spare become removed
1088 *
1089 * disks[0] gets initialised to REMOVED because
1090 * we cannot be sure from other fields if it has
1091 * been initialised or not.
1092 */
1093 int i;
1094 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1095
NeilBrown61181562005-09-09 16:24:02 -07001096 rdev->sb_size = MD_SB_BYTES;
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 sb = (mdp_super_t*)page_address(rdev->sb_page);
1099
1100 memset(sb, 0, sizeof(*sb));
1101
1102 sb->md_magic = MD_SB_MAGIC;
1103 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 sb->patch_version = mddev->patch_version;
1105 sb->gvalid_words = 0; /* ignored */
1106 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1107 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1108 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1109 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1110
1111 sb->ctime = mddev->ctime;
1112 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001113 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 sb->raid_disks = mddev->raid_disks;
1115 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001116 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 sb->utime = mddev->utime;
1118 sb->state = 0;
1119 sb->events_hi = (mddev->events>>32);
1120 sb->events_lo = (u32)mddev->events;
1121
NeilBrownf6705572006-03-27 01:18:11 -08001122 if (mddev->reshape_position == MaxSector)
1123 sb->minor_version = 90;
1124 else {
1125 sb->minor_version = 91;
1126 sb->reshape_position = mddev->reshape_position;
1127 sb->new_level = mddev->new_level;
1128 sb->delta_disks = mddev->delta_disks;
1129 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001130 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001131 }
1132 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (mddev->in_sync)
1134 {
1135 sb->recovery_cp = mddev->recovery_cp;
1136 sb->cp_events_hi = (mddev->events>>32);
1137 sb->cp_events_lo = (u32)mddev->events;
1138 if (mddev->recovery_cp == MaxSector)
1139 sb->state = (1<< MD_SB_CLEAN);
1140 } else
1141 sb->recovery_cp = 0;
1142
1143 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001144 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
NeilBrownc3d97142009-12-14 12:49:52 +11001146 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001147 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001150 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001152 int desc_nr;
NeilBrown0261cd92009-11-13 17:40:48 +11001153 int is_active = test_bit(In_sync, &rdev2->flags);
1154
1155 if (rdev2->raid_disk >= 0 &&
1156 sb->minor_version >= 91)
1157 /* we have nowhere to store the recovery_offset,
1158 * but if it is not below the reshape_position,
1159 * we can piggy-back on that.
1160 */
1161 is_active = 1;
1162 if (rdev2->raid_disk < 0 ||
1163 test_bit(Faulty, &rdev2->flags))
1164 is_active = 0;
1165 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001166 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001168 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001169 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 d = &sb->disks[rdev2->desc_nr];
1171 nr_disks++;
1172 d->number = rdev2->desc_nr;
1173 d->major = MAJOR(rdev2->bdev->bd_dev);
1174 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd92009-11-13 17:40:48 +11001175 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 d->raid_disk = rdev2->raid_disk;
1177 else
1178 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001179 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd92009-11-13 17:40:48 +11001181 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd92009-11-13 17:40:48 +11001183 if (test_bit(In_sync, &rdev2->flags))
1184 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 active++;
1186 working++;
1187 } else {
1188 d->state = 0;
1189 spare++;
1190 working++;
1191 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001192 if (test_bit(WriteMostly, &rdev2->flags))
1193 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 /* now set the "removed" and "faulty" bits on any missing devices */
1196 for (i=0 ; i < mddev->raid_disks ; i++) {
1197 mdp_disk_t *d = &sb->disks[i];
1198 if (d->state == 0 && d->number == 0) {
1199 d->number = i;
1200 d->raid_disk = i;
1201 d->state = (1<<MD_DISK_REMOVED);
1202 d->state |= (1<<MD_DISK_FAULTY);
1203 failed++;
1204 }
1205 }
1206 sb->nr_disks = nr_disks;
1207 sb->active_disks = active;
1208 sb->working_disks = working;
1209 sb->failed_disks = failed;
1210 sb->spare_disks = spare;
1211
1212 sb->this_disk = sb->disks[rdev->desc_nr];
1213 sb->sb_csum = calc_sb_csum(sb);
1214}
1215
1216/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001217 * rdev_size_change for 0.90.0
1218 */
1219static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001220super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001221{
Andre Noll58c0fed2009-03-31 14:33:13 +11001222 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001223 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001224 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001225 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001226 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001227 if (!num_sectors || num_sectors > rdev->sb_start)
1228 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001229 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001230 rdev->sb_page);
1231 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001232 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001233}
1234
1235
1236/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 * version 1 superblock
1238 */
1239
NeilBrown1c05b4b2006-10-21 10:24:08 -07001240static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001242 __le32 disk_csum;
1243 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 unsigned long long newcsum;
1245 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001246 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 int i;
1248
1249 disk_csum = sb->sb_csum;
1250 sb->sb_csum = 0;
1251 newcsum = 0;
1252 for (i=0; size>=4; size -= 4 )
1253 newcsum += le32_to_cpu(*isuper++);
1254
1255 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001256 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1259 sb->sb_csum = disk_csum;
1260 return cpu_to_le32(csum);
1261}
1262
1263static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1264{
1265 struct mdp_superblock_1 *sb;
1266 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001267 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001269 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 /*
Andre Noll0f420352008-07-11 22:02:23 +10001272 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 * It is always aligned to a 4K boundary and
1274 * depeding on minor_version, it can be:
1275 * 0: At least 8K, but less than 12K, from end of device
1276 * 1: At start of device
1277 * 2: 4K from start of device.
1278 */
1279 switch(minor_version) {
1280 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001281 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1282 sb_start -= 8*2;
1283 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 break;
1285 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001286 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 break;
1288 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001289 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 break;
1291 default:
1292 return -EINVAL;
1293 }
Andre Noll0f420352008-07-11 22:02:23 +10001294 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
NeilBrown0002b272005-09-09 16:23:53 -07001296 /* superblock is rarely larger than 1K, but it can be larger,
1297 * and it is safe to read 4k, so we do that
1298 */
1299 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (ret) return ret;
1301
1302
1303 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1304
1305 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1306 sb->major_version != cpu_to_le32(1) ||
1307 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001308 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001309 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return -EINVAL;
1311
1312 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1313 printk("md: invalid superblock checksum on %s\n",
1314 bdevname(rdev->bdev,b));
1315 return -EINVAL;
1316 }
1317 if (le64_to_cpu(sb->data_size) < 10) {
1318 printk("md: data_size too small on %s\n",
1319 bdevname(rdev->bdev,b));
1320 return -EINVAL;
1321 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 rdev->preferred_minor = 0xffff;
1324 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001325 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
NeilBrown0002b272005-09-09 16:23:53 -07001327 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001328 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001329 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001330 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1331
1332 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001333 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001334 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001335
NeilBrown31b65a02006-07-10 04:44:14 -07001336 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1337 rdev->desc_nr = -1;
1338 else
1339 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1340
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001341 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001342 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001343 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 __u64 ev1, ev2;
1345 struct mdp_superblock_1 *refsb =
1346 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1347
1348 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1349 sb->level != refsb->level ||
1350 sb->layout != refsb->layout ||
1351 sb->chunksize != refsb->chunksize) {
1352 printk(KERN_WARNING "md: %s has strangely different"
1353 " superblock to %s\n",
1354 bdevname(rdev->bdev,b),
1355 bdevname(refdev->bdev,b2));
1356 return -EINVAL;
1357 }
1358 ev1 = le64_to_cpu(sb->events);
1359 ev2 = le64_to_cpu(refsb->events);
1360
1361 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001362 ret = 1;
1363 else
1364 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 }
NeilBrowna1801f82008-03-04 14:29:31 -08001366 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001367 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1368 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001370 rdev->sectors = rdev->sb_start;
1371 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001373 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001374 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001375 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001376 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377}
1378
1379static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1380{
1381 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001382 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
NeilBrown41158c72005-06-21 17:17:25 -07001384 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001385 clear_bit(Faulty, &rdev->flags);
1386 clear_bit(In_sync, &rdev->flags);
1387 clear_bit(WriteMostly, &rdev->flags);
1388 clear_bit(BarriersNotsupp, &rdev->flags);
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 if (mddev->raid_disks == 0) {
1391 mddev->major_version = 1;
1392 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001393 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001394 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1396 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1397 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001398 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 mddev->layout = le32_to_cpu(sb->layout);
1400 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001401 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001402 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001403 mddev->bitmap_info.offset = 0;
1404 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1407 memcpy(mddev->uuid, sb->set_uuid, 16);
1408
1409 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001410
NeilBrown71c08052005-09-09 16:23:51 -07001411 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001412 mddev->bitmap_info.file == NULL )
1413 mddev->bitmap_info.offset =
1414 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001415
NeilBrownf6705572006-03-27 01:18:11 -08001416 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1417 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1418 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1419 mddev->new_level = le32_to_cpu(sb->new_level);
1420 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001421 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001422 } else {
1423 mddev->reshape_position = MaxSector;
1424 mddev->delta_disks = 0;
1425 mddev->new_level = mddev->level;
1426 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001427 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001428 }
1429
NeilBrown41158c72005-06-21 17:17:25 -07001430 } else if (mddev->pers == NULL) {
1431 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 ++ev1;
1433 if (ev1 < mddev->events)
1434 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001435 } else if (mddev->bitmap) {
1436 /* If adding to array with a bitmap, then we can accept an
1437 * older device, but not too old.
1438 */
NeilBrown41158c72005-06-21 17:17:25 -07001439 if (ev1 < mddev->bitmap->events_cleared)
1440 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001441 } else {
1442 if (ev1 < mddev->events)
1443 /* just a hot-add of a new device, leave raid_disk at -1 */
1444 return 0;
1445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (mddev->level != LEVEL_MULTIPATH) {
1447 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001448 if (rdev->desc_nr < 0 ||
1449 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1450 role = 0xffff;
1451 rdev->desc_nr = -1;
1452 } else
1453 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 switch(role) {
1455 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 break;
1457 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001458 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 break;
1460 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001461 if ((le32_to_cpu(sb->feature_map) &
1462 MD_FEATURE_RECOVERY_OFFSET))
1463 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1464 else
1465 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 rdev->raid_disk = role;
1467 break;
1468 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001469 if (sb->devflags & WriteMostly1)
1470 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001471 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001472 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return 0;
1475}
1476
1477static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1478{
1479 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 mdk_rdev_t *rdev2;
1481 int max_dev, i;
1482 /* make rdev->sb match mddev and rdev data. */
1483
1484 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1485
1486 sb->feature_map = 0;
1487 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001488 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 memset(sb->pad1, 0, sizeof(sb->pad1));
1490 memset(sb->pad2, 0, sizeof(sb->pad2));
1491 memset(sb->pad3, 0, sizeof(sb->pad3));
1492
1493 sb->utime = cpu_to_le64((__u64)mddev->utime);
1494 sb->events = cpu_to_le64(mddev->events);
1495 if (mddev->in_sync)
1496 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1497 else
1498 sb->resync_offset = cpu_to_le64(0);
1499
NeilBrown1c05b4b2006-10-21 10:24:08 -07001500 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001501
NeilBrownf0ca3402006-02-02 14:28:04 -08001502 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001503 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001504 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001505 sb->level = cpu_to_le32(mddev->level);
1506 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001507
NeilBrownc3d97142009-12-14 12:49:52 +11001508 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1509 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001510 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001511 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001512
1513 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001514 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001515 sb->feature_map |=
1516 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1517 sb->recovery_offset =
1518 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001519 }
1520
NeilBrownf6705572006-03-27 01:18:11 -08001521 if (mddev->reshape_position != MaxSector) {
1522 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1523 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1524 sb->new_layout = cpu_to_le32(mddev->new_layout);
1525 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1526 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001527 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001528 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001531 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (rdev2->desc_nr+1 > max_dev)
1533 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001534
NeilBrown70471da2009-08-03 10:59:57 +10001535 if (max_dev > le32_to_cpu(sb->max_dev)) {
1536 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001537 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001538 rdev->sb_size = max_dev * 2 + 256;
1539 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1540 if (rdev->sb_size & bmask)
1541 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 for (i=0; i<max_dev;i++)
1544 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1545
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001546 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001548 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001550 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001552 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001553 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 else
1555 sb->dev_roles[i] = cpu_to_le16(0xffff);
1556 }
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 sb->sb_csum = calc_sb_1_csum(sb);
1559}
1560
Chris Webb0cd17fe2008-06-28 08:31:46 +10001561static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001562super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001563{
1564 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001565 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001566 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001567 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001568 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001569 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001570 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1571 max_sectors -= rdev->data_offset;
1572 if (!num_sectors || num_sectors > max_sectors)
1573 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001574 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001575 /* minor version 0 with bitmap we can't move */
1576 return 0;
1577 } else {
1578 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001579 sector_t sb_start;
1580 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1581 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001582 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001583 if (!num_sectors || num_sectors > max_sectors)
1584 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001585 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001586 }
1587 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001588 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001589 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001590 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001591 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001592 rdev->sb_page);
1593 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001594 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001595}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Adrian Bunk75c96f82005-05-05 16:16:09 -07001597static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 [0] = {
1599 .name = "0.90.0",
1600 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001601 .load_super = super_90_load,
1602 .validate_super = super_90_validate,
1603 .sync_super = super_90_sync,
1604 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 },
1606 [1] = {
1607 .name = "md-1",
1608 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001609 .load_super = super_1_load,
1610 .validate_super = super_1_validate,
1611 .sync_super = super_1_sync,
1612 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 },
1614};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1617{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001618 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
NeilBrown4b809912008-07-21 17:05:25 +10001620 rcu_read_lock();
1621 rdev_for_each_rcu(rdev, mddev1)
1622 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001623 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001624 rdev2->bdev->bd_contains) {
1625 rcu_read_unlock();
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001626 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001627 }
1628 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 return 0;
1630}
1631
1632static LIST_HEAD(pending_raid_disks);
1633
Andre Nollac5e7112009-08-03 10:59:47 +10001634/*
1635 * Try to register data integrity profile for an mddev
1636 *
1637 * This is called when an array is started and after a disk has been kicked
1638 * from the array. It only succeeds if all working and active component devices
1639 * are integrity capable with matching profiles.
1640 */
1641int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001642{
Andre Nollac5e7112009-08-03 10:59:47 +10001643 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001644
Andre Nollac5e7112009-08-03 10:59:47 +10001645 if (list_empty(&mddev->disks))
1646 return 0; /* nothing to do */
1647 if (blk_get_integrity(mddev->gendisk))
1648 return 0; /* already registered */
1649 list_for_each_entry(rdev, &mddev->disks, same_set) {
1650 /* skip spares and non-functional disks */
1651 if (test_bit(Faulty, &rdev->flags))
1652 continue;
1653 if (rdev->raid_disk < 0)
1654 continue;
1655 /*
1656 * If at least one rdev is not integrity capable, we can not
1657 * enable data integrity for the md device.
1658 */
1659 if (!bdev_get_integrity(rdev->bdev))
1660 return -EINVAL;
1661 if (!reference) {
1662 /* Use the first rdev as the reference */
1663 reference = rdev;
1664 continue;
1665 }
1666 /* does this rdev's profile match the reference profile? */
1667 if (blk_integrity_compare(reference->bdev->bd_disk,
1668 rdev->bdev->bd_disk) < 0)
1669 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001670 }
Andre Nollac5e7112009-08-03 10:59:47 +10001671 /*
1672 * All component devices are integrity capable and have matching
1673 * profiles, register the common profile for the md device.
1674 */
1675 if (blk_integrity_register(mddev->gendisk,
1676 bdev_get_integrity(reference->bdev)) != 0) {
1677 printk(KERN_ERR "md: failed to register integrity for %s\n",
1678 mdname(mddev));
1679 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001680 }
Andre Nollac5e7112009-08-03 10:59:47 +10001681 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1682 mdname(mddev));
1683 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001684}
Andre Nollac5e7112009-08-03 10:59:47 +10001685EXPORT_SYMBOL(md_integrity_register);
1686
1687/* Disable data integrity if non-capable/non-matching disk is being added */
1688void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1689{
1690 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1691 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1692
1693 if (!bi_mddev) /* nothing to do */
1694 return;
1695 if (rdev->raid_disk < 0) /* skip spares */
1696 return;
1697 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1698 rdev->bdev->bd_disk) >= 0)
1699 return;
1700 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1701 blk_integrity_unregister(mddev->gendisk);
1702}
1703EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1706{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001707 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001708 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001709 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001710 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 if (rdev->mddev) {
1713 MD_BUG();
1714 return -EINVAL;
1715 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001716
1717 /* prevent duplicates */
1718 if (find_rdev(mddev, rdev->bdev->bd_dev))
1719 return -EEXIST;
1720
Andre Nolldd8ac332009-03-31 14:33:13 +11001721 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1722 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1723 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001724 if (mddev->pers) {
1725 /* Cannot change size, so fail
1726 * If mddev->level <= 0, then we don't care
1727 * about aligning sizes (e.g. linear)
1728 */
1729 if (mddev->level > 0)
1730 return -ENOSPC;
1731 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001732 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 /* Verify rdev->desc_nr is unique.
1736 * If it is -1, assign a free number, else
1737 * check number is not in use
1738 */
1739 if (rdev->desc_nr < 0) {
1740 int choice = 0;
1741 if (mddev->pers) choice = mddev->raid_disks;
1742 while (find_rdev_nr(mddev, choice))
1743 choice++;
1744 rdev->desc_nr = choice;
1745 } else {
1746 if (find_rdev_nr(mddev, rdev->desc_nr))
1747 return -EBUSY;
1748 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001749 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1750 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1751 mdname(mddev), mddev->max_disks);
1752 return -EBUSY;
1753 }
NeilBrown19133a42005-11-08 21:39:35 -08001754 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001755 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001756 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001759 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001760
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001761 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001762 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001763
Tejun Heo0762b8b2008-08-25 19:56:12 +09001764 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001765 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1766 kobject_del(&rdev->kobj);
1767 goto fail;
1768 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001769 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1770
NeilBrown4b809912008-07-21 17:05:25 +10001771 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001772 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001773
1774 /* May as well allow recovery to be retried once */
1775 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001778
1779 fail:
1780 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1781 b, mdname(mddev));
1782 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
1784
NeilBrown177a99b2008-02-06 01:39:56 -08001785static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001786{
1787 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1788 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001789 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001790}
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1793{
1794 char b[BDEVNAME_SIZE];
1795 if (!rdev->mddev) {
1796 MD_BUG();
1797 return;
1798 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001799 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001800 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1802 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001803 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001804 sysfs_put(rdev->sysfs_state);
1805 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001806 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001807 * writing to 'remove' to "dev/state". We also need
1808 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001809 */
NeilBrown4b809912008-07-21 17:05:25 +10001810 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001811 INIT_WORK(&rdev->del_work, md_delayed_delete);
1812 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001813 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814}
1815
1816/*
1817 * prevent the device from being mounted, repartitioned or
1818 * otherwise reused by a RAID array (or any other kernel
1819 * subsystem), by bd_claiming the device.
1820 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001821static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
1823 int err = 0;
1824 struct block_device *bdev;
1825 char b[BDEVNAME_SIZE];
1826
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001827 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 if (IS_ERR(bdev)) {
1829 printk(KERN_ERR "md: could not open %s.\n",
1830 __bdevname(dev, b));
1831 return PTR_ERR(bdev);
1832 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001833 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 if (err) {
1835 printk(KERN_ERR "md: could not bd_claim %s.\n",
1836 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001837 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 return err;
1839 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001840 if (!shared)
1841 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 rdev->bdev = bdev;
1843 return err;
1844}
1845
1846static void unlock_rdev(mdk_rdev_t *rdev)
1847{
1848 struct block_device *bdev = rdev->bdev;
1849 rdev->bdev = NULL;
1850 if (!bdev)
1851 MD_BUG();
1852 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001853 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855
1856void md_autodetect_dev(dev_t dev);
1857
1858static void export_rdev(mdk_rdev_t * rdev)
1859{
1860 char b[BDEVNAME_SIZE];
1861 printk(KERN_INFO "md: export_rdev(%s)\n",
1862 bdevname(rdev->bdev,b));
1863 if (rdev->mddev)
1864 MD_BUG();
1865 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001867 if (test_bit(AutoDetected, &rdev->flags))
1868 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869#endif
1870 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001871 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872}
1873
1874static void kick_rdev_from_array(mdk_rdev_t * rdev)
1875{
1876 unbind_rdev_from_array(rdev);
1877 export_rdev(rdev);
1878}
1879
1880static void export_array(mddev_t *mddev)
1881{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001882 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
NeilBrownd089c6a2008-02-06 01:39:59 -08001884 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if (!rdev->mddev) {
1886 MD_BUG();
1887 continue;
1888 }
1889 kick_rdev_from_array(rdev);
1890 }
1891 if (!list_empty(&mddev->disks))
1892 MD_BUG();
1893 mddev->raid_disks = 0;
1894 mddev->major_version = 0;
1895}
1896
1897static void print_desc(mdp_disk_t *desc)
1898{
1899 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1900 desc->major,desc->minor,desc->raid_disk,desc->state);
1901}
1902
Cheng Renquancd2ac932009-01-09 08:31:08 +11001903static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
1905 int i;
1906
1907 printk(KERN_INFO
1908 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1909 sb->major_version, sb->minor_version, sb->patch_version,
1910 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1911 sb->ctime);
1912 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1913 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1914 sb->md_minor, sb->layout, sb->chunk_size);
1915 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1916 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1917 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1918 sb->failed_disks, sb->spare_disks,
1919 sb->sb_csum, (unsigned long)sb->events_lo);
1920
1921 printk(KERN_INFO);
1922 for (i = 0; i < MD_SB_DISKS; i++) {
1923 mdp_disk_t *desc;
1924
1925 desc = sb->disks + i;
1926 if (desc->number || desc->major || desc->minor ||
1927 desc->raid_disk || (desc->state && (desc->state != 4))) {
1928 printk(" D %2d: ", i);
1929 print_desc(desc);
1930 }
1931 }
1932 printk(KERN_INFO "md: THIS: ");
1933 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934}
1935
Cheng Renquancd2ac932009-01-09 08:31:08 +11001936static void print_sb_1(struct mdp_superblock_1 *sb)
1937{
1938 __u8 *uuid;
1939
1940 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001941 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08001942 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001943 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001944 le32_to_cpu(sb->major_version),
1945 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08001946 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11001947 sb->set_name,
1948 (unsigned long long)le64_to_cpu(sb->ctime)
1949 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1950
1951 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001952 printk(KERN_INFO
1953 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11001954 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08001955 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001956 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1957 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001958 le32_to_cpu(sb->level),
1959 (unsigned long long)le64_to_cpu(sb->size),
1960 le32_to_cpu(sb->raid_disks),
1961 le32_to_cpu(sb->layout),
1962 le32_to_cpu(sb->chunksize),
1963 (unsigned long long)le64_to_cpu(sb->data_offset),
1964 (unsigned long long)le64_to_cpu(sb->data_size),
1965 (unsigned long long)le64_to_cpu(sb->super_offset),
1966 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1967 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08001968 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11001969 sb->devflags,
1970 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1971 (unsigned long long)le64_to_cpu(sb->events),
1972 (unsigned long long)le64_to_cpu(sb->resync_offset),
1973 le32_to_cpu(sb->sb_csum),
1974 le32_to_cpu(sb->max_dev)
1975 );
1976}
1977
1978static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
1980 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11001981 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1982 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08001983 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1984 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11001986 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1987 switch (major_version) {
1988 case 0:
1989 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1990 break;
1991 case 1:
1992 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1993 break;
1994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 } else
1996 printk(KERN_INFO "md: no rdev superblock!\n");
1997}
1998
Adrian Bunk5e563412006-06-26 00:27:42 -07001999static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002001 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 mdk_rdev_t *rdev;
2003 mddev_t *mddev;
2004 char b[BDEVNAME_SIZE];
2005
2006 printk("\n");
2007 printk("md: **********************************\n");
2008 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2009 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002010 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
NeilBrown32a76272005-06-21 17:17:14 -07002012 if (mddev->bitmap)
2013 bitmap_print_sb(mddev->bitmap);
2014 else
2015 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002016 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 printk("<%s>", bdevname(rdev->bdev,b));
2018 printk("\n");
2019
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002020 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002021 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
2023 printk("md: **********************************\n");
2024 printk("\n");
2025}
2026
2027
NeilBrown42543762006-06-26 00:27:57 -07002028static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
NeilBrown42543762006-06-26 00:27:57 -07002030 /* Update each superblock (in-memory image), but
2031 * if we are allowed to, skip spares which already
2032 * have the right event counter, or have one earlier
2033 * (which would mean they aren't being marked as dirty
2034 * with the rest of the array)
2035 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
NeilBrown5e865102009-11-12 12:08:04 +11002038 /* First make sure individual recovery_offsets are correct */
2039 list_for_each_entry(rdev, &mddev->disks, same_set) {
2040 if (rdev->raid_disk >= 0 &&
2041 !test_bit(In_sync, &rdev->flags) &&
2042 mddev->curr_resync_completed > rdev->recovery_offset)
2043 rdev->recovery_offset = mddev->curr_resync_completed;
2044
2045 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002046 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002047 if (rdev->sb_events == mddev->events ||
2048 (nospares &&
2049 rdev->raid_disk < 0 &&
2050 (rdev->sb_events&1)==0 &&
2051 rdev->sb_events+1 == mddev->events)) {
2052 /* Don't update this superblock */
2053 rdev->sb_loaded = 2;
2054 } else {
2055 super_types[mddev->major_version].
2056 sync_super(mddev, rdev);
2057 rdev->sb_loaded = 1;
2058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 }
2060}
2061
NeilBrown850b2b42006-10-03 01:15:46 -07002062static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002065 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002066 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
NeilBrown1b57f132009-06-18 08:48:19 +10002068 mddev->utime = get_seconds();
NeilBrown8377bc82008-04-30 00:52:28 -07002069 if (mddev->external)
2070 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08002072 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002073
NeilBrown850b2b42006-10-03 01:15:46 -07002074 set_bit(MD_CHANGE_PENDING, &mddev->flags);
2075 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2076 force_change = 1;
2077 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2078 /* just a clean<-> dirty transition, possibly leave spares alone,
2079 * though if events isn't the right even/odd, we will have to do
2080 * spares after all
2081 */
2082 nospares = 1;
2083 if (force_change)
2084 nospares = 0;
2085 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002086 /* If the array is degraded, then skipping spares is both
2087 * dangerous and fairly pointless.
2088 * Dangerous because a device that was removed from the array
2089 * might have a event_count that still looks up-to-date,
2090 * so it can be re-added without a resync.
2091 * Pointless because if there are any spares to skip,
2092 * then a recovery will happen and soon that array won't
2093 * be degraded any more and the spare can go back to sleep then.
2094 */
NeilBrown850b2b42006-10-03 01:15:46 -07002095 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002096
NeilBrown06d91a52005-06-21 17:17:12 -07002097 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002098
2099 /* If this is just a dirty<->clean transition, and the array is clean
2100 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002101 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002102 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08002103 && (mddev->events & 1)
2104 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07002105 mddev->events--;
2106 else {
2107 /* otherwise we have to go forward and ... */
2108 mddev->events ++;
2109 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
NeilBrown51d56682009-08-13 09:54:02 +10002110 /* .. if the array isn't clean, an 'even' event must also go
2111 * to spares. */
2112 if ((mddev->events&1)==0)
NeilBrown42543762006-06-26 00:27:57 -07002113 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002114 } else {
NeilBrown51d56682009-08-13 09:54:02 +10002115 /* otherwise an 'odd' event must go to spares */
2116 if ((mddev->events&1))
NeilBrown42543762006-06-26 00:27:57 -07002117 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002118 }
2119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121 if (!mddev->events) {
2122 /*
2123 * oops, this 64-bit counter should never wrap.
2124 * Either we are in around ~1 trillion A.C., assuming
2125 * 1 reboot per second, or we have a bug:
2126 */
2127 MD_BUG();
2128 mddev->events --;
2129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
2131 /*
2132 * do not write anything to disk if using
2133 * nonpersistent superblocks
2134 */
NeilBrown06d91a52005-06-21 17:17:12 -07002135 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08002136 if (!mddev->external)
2137 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2138
NeilBrowna9701a32005-11-08 21:39:34 -08002139 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002140 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return;
NeilBrown06d91a52005-06-21 17:17:12 -07002142 }
NeilBrowne6910632008-02-06 01:39:51 -08002143 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002144 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 dprintk(KERN_INFO
2147 "md: updating %s RAID superblock on device (in sync %d)\n",
2148 mdname(mddev),mddev->in_sync);
2149
NeilBrown4ad13662007-07-17 04:06:13 -07002150 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002151 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 char b[BDEVNAME_SIZE];
2153 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002154 if (rdev->sb_loaded != 1)
2155 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002156 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 dprintk("(skipping faulty ");
2158
2159 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002160 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002161 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002162 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002163 rdev->sb_page);
2164 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2165 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002166 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002167 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 } else
2170 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002171 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 /* only need to write one superblock... */
2173 break;
2174 }
NeilBrowna9701a32005-11-08 21:39:34 -08002175 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002176 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002177
NeilBrowna9701a32005-11-08 21:39:34 -08002178 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002179 if (mddev->in_sync != sync_req ||
2180 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002181 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002182 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002183 goto repeat;
2184 }
NeilBrown850b2b42006-10-03 01:15:46 -07002185 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002186 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002187 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002188 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2189 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191}
2192
Andre Noll7f6ce762008-03-23 18:34:54 +01002193/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002194 * We want to accept with case. For this we use cmd_match.
2195 */
2196static int cmd_match(const char *cmd, const char *str)
2197{
2198 /* See if cmd, written into a sysfs file, matches
2199 * str. They must either be the same, or cmd can
2200 * have a trailing newline
2201 */
2202 while (*cmd && *str && *cmd == *str) {
2203 cmd++;
2204 str++;
2205 }
2206 if (*cmd == '\n')
2207 cmd++;
2208 if (*str || *cmd)
2209 return 0;
2210 return 1;
2211}
2212
NeilBrown86e6ffd2005-11-08 21:39:24 -08002213struct rdev_sysfs_entry {
2214 struct attribute attr;
2215 ssize_t (*show)(mdk_rdev_t *, char *);
2216 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2217};
2218
2219static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002220state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002221{
2222 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002223 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002224
NeilBrownb2d444d2005-11-08 21:39:31 -08002225 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002226 len+= sprintf(page+len, "%sfaulty",sep);
2227 sep = ",";
2228 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002229 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002230 len += sprintf(page+len, "%sin_sync",sep);
2231 sep = ",";
2232 }
NeilBrownf6556752006-06-26 00:28:01 -07002233 if (test_bit(WriteMostly, &rdev->flags)) {
2234 len += sprintf(page+len, "%swrite_mostly",sep);
2235 sep = ",";
2236 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002237 if (test_bit(Blocked, &rdev->flags)) {
2238 len += sprintf(page+len, "%sblocked", sep);
2239 sep = ",";
2240 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002241 if (!test_bit(Faulty, &rdev->flags) &&
2242 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002243 len += sprintf(page+len, "%sspare", sep);
2244 sep = ",";
2245 }
2246 return len+sprintf(page+len, "\n");
2247}
2248
NeilBrown45dc2de2006-06-26 00:27:58 -07002249static ssize_t
2250state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2251{
2252 /* can write
2253 * faulty - simulates and error
2254 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002255 * writemostly - sets write_mostly
2256 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002257 * blocked - sets the Blocked flag
2258 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002259 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002260 */
2261 int err = -EINVAL;
2262 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2263 md_error(rdev->mddev, rdev);
2264 err = 0;
2265 } else if (cmd_match(buf, "remove")) {
2266 if (rdev->raid_disk >= 0)
2267 err = -EBUSY;
2268 else {
2269 mddev_t *mddev = rdev->mddev;
2270 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002271 if (mddev->pers)
2272 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002273 md_new_event(mddev);
2274 err = 0;
2275 }
NeilBrownf6556752006-06-26 00:28:01 -07002276 } else if (cmd_match(buf, "writemostly")) {
2277 set_bit(WriteMostly, &rdev->flags);
2278 err = 0;
2279 } else if (cmd_match(buf, "-writemostly")) {
2280 clear_bit(WriteMostly, &rdev->flags);
2281 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002282 } else if (cmd_match(buf, "blocked")) {
2283 set_bit(Blocked, &rdev->flags);
2284 err = 0;
2285 } else if (cmd_match(buf, "-blocked")) {
2286 clear_bit(Blocked, &rdev->flags);
2287 wake_up(&rdev->blocked_wait);
2288 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2289 md_wakeup_thread(rdev->mddev->thread);
2290
2291 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002292 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2293 set_bit(In_sync, &rdev->flags);
2294 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002295 }
NeilBrown3c0ee632008-10-21 13:25:28 +11002296 if (!err && rdev->sysfs_state)
2297 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002298 return err ? err : len;
2299}
NeilBrown80ca3a42006-07-10 04:44:18 -07002300static struct rdev_sysfs_entry rdev_state =
2301__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002302
2303static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002304errors_show(mdk_rdev_t *rdev, char *page)
2305{
2306 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2307}
2308
2309static ssize_t
2310errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2311{
2312 char *e;
2313 unsigned long n = simple_strtoul(buf, &e, 10);
2314 if (*buf && (*e == 0 || *e == '\n')) {
2315 atomic_set(&rdev->corrected_errors, n);
2316 return len;
2317 }
2318 return -EINVAL;
2319}
2320static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002321__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002322
NeilBrown014236d2006-01-06 00:20:55 -08002323static ssize_t
2324slot_show(mdk_rdev_t *rdev, char *page)
2325{
2326 if (rdev->raid_disk < 0)
2327 return sprintf(page, "none\n");
2328 else
2329 return sprintf(page, "%d\n", rdev->raid_disk);
2330}
2331
2332static ssize_t
2333slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2334{
2335 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002336 int err;
2337 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002338 int slot = simple_strtoul(buf, &e, 10);
2339 if (strncmp(buf, "none", 4)==0)
2340 slot = -1;
2341 else if (e==buf || (*e && *e!= '\n'))
2342 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002343 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002344 /* Setting 'slot' on an active array requires also
2345 * updating the 'rd%d' link, and communicating
2346 * with the personality with ->hot_*_disk.
2347 * For now we only support removing
2348 * failed/spare devices. This normally happens automatically,
2349 * but not when the metadata is externally managed.
2350 */
NeilBrownc303da62008-02-06 01:39:51 -08002351 if (rdev->raid_disk == -1)
2352 return -EEXIST;
2353 /* personality does all needed checks */
2354 if (rdev->mddev->pers->hot_add_disk == NULL)
2355 return -EINVAL;
2356 err = rdev->mddev->pers->
2357 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2358 if (err)
2359 return err;
2360 sprintf(nm, "rd%d", rdev->raid_disk);
2361 sysfs_remove_link(&rdev->mddev->kobj, nm);
2362 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2363 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002364 } else if (rdev->mddev->pers) {
2365 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002366 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002367 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002368 */
2369
2370 if (rdev->raid_disk != -1)
2371 return -EBUSY;
2372
2373 if (rdev->mddev->pers->hot_add_disk == NULL)
2374 return -EINVAL;
2375
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002376 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002377 if (rdev2->raid_disk == slot)
2378 return -EEXIST;
2379
2380 rdev->raid_disk = slot;
2381 if (test_bit(In_sync, &rdev->flags))
2382 rdev->saved_raid_disk = slot;
2383 else
2384 rdev->saved_raid_disk = -1;
2385 err = rdev->mddev->pers->
2386 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002387 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002388 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002389 return err;
Neil Brown52664732008-06-28 08:31:44 +10002390 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002391 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002392 sprintf(nm, "rd%d", rdev->raid_disk);
2393 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2394 printk(KERN_WARNING
2395 "md: cannot register "
2396 "%s for %s\n",
2397 nm, mdname(rdev->mddev));
2398
2399 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002400 } else {
2401 if (slot >= rdev->mddev->raid_disks)
2402 return -ENOSPC;
2403 rdev->raid_disk = slot;
2404 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002405 clear_bit(Faulty, &rdev->flags);
2406 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002407 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002408 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002409 }
NeilBrown014236d2006-01-06 00:20:55 -08002410 return len;
2411}
2412
2413
2414static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002415__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002416
NeilBrown93c8cad2006-01-06 00:20:56 -08002417static ssize_t
2418offset_show(mdk_rdev_t *rdev, char *page)
2419{
Andrew Morton6961ece2006-01-06 00:20:59 -08002420 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002421}
2422
2423static ssize_t
2424offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2425{
2426 char *e;
2427 unsigned long long offset = simple_strtoull(buf, &e, 10);
2428 if (e==buf || (*e && *e != '\n'))
2429 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002430 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002431 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002432 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002433 /* Must set offset before size, so overlap checks
2434 * can be sane */
2435 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002436 rdev->data_offset = offset;
2437 return len;
2438}
2439
2440static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002441__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002442
NeilBrown83303b62006-01-06 00:21:06 -08002443static ssize_t
2444rdev_size_show(mdk_rdev_t *rdev, char *page)
2445{
Andre Nolldd8ac332009-03-31 14:33:13 +11002446 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002447}
2448
NeilBrownc5d79ad2008-02-06 01:39:54 -08002449static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2450{
2451 /* check if two start/length pairs overlap */
2452 if (s1+l1 <= s2)
2453 return 0;
2454 if (s2+l2 <= s1)
2455 return 0;
2456 return 1;
2457}
2458
Dan Williamsb522adc2009-03-31 15:00:31 +11002459static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2460{
2461 unsigned long long blocks;
2462 sector_t new;
2463
2464 if (strict_strtoull(buf, 10, &blocks) < 0)
2465 return -EINVAL;
2466
2467 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2468 return -EINVAL; /* sector conversion overflow */
2469
2470 new = blocks * 2;
2471 if (new != blocks * 2)
2472 return -EINVAL; /* unsigned long long to sector_t overflow */
2473
2474 *sectors = new;
2475 return 0;
2476}
2477
NeilBrown83303b62006-01-06 00:21:06 -08002478static ssize_t
2479rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2480{
NeilBrown27c529b2008-03-04 14:29:33 -08002481 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002482 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002483 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002484
Dan Williamsb522adc2009-03-31 15:00:31 +11002485 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002486 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002487 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002488 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002489 sectors = super_types[my_mddev->major_version].
2490 rdev_size_change(rdev, sectors);
2491 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002492 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002493 } else if (!sectors)
2494 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2495 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002496 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002497 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002498 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002499
Andre Nolldd8ac332009-03-31 14:33:13 +11002500 rdev->sectors = sectors;
2501 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002502 /* need to check that all other rdevs with the same ->bdev
2503 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002504 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002505 * we have to change it back, we will have the lock again.
2506 */
2507 mddev_t *mddev;
2508 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002509 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002510
NeilBrown27c529b2008-03-04 14:29:33 -08002511 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002512 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002513 mdk_rdev_t *rdev2;
2514
2515 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002516 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002517 if (test_bit(AllReserved, &rdev2->flags) ||
2518 (rdev->bdev == rdev2->bdev &&
2519 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002520 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002521 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002522 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002523 overlap = 1;
2524 break;
2525 }
2526 mddev_unlock(mddev);
2527 if (overlap) {
2528 mddev_put(mddev);
2529 break;
2530 }
2531 }
NeilBrown27c529b2008-03-04 14:29:33 -08002532 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002533 if (overlap) {
2534 /* Someone else could have slipped in a size
2535 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002536 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002537 * safe, and trust userspace not to race with
2538 * itself
2539 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002540 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002541 return -EBUSY;
2542 }
2543 }
NeilBrown83303b62006-01-06 00:21:06 -08002544 return len;
2545}
2546
2547static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002548__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002549
Dan Williams06e3c812009-12-12 21:17:12 -07002550
2551static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2552{
2553 unsigned long long recovery_start = rdev->recovery_offset;
2554
2555 if (test_bit(In_sync, &rdev->flags) ||
2556 recovery_start == MaxSector)
2557 return sprintf(page, "none\n");
2558
2559 return sprintf(page, "%llu\n", recovery_start);
2560}
2561
2562static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2563{
2564 unsigned long long recovery_start;
2565
2566 if (cmd_match(buf, "none"))
2567 recovery_start = MaxSector;
2568 else if (strict_strtoull(buf, 10, &recovery_start))
2569 return -EINVAL;
2570
2571 if (rdev->mddev->pers &&
2572 rdev->raid_disk >= 0)
2573 return -EBUSY;
2574
2575 rdev->recovery_offset = recovery_start;
2576 if (recovery_start == MaxSector)
2577 set_bit(In_sync, &rdev->flags);
2578 else
2579 clear_bit(In_sync, &rdev->flags);
2580 return len;
2581}
2582
2583static struct rdev_sysfs_entry rdev_recovery_start =
2584__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2585
NeilBrown86e6ffd2005-11-08 21:39:24 -08002586static struct attribute *rdev_default_attrs[] = {
2587 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002588 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002589 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002590 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002591 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002592 &rdev_recovery_start.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002593 NULL,
2594};
2595static ssize_t
2596rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2597{
2598 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2599 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002600 mddev_t *mddev = rdev->mddev;
2601 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002602
2603 if (!entry->show)
2604 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002605
2606 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2607 if (!rv) {
2608 if (rdev->mddev == NULL)
2609 rv = -EBUSY;
2610 else
2611 rv = entry->show(rdev, page);
2612 mddev_unlock(mddev);
2613 }
2614 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002615}
2616
2617static ssize_t
2618rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2619 const char *page, size_t length)
2620{
2621 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2622 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002623 ssize_t rv;
2624 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002625
2626 if (!entry->store)
2627 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002628 if (!capable(CAP_SYS_ADMIN))
2629 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002630 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002631 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002632 if (rdev->mddev == NULL)
2633 rv = -EBUSY;
2634 else
2635 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002636 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002637 }
2638 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002639}
2640
2641static void rdev_free(struct kobject *ko)
2642{
2643 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2644 kfree(rdev);
2645}
Emese Revfy52cf25d2010-01-19 02:58:23 +01002646static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002647 .show = rdev_attr_show,
2648 .store = rdev_attr_store,
2649};
2650static struct kobj_type rdev_ktype = {
2651 .release = rdev_free,
2652 .sysfs_ops = &rdev_sysfs_ops,
2653 .default_attrs = rdev_default_attrs,
2654};
2655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656/*
2657 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2658 *
2659 * mark the device faulty if:
2660 *
2661 * - the device is nonexistent (zero size)
2662 * - the device has no valid superblock
2663 *
2664 * a faulty rdev _never_ has rdev->sb set.
2665 */
2666static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2667{
2668 char b[BDEVNAME_SIZE];
2669 int err;
2670 mdk_rdev_t *rdev;
2671 sector_t size;
2672
NeilBrown9ffae0c2006-01-06 00:20:32 -08002673 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 if (!rdev) {
2675 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2676 return ERR_PTR(-ENOMEM);
2677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
2679 if ((err = alloc_disk_sb(rdev)))
2680 goto abort_free;
2681
NeilBrownc5d79ad2008-02-06 01:39:54 -08002682 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 if (err)
2684 goto abort_free;
2685
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002686 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002689 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002690 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002691 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002693 rdev->sb_events = 0;
Robert Becker1e509152009-12-14 12:49:58 +11002694 rdev->last_read_error.tv_sec = 0;
2695 rdev->last_read_error.tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002697 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002698 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
2700 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2701 if (!size) {
2702 printk(KERN_WARNING
2703 "md: %s has zero or unknown size, marking faulty!\n",
2704 bdevname(rdev->bdev,b));
2705 err = -EINVAL;
2706 goto abort_free;
2707 }
2708
2709 if (super_format >= 0) {
2710 err = super_types[super_format].
2711 load_super(rdev, NULL, super_minor);
2712 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002713 printk(KERN_WARNING
2714 "md: %s does not have a valid v%d.%d "
2715 "superblock, not importing!\n",
2716 bdevname(rdev->bdev,b),
2717 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 goto abort_free;
2719 }
2720 if (err < 0) {
2721 printk(KERN_WARNING
2722 "md: could not read %s's sb, not importing!\n",
2723 bdevname(rdev->bdev,b));
2724 goto abort_free;
2725 }
2726 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002729 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
2731 return rdev;
2732
2733abort_free:
2734 if (rdev->sb_page) {
2735 if (rdev->bdev)
2736 unlock_rdev(rdev);
2737 free_disk_sb(rdev);
2738 }
2739 kfree(rdev);
2740 return ERR_PTR(err);
2741}
2742
2743/*
2744 * Check a full RAID array for plausibility
2745 */
2746
2747
NeilBrowna757e642005-04-16 15:26:42 -07002748static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749{
2750 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002751 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 char b[BDEVNAME_SIZE];
2753
2754 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002755 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 switch (super_types[mddev->major_version].
2757 load_super(rdev, freshest, mddev->minor_version)) {
2758 case 1:
2759 freshest = rdev;
2760 break;
2761 case 0:
2762 break;
2763 default:
2764 printk( KERN_ERR \
2765 "md: fatal superblock inconsistency in %s"
2766 " -- removing from array\n",
2767 bdevname(rdev->bdev,b));
2768 kick_rdev_from_array(rdev);
2769 }
2770
2771
2772 super_types[mddev->major_version].
2773 validate_super(mddev, freshest);
2774
2775 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002776 rdev_for_each(rdev, tmp, mddev) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002777 if (rdev->desc_nr >= mddev->max_disks ||
2778 i > mddev->max_disks) {
2779 printk(KERN_WARNING
2780 "md: %s: %s: only %d devices permitted\n",
2781 mdname(mddev), bdevname(rdev->bdev, b),
2782 mddev->max_disks);
2783 kick_rdev_from_array(rdev);
2784 continue;
2785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 if (rdev != freshest)
2787 if (super_types[mddev->major_version].
2788 validate_super(mddev, rdev)) {
2789 printk(KERN_WARNING "md: kicking non-fresh %s"
2790 " from array!\n",
2791 bdevname(rdev->bdev,b));
2792 kick_rdev_from_array(rdev);
2793 continue;
2794 }
2795 if (mddev->level == LEVEL_MULTIPATH) {
2796 rdev->desc_nr = i++;
2797 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002798 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002799 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002800 rdev->raid_disk = -1;
2801 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 }
2803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804}
2805
NeilBrown72e02072009-12-14 12:49:55 +11002806/* Read a fixed-point number.
2807 * Numbers in sysfs attributes should be in "standard" units where
2808 * possible, so time should be in seconds.
2809 * However we internally use a a much smaller unit such as
2810 * milliseconds or jiffies.
2811 * This function takes a decimal number with a possible fractional
2812 * component, and produces an integer which is the result of
2813 * multiplying that number by 10^'scale'.
2814 * all without any floating-point arithmetic.
2815 */
2816int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2817{
2818 unsigned long result = 0;
2819 long decimals = -1;
2820 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2821 if (*cp == '.')
2822 decimals = 0;
2823 else if (decimals < scale) {
2824 unsigned int value;
2825 value = *cp - '0';
2826 result = result * 10 + value;
2827 if (decimals >= 0)
2828 decimals++;
2829 }
2830 cp++;
2831 }
2832 if (*cp == '\n')
2833 cp++;
2834 if (*cp)
2835 return -EINVAL;
2836 if (decimals < 0)
2837 decimals = 0;
2838 while (decimals < scale) {
2839 result *= 10;
2840 decimals ++;
2841 }
2842 *res = result;
2843 return 0;
2844}
2845
2846
NeilBrown19052c02008-08-05 15:54:13 +10002847static void md_safemode_timeout(unsigned long data);
2848
NeilBrowneae17012005-11-08 21:39:23 -08002849static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002850safe_delay_show(mddev_t *mddev, char *page)
2851{
2852 int msec = (mddev->safemode_delay*1000)/HZ;
2853 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2854}
2855static ssize_t
2856safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2857{
NeilBrown16f17b32006-06-26 00:27:37 -07002858 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07002859
NeilBrown72e02072009-12-14 12:49:55 +11002860 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002861 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07002862 if (msec == 0)
2863 mddev->safemode_delay = 0;
2864 else {
NeilBrown19052c02008-08-05 15:54:13 +10002865 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002866 mddev->safemode_delay = (msec*HZ)/1000;
2867 if (mddev->safemode_delay == 0)
2868 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002869 if (mddev->safemode_delay < old_delay)
2870 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002871 }
2872 return len;
2873}
2874static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002875__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002876
2877static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002878level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002879{
NeilBrown2604b702006-01-06 00:20:36 -08002880 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002881 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002882 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002883 else if (mddev->clevel[0])
2884 return sprintf(page, "%s\n", mddev->clevel);
2885 else if (mddev->level != LEVEL_NONE)
2886 return sprintf(page, "%d\n", mddev->level);
2887 else
2888 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002889}
2890
NeilBrownd9d166c2006-01-06 00:20:51 -08002891static ssize_t
2892level_store(mddev_t *mddev, const char *buf, size_t len)
2893{
NeilBrown245f46c2009-03-31 14:39:39 +11002894 char level[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002895 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002896 struct mdk_personality *pers;
2897 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002898 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002899
2900 if (mddev->pers == NULL) {
2901 if (len == 0)
2902 return 0;
2903 if (len >= sizeof(mddev->clevel))
2904 return -ENOSPC;
2905 strncpy(mddev->clevel, buf, len);
2906 if (mddev->clevel[len-1] == '\n')
2907 len--;
2908 mddev->clevel[len] = 0;
2909 mddev->level = LEVEL_NONE;
2910 return rv;
2911 }
2912
2913 /* request to change the personality. Need to ensure:
2914 * - array is not engaged in resync/recovery/reshape
2915 * - old personality can be suspended
2916 * - new personality will access other array.
2917 */
2918
2919 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownd9d166c2006-01-06 00:20:51 -08002920 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002921
2922 if (!mddev->pers->quiesce) {
2923 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2924 mdname(mddev), mddev->pers->name);
2925 return -EINVAL;
2926 }
2927
2928 /* Now find the new personality */
2929 if (len == 0 || len >= sizeof(level))
2930 return -EINVAL;
2931 strncpy(level, buf, len);
2932 if (level[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002933 len--;
NeilBrown245f46c2009-03-31 14:39:39 +11002934 level[len] = 0;
2935
2936 request_module("md-%s", level);
2937 spin_lock(&pers_lock);
2938 pers = find_pers(LEVEL_NONE, level);
2939 if (!pers || !try_module_get(pers->owner)) {
2940 spin_unlock(&pers_lock);
2941 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2942 return -EINVAL;
2943 }
2944 spin_unlock(&pers_lock);
2945
2946 if (pers == mddev->pers) {
2947 /* Nothing to do! */
2948 module_put(pers->owner);
2949 return rv;
2950 }
2951 if (!pers->takeover) {
2952 module_put(pers->owner);
2953 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2954 mdname(mddev), level);
2955 return -EINVAL;
2956 }
2957
2958 /* ->takeover must set new_* and/or delta_disks
2959 * if it succeeds, and may set them when it fails.
2960 */
2961 priv = pers->takeover(mddev);
2962 if (IS_ERR(priv)) {
2963 mddev->new_level = mddev->level;
2964 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002965 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002966 mddev->raid_disks -= mddev->delta_disks;
2967 mddev->delta_disks = 0;
2968 module_put(pers->owner);
2969 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2970 mdname(mddev), level);
2971 return PTR_ERR(priv);
2972 }
2973
2974 /* Looks like we have a winner */
2975 mddev_suspend(mddev);
2976 mddev->pers->stop(mddev);
2977 module_put(mddev->pers->owner);
NeilBrown3a981b02009-08-03 10:59:55 +10002978 /* Invalidate devices that are now superfluous */
2979 list_for_each_entry(rdev, &mddev->disks, same_set)
2980 if (rdev->raid_disk >= mddev->raid_disks) {
2981 rdev->raid_disk = -1;
2982 clear_bit(In_sync, &rdev->flags);
2983 }
NeilBrown245f46c2009-03-31 14:39:39 +11002984 mddev->pers = pers;
2985 mddev->private = priv;
2986 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2987 mddev->level = mddev->new_level;
2988 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002989 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002990 mddev->delta_disks = 0;
2991 pers->run(mddev);
2992 mddev_resume(mddev);
2993 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2994 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2995 md_wakeup_thread(mddev->thread);
NeilBrownd9d166c2006-01-06 00:20:51 -08002996 return rv;
2997}
2998
2999static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003000__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003001
NeilBrownd4dbd022006-06-26 00:27:59 -07003002
3003static ssize_t
3004layout_show(mddev_t *mddev, char *page)
3005{
3006 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003007 if (mddev->reshape_position != MaxSector &&
3008 mddev->layout != mddev->new_layout)
3009 return sprintf(page, "%d (%d)\n",
3010 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003011 return sprintf(page, "%d\n", mddev->layout);
3012}
3013
3014static ssize_t
3015layout_store(mddev_t *mddev, const char *buf, size_t len)
3016{
3017 char *e;
3018 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003019
3020 if (!*buf || (*e && *e != '\n'))
3021 return -EINVAL;
3022
NeilBrownb3546032009-03-31 14:56:41 +11003023 if (mddev->pers) {
3024 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003025 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003026 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003027 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003028 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003029 if (err) {
3030 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003031 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003032 }
NeilBrownb3546032009-03-31 14:56:41 +11003033 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003034 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003035 if (mddev->reshape_position == MaxSector)
3036 mddev->layout = n;
3037 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003038 return len;
3039}
3040static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003041__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003042
3043
NeilBrowneae17012005-11-08 21:39:23 -08003044static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003045raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003046{
NeilBrownbb636542005-11-08 21:39:45 -08003047 if (mddev->raid_disks == 0)
3048 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003049 if (mddev->reshape_position != MaxSector &&
3050 mddev->delta_disks != 0)
3051 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3052 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003053 return sprintf(page, "%d\n", mddev->raid_disks);
3054}
3055
NeilBrownda943b92006-01-06 00:20:54 -08003056static int update_raid_disks(mddev_t *mddev, int raid_disks);
3057
3058static ssize_t
3059raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3060{
NeilBrownda943b92006-01-06 00:20:54 -08003061 char *e;
3062 int rv = 0;
3063 unsigned long n = simple_strtoul(buf, &e, 10);
3064
3065 if (!*buf || (*e && *e != '\n'))
3066 return -EINVAL;
3067
3068 if (mddev->pers)
3069 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003070 else if (mddev->reshape_position != MaxSector) {
3071 int olddisks = mddev->raid_disks - mddev->delta_disks;
3072 mddev->delta_disks = n - olddisks;
3073 mddev->raid_disks = n;
3074 } else
NeilBrownda943b92006-01-06 00:20:54 -08003075 mddev->raid_disks = n;
3076 return rv ? rv : len;
3077}
3078static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003079__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003080
NeilBrown24dd4692005-11-08 21:39:26 -08003081static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003082chunk_size_show(mddev_t *mddev, char *page)
3083{
NeilBrown08a02ec2007-05-09 02:35:38 -07003084 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003085 mddev->chunk_sectors != mddev->new_chunk_sectors)
3086 return sprintf(page, "%d (%d)\n",
3087 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003088 mddev->chunk_sectors << 9);
3089 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003090}
3091
3092static ssize_t
3093chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3094{
NeilBrown3b343802006-01-06 00:20:47 -08003095 char *e;
3096 unsigned long n = simple_strtoul(buf, &e, 10);
3097
NeilBrown3b343802006-01-06 00:20:47 -08003098 if (!*buf || (*e && *e != '\n'))
3099 return -EINVAL;
3100
NeilBrownb3546032009-03-31 14:56:41 +11003101 if (mddev->pers) {
3102 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003103 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003104 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003105 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003106 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003107 if (err) {
3108 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003109 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003110 }
NeilBrownb3546032009-03-31 14:56:41 +11003111 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003112 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003113 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003114 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003115 }
NeilBrown3b343802006-01-06 00:20:47 -08003116 return len;
3117}
3118static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003119__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003120
NeilBrowna94213b2006-06-26 00:28:00 -07003121static ssize_t
3122resync_start_show(mddev_t *mddev, char *page)
3123{
NeilBrownd1a7c502009-03-31 15:24:32 +11003124 if (mddev->recovery_cp == MaxSector)
3125 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003126 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3127}
3128
3129static ssize_t
3130resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3131{
NeilBrowna94213b2006-06-26 00:28:00 -07003132 char *e;
3133 unsigned long long n = simple_strtoull(buf, &e, 10);
3134
3135 if (mddev->pers)
3136 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003137 if (cmd_match(buf, "none"))
3138 n = MaxSector;
3139 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003140 return -EINVAL;
3141
3142 mddev->recovery_cp = n;
3143 return len;
3144}
3145static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003146__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003147
NeilBrown9e653b62006-06-26 00:27:58 -07003148/*
3149 * The array state can be:
3150 *
3151 * clear
3152 * No devices, no size, no level
3153 * Equivalent to STOP_ARRAY ioctl
3154 * inactive
3155 * May have some settings, but array is not active
3156 * all IO results in error
3157 * When written, doesn't tear down array, but just stops it
3158 * suspended (not supported yet)
3159 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003160 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003161 * readonly
3162 * no resync can happen. no superblocks get written.
3163 * write requests fail
3164 * read-auto
3165 * like readonly, but behaves like 'clean' on a write request.
3166 *
3167 * clean - no pending writes, but otherwise active.
3168 * When written to inactive array, starts without resync
3169 * If a write request arrives then
3170 * if metadata is known, mark 'dirty' and switch to 'active'.
3171 * if not known, block and switch to write-pending
3172 * If written to an active array that has pending writes, then fails.
3173 * active
3174 * fully active: IO and resync can be happening.
3175 * When written to inactive array, starts with resync
3176 *
3177 * write-pending
3178 * clean, but writes are blocked waiting for 'active' to be written.
3179 *
3180 * active-idle
3181 * like active, but no writes have been seen for a while (100msec).
3182 *
3183 */
3184enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3185 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003186static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003187 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3188 "write-pending", "active-idle", NULL };
3189
3190static int match_word(const char *word, char **list)
3191{
3192 int n;
3193 for (n=0; list[n]; n++)
3194 if (cmd_match(word, list[n]))
3195 break;
3196 return n;
3197}
3198
3199static ssize_t
3200array_state_show(mddev_t *mddev, char *page)
3201{
3202 enum array_state st = inactive;
3203
3204 if (mddev->pers)
3205 switch(mddev->ro) {
3206 case 1:
3207 st = readonly;
3208 break;
3209 case 2:
3210 st = read_auto;
3211 break;
3212 case 0:
3213 if (mddev->in_sync)
3214 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08003215 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3216 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003217 else if (mddev->safemode)
3218 st = active_idle;
3219 else
3220 st = active;
3221 }
3222 else {
3223 if (list_empty(&mddev->disks) &&
3224 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003225 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003226 st = clear;
3227 else
3228 st = inactive;
3229 }
3230 return sprintf(page, "%s\n", array_states[st]);
3231}
3232
Neil Browndf5b20c2008-07-11 22:02:22 +10003233static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003234static int do_md_run(mddev_t * mddev);
3235static int restart_array(mddev_t *mddev);
3236
3237static ssize_t
3238array_state_store(mddev_t *mddev, const char *buf, size_t len)
3239{
3240 int err = -EINVAL;
3241 enum array_state st = match_word(buf, array_states);
3242 switch(st) {
3243 case bad_word:
3244 break;
3245 case clear:
3246 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003247 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003248 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003249 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003250 break;
3251 case inactive:
3252 /* stopping an active array */
3253 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003254 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003255 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003256 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003257 } else
3258 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003259 break;
3260 case suspended:
3261 break; /* not supported yet */
3262 case readonly:
3263 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10003264 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003265 else {
3266 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003267 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003268 err = do_md_run(mddev);
3269 }
3270 break;
3271 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003272 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003273 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10003274 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003275 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003276 err = restart_array(mddev);
3277 if (err == 0) {
3278 mddev->ro = 2;
3279 set_disk_ro(mddev->gendisk, 0);
3280 }
NeilBrown9e653b62006-06-26 00:27:58 -07003281 } else {
3282 mddev->ro = 2;
3283 err = do_md_run(mddev);
3284 }
3285 break;
3286 case clean:
3287 if (mddev->pers) {
3288 restart_array(mddev);
3289 spin_lock_irq(&mddev->write_lock);
3290 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003291 if (mddev->in_sync == 0) {
3292 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003293 if (mddev->safemode == 1)
3294 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003295 if (mddev->persistent)
3296 set_bit(MD_CHANGE_CLEAN,
3297 &mddev->flags);
3298 }
3299 err = 0;
3300 } else
3301 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003302 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003303 } else
3304 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003305 break;
3306 case active:
3307 if (mddev->pers) {
3308 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003309 if (mddev->external)
3310 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003311 wake_up(&mddev->sb_wait);
3312 err = 0;
3313 } else {
3314 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003315 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003316 err = do_md_run(mddev);
3317 }
3318 break;
3319 case write_pending:
3320 case active_idle:
3321 /* these cannot be set */
3322 break;
3323 }
3324 if (err)
3325 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003326 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003327 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003328 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003329 }
NeilBrown9e653b62006-06-26 00:27:58 -07003330}
NeilBrown80ca3a42006-07-10 04:44:18 -07003331static struct md_sysfs_entry md_array_state =
3332__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003333
NeilBrown6d7ff732006-01-06 00:21:16 -08003334static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003335max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3336 return sprintf(page, "%d\n",
3337 atomic_read(&mddev->max_corr_read_errors));
3338}
3339
3340static ssize_t
3341max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3342{
3343 char *e;
3344 unsigned long n = simple_strtoul(buf, &e, 10);
3345
3346 if (*buf && (*e == 0 || *e == '\n')) {
3347 atomic_set(&mddev->max_corr_read_errors, n);
3348 return len;
3349 }
3350 return -EINVAL;
3351}
3352
3353static struct md_sysfs_entry max_corr_read_errors =
3354__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3355 max_corrected_read_errors_store);
3356
3357static ssize_t
NeilBrown6d7ff732006-01-06 00:21:16 -08003358null_show(mddev_t *mddev, char *page)
3359{
3360 return -EINVAL;
3361}
3362
3363static ssize_t
3364new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3365{
3366 /* buf must be %d:%d\n? giving major and minor numbers */
3367 /* The new device is added to the array.
3368 * If the array has a persistent superblock, we read the
3369 * superblock to initialise info and check validity.
3370 * Otherwise, only checking done is that in bind_rdev_to_array,
3371 * which mainly checks size.
3372 */
3373 char *e;
3374 int major = simple_strtoul(buf, &e, 10);
3375 int minor;
3376 dev_t dev;
3377 mdk_rdev_t *rdev;
3378 int err;
3379
3380 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3381 return -EINVAL;
3382 minor = simple_strtoul(e+1, &e, 10);
3383 if (*e && *e != '\n')
3384 return -EINVAL;
3385 dev = MKDEV(major, minor);
3386 if (major != MAJOR(dev) ||
3387 minor != MINOR(dev))
3388 return -EOVERFLOW;
3389
3390
3391 if (mddev->persistent) {
3392 rdev = md_import_device(dev, mddev->major_version,
3393 mddev->minor_version);
3394 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3395 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3396 mdk_rdev_t, same_set);
3397 err = super_types[mddev->major_version]
3398 .load_super(rdev, rdev0, mddev->minor_version);
3399 if (err < 0)
3400 goto out;
3401 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003402 } else if (mddev->external)
3403 rdev = md_import_device(dev, -2, -1);
3404 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003405 rdev = md_import_device(dev, -1, -1);
3406
3407 if (IS_ERR(rdev))
3408 return PTR_ERR(rdev);
3409 err = bind_rdev_to_array(rdev, mddev);
3410 out:
3411 if (err)
3412 export_rdev(rdev);
3413 return err ? err : len;
3414}
3415
3416static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003417__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003418
3419static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003420bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3421{
3422 char *end;
3423 unsigned long chunk, end_chunk;
3424
3425 if (!mddev->bitmap)
3426 goto out;
3427 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3428 while (*buf) {
3429 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3430 if (buf == end) break;
3431 if (*end == '-') { /* range */
3432 buf = end + 1;
3433 end_chunk = simple_strtoul(buf, &end, 0);
3434 if (buf == end) break;
3435 }
3436 if (*end && !isspace(*end)) break;
3437 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003438 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003439 }
3440 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3441out:
3442 return len;
3443}
3444
3445static struct md_sysfs_entry md_bitmap =
3446__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3447
3448static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003449size_show(mddev_t *mddev, char *page)
3450{
Andre Noll58c0fed2009-03-31 14:33:13 +11003451 return sprintf(page, "%llu\n",
3452 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003453}
3454
Andre Nolld71f9f82008-07-11 22:02:22 +10003455static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003456
3457static ssize_t
3458size_store(mddev_t *mddev, const char *buf, size_t len)
3459{
3460 /* If array is inactive, we can reduce the component size, but
3461 * not increase it (except from 0).
3462 * If array is active, we can try an on-line resize
3463 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003464 sector_t sectors;
3465 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003466
Andre Noll58c0fed2009-03-31 14:33:13 +11003467 if (err < 0)
3468 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003469 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003470 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003471 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003472 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003473 if (mddev->dev_sectors == 0 ||
3474 mddev->dev_sectors > sectors)
3475 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003476 else
3477 err = -ENOSPC;
3478 }
3479 return err ? err : len;
3480}
3481
3482static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003483__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003484
NeilBrown8bb93aa2006-01-06 00:20:50 -08003485
3486/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003487 * This is one of
3488 * 'none' for arrays with no metadata (good luck...)
3489 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003490 * or N.M for internally known formats
3491 */
3492static ssize_t
3493metadata_show(mddev_t *mddev, char *page)
3494{
3495 if (mddev->persistent)
3496 return sprintf(page, "%d.%d\n",
3497 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003498 else if (mddev->external)
3499 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003500 else
3501 return sprintf(page, "none\n");
3502}
3503
3504static ssize_t
3505metadata_store(mddev_t *mddev, const char *buf, size_t len)
3506{
3507 int major, minor;
3508 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003509 /* Changing the details of 'external' metadata is
3510 * always permitted. Otherwise there must be
3511 * no devices attached to the array.
3512 */
3513 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3514 ;
3515 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003516 return -EBUSY;
3517
3518 if (cmd_match(buf, "none")) {
3519 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003520 mddev->external = 0;
3521 mddev->major_version = 0;
3522 mddev->minor_version = 90;
3523 return len;
3524 }
3525 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003526 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003527 if (namelen >= sizeof(mddev->metadata_type))
3528 namelen = sizeof(mddev->metadata_type)-1;
3529 strncpy(mddev->metadata_type, buf+9, namelen);
3530 mddev->metadata_type[namelen] = 0;
3531 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3532 mddev->metadata_type[--namelen] = 0;
3533 mddev->persistent = 0;
3534 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003535 mddev->major_version = 0;
3536 mddev->minor_version = 90;
3537 return len;
3538 }
3539 major = simple_strtoul(buf, &e, 10);
3540 if (e==buf || *e != '.')
3541 return -EINVAL;
3542 buf = e+1;
3543 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003544 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003545 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003546 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003547 return -ENOENT;
3548 mddev->major_version = major;
3549 mddev->minor_version = minor;
3550 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003551 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003552 return len;
3553}
3554
3555static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003556__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003557
NeilBrowna35b0d62006-01-06 00:20:49 -08003558static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003559action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003560{
NeilBrown7eec3142005-11-08 21:39:44 -08003561 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003562 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3563 type = "frozen";
3564 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003565 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003566 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3567 type = "reshape";
3568 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003569 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3570 type = "resync";
3571 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3572 type = "check";
3573 else
3574 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003575 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003576 type = "recover";
3577 }
3578 return sprintf(page, "%s\n", type);
3579}
3580
3581static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003582action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003583{
NeilBrown7eec3142005-11-08 21:39:44 -08003584 if (!mddev->pers || !mddev->pers->sync_request)
3585 return -EINVAL;
3586
NeilBrownb6a9ce62009-05-26 09:41:17 +10003587 if (cmd_match(page, "frozen"))
3588 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3589 else
3590 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3591
3592 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003593 if (mddev->sync_thread) {
3594 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3595 md_unregister_thread(mddev->sync_thread);
3596 mddev->sync_thread = NULL;
3597 mddev->recovery = 0;
3598 }
NeilBrown03c902e2006-01-06 00:20:46 -08003599 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3600 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003601 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003602 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003603 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003604 else if (cmd_match(page, "recover")) {
3605 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3606 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3607 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003608 int err;
3609 if (mddev->pers->start_reshape == NULL)
3610 return -EINVAL;
3611 err = mddev->pers->start_reshape(mddev);
3612 if (err)
3613 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003614 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003615 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003616 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003617 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003618 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003619 return -EINVAL;
3620 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3621 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003622 }
NeilBrown03c902e2006-01-06 00:20:46 -08003623 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003624 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003625 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003626 return len;
3627}
3628
NeilBrown9d888832005-11-08 21:39:26 -08003629static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003630mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003631{
3632 return sprintf(page, "%llu\n",
3633 (unsigned long long) mddev->resync_mismatches);
3634}
3635
NeilBrown80ca3a42006-07-10 04:44:18 -07003636static struct md_sysfs_entry md_scan_mode =
3637__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003638
NeilBrown96de1e62005-11-08 21:39:39 -08003639
NeilBrown80ca3a42006-07-10 04:44:18 -07003640static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003641
NeilBrown88202a02006-01-06 00:21:36 -08003642static ssize_t
3643sync_min_show(mddev_t *mddev, char *page)
3644{
3645 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3646 mddev->sync_speed_min ? "local": "system");
3647}
3648
3649static ssize_t
3650sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3651{
3652 int min;
3653 char *e;
3654 if (strncmp(buf, "system", 6)==0) {
3655 mddev->sync_speed_min = 0;
3656 return len;
3657 }
3658 min = simple_strtoul(buf, &e, 10);
3659 if (buf == e || (*e && *e != '\n') || min <= 0)
3660 return -EINVAL;
3661 mddev->sync_speed_min = min;
3662 return len;
3663}
3664
3665static struct md_sysfs_entry md_sync_min =
3666__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3667
3668static ssize_t
3669sync_max_show(mddev_t *mddev, char *page)
3670{
3671 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3672 mddev->sync_speed_max ? "local": "system");
3673}
3674
3675static ssize_t
3676sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3677{
3678 int max;
3679 char *e;
3680 if (strncmp(buf, "system", 6)==0) {
3681 mddev->sync_speed_max = 0;
3682 return len;
3683 }
3684 max = simple_strtoul(buf, &e, 10);
3685 if (buf == e || (*e && *e != '\n') || max <= 0)
3686 return -EINVAL;
3687 mddev->sync_speed_max = max;
3688 return len;
3689}
3690
3691static struct md_sysfs_entry md_sync_max =
3692__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3693
Iustin Popd7f3d292007-10-16 23:30:54 -07003694static ssize_t
3695degraded_show(mddev_t *mddev, char *page)
3696{
3697 return sprintf(page, "%d\n", mddev->degraded);
3698}
3699static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003700
3701static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003702sync_force_parallel_show(mddev_t *mddev, char *page)
3703{
3704 return sprintf(page, "%d\n", mddev->parallel_resync);
3705}
3706
3707static ssize_t
3708sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3709{
3710 long n;
3711
3712 if (strict_strtol(buf, 10, &n))
3713 return -EINVAL;
3714
3715 if (n != 0 && n != 1)
3716 return -EINVAL;
3717
3718 mddev->parallel_resync = n;
3719
3720 if (mddev->sync_thread)
3721 wake_up(&resync_wait);
3722
3723 return len;
3724}
3725
3726/* force parallel resync, even with shared block devices */
3727static struct md_sysfs_entry md_sync_force_parallel =
3728__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3729 sync_force_parallel_show, sync_force_parallel_store);
3730
3731static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003732sync_speed_show(mddev_t *mddev, char *page)
3733{
3734 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003735 if (mddev->curr_resync == 0)
3736 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003737 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3738 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003739 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003740 db = resync - mddev->resync_mark_cnt;
3741 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003742}
3743
NeilBrown80ca3a42006-07-10 04:44:18 -07003744static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003745
3746static ssize_t
3747sync_completed_show(mddev_t *mddev, char *page)
3748{
Andre Noll58c0fed2009-03-31 14:33:13 +11003749 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003750
NeilBrownacb180b2009-04-14 16:28:34 +10003751 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3752 return sprintf(page, "none\n");
3753
NeilBrown88202a02006-01-06 00:21:36 -08003754 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003755 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003756 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003757 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003758
NeilBrownacb180b2009-04-14 16:28:34 +10003759 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003760 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003761}
3762
NeilBrown80ca3a42006-07-10 04:44:18 -07003763static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003764
NeilBrowne464eaf2006-03-27 01:18:14 -08003765static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003766min_sync_show(mddev_t *mddev, char *page)
3767{
3768 return sprintf(page, "%llu\n",
3769 (unsigned long long)mddev->resync_min);
3770}
3771static ssize_t
3772min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3773{
3774 unsigned long long min;
3775 if (strict_strtoull(buf, 10, &min))
3776 return -EINVAL;
3777 if (min > mddev->resync_max)
3778 return -EINVAL;
3779 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3780 return -EBUSY;
3781
3782 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003783 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003784 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003785 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003786 return -EINVAL;
3787 }
3788 mddev->resync_min = min;
3789
3790 return len;
3791}
3792
3793static struct md_sysfs_entry md_min_sync =
3794__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3795
3796static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003797max_sync_show(mddev_t *mddev, char *page)
3798{
3799 if (mddev->resync_max == MaxSector)
3800 return sprintf(page, "max\n");
3801 else
3802 return sprintf(page, "%llu\n",
3803 (unsigned long long)mddev->resync_max);
3804}
3805static ssize_t
3806max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3807{
3808 if (strncmp(buf, "max", 3) == 0)
3809 mddev->resync_max = MaxSector;
3810 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003811 unsigned long long max;
3812 if (strict_strtoull(buf, 10, &max))
3813 return -EINVAL;
3814 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003815 return -EINVAL;
3816 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003817 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003818 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3819 return -EBUSY;
3820
3821 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003822 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003823 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003824 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003825 return -EINVAL;
3826 }
3827 mddev->resync_max = max;
3828 }
3829 wake_up(&mddev->recovery_wait);
3830 return len;
3831}
3832
3833static struct md_sysfs_entry md_max_sync =
3834__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3835
3836static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003837suspend_lo_show(mddev_t *mddev, char *page)
3838{
3839 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3840}
3841
3842static ssize_t
3843suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3844{
3845 char *e;
3846 unsigned long long new = simple_strtoull(buf, &e, 10);
3847
NeilBrownb8d966e2009-07-01 11:14:04 +10003848 if (mddev->pers == NULL ||
3849 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003850 return -EINVAL;
3851 if (buf == e || (*e && *e != '\n'))
3852 return -EINVAL;
3853 if (new >= mddev->suspend_hi ||
3854 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3855 mddev->suspend_lo = new;
3856 mddev->pers->quiesce(mddev, 2);
3857 return len;
3858 } else
3859 return -EINVAL;
3860}
3861static struct md_sysfs_entry md_suspend_lo =
3862__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3863
3864
3865static ssize_t
3866suspend_hi_show(mddev_t *mddev, char *page)
3867{
3868 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3869}
3870
3871static ssize_t
3872suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3873{
3874 char *e;
3875 unsigned long long new = simple_strtoull(buf, &e, 10);
3876
NeilBrownb8d966e2009-07-01 11:14:04 +10003877 if (mddev->pers == NULL ||
3878 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003879 return -EINVAL;
3880 if (buf == e || (*e && *e != '\n'))
3881 return -EINVAL;
3882 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3883 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3884 mddev->suspend_hi = new;
3885 mddev->pers->quiesce(mddev, 1);
3886 mddev->pers->quiesce(mddev, 0);
3887 return len;
3888 } else
3889 return -EINVAL;
3890}
3891static struct md_sysfs_entry md_suspend_hi =
3892__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3893
NeilBrown08a02ec2007-05-09 02:35:38 -07003894static ssize_t
3895reshape_position_show(mddev_t *mddev, char *page)
3896{
3897 if (mddev->reshape_position != MaxSector)
3898 return sprintf(page, "%llu\n",
3899 (unsigned long long)mddev->reshape_position);
3900 strcpy(page, "none\n");
3901 return 5;
3902}
3903
3904static ssize_t
3905reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3906{
3907 char *e;
3908 unsigned long long new = simple_strtoull(buf, &e, 10);
3909 if (mddev->pers)
3910 return -EBUSY;
3911 if (buf == e || (*e && *e != '\n'))
3912 return -EINVAL;
3913 mddev->reshape_position = new;
3914 mddev->delta_disks = 0;
3915 mddev->new_level = mddev->level;
3916 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003917 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07003918 return len;
3919}
3920
3921static struct md_sysfs_entry md_reshape_position =
3922__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3923 reshape_position_store);
3924
Dan Williamsb522adc2009-03-31 15:00:31 +11003925static ssize_t
3926array_size_show(mddev_t *mddev, char *page)
3927{
3928 if (mddev->external_size)
3929 return sprintf(page, "%llu\n",
3930 (unsigned long long)mddev->array_sectors/2);
3931 else
3932 return sprintf(page, "default\n");
3933}
3934
3935static ssize_t
3936array_size_store(mddev_t *mddev, const char *buf, size_t len)
3937{
3938 sector_t sectors;
3939
3940 if (strncmp(buf, "default", 7) == 0) {
3941 if (mddev->pers)
3942 sectors = mddev->pers->size(mddev, 0, 0);
3943 else
3944 sectors = mddev->array_sectors;
3945
3946 mddev->external_size = 0;
3947 } else {
3948 if (strict_blocks_to_sectors(buf, &sectors) < 0)
3949 return -EINVAL;
3950 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10003951 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11003952
3953 mddev->external_size = 1;
3954 }
3955
3956 mddev->array_sectors = sectors;
3957 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10003958 if (mddev->pers)
3959 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11003960
3961 return len;
3962}
3963
3964static struct md_sysfs_entry md_array_size =
3965__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
3966 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08003967
NeilBrowneae17012005-11-08 21:39:23 -08003968static struct attribute *md_default_attrs[] = {
3969 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003970 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003971 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003972 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003973 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003974 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003975 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08003976 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003977 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003978 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003979 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11003980 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11003981 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003982 NULL,
3983};
3984
3985static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003986 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003987 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003988 &md_sync_min.attr,
3989 &md_sync_max.attr,
3990 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003991 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003992 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003993 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003994 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003995 &md_suspend_lo.attr,
3996 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003997 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003998 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003999 NULL,
4000};
NeilBrown411036f2005-11-08 21:39:40 -08004001static struct attribute_group md_redundancy_group = {
4002 .name = NULL,
4003 .attrs = md_redundancy_attrs,
4004};
4005
NeilBrowneae17012005-11-08 21:39:23 -08004006
4007static ssize_t
4008md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4009{
4010 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4011 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004012 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004013
4014 if (!entry->show)
4015 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004016 rv = mddev_lock(mddev);
4017 if (!rv) {
4018 rv = entry->show(mddev, page);
4019 mddev_unlock(mddev);
4020 }
NeilBrown96de1e62005-11-08 21:39:39 -08004021 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004022}
4023
4024static ssize_t
4025md_attr_store(struct kobject *kobj, struct attribute *attr,
4026 const char *page, size_t length)
4027{
4028 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4029 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004030 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004031
4032 if (!entry->store)
4033 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004034 if (!capable(CAP_SYS_ADMIN))
4035 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004036 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004037 if (mddev->hold_active == UNTIL_IOCTL)
4038 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004039 if (!rv) {
4040 rv = entry->store(mddev, page, length);
4041 mddev_unlock(mddev);
4042 }
NeilBrown96de1e62005-11-08 21:39:39 -08004043 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004044}
4045
4046static void md_free(struct kobject *ko)
4047{
4048 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004049
4050 if (mddev->sysfs_state)
4051 sysfs_put(mddev->sysfs_state);
4052
4053 if (mddev->gendisk) {
4054 del_gendisk(mddev->gendisk);
4055 put_disk(mddev->gendisk);
4056 }
4057 if (mddev->queue)
4058 blk_cleanup_queue(mddev->queue);
4059
NeilBrowneae17012005-11-08 21:39:23 -08004060 kfree(mddev);
4061}
4062
Emese Revfy52cf25d2010-01-19 02:58:23 +01004063static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004064 .show = md_attr_show,
4065 .store = md_attr_store,
4066};
4067static struct kobj_type md_ktype = {
4068 .release = md_free,
4069 .sysfs_ops = &md_sysfs_ops,
4070 .default_attrs = md_default_attrs,
4071};
4072
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073int mdp_major = 0;
4074
Dan Williams5fd3a172009-03-04 00:57:25 -07004075static void mddev_delayed_delete(struct work_struct *ws)
4076{
4077 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4078
NeilBrownef286f62010-02-09 16:34:14 +11004079 if (mddev->private) {
Dan Williams5fd3a172009-03-04 00:57:25 -07004080 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
NeilBrownef286f62010-02-09 16:34:14 +11004081 if (mddev->private != (void*)1)
4082 sysfs_remove_group(&mddev->kobj, mddev->private);
Dan Williams5fd3a172009-03-04 00:57:25 -07004083 if (mddev->sysfs_action)
4084 sysfs_put(mddev->sysfs_action);
4085 mddev->sysfs_action = NULL;
4086 mddev->private = NULL;
4087 }
NeilBrown43a70502009-12-14 12:49:55 +11004088 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004089 kobject_del(&mddev->kobj);
4090 kobject_put(&mddev->kobj);
4091}
4092
NeilBrownefeb53c2009-01-09 08:31:10 +11004093static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004095 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 mddev_t *mddev = mddev_find(dev);
4097 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004098 int partitioned;
4099 int shift;
4100 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004101 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102
4103 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004104 return -ENODEV;
4105
4106 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4107 shift = partitioned ? MdpMinorShift : 0;
4108 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
NeilBrownd3374822009-01-09 08:31:10 +11004110 /* wait for any previous instance if this device
4111 * to be completed removed (mddev_delayed_delete).
4112 */
4113 flush_scheduled_work();
4114
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004115 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004116 error = -EEXIST;
4117 if (mddev->gendisk)
4118 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004119
4120 if (name) {
4121 /* Need to ensure that 'name' is not a duplicate.
4122 */
4123 mddev_t *mddev2;
4124 spin_lock(&all_mddevs_lock);
4125
4126 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4127 if (mddev2->gendisk &&
4128 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4129 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004130 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004131 }
4132 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 }
NeilBrown8b765392009-01-09 08:31:08 +11004134
NeilBrown0909dc42009-07-01 12:27:21 +10004135 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004136 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004137 if (!mddev->queue)
4138 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004139 mddev->queue->queuedata = mddev;
4140
NeilBrown8b765392009-01-09 08:31:08 +11004141 /* Can be unlocked because the queue is new: no concurrency */
4142 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4143
NeilBrown409c57f2009-03-31 14:39:39 +11004144 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004145
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 disk = alloc_disk(1 << shift);
4147 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004148 blk_cleanup_queue(mddev->queue);
4149 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004150 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004152 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004154 if (name)
4155 strcpy(disk->disk_name, name);
4156 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004158 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 disk->fops = &md_fops;
4161 disk->private_data = mddev;
4162 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004163 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004164 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004165 * remove it now.
4166 */
4167 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 add_disk(disk);
4169 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004170 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4171 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004172 if (error) {
4173 /* This isn't possible, but as kobject_init_and_add is marked
4174 * __must_check, we must do something with the result
4175 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004176 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4177 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004178 error = 0;
4179 }
NeilBrown43a70502009-12-14 12:49:55 +11004180 if (sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4181 printk(KERN_DEBUG "pointless warning\n");
NeilBrown0909dc42009-07-01 12:27:21 +10004182 abort:
4183 mutex_unlock(&disks_mutex);
4184 if (!error) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004185 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11004186 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
4187 }
NeilBrownd3374822009-01-09 08:31:10 +11004188 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004189 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004190}
4191
4192static struct kobject *md_probe(dev_t dev, int *part, void *data)
4193{
4194 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 return NULL;
4196}
4197
NeilBrownefeb53c2009-01-09 08:31:10 +11004198static int add_named_array(const char *val, struct kernel_param *kp)
4199{
4200 /* val must be "md_*" where * is not all digits.
4201 * We allocate an array with a large free minor number, and
4202 * set the name to val. val must not already be an active name.
4203 */
4204 int len = strlen(val);
4205 char buf[DISK_NAME_LEN];
4206
4207 while (len && val[len-1] == '\n')
4208 len--;
4209 if (len >= DISK_NAME_LEN)
4210 return -E2BIG;
4211 strlcpy(buf, val, len+1);
4212 if (strncmp(buf, "md_", 3) != 0)
4213 return -EINVAL;
4214 return md_alloc(0, buf);
4215}
4216
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217static void md_safemode_timeout(unsigned long data)
4218{
4219 mddev_t *mddev = (mddev_t *) data;
4220
Neil Brown0fd62b82008-06-28 08:31:36 +10004221 if (!atomic_read(&mddev->writes_pending)) {
4222 mddev->safemode = 1;
4223 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11004224 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 md_wakeup_thread(mddev->thread);
4227}
4228
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004229static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230
4231static int do_md_run(mddev_t * mddev)
4232{
NeilBrown2604b702006-01-06 00:20:36 -08004233 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 mdk_rdev_t *rdev;
4235 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08004236 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
NeilBrowna757e642005-04-16 15:26:42 -07004238 if (list_empty(&mddev->disks))
4239 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
4242 if (mddev->pers)
4243 return -EBUSY;
4244
4245 /*
4246 * Analyze all RAID superblock(s)
4247 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004248 if (!mddev->raid_disks) {
4249 if (!mddev->persistent)
4250 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004251 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253
NeilBrownd9d166c2006-01-06 00:20:51 -08004254 if (mddev->level != LEVEL_NONE)
4255 request_module("md-level-%d", mddev->level);
4256 else if (mddev->clevel[0])
4257 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258
4259 /*
4260 * Drop all container device buffers, from now on
4261 * the only valid external interface is through the md
4262 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004264 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004265 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 continue;
4267 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004268 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004269
4270 /* perform some consistency tests on the device.
4271 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004272 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004273 */
Andre Noll0f420352008-07-11 22:02:23 +10004274 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004275 if (mddev->dev_sectors &&
4276 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004277 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004278 printk("md: %s: data overlaps metadata\n",
4279 mdname(mddev));
4280 return -EINVAL;
4281 }
4282 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004283 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004284 > rdev->data_offset) {
4285 printk("md: %s: metadata overlaps data\n",
4286 mdname(mddev));
4287 return -EINVAL;
4288 }
4289 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004290 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 }
4292
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
4295 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004296 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004297 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004299 if (mddev->level != LEVEL_NONE)
4300 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4301 mddev->level);
4302 else
4303 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4304 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 return -EINVAL;
4306 }
NeilBrown2604b702006-01-06 00:20:36 -08004307 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004309 if (mddev->level != pers->level) {
4310 mddev->level = pers->level;
4311 mddev->new_level = pers->level;
4312 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004313 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314
NeilBrownf6705572006-03-27 01:18:11 -08004315 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004316 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004317 /* This personality cannot handle reshaping... */
4318 mddev->pers = NULL;
4319 module_put(pers->owner);
4320 return -EINVAL;
4321 }
4322
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004323 if (pers->sync_request) {
4324 /* Warn if this is a potentially silly
4325 * configuration.
4326 */
4327 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4328 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004329 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004330
4331 list_for_each_entry(rdev, &mddev->disks, same_set)
4332 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004333 if (rdev < rdev2 &&
4334 rdev->bdev->bd_contains ==
4335 rdev2->bdev->bd_contains) {
4336 printk(KERN_WARNING
4337 "%s: WARNING: %s appears to be"
4338 " on the same physical disk as"
4339 " %s.\n",
4340 mdname(mddev),
4341 bdevname(rdev->bdev,b),
4342 bdevname(rdev2->bdev,b2));
4343 warned = 1;
4344 }
4345 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004346
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004347 if (warned)
4348 printk(KERN_WARNING
4349 "True protection against single-disk"
4350 " failure might be compromised.\n");
4351 }
4352
NeilBrown657390d2005-08-26 18:34:16 -07004353 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004354 /* may be over-ridden by personality */
4355 mddev->resync_max_sectors = mddev->dev_sectors;
4356
NeilBrowna9701a32005-11-08 21:39:34 -08004357 mddev->barriers_work = 1;
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004358 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
NeilBrown0f9552b2009-12-30 12:08:50 +11004360 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004361 mddev->ro = 2; /* read-only, but switch on first write */
4362
NeilBrownb15c2e52006-01-06 00:20:16 -08004363 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004364 if (err)
4365 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004366 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4367 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4368 " but 'external_size' not in effect?\n", __func__);
4369 printk(KERN_ERR
4370 "md: invalid array_size %llu > default size %llu\n",
4371 (unsigned long long)mddev->array_sectors / 2,
4372 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4373 err = -EINVAL;
4374 mddev->pers->stop(mddev);
4375 }
4376 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004377 err = bitmap_create(mddev);
4378 if (err) {
4379 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4380 mdname(mddev), err);
4381 mddev->pers->stop(mddev);
4382 }
4383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 module_put(mddev->pers->owner);
4386 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004387 bitmap_destroy(mddev);
4388 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004390 if (mddev->pers->sync_request) {
4391 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4392 printk(KERN_WARNING
4393 "md: cannot register extra attributes for %s\n",
4394 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004395 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004396 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004397 mddev->ro = 0;
4398
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004400 atomic_set(&mddev->max_corr_read_errors,
4401 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 mddev->safemode = 0;
4403 mddev->safemode_timer.function = md_safemode_timeout;
4404 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004405 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004407
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004408 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004409 if (rdev->raid_disk >= 0) {
4410 char nm[20];
4411 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004412 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4413 printk("md: cannot register %s for %s\n",
4414 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416
4417 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4418
NeilBrown850b2b42006-10-03 01:15:46 -07004419 if (mddev->flags)
4420 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
Andre Nollf233ea52008-07-21 17:05:22 +10004422 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
NeilBrown0b8c9de2006-07-10 04:44:15 -07004424 md_wakeup_thread(mddev->thread);
4425 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004426
NeilBrown449aad32009-08-03 10:59:58 +10004427 revalidate_disk(mddev->gendisk);
Linus Torvalds44ce6292007-05-09 18:51:36 -07004428 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08004429 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004430 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004431 if (mddev->sysfs_action)
4432 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004433 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09004434 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 return 0;
4436}
4437
4438static int restart_array(mddev_t *mddev)
4439{
4440 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441
Andre Noll80fab1d2008-07-11 22:02:21 +10004442 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004444 return -ENXIO;
4445 if (!mddev->pers)
4446 return -EINVAL;
4447 if (!mddev->ro)
4448 return -EBUSY;
4449 mddev->safemode = 0;
4450 mddev->ro = 0;
4451 set_disk_ro(disk, 0);
4452 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4453 mdname(mddev));
4454 /* Kick recovery or resync if necessary */
4455 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4456 md_wakeup_thread(mddev->thread);
4457 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004458 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004459 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460}
4461
NeilBrownacc55e22006-06-26 00:27:47 -07004462/* similar to deny_write_access, but accounts for our holding a reference
4463 * to the file ourselves */
4464static int deny_bitmap_write_access(struct file * file)
4465{
4466 struct inode *inode = file->f_mapping->host;
4467
4468 spin_lock(&inode->i_lock);
4469 if (atomic_read(&inode->i_writecount) > 1) {
4470 spin_unlock(&inode->i_lock);
4471 return -ETXTBSY;
4472 }
4473 atomic_set(&inode->i_writecount, -1);
4474 spin_unlock(&inode->i_lock);
4475
4476 return 0;
4477}
4478
NeilBrown43a70502009-12-14 12:49:55 +11004479void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004480{
4481 struct inode *inode = file->f_mapping->host;
4482
4483 spin_lock(&inode->i_lock);
4484 atomic_set(&inode->i_writecount, 1);
4485 spin_unlock(&inode->i_lock);
4486}
4487
NeilBrown9e653b62006-06-26 00:27:58 -07004488/* mode:
4489 * 0 - completely stop and dis-assemble array
4490 * 1 - switch to readonly
4491 * 2 - stop but do not disassemble array
4492 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004493static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494{
4495 int err = 0;
4496 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004497 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498
NeilBrownc8c00a62009-08-10 12:50:52 +10004499 mutex_lock(&mddev->open_mutex);
NeilBrownf2ea68c2008-07-21 17:05:25 +10004500 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004501 printk("md: %s still in use.\n",mdname(mddev));
NeilBrownc8c00a62009-08-10 12:50:52 +10004502 err = -EBUSY;
4503 } else if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504
4505 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004506 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4508 md_unregister_thread(mddev->sync_thread);
4509 mddev->sync_thread = NULL;
4510 }
4511
4512 del_timer_sync(&mddev->safemode_timer);
4513
NeilBrown9e653b62006-06-26 00:27:58 -07004514 switch(mode) {
4515 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004517 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 goto out;
4519 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004520 break;
4521 case 0: /* disassemble */
4522 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004523 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004524 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 if (mddev->ro)
4526 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004527
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004529 mddev->queue->merge_bvec_fn = NULL;
4530 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004531 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 module_put(mddev->pers->owner);
NeilBrownef286f62010-02-09 16:34:14 +11004533 if (mddev->pers->sync_request && mddev->private == NULL)
4534 mddev->private = (void*)1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004536 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004537 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004538
NeilBrownc4647292009-05-07 12:51:06 +10004539 list_for_each_entry(rdev, &mddev->disks, same_set)
4540 if (rdev->raid_disk >= 0) {
4541 char nm[20];
4542 sprintf(nm, "rd%d", rdev->raid_disk);
4543 sysfs_remove_link(&mddev->kobj, nm);
4544 }
4545
NeilBrown0d4ca602006-12-10 02:20:44 -08004546 set_capacity(disk, 0);
Linus Torvalds44ce6292007-05-09 18:51:36 -07004547 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08004548
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 if (mddev->ro)
4550 mddev->ro = 0;
4551 }
NeilBrown850b2b42006-10-03 01:15:46 -07004552 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 /* mark array as shutdown cleanly */
4554 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07004555 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 }
NeilBrown9e653b62006-06-26 00:27:58 -07004557 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004559 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown80ffb3c2009-08-18 10:35:26 +10004560 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 }
NeilBrownc8c00a62009-08-10 12:50:52 +10004562out:
4563 mutex_unlock(&mddev->open_mutex);
4564 if (err)
4565 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 /*
4567 * Free resources if final stop
4568 */
NeilBrown9e653b62006-06-26 00:27:58 -07004569 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004570
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4572
NeilBrown978f9462006-02-02 14:28:05 -08004573 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004574 if (mddev->bitmap_info.file) {
4575 restore_bitmap_write_access(mddev->bitmap_info.file);
4576 fput(mddev->bitmap_info.file);
4577 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004578 }
NeilBrownc3d97142009-12-14 12:49:52 +11004579 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004580
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 export_array(mddev);
4582
Andre Nollf233ea52008-07-21 17:05:22 +10004583 mddev->array_sectors = 0;
Dan Williamsb522adc2009-03-31 15:00:31 +11004584 mddev->external_size = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004585 mddev->dev_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004586 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004587 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004588 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004589 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004590 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004591 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004592 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004593 mddev->level = LEVEL_NONE;
4594 mddev->clevel[0] = 0;
4595 mddev->flags = 0;
4596 mddev->ro = 0;
4597 mddev->metadata_type[0] = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10004598 mddev->chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004599 mddev->ctime = mddev->utime = 0;
4600 mddev->layout = 0;
4601 mddev->max_disks = 0;
4602 mddev->events = 0;
4603 mddev->delta_disks = 0;
4604 mddev->new_level = LEVEL_NONE;
4605 mddev->new_layout = 0;
Andre Noll664e7c42009-06-18 08:45:27 +10004606 mddev->new_chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004607 mddev->curr_resync = 0;
4608 mddev->resync_mismatches = 0;
4609 mddev->suspend_lo = mddev->suspend_hi = 0;
4610 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4611 mddev->recovery = 0;
4612 mddev->in_sync = 0;
4613 mddev->changed = 0;
4614 mddev->degraded = 0;
4615 mddev->barriers_work = 0;
4616 mddev->safemode = 0;
NeilBrownc3d97142009-12-14 12:49:52 +11004617 mddev->bitmap_info.offset = 0;
4618 mddev->bitmap_info.default_offset = 0;
NeilBrown42a04b52009-12-14 12:49:53 +11004619 mddev->bitmap_info.chunksize = 0;
4620 mddev->bitmap_info.daemon_sleep = 0;
4621 mddev->bitmap_info.max_write_behind = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004622 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004623 if (mddev->hold_active == UNTIL_STOP)
4624 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004625
NeilBrowna8a55c32006-06-26 00:27:39 -07004626 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4628 mdname(mddev));
4629 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004630 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004631 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004632 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 return err;
4634}
4635
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004636#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637static void autorun_array(mddev_t *mddev)
4638{
4639 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 int err;
4641
NeilBrowna757e642005-04-16 15:26:42 -07004642 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644
4645 printk(KERN_INFO "md: running: ");
4646
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004647 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 char b[BDEVNAME_SIZE];
4649 printk("<%s>", bdevname(rdev->bdev,b));
4650 }
4651 printk("\n");
4652
NeilBrownd710e132008-10-13 11:55:12 +11004653 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 if (err) {
4655 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004656 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 }
4658}
4659
4660/*
4661 * lets try to run arrays based on all disks that have arrived
4662 * until now. (those are in pending_raid_disks)
4663 *
4664 * the method: pick the first pending disk, collect all disks with
4665 * the same UUID, remove all from the pending list and put them into
4666 * the 'same_array' list. Then order this list based on superblock
4667 * update time (freshest comes first), kick out 'old' disks and
4668 * compare superblocks. If everything's fine then run it.
4669 *
4670 * If "unit" is allocated, then bump its reference count
4671 */
4672static void autorun_devices(int part)
4673{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004674 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 mddev_t *mddev;
4676 char b[BDEVNAME_SIZE];
4677
4678 printk(KERN_INFO "md: autorun ...\n");
4679 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004680 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004682 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 rdev0 = list_entry(pending_raid_disks.next,
4684 mdk_rdev_t, same_set);
4685
4686 printk(KERN_INFO "md: considering %s ...\n",
4687 bdevname(rdev0->bdev,b));
4688 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004689 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 if (super_90_load(rdev, rdev0, 0) >= 0) {
4691 printk(KERN_INFO "md: adding %s ...\n",
4692 bdevname(rdev->bdev,b));
4693 list_move(&rdev->same_set, &candidates);
4694 }
4695 /*
4696 * now we have a set of devices, with all of them having
4697 * mostly sane superblocks. It's time to allocate the
4698 * mddev.
4699 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004700 if (part) {
4701 dev = MKDEV(mdp_major,
4702 rdev0->preferred_minor << MdpMinorShift);
4703 unit = MINOR(dev) >> MdpMinorShift;
4704 } else {
4705 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4706 unit = MINOR(dev);
4707 }
4708 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4710 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4711 break;
4712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713
4714 md_probe(dev, NULL, NULL);
4715 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004716 if (!mddev || !mddev->gendisk) {
4717 if (mddev)
4718 mddev_put(mddev);
4719 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 "md: cannot allocate memory for md drive.\n");
4721 break;
4722 }
4723 if (mddev_lock(mddev))
4724 printk(KERN_WARNING "md: %s locked, cannot run\n",
4725 mdname(mddev));
4726 else if (mddev->raid_disks || mddev->major_version
4727 || !list_empty(&mddev->disks)) {
4728 printk(KERN_WARNING
4729 "md: %s already running, cannot run %s\n",
4730 mdname(mddev), bdevname(rdev0->bdev,b));
4731 mddev_unlock(mddev);
4732 } else {
4733 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004734 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004735 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 list_del_init(&rdev->same_set);
4737 if (bind_rdev_to_array(rdev, mddev))
4738 export_rdev(rdev);
4739 }
4740 autorun_array(mddev);
4741 mddev_unlock(mddev);
4742 }
4743 /* on success, candidates will be empty, on error
4744 * it won't...
4745 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004746 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004747 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 mddev_put(mddev);
4751 }
4752 printk(KERN_INFO "md: ... autorun DONE.\n");
4753}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004754#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756static int get_version(void __user * arg)
4757{
4758 mdu_version_t ver;
4759
4760 ver.major = MD_MAJOR_VERSION;
4761 ver.minor = MD_MINOR_VERSION;
4762 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4763
4764 if (copy_to_user(arg, &ver, sizeof(ver)))
4765 return -EFAULT;
4766
4767 return 0;
4768}
4769
4770static int get_array_info(mddev_t * mddev, void __user * arg)
4771{
4772 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004773 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775
NeilBrowna9f326e2009-09-23 18:06:41 +10004776 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004777 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004779 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 failed++;
4781 else {
4782 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004783 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004784 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 else
4786 spare++;
4787 }
4788 }
4789
4790 info.major_version = mddev->major_version;
4791 info.minor_version = mddev->minor_version;
4792 info.patch_version = MD_PATCHLEVEL_VERSION;
4793 info.ctime = mddev->ctime;
4794 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004795 info.size = mddev->dev_sectors / 2;
4796 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004797 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 info.nr_disks = nr;
4799 info.raid_disks = mddev->raid_disks;
4800 info.md_minor = mddev->md_minor;
4801 info.not_persistent= !mddev->persistent;
4802
4803 info.utime = mddev->utime;
4804 info.state = 0;
4805 if (mddev->in_sync)
4806 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11004807 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07004808 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004809 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 info.working_disks = working;
4811 info.failed_disks = failed;
4812 info.spare_disks = spare;
4813
4814 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004815 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816
4817 if (copy_to_user(arg, &info, sizeof(info)))
4818 return -EFAULT;
4819
4820 return 0;
4821}
4822
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004823static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004824{
4825 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4826 char *ptr, *buf = NULL;
4827 int err = -ENOMEM;
4828
Dan Williamsb5470dc2008-06-27 21:44:04 -07004829 if (md_allow_write(mddev))
4830 file = kmalloc(sizeof(*file), GFP_NOIO);
4831 else
4832 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004833
NeilBrown32a76272005-06-21 17:17:14 -07004834 if (!file)
4835 goto out;
4836
4837 /* bitmap disabled, zero the first byte and copy out */
4838 if (!mddev->bitmap || !mddev->bitmap->file) {
4839 file->pathname[0] = '\0';
4840 goto copy_out;
4841 }
4842
4843 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4844 if (!buf)
4845 goto out;
4846
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004847 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4848 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004849 goto out;
4850
4851 strcpy(file->pathname, ptr);
4852
4853copy_out:
4854 err = 0;
4855 if (copy_to_user(arg, file, sizeof(*file)))
4856 err = -EFAULT;
4857out:
4858 kfree(buf);
4859 kfree(file);
4860 return err;
4861}
4862
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863static int get_disk_info(mddev_t * mddev, void __user * arg)
4864{
4865 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 mdk_rdev_t *rdev;
4867
4868 if (copy_from_user(&info, arg, sizeof(info)))
4869 return -EFAULT;
4870
Andre Noll26ef3792008-07-11 22:02:21 +10004871 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 if (rdev) {
4873 info.major = MAJOR(rdev->bdev->bd_dev);
4874 info.minor = MINOR(rdev->bdev->bd_dev);
4875 info.raid_disk = rdev->raid_disk;
4876 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004877 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004879 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 info.state |= (1<<MD_DISK_ACTIVE);
4881 info.state |= (1<<MD_DISK_SYNC);
4882 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004883 if (test_bit(WriteMostly, &rdev->flags))
4884 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 } else {
4886 info.major = info.minor = 0;
4887 info.raid_disk = -1;
4888 info.state = (1<<MD_DISK_REMOVED);
4889 }
4890
4891 if (copy_to_user(arg, &info, sizeof(info)))
4892 return -EFAULT;
4893
4894 return 0;
4895}
4896
4897static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4898{
4899 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4900 mdk_rdev_t *rdev;
4901 dev_t dev = MKDEV(info->major,info->minor);
4902
4903 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4904 return -EOVERFLOW;
4905
4906 if (!mddev->raid_disks) {
4907 int err;
4908 /* expecting a device which has a superblock */
4909 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4910 if (IS_ERR(rdev)) {
4911 printk(KERN_WARNING
4912 "md: md_import_device returned %ld\n",
4913 PTR_ERR(rdev));
4914 return PTR_ERR(rdev);
4915 }
4916 if (!list_empty(&mddev->disks)) {
4917 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4918 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10004919 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 .load_super(rdev, rdev0, mddev->minor_version);
4921 if (err < 0) {
4922 printk(KERN_WARNING
4923 "md: %s has different UUID to %s\n",
4924 bdevname(rdev->bdev,b),
4925 bdevname(rdev0->bdev,b2));
4926 export_rdev(rdev);
4927 return -EINVAL;
4928 }
4929 }
4930 err = bind_rdev_to_array(rdev, mddev);
4931 if (err)
4932 export_rdev(rdev);
4933 return err;
4934 }
4935
4936 /*
4937 * add_new_disk can be used once the array is assembled
4938 * to add "hot spares". They must already have a superblock
4939 * written
4940 */
4941 if (mddev->pers) {
4942 int err;
4943 if (!mddev->pers->hot_add_disk) {
4944 printk(KERN_WARNING
4945 "%s: personality does not support diskops!\n",
4946 mdname(mddev));
4947 return -EINVAL;
4948 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004949 if (mddev->persistent)
4950 rdev = md_import_device(dev, mddev->major_version,
4951 mddev->minor_version);
4952 else
4953 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 if (IS_ERR(rdev)) {
4955 printk(KERN_WARNING
4956 "md: md_import_device returned %ld\n",
4957 PTR_ERR(rdev));
4958 return PTR_ERR(rdev);
4959 }
NeilBrown41158c72005-06-21 17:17:25 -07004960 /* set save_raid_disk if appropriate */
4961 if (!mddev->persistent) {
4962 if (info->state & (1<<MD_DISK_SYNC) &&
4963 info->raid_disk < mddev->raid_disks)
4964 rdev->raid_disk = info->raid_disk;
4965 else
4966 rdev->raid_disk = -1;
4967 } else
4968 super_types[mddev->major_version].
4969 validate_super(mddev, rdev);
4970 rdev->saved_raid_disk = rdev->raid_disk;
4971
NeilBrownb2d444d2005-11-08 21:39:31 -08004972 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004973 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4974 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11004975 else
4976 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004977
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 rdev->raid_disk = -1;
4979 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004980 if (!err && !mddev->pers->hot_remove_disk) {
4981 /* If there is hot_add_disk but no hot_remove_disk
4982 * then added disks for geometry changes,
4983 * and should be added immediately.
4984 */
4985 super_types[mddev->major_version].
4986 validate_super(mddev, rdev);
4987 err = mddev->pers->hot_add_disk(mddev, rdev);
4988 if (err)
4989 unbind_rdev_from_array(rdev);
4990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 if (err)
4992 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004993 else
NeilBrown3c0ee632008-10-21 13:25:28 +11004994 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07004995
NeilBrown17571282006-12-10 02:20:52 -08004996 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004997 if (mddev->degraded)
4998 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004999 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07005000 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 return err;
5002 }
5003
5004 /* otherwise, add_new_disk is only allowed
5005 * for major_version==0 superblocks
5006 */
5007 if (mddev->major_version != 0) {
5008 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5009 mdname(mddev));
5010 return -EINVAL;
5011 }
5012
5013 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5014 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005015 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 if (IS_ERR(rdev)) {
5017 printk(KERN_WARNING
5018 "md: error, md_import_device() returned %ld\n",
5019 PTR_ERR(rdev));
5020 return PTR_ERR(rdev);
5021 }
5022 rdev->desc_nr = info->number;
5023 if (info->raid_disk < mddev->raid_disks)
5024 rdev->raid_disk = info->raid_disk;
5025 else
5026 rdev->raid_disk = -1;
5027
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005029 if (info->state & (1<<MD_DISK_SYNC))
5030 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005032 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5033 set_bit(WriteMostly, &rdev->flags);
5034
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 if (!mddev->persistent) {
5036 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10005037 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 } else
Andre Noll0f420352008-07-11 22:02:23 +10005039 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10005040 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041
NeilBrown2bf071b2006-01-06 00:20:55 -08005042 err = bind_rdev_to_array(rdev, mddev);
5043 if (err) {
5044 export_rdev(rdev);
5045 return err;
5046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 }
5048
5049 return 0;
5050}
5051
5052static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5053{
5054 char b[BDEVNAME_SIZE];
5055 mdk_rdev_t *rdev;
5056
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 rdev = find_rdev(mddev, dev);
5058 if (!rdev)
5059 return -ENXIO;
5060
5061 if (rdev->raid_disk >= 0)
5062 goto busy;
5063
5064 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005065 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005066 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067
5068 return 0;
5069busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005070 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 bdevname(rdev->bdev,b), mdname(mddev));
5072 return -EBUSY;
5073}
5074
5075static int hot_add_disk(mddev_t * mddev, dev_t dev)
5076{
5077 char b[BDEVNAME_SIZE];
5078 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 mdk_rdev_t *rdev;
5080
5081 if (!mddev->pers)
5082 return -ENODEV;
5083
5084 if (mddev->major_version != 0) {
5085 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5086 " version-0 superblocks.\n",
5087 mdname(mddev));
5088 return -EINVAL;
5089 }
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 }
5096
NeilBrownd710e132008-10-13 11:55:12 +11005097 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 if (IS_ERR(rdev)) {
5099 printk(KERN_WARNING
5100 "md: error, md_import_device() returned %ld\n",
5101 PTR_ERR(rdev));
5102 return -EINVAL;
5103 }
5104
5105 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10005106 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 else
Andre Noll0f420352008-07-11 22:02:23 +10005108 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109
NeilBrown8190e752009-06-18 08:48:58 +10005110 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111
NeilBrownb2d444d2005-11-08 21:39:31 -08005112 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 printk(KERN_WARNING
5114 "md: can not hot-add faulty %s disk to %s!\n",
5115 bdevname(rdev->bdev,b), mdname(mddev));
5116 err = -EINVAL;
5117 goto abort_export;
5118 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005119 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005121 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005122 err = bind_rdev_to_array(rdev, mddev);
5123 if (err)
5124 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
5126 /*
5127 * The rest should better be atomic, we can have disk failures
5128 * noticed in interrupt contexts ...
5129 */
5130
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 rdev->raid_disk = -1;
5132
NeilBrown850b2b42006-10-03 01:15:46 -07005133 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134
5135 /*
5136 * Kick recovery, maybe this spare has to be added to the
5137 * array immediately.
5138 */
5139 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5140 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005141 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 return 0;
5143
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144abort_export:
5145 export_rdev(rdev);
5146 return err;
5147}
5148
NeilBrown32a76272005-06-21 17:17:14 -07005149static int set_bitmap_file(mddev_t *mddev, int fd)
5150{
5151 int err;
5152
NeilBrown36fa3062005-09-09 16:23:45 -07005153 if (mddev->pers) {
5154 if (!mddev->pers->quiesce)
5155 return -EBUSY;
5156 if (mddev->recovery || mddev->sync_thread)
5157 return -EBUSY;
5158 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005159 }
5160
NeilBrown36fa3062005-09-09 16:23:45 -07005161
5162 if (fd >= 0) {
5163 if (mddev->bitmap)
5164 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005165 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005166
NeilBrownc3d97142009-12-14 12:49:52 +11005167 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005168 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5169 mdname(mddev));
5170 return -EBADF;
5171 }
5172
NeilBrownc3d97142009-12-14 12:49:52 +11005173 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005174 if (err) {
5175 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5176 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005177 fput(mddev->bitmap_info.file);
5178 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005179 return err;
5180 }
NeilBrownc3d97142009-12-14 12:49:52 +11005181 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005182 } else if (mddev->bitmap == NULL)
5183 return -ENOENT; /* cannot remove what isn't there */
5184 err = 0;
5185 if (mddev->pers) {
5186 mddev->pers->quiesce(mddev, 1);
5187 if (fd >= 0)
5188 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005189 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005190 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005191 fd = -1; /* make sure to put the file */
5192 }
NeilBrown36fa3062005-09-09 16:23:45 -07005193 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005194 }
5195 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005196 if (mddev->bitmap_info.file) {
5197 restore_bitmap_write_access(mddev->bitmap_info.file);
5198 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005199 }
NeilBrownc3d97142009-12-14 12:49:52 +11005200 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005201 }
5202
NeilBrown32a76272005-06-21 17:17:14 -07005203 return err;
5204}
5205
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206/*
5207 * set_array_info is used two different ways
5208 * The original usage is when creating a new array.
5209 * In this usage, raid_disks is > 0 and it together with
5210 * level, size, not_persistent,layout,chunksize determine the
5211 * shape of the array.
5212 * This will always create an array with a type-0.90.0 superblock.
5213 * The newer usage is when assembling an array.
5214 * In this case raid_disks will be 0, and the major_version field is
5215 * use to determine which style super-blocks are to be found on the devices.
5216 * The minor and patch _version numbers are also kept incase the
5217 * super_block handler wishes to interpret them.
5218 */
5219static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5220{
5221
5222 if (info->raid_disks == 0) {
5223 /* just setting version number for superblock loading */
5224 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005225 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 super_types[info->major_version].name == NULL) {
5227 /* maybe try to auto-load a module? */
5228 printk(KERN_INFO
5229 "md: superblock version %d not known\n",
5230 info->major_version);
5231 return -EINVAL;
5232 }
5233 mddev->major_version = info->major_version;
5234 mddev->minor_version = info->minor_version;
5235 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005236 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005237 /* ensure mddev_put doesn't delete this now that there
5238 * is some minimal configuration.
5239 */
5240 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 return 0;
5242 }
5243 mddev->major_version = MD_MAJOR_VERSION;
5244 mddev->minor_version = MD_MINOR_VERSION;
5245 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5246 mddev->ctime = get_seconds();
5247
5248 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005249 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005250 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 mddev->raid_disks = info->raid_disks;
5252 /* don't set md_minor, it is determined by which /dev/md* was
5253 * openned
5254 */
5255 if (info->state & (1<<MD_SB_CLEAN))
5256 mddev->recovery_cp = MaxSector;
5257 else
5258 mddev->recovery_cp = 0;
5259 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005260 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261
5262 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005263 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264
5265 mddev->max_disks = MD_SB_DISKS;
5266
NeilBrowne6910632008-02-06 01:39:51 -08005267 if (mddev->persistent)
5268 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005269 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270
NeilBrownc3d97142009-12-14 12:49:52 +11005271 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5272 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005273
NeilBrownf6705572006-03-27 01:18:11 -08005274 mddev->reshape_position = MaxSector;
5275
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 /*
5277 * Generate a 128 bit UUID
5278 */
5279 get_random_bytes(mddev->uuid, 16);
5280
NeilBrownf6705572006-03-27 01:18:11 -08005281 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005282 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005283 mddev->new_layout = mddev->layout;
5284 mddev->delta_disks = 0;
5285
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 return 0;
5287}
5288
Dan Williams1f403622009-03-31 14:59:03 +11005289void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5290{
Dan Williamsb522adc2009-03-31 15:00:31 +11005291 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5292
5293 if (mddev->external_size)
5294 return;
5295
Dan Williams1f403622009-03-31 14:59:03 +11005296 mddev->array_sectors = array_sectors;
5297}
5298EXPORT_SYMBOL(md_set_array_sectors);
5299
Andre Nolld71f9f82008-07-11 22:02:22 +10005300static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005301{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005302 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005303 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005304 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005305
5306 if (mddev->pers->resize == NULL)
5307 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005308 /* The "num_sectors" is the number of sectors of each device that
5309 * is used. This can only make sense for arrays with redundancy.
5310 * linear and raid0 always use whatever space is available. We can only
5311 * consider changing this number if no resync or reconstruction is
5312 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005313 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005314 * of each device. If num_sectors is zero, we find the largest size
5315 * that fits.
5316
NeilBrowna35b0d62006-01-06 00:20:49 -08005317 */
5318 if (mddev->sync_thread)
5319 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005320 if (mddev->bitmap)
5321 /* Sorry, cannot grow a bitmap yet, just remove it,
5322 * grow, and re-add.
5323 */
5324 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005325 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005326 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005327
Andre Nolld71f9f82008-07-11 22:02:22 +10005328 if (fit && (num_sectors == 0 || num_sectors > avail))
5329 num_sectors = avail;
5330 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005331 return -ENOSPC;
5332 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005333 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005334 if (!rv)
5335 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005336 return rv;
5337}
5338
NeilBrownda943b92006-01-06 00:20:54 -08005339static int update_raid_disks(mddev_t *mddev, int raid_disks)
5340{
5341 int rv;
5342 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005343 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b92006-01-06 00:20:54 -08005344 return -EINVAL;
5345 if (raid_disks <= 0 ||
5346 raid_disks >= mddev->max_disks)
5347 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005348 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b92006-01-06 00:20:54 -08005349 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005350 mddev->delta_disks = raid_disks - mddev->raid_disks;
5351
5352 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b92006-01-06 00:20:54 -08005353 return rv;
5354}
5355
5356
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357/*
5358 * update_array_info is used to change the configuration of an
5359 * on-line array.
5360 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5361 * fields in the info are checked against the array.
5362 * Any differences that cannot be handled will cause an error.
5363 * Normally, only one change can be managed at a time.
5364 */
5365static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5366{
5367 int rv = 0;
5368 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005369 int state = 0;
5370
5371 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005372 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005373 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374
5375 if (mddev->major_version != info->major_version ||
5376 mddev->minor_version != info->minor_version ||
5377/* mddev->patch_version != info->patch_version || */
5378 mddev->ctime != info->ctime ||
5379 mddev->level != info->level ||
5380/* mddev->layout != info->layout || */
5381 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005382 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005383 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5384 ((state^info->state) & 0xfffffe00)
5385 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 return -EINVAL;
5387 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005388 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5389 cnt++;
5390 if (mddev->raid_disks != info->raid_disks)
5391 cnt++;
5392 if (mddev->layout != info->layout)
5393 cnt++;
5394 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5395 cnt++;
5396 if (cnt == 0)
5397 return 0;
5398 if (cnt > 1)
5399 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400
5401 if (mddev->layout != info->layout) {
5402 /* Change layout
5403 * we don't need to do anything at the md level, the
5404 * personality will take care of it all.
5405 */
NeilBrown50ac1682009-06-18 08:47:55 +10005406 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005408 else {
5409 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005410 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005411 if (rv)
5412 mddev->new_layout = mddev->layout;
5413 return rv;
5414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005416 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005417 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418
NeilBrownda943b92006-01-06 00:20:54 -08005419 if (mddev->raid_disks != info->raid_disks)
5420 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421
NeilBrown36fa3062005-09-09 16:23:45 -07005422 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5423 if (mddev->pers->quiesce == NULL)
5424 return -EINVAL;
5425 if (mddev->recovery || mddev->sync_thread)
5426 return -EBUSY;
5427 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5428 /* add the bitmap */
5429 if (mddev->bitmap)
5430 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005431 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005432 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005433 mddev->bitmap_info.offset =
5434 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005435 mddev->pers->quiesce(mddev, 1);
5436 rv = bitmap_create(mddev);
5437 if (rv)
5438 bitmap_destroy(mddev);
5439 mddev->pers->quiesce(mddev, 0);
5440 } else {
5441 /* remove the bitmap */
5442 if (!mddev->bitmap)
5443 return -ENOENT;
5444 if (mddev->bitmap->file)
5445 return -EINVAL;
5446 mddev->pers->quiesce(mddev, 1);
5447 bitmap_destroy(mddev);
5448 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005449 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005450 }
5451 }
NeilBrown850b2b42006-10-03 01:15:46 -07005452 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 return rv;
5454}
5455
5456static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5457{
5458 mdk_rdev_t *rdev;
5459
5460 if (mddev->pers == NULL)
5461 return -ENODEV;
5462
5463 rdev = find_rdev(mddev, dev);
5464 if (!rdev)
5465 return -ENODEV;
5466
5467 md_error(mddev, rdev);
5468 return 0;
5469}
5470
Andre Noll2f9618c2008-04-25 18:57:58 +02005471/*
5472 * We have a problem here : there is no easy way to give a CHS
5473 * virtual geometry. We currently pretend that we have a 2 heads
5474 * 4 sectors (with a BIG number of cylinders...). This drives
5475 * dosfs just mad... ;-)
5476 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005477static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5478{
5479 mddev_t *mddev = bdev->bd_disk->private_data;
5480
5481 geo->heads = 2;
5482 geo->sectors = 4;
5483 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5484 return 0;
5485}
5486
Al Viroa39907f2008-03-02 10:31:15 -05005487static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 unsigned int cmd, unsigned long arg)
5489{
5490 int err = 0;
5491 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 mddev_t *mddev = NULL;
5493
5494 if (!capable(CAP_SYS_ADMIN))
5495 return -EACCES;
5496
5497 /*
5498 * Commands dealing with the RAID driver but not any
5499 * particular array:
5500 */
5501 switch (cmd)
5502 {
5503 case RAID_VERSION:
5504 err = get_version(argp);
5505 goto done;
5506
5507 case PRINT_RAID_DEBUG:
5508 err = 0;
5509 md_print_devices();
5510 goto done;
5511
5512#ifndef MODULE
5513 case RAID_AUTORUN:
5514 err = 0;
5515 autostart_arrays(arg);
5516 goto done;
5517#endif
5518 default:;
5519 }
5520
5521 /*
5522 * Commands creating/starting a new array:
5523 */
5524
Al Viroa39907f2008-03-02 10:31:15 -05005525 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526
5527 if (!mddev) {
5528 BUG();
5529 goto abort;
5530 }
5531
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 err = mddev_lock(mddev);
5533 if (err) {
5534 printk(KERN_INFO
5535 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5536 err, cmd);
5537 goto abort;
5538 }
5539
5540 switch (cmd)
5541 {
5542 case SET_ARRAY_INFO:
5543 {
5544 mdu_array_info_t info;
5545 if (!arg)
5546 memset(&info, 0, sizeof(info));
5547 else if (copy_from_user(&info, argp, sizeof(info))) {
5548 err = -EFAULT;
5549 goto abort_unlock;
5550 }
5551 if (mddev->pers) {
5552 err = update_array_info(mddev, &info);
5553 if (err) {
5554 printk(KERN_WARNING "md: couldn't update"
5555 " array info. %d\n", err);
5556 goto abort_unlock;
5557 }
5558 goto done_unlock;
5559 }
5560 if (!list_empty(&mddev->disks)) {
5561 printk(KERN_WARNING
5562 "md: array %s already has disks!\n",
5563 mdname(mddev));
5564 err = -EBUSY;
5565 goto abort_unlock;
5566 }
5567 if (mddev->raid_disks) {
5568 printk(KERN_WARNING
5569 "md: array %s already initialised!\n",
5570 mdname(mddev));
5571 err = -EBUSY;
5572 goto abort_unlock;
5573 }
5574 err = set_array_info(mddev, &info);
5575 if (err) {
5576 printk(KERN_WARNING "md: couldn't set"
5577 " array info. %d\n", err);
5578 goto abort_unlock;
5579 }
5580 }
5581 goto done_unlock;
5582
5583 default:;
5584 }
5585
5586 /*
5587 * Commands querying/configuring an existing array:
5588 */
NeilBrown32a76272005-06-21 17:17:14 -07005589 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005590 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005591 if ((!mddev->raid_disks && !mddev->external)
5592 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5593 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5594 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 err = -ENODEV;
5596 goto abort_unlock;
5597 }
5598
5599 /*
5600 * Commands even a read-only array can execute:
5601 */
5602 switch (cmd)
5603 {
5604 case GET_ARRAY_INFO:
5605 err = get_array_info(mddev, argp);
5606 goto done_unlock;
5607
NeilBrown32a76272005-06-21 17:17:14 -07005608 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005609 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005610 goto done_unlock;
5611
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 case GET_DISK_INFO:
5613 err = get_disk_info(mddev, argp);
5614 goto done_unlock;
5615
5616 case RESTART_ARRAY_RW:
5617 err = restart_array(mddev);
5618 goto done_unlock;
5619
5620 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005621 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 goto done_unlock;
5623
5624 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005625 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 goto done_unlock;
5627
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 }
5629
5630 /*
5631 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005632 * superblock, so we do not allow them on read-only arrays.
5633 * However non-MD ioctls (e.g. get-size) will still come through
5634 * here and hit the 'default' below, so only disallow
5635 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005637 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005638 if (mddev->ro == 2) {
5639 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005640 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005641 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5642 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005643 } else {
5644 err = -EROFS;
5645 goto abort_unlock;
5646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 }
5648
5649 switch (cmd)
5650 {
5651 case ADD_NEW_DISK:
5652 {
5653 mdu_disk_info_t info;
5654 if (copy_from_user(&info, argp, sizeof(info)))
5655 err = -EFAULT;
5656 else
5657 err = add_new_disk(mddev, &info);
5658 goto done_unlock;
5659 }
5660
5661 case HOT_REMOVE_DISK:
5662 err = hot_remove_disk(mddev, new_decode_dev(arg));
5663 goto done_unlock;
5664
5665 case HOT_ADD_DISK:
5666 err = hot_add_disk(mddev, new_decode_dev(arg));
5667 goto done_unlock;
5668
5669 case SET_DISK_FAULTY:
5670 err = set_disk_faulty(mddev, new_decode_dev(arg));
5671 goto done_unlock;
5672
5673 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005674 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 goto done_unlock;
5676
NeilBrown32a76272005-06-21 17:17:14 -07005677 case SET_BITMAP_FILE:
5678 err = set_bitmap_file(mddev, (int)arg);
5679 goto done_unlock;
5680
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 err = -EINVAL;
5683 goto abort_unlock;
5684 }
5685
5686done_unlock:
5687abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005688 if (mddev->hold_active == UNTIL_IOCTL &&
5689 err != -EINVAL)
5690 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 mddev_unlock(mddev);
5692
5693 return err;
5694done:
5695 if (err)
5696 MD_BUG();
5697abort:
5698 return err;
5699}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005700#ifdef CONFIG_COMPAT
5701static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5702 unsigned int cmd, unsigned long arg)
5703{
5704 switch (cmd) {
5705 case HOT_REMOVE_DISK:
5706 case HOT_ADD_DISK:
5707 case SET_DISK_FAULTY:
5708 case SET_BITMAP_FILE:
5709 /* These take in integer arg, do not convert */
5710 break;
5711 default:
5712 arg = (unsigned long)compat_ptr(arg);
5713 break;
5714 }
5715
5716 return md_ioctl(bdev, mode, cmd, arg);
5717}
5718#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719
Al Viroa39907f2008-03-02 10:31:15 -05005720static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721{
5722 /*
5723 * Succeed if we can lock the mddev, which confirms that
5724 * it isn't being stopped right now.
5725 */
NeilBrownd3374822009-01-09 08:31:10 +11005726 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 int err;
5728
NeilBrownd3374822009-01-09 08:31:10 +11005729 if (mddev->gendisk != bdev->bd_disk) {
5730 /* we are racing with mddev_put which is discarding this
5731 * bd_disk.
5732 */
5733 mddev_put(mddev);
5734 /* Wait until bdev->bd_disk is definitely gone */
5735 flush_scheduled_work();
5736 /* Then retry the open from the top */
5737 return -ERESTARTSYS;
5738 }
5739 BUG_ON(mddev != bdev->bd_disk->private_data);
5740
NeilBrownc8c00a62009-08-10 12:50:52 +10005741 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 goto out;
5743
5744 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005745 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005746 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747
Al Viroa39907f2008-03-02 10:31:15 -05005748 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 out:
5750 return err;
5751}
5752
Al Viroa39907f2008-03-02 10:31:15 -05005753static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754{
Al Viroa39907f2008-03-02 10:31:15 -05005755 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005757 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005758 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 mddev_put(mddev);
5760
5761 return 0;
5762}
5763
Linus Torvalds44ce6292007-05-09 18:51:36 -07005764static int md_media_changed(struct gendisk *disk)
5765{
5766 mddev_t *mddev = disk->private_data;
5767
5768 return mddev->changed;
5769}
5770
5771static int md_revalidate(struct gendisk *disk)
5772{
5773 mddev_t *mddev = disk->private_data;
5774
5775 mddev->changed = 0;
5776 return 0;
5777}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005778static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779{
5780 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005781 .open = md_open,
5782 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005783 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005784#ifdef CONFIG_COMPAT
5785 .compat_ioctl = md_compat_ioctl,
5786#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005787 .getgeo = md_getgeo,
Linus Torvalds44ce6292007-05-09 18:51:36 -07005788 .media_changed = md_media_changed,
5789 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790};
5791
Adrian Bunk75c96f82005-05-05 16:16:09 -07005792static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793{
5794 mdk_thread_t *thread = arg;
5795
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 /*
5797 * md_thread is a 'system-thread', it's priority should be very
5798 * high. We avoid resource deadlocks individually in each
5799 * raid personality. (RAID5 does preallocation) We also use RR and
5800 * the very same RT priority as kswapd, thus we will never get
5801 * into a priority inversion deadlock.
5802 *
5803 * we definitely have to have equal or higher priority than
5804 * bdflush, otherwise bdflush will deadlock if there are too
5805 * many dirty RAID5 blocks.
5806 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807
NeilBrown6985c432005-10-19 21:23:47 -07005808 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005809 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810
NeilBrown93588e22005-11-15 00:09:12 -08005811 /* We need to wait INTERRUPTIBLE so that
5812 * we don't add to the load-average.
5813 * That means we need to be sure no signals are
5814 * pending
5815 */
5816 if (signal_pending(current))
5817 flush_signals(current);
5818
5819 wait_event_interruptible_timeout
5820 (thread->wqueue,
5821 test_bit(THREAD_WAKEUP, &thread->flags)
5822 || kthread_should_stop(),
5823 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824
5825 clear_bit(THREAD_WAKEUP, &thread->flags);
5826
NeilBrown787453c2005-11-08 21:39:43 -08005827 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005829
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 return 0;
5831}
5832
5833void md_wakeup_thread(mdk_thread_t *thread)
5834{
5835 if (thread) {
5836 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5837 set_bit(THREAD_WAKEUP, &thread->flags);
5838 wake_up(&thread->wqueue);
5839 }
5840}
5841
5842mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5843 const char *name)
5844{
5845 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846
NeilBrown9ffae0c2006-01-06 00:20:32 -08005847 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 if (!thread)
5849 return NULL;
5850
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 init_waitqueue_head(&thread->wqueue);
5852
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 thread->run = run;
5854 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005855 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10005856 thread->tsk = kthread_run(md_thread, thread,
5857 "%s_%s",
5858 mdname(thread->mddev),
5859 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07005860 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 kfree(thread);
5862 return NULL;
5863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 return thread;
5865}
5866
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867void md_unregister_thread(mdk_thread_t *thread)
5868{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005869 if (!thread)
5870 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005871 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005872
5873 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 kfree(thread);
5875}
5876
5877void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5878{
5879 if (!mddev) {
5880 MD_BUG();
5881 return;
5882 }
5883
NeilBrownb2d444d2005-11-08 21:39:31 -08005884 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005886
5887 if (mddev->external)
5888 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005889/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5891 mdname(mddev),
5892 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5893 __builtin_return_address(0),__builtin_return_address(1),
5894 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005895*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005896 if (!mddev->pers)
5897 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 if (!mddev->pers->error_handler)
5899 return;
5900 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005901 if (mddev->degraded)
5902 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005903 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5905 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5906 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005907 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908}
5909
5910/* seq_file implementation /proc/mdstat */
5911
5912static void status_unused(struct seq_file *seq)
5913{
5914 int i = 0;
5915 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916
5917 seq_printf(seq, "unused devices: ");
5918
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005919 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 char b[BDEVNAME_SIZE];
5921 i++;
5922 seq_printf(seq, "%s ",
5923 bdevname(rdev->bdev,b));
5924 }
5925 if (!i)
5926 seq_printf(seq, "<none>");
5927
5928 seq_printf(seq, "\n");
5929}
5930
5931
5932static void status_resync(struct seq_file *seq, mddev_t * mddev)
5933{
NeilBrowndd71cf62009-05-07 12:49:35 +10005934 sector_t max_sectors, resync, res;
5935 unsigned long dt, db;
5936 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08005937 int scale;
5938 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939
NeilBrowndd71cf62009-05-07 12:49:35 +10005940 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941
5942 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10005943 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 else
NeilBrowndd71cf62009-05-07 12:49:35 +10005945 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946
5947 /*
5948 * Should not happen.
5949 */
NeilBrowndd71cf62009-05-07 12:49:35 +10005950 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 MD_BUG();
5952 return;
5953 }
NeilBrown4588b422006-03-27 01:18:04 -08005954 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10005955 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08005956 * u32, as those are the requirements for sector_div.
5957 * Thus 'scale' must be at least 10
5958 */
5959 scale = 10;
5960 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10005961 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08005962 scale++;
5963 }
5964 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10005965 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08005966
5967 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 {
NeilBrown4588b422006-03-27 01:18:04 -08005969 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 seq_printf(seq, "[");
5971 for (i = 0; i < x; i++)
5972 seq_printf(seq, "=");
5973 seq_printf(seq, ">");
5974 for (i = 0; i < y; i++)
5975 seq_printf(seq, ".");
5976 seq_printf(seq, "] ");
5977 }
NeilBrown4588b422006-03-27 01:18:04 -08005978 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005979 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5980 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005981 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5982 "check" :
5983 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5984 "resync" : "recovery"))),
5985 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10005986 (unsigned long long) resync/2,
5987 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988
5989 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990 * dt: time from mark until now
5991 * db: blocks written from mark until now
5992 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10005993 *
5994 * rt is a sector_t, so could be 32bit or 64bit.
5995 * So we divide before multiply in case it is 32bit and close
5996 * to the limit.
5997 * We scale the divisor (db) by 32 to avoid loosing precision
5998 * near the end of resync when the number of remaining sectors
5999 * is close to 'db'.
6000 * We then divide rt by 32 after multiplying by db to compensate.
6001 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 */
6003 dt = ((jiffies - mddev->resync_mark) / HZ);
6004 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006005 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6006 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007
NeilBrowndd71cf62009-05-07 12:49:35 +10006008 rt = max_sectors - resync; /* number of remaining sectors */
6009 sector_div(rt, db/32+1);
6010 rt *= dt;
6011 rt >>= 5;
6012
6013 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6014 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015
NeilBrownff4e8d92006-07-10 04:44:16 -07006016 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017}
6018
6019static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6020{
6021 struct list_head *tmp;
6022 loff_t l = *pos;
6023 mddev_t *mddev;
6024
6025 if (l >= 0x10000)
6026 return NULL;
6027 if (!l--)
6028 /* header */
6029 return (void*)1;
6030
6031 spin_lock(&all_mddevs_lock);
6032 list_for_each(tmp,&all_mddevs)
6033 if (!l--) {
6034 mddev = list_entry(tmp, mddev_t, all_mddevs);
6035 mddev_get(mddev);
6036 spin_unlock(&all_mddevs_lock);
6037 return mddev;
6038 }
6039 spin_unlock(&all_mddevs_lock);
6040 if (!l--)
6041 return (void*)2;/* tail */
6042 return NULL;
6043}
6044
6045static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6046{
6047 struct list_head *tmp;
6048 mddev_t *next_mddev, *mddev = v;
6049
6050 ++*pos;
6051 if (v == (void*)2)
6052 return NULL;
6053
6054 spin_lock(&all_mddevs_lock);
6055 if (v == (void*)1)
6056 tmp = all_mddevs.next;
6057 else
6058 tmp = mddev->all_mddevs.next;
6059 if (tmp != &all_mddevs)
6060 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6061 else {
6062 next_mddev = (void*)2;
6063 *pos = 0x10000;
6064 }
6065 spin_unlock(&all_mddevs_lock);
6066
6067 if (v != (void*)1)
6068 mddev_put(mddev);
6069 return next_mddev;
6070
6071}
6072
6073static void md_seq_stop(struct seq_file *seq, void *v)
6074{
6075 mddev_t *mddev = v;
6076
6077 if (mddev && v != (void*)1 && v != (void*)2)
6078 mddev_put(mddev);
6079}
6080
NeilBrownd7603b72006-01-06 00:20:30 -08006081struct mdstat_info {
6082 int event;
6083};
6084
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085static int md_seq_show(struct seq_file *seq, void *v)
6086{
6087 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006088 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006090 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006091 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092
6093 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006094 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 seq_printf(seq, "Personalities : ");
6096 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006097 list_for_each_entry(pers, &pers_list, list)
6098 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099
6100 spin_unlock(&pers_lock);
6101 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006102 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103 return 0;
6104 }
6105 if (v == (void*)2) {
6106 status_unused(seq);
6107 return 0;
6108 }
6109
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006110 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006112
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6114 seq_printf(seq, "%s : %sactive", mdname(mddev),
6115 mddev->pers ? "" : "in");
6116 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006117 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006119 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006120 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 seq_printf(seq, " %s", mddev->pers->name);
6122 }
6123
Andre Nolldd8ac332009-03-31 14:33:13 +11006124 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006125 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126 char b[BDEVNAME_SIZE];
6127 seq_printf(seq, " %s[%d]",
6128 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006129 if (test_bit(WriteMostly, &rdev->flags))
6130 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006131 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 seq_printf(seq, "(F)");
6133 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006134 } else if (rdev->raid_disk < 0)
6135 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006136 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 }
6138
6139 if (!list_empty(&mddev->disks)) {
6140 if (mddev->pers)
6141 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006142 (unsigned long long)
6143 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 else
6145 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006146 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006148 if (mddev->persistent) {
6149 if (mddev->major_version != 0 ||
6150 mddev->minor_version != 90) {
6151 seq_printf(seq," super %d.%d",
6152 mddev->major_version,
6153 mddev->minor_version);
6154 }
NeilBrowne6910632008-02-06 01:39:51 -08006155 } else if (mddev->external)
6156 seq_printf(seq, " super external:%s",
6157 mddev->metadata_type);
6158 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006159 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160
6161 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006162 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006164 if (mddev->pers->sync_request) {
6165 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006166 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006167 seq_printf(seq, "\n ");
6168 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6169 seq_printf(seq, "\tresync=DELAYED\n ");
6170 else if (mddev->recovery_cp < MaxSector)
6171 seq_printf(seq, "\tresync=PENDING\n ");
6172 }
NeilBrown32a76272005-06-21 17:17:14 -07006173 } else
6174 seq_printf(seq, "\n ");
6175
6176 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006177 unsigned long chunk_kb;
6178 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006179 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006180 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006181 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6182 "%lu%s chunk",
6183 bitmap->pages - bitmap->missing_pages,
6184 bitmap->pages,
6185 (bitmap->pages - bitmap->missing_pages)
6186 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006187 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006188 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006189 if (bitmap->file) {
6190 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006191 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006192 }
NeilBrown78d742d2005-06-21 17:17:15 -07006193
NeilBrown32a76272005-06-21 17:17:14 -07006194 seq_printf(seq, "\n");
6195 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 }
6197
6198 seq_printf(seq, "\n");
6199 }
6200 mddev_unlock(mddev);
6201
6202 return 0;
6203}
6204
Jan Engelhardt110518b2009-05-07 12:49:37 +10006205static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 .start = md_seq_start,
6207 .next = md_seq_next,
6208 .stop = md_seq_stop,
6209 .show = md_seq_show,
6210};
6211
6212static int md_seq_open(struct inode *inode, struct file *file)
6213{
6214 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006215 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6216 if (mi == NULL)
6217 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218
6219 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006220 if (error)
6221 kfree(mi);
6222 else {
6223 struct seq_file *p = file->private_data;
6224 p->private = mi;
6225 mi->event = atomic_read(&md_event_count);
6226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 return error;
6228}
6229
NeilBrownd7603b72006-01-06 00:20:30 -08006230static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6231{
6232 struct seq_file *m = filp->private_data;
6233 struct mdstat_info *mi = m->private;
6234 int mask;
6235
6236 poll_wait(filp, &md_event_waiters, wait);
6237
6238 /* always allow read */
6239 mask = POLLIN | POLLRDNORM;
6240
6241 if (mi->event != atomic_read(&md_event_count))
6242 mask |= POLLERR | POLLPRI;
6243 return mask;
6244}
6245
Arjan van de Venfa027c22007-02-12 00:55:33 -08006246static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006247 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 .open = md_seq_open,
6249 .read = seq_read,
6250 .llseek = seq_lseek,
Martin Peschkec3f94b42007-05-09 02:35:35 -07006251 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006252 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253};
6254
NeilBrown2604b702006-01-06 00:20:36 -08006255int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006258 list_add_tail(&p->list, &pers_list);
6259 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260 spin_unlock(&pers_lock);
6261 return 0;
6262}
6263
NeilBrown2604b702006-01-06 00:20:36 -08006264int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265{
NeilBrown2604b702006-01-06 00:20:36 -08006266 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006268 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269 spin_unlock(&pers_lock);
6270 return 0;
6271}
6272
NeilBrowneea1bf32009-03-31 14:27:02 +11006273static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274{
6275 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006277 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278
6279 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006280 rcu_read_lock();
6281 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006283 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6284 (int)part_stat_read(&disk->part0, sectors[1]) -
6285 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006286 /* sync IO will cause sync_io to increase before the disk_stats
6287 * as sync_io is counted when a request starts, and
6288 * disk_stats is counted when it completes.
6289 * So resync activity will cause curr_events to be smaller than
6290 * when there was no such activity.
6291 * non-sync IO will cause disk_stat to increase without
6292 * increasing sync_io so curr_events will (eventually)
6293 * be larger than it was before. Once it becomes
6294 * substantially larger, the test below will cause
6295 * the array to appear non-idle, and resync will slow
6296 * down.
6297 * If there is a lot of outstanding resync activity when
6298 * we set last_event to curr_events, then all that activity
6299 * completing might cause the array to appear non-idle
6300 * and resync will be slowed down even though there might
6301 * not have been non-resync activity. This will only
6302 * happen once though. 'last_events' will soon reflect
6303 * the state where there is little or no outstanding
6304 * resync requests, and further resync activity will
6305 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006306 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006308 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309 rdev->last_events = curr_events;
6310 idle = 0;
6311 }
6312 }
NeilBrown4b809912008-07-21 17:05:25 +10006313 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 return idle;
6315}
6316
6317void md_done_sync(mddev_t *mddev, int blocks, int ok)
6318{
6319 /* another "blocks" (512byte) blocks have been synced */
6320 atomic_sub(blocks, &mddev->recovery_active);
6321 wake_up(&mddev->recovery_wait);
6322 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006323 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 md_wakeup_thread(mddev->thread);
6325 // stop recovery, signal do_sync ....
6326 }
6327}
6328
6329
NeilBrown06d91a52005-06-21 17:17:12 -07006330/* md_write_start(mddev, bi)
6331 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006332 * in superblock) before writing, schedule a superblock update
6333 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006334 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006335void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336{
Neil Brown0fd62b82008-06-28 08:31:36 +10006337 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006338 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006339 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006340
NeilBrownf91de922005-11-08 21:39:36 -08006341 BUG_ON(mddev->ro == 1);
6342 if (mddev->ro == 2) {
6343 /* need to switch to read/write */
6344 mddev->ro = 0;
6345 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6346 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006347 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006348 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006349 }
NeilBrown06d91a52005-06-21 17:17:12 -07006350 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006351 if (mddev->safemode == 1)
6352 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006353 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006354 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006355 if (mddev->in_sync) {
6356 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006357 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006358 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006359 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006360 }
NeilBrowna9701a32005-11-08 21:39:34 -08006361 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006362 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006363 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006364 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006365 wait_event(mddev->sb_wait,
6366 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6367 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368}
6369
6370void md_write_end(mddev_t *mddev)
6371{
6372 if (atomic_dec_and_test(&mddev->writes_pending)) {
6373 if (mddev->safemode == 2)
6374 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006375 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6377 }
6378}
6379
NeilBrown2a2275d2007-01-26 00:57:11 -08006380/* md_allow_write(mddev)
6381 * Calling this ensures that the array is marked 'active' so that writes
6382 * may proceed without blocking. It is important to call this before
6383 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6384 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006385 *
6386 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6387 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006388 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006389int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006390{
6391 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006392 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006393 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006394 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006395 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006396 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006397
6398 spin_lock_irq(&mddev->write_lock);
6399 if (mddev->in_sync) {
6400 mddev->in_sync = 0;
6401 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6402 if (mddev->safemode_delay &&
6403 mddev->safemode == 0)
6404 mddev->safemode = 1;
6405 spin_unlock_irq(&mddev->write_lock);
6406 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006407 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006408 } else
6409 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006410
6411 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6412 return -EAGAIN;
6413 else
6414 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006415}
6416EXPORT_SYMBOL_GPL(md_allow_write);
6417
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418#define SYNC_MARKS 10
6419#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006420void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421{
6422 mddev_t *mddev2;
6423 unsigned int currspeed = 0,
6424 window;
NeilBrown57afd892005-06-21 17:17:13 -07006425 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 unsigned long mark[SYNC_MARKS];
6427 sector_t mark_cnt[SYNC_MARKS];
6428 int last_mark,m;
6429 struct list_head *tmp;
6430 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006431 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006432 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006433 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434
6435 /* just incase thread restarts... */
6436 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6437 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006438 if (mddev->ro) /* never try to sync a read-only array */
6439 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440
NeilBrown61df9d92006-10-03 01:15:57 -07006441 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6442 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6443 desc = "data-check";
6444 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6445 desc = "requested-resync";
6446 else
6447 desc = "resync";
6448 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6449 desc = "reshape";
6450 else
6451 desc = "recovery";
6452
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453 /* we overload curr_resync somewhat here.
6454 * 0 == not engaged in resync at all
6455 * 2 == checking that there is no conflict with another sync
6456 * 1 == like 2, but have yielded to allow conflicting resync to
6457 * commense
6458 * other == active in resync - this many blocks
6459 *
6460 * Before starting a resync we must have set curr_resync to
6461 * 2, and then checked that every "conflicting" array has curr_resync
6462 * less than ours. When we find one that is the same or higher
6463 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6464 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6465 * This will mean we have to start checking from the beginning again.
6466 *
6467 */
6468
6469 do {
6470 mddev->curr_resync = 2;
6471
6472 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006473 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006474 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006475
6476 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006478 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479 if (mddev2 == mddev)
6480 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006481 if (!mddev->parallel_resync
6482 && mddev2->curr_resync
6483 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484 DEFINE_WAIT(wq);
6485 if (mddev < mddev2 && mddev->curr_resync == 2) {
6486 /* arbitrarily yield */
6487 mddev->curr_resync = 1;
6488 wake_up(&resync_wait);
6489 }
6490 if (mddev > mddev2 && mddev->curr_resync == 1)
6491 /* no need to wait here, we can wait the next
6492 * time 'round when curr_resync == 2
6493 */
6494 continue;
NeilBrown97441972008-09-19 11:49:54 +10006495 /* We need to wait 'interruptible' so as not to
6496 * contribute to the load average, and not to
6497 * be caught by 'softlockup'
6498 */
6499 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006500 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006501 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006502 printk(KERN_INFO "md: delaying %s of %s"
6503 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006505 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006507 if (signal_pending(current))
6508 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509 schedule();
6510 finish_wait(&resync_wait, &wq);
6511 goto try_again;
6512 }
6513 finish_wait(&resync_wait, &wq);
6514 }
6515 }
6516 } while (mddev->curr_resync < 2);
6517
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006518 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006519 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006521 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522 */
6523 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006524 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006525 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006526 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6527 j = mddev->resync_min;
6528 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006529 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006530
NeilBrownccfcc3c2006-03-27 01:18:09 -08006531 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006532 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006533 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006535 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006536 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006537 rcu_read_lock();
6538 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006539 if (rdev->raid_disk >= 0 &&
6540 !test_bit(Faulty, &rdev->flags) &&
6541 !test_bit(In_sync, &rdev->flags) &&
6542 rdev->recovery_offset < j)
6543 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006544 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546
NeilBrown61df9d92006-10-03 01:15:57 -07006547 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6548 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6549 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006550 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006551 "(but not more than %d KB/sec) for %s.\n",
6552 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553
NeilBrowneea1bf32009-03-31 14:27:02 +11006554 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006555
NeilBrown57afd892005-06-21 17:17:13 -07006556 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557 for (m = 0; m < SYNC_MARKS; m++) {
6558 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006559 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 }
6561 last_mark = 0;
6562 mddev->resync_mark = mark[last_mark];
6563 mddev->resync_mark_cnt = mark_cnt[last_mark];
6564
6565 /*
6566 * Tune reconstruction:
6567 */
6568 window = 32*(PAGE_SIZE/512);
6569 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6570 window/2,(unsigned long long) max_sectors/2);
6571
6572 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573 last_check = 0;
6574
6575 if (j>2) {
6576 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006577 "md: resuming %s of %s from checkpoint.\n",
6578 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579 mddev->curr_resync = j;
6580 }
NeilBrownefa59332009-12-14 12:49:49 +11006581 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582
6583 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006584 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585
NeilBrown57afd892005-06-21 17:17:13 -07006586 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006587
NeilBrown7a91ee12009-05-26 12:57:21 +10006588 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6589 ((mddev->curr_resync > mddev->curr_resync_completed &&
6590 (mddev->curr_resync - mddev->curr_resync_completed)
6591 > (max_sectors >> 4)) ||
6592 (j - mddev->curr_resync_completed)*2
6593 >= mddev->resync_max - mddev->curr_resync_completed
6594 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006595 /* time to update curr_resync_completed */
6596 blk_unplug(mddev->queue);
6597 wait_event(mddev->recovery_wait,
6598 atomic_read(&mddev->recovery_active) == 0);
6599 mddev->curr_resync_completed =
6600 mddev->curr_resync;
6601 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006602 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006603 }
NeilBrownacb180b2009-04-14 16:28:34 +10006604
NeilBrowne62e58a2009-07-01 13:15:35 +10006605 while (j >= mddev->resync_max && !kthread_should_stop()) {
6606 /* As this condition is controlled by user-space,
6607 * we can block indefinitely, so use '_interruptible'
6608 * to avoid triggering warnings.
6609 */
6610 flush_signals(current); /* just in case */
6611 wait_event_interruptible(mddev->recovery_wait,
6612 mddev->resync_max > j
6613 || kthread_should_stop());
6614 }
NeilBrownacb180b2009-04-14 16:28:34 +10006615
6616 if (kthread_should_stop())
6617 goto interrupted;
6618
NeilBrown57afd892005-06-21 17:17:13 -07006619 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006620 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006621 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006622 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623 goto out;
6624 }
NeilBrown57afd892005-06-21 17:17:13 -07006625
6626 if (!skipped) { /* actual IO requested */
6627 io_sectors += sectors;
6628 atomic_add(sectors, &mddev->recovery_active);
6629 }
6630
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631 j += sectors;
6632 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006633 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006634 if (last_check == 0)
6635 /* this is the earliers that rebuilt will be
6636 * visible in /proc/mdstat
6637 */
6638 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006639
6640 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641 continue;
6642
NeilBrown57afd892005-06-21 17:17:13 -07006643 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644
NeilBrowndfc70642008-05-23 13:04:39 -07006645 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646 break;
6647
6648 repeat:
6649 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6650 /* step marks */
6651 int next = (last_mark+1) % SYNC_MARKS;
6652
6653 mddev->resync_mark = mark[next];
6654 mddev->resync_mark_cnt = mark_cnt[next];
6655 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006656 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657 last_mark = next;
6658 }
6659
6660
NeilBrownc6207272008-02-06 01:39:52 -08006661 if (kthread_should_stop())
6662 goto interrupted;
6663
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664
6665 /*
6666 * this loop exits only if either when we are slower than
6667 * the 'hard' speed limit, or the system was IO-idle for
6668 * a jiffy.
6669 * the system might be non-idle CPU-wise, but we only care
6670 * about not overloading the IO subsystem. (things like an
6671 * e2fsck being done on the RAID array should execute fast)
6672 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006673 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674 cond_resched();
6675
NeilBrown57afd892005-06-21 17:17:13 -07006676 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6677 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678
NeilBrown88202a02006-01-06 00:21:36 -08006679 if (currspeed > speed_min(mddev)) {
6680 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006681 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006682 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683 goto repeat;
6684 }
6685 }
6686 }
NeilBrown61df9d92006-10-03 01:15:57 -07006687 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688 /*
6689 * this also signals 'finished resyncing' to md_stop
6690 */
6691 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006692 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693
6694 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6695
6696 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006697 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698
NeilBrowndfc70642008-05-23 13:04:39 -07006699 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006700 mddev->curr_resync > 2) {
6701 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6702 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6703 if (mddev->curr_resync >= mddev->recovery_cp) {
6704 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006705 "md: checkpointing %s of %s.\n",
6706 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006707 mddev->recovery_cp = mddev->curr_resync;
6708 }
6709 } else
6710 mddev->recovery_cp = MaxSector;
6711 } else {
6712 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6713 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006714 rcu_read_lock();
6715 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006716 if (rdev->raid_disk >= 0 &&
6717 !test_bit(Faulty, &rdev->flags) &&
6718 !test_bit(In_sync, &rdev->flags) &&
6719 rdev->recovery_offset < mddev->curr_resync)
6720 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07006721 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723 }
NeilBrown17571282006-12-10 02:20:52 -08006724 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006727 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6728 /* We completed so min/max setting can be forgotten if used. */
6729 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6730 mddev->resync_min = 0;
6731 mddev->resync_max = MaxSector;
6732 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6733 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006735 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6736 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006737 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738 wake_up(&resync_wait);
6739 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6740 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006741 return;
6742
6743 interrupted:
6744 /*
6745 * got a signal, exit.
6746 */
6747 printk(KERN_INFO
6748 "md: md_do_sync() got signal ... exiting\n");
6749 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6750 goto out;
6751
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752}
NeilBrown29269552006-03-27 01:18:10 -08006753EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754
6755
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006756static int remove_and_add_spares(mddev_t *mddev)
6757{
6758 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006759 int spares = 0;
6760
NeilBrown97e4f422009-03-31 14:33:13 +11006761 mddev->curr_resync_completed = 0;
6762
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006763 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006764 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006765 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006766 (test_bit(Faulty, &rdev->flags) ||
6767 ! test_bit(In_sync, &rdev->flags)) &&
6768 atomic_read(&rdev->nr_pending)==0) {
6769 if (mddev->pers->hot_remove_disk(
6770 mddev, rdev->raid_disk)==0) {
6771 char nm[20];
6772 sprintf(nm,"rd%d", rdev->raid_disk);
6773 sysfs_remove_link(&mddev->kobj, nm);
6774 rdev->raid_disk = -1;
6775 }
6776 }
6777
NeilBrown4044ba52009-01-09 08:31:11 +11006778 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006779 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006780 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006781 !test_bit(In_sync, &rdev->flags) &&
6782 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006783 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006784 if (rdev->raid_disk < 0
6785 && !test_bit(Faulty, &rdev->flags)) {
6786 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006787 if (mddev->pers->
6788 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006789 char nm[20];
6790 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006791 if (sysfs_create_link(&mddev->kobj,
6792 &rdev->kobj, nm))
6793 printk(KERN_WARNING
6794 "md: cannot register "
6795 "%s for %s\n",
6796 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006797 spares++;
6798 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11006799 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006800 } else
6801 break;
6802 }
NeilBrowndfc70642008-05-23 13:04:39 -07006803 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006804 }
6805 return spares;
6806}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807/*
6808 * This routine is regularly called by all per-raid-array threads to
6809 * deal with generic issues like resync and super-block update.
6810 * Raid personalities that don't have a thread (linear/raid0) do not
6811 * need this as they never do any recovery or update the superblock.
6812 *
6813 * It does not do any resync itself, but rather "forks" off other threads
6814 * to do that as needed.
6815 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6816 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006817 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818 * and wakeups up this thread which will reap the thread and finish up.
6819 * This thread also removes any faulty devices (with nr_pending == 0).
6820 *
6821 * The overall approach is:
6822 * 1/ if the superblock needs updating, update it.
6823 * 2/ If a recovery thread is running, don't do anything else.
6824 * 3/ If recovery has finished, clean up, possibly marking spares active.
6825 * 4/ If there are any faulty devices, remove them.
6826 * 5/ If array is degraded, try to add spares devices
6827 * 6/ If array has spares or is not in-sync, start a resync thread.
6828 */
6829void md_check_recovery(mddev_t *mddev)
6830{
6831 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832
6833
NeilBrown5f404022005-06-21 17:17:16 -07006834 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11006835 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836
6837 if (mddev->ro)
6838 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006839
6840 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006841 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006842 printk(KERN_INFO "md: %s in immediate safe mode\n",
6843 mdname(mddev));
6844 mddev->safemode = 2;
6845 }
6846 flush_signals(current);
6847 }
6848
NeilBrownc89a8ee2008-08-05 15:54:13 +10006849 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6850 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006851 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006852 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006854 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006855 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006856 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6857 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858 ))
6859 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006860
NeilBrowndf5b89b2006-03-27 01:18:20 -08006861 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006862 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006863
NeilBrownc89a8ee2008-08-05 15:54:13 +10006864 if (mddev->ro) {
6865 /* Only thing we do on a ro array is remove
6866 * failed devices.
6867 */
6868 remove_and_add_spares(mddev);
6869 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6870 goto unlock;
6871 }
6872
NeilBrown31a59e32008-04-30 00:52:30 -07006873 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006874 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006875 spin_lock_irq(&mddev->write_lock);
6876 if (mddev->safemode &&
6877 !atomic_read(&mddev->writes_pending) &&
6878 !mddev->in_sync &&
6879 mddev->recovery_cp == MaxSector) {
6880 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006881 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006882 if (mddev->persistent)
6883 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6884 }
6885 if (mddev->safemode == 1)
6886 mddev->safemode = 0;
6887 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006888 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006889 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006890 }
NeilBrownfca4d842005-06-21 17:17:11 -07006891
NeilBrown850b2b42006-10-03 01:15:46 -07006892 if (mddev->flags)
6893 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006894
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006895 list_for_each_entry(rdev, &mddev->disks, same_set)
Neil Brown52664732008-06-28 08:31:44 +10006896 if (test_and_clear_bit(StateChanged, &rdev->flags))
NeilBrown3c0ee632008-10-21 13:25:28 +11006897 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown52664732008-06-28 08:31:44 +10006898
NeilBrown06d91a52005-06-21 17:17:12 -07006899
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6901 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6902 /* resync/recovery still happening */
6903 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6904 goto unlock;
6905 }
6906 if (mddev->sync_thread) {
6907 /* resync has finished, collect result */
6908 md_unregister_thread(mddev->sync_thread);
6909 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006910 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6911 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912 /* success...*/
6913 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006914 if (mddev->pers->spare_active(mddev))
6915 sysfs_notify(&mddev->kobj, NULL,
6916 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917 }
NeilBrowncea9c222009-03-31 15:15:05 +11006918 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6919 mddev->pers->finish_reshape)
6920 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07006921 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006922
6923 /* if array is no-longer degraded, then any saved_raid_disk
6924 * information must be scrapped
6925 */
6926 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006927 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07006928 rdev->saved_raid_disk = -1;
6929
Linus Torvalds1da177e2005-04-16 15:20:36 -07006930 mddev->recovery = 0;
6931 /* flag recovery needed just to double check */
6932 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11006933 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006934 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935 goto unlock;
6936 }
Neil Brown72a23c22008-06-28 08:31:41 +10006937 /* Set RUNNING before clearing NEEDED to avoid
6938 * any transients in the value of "sync_action".
6939 */
6940 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6941 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006942 /* Clear some bits that don't mean anything, but
6943 * might be left set
6944 */
NeilBrown24dd4692005-11-08 21:39:26 -08006945 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6946 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006948 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6949 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950 /* no recovery is running.
6951 * remove any failed drives, then
6952 * add spares if possible.
6953 * Spare are also removed and re-added, to allow
6954 * the personality to fail the re-add.
6955 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006957 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10006958 if (mddev->pers->check_reshape == NULL ||
6959 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006960 /* Cannot proceed */
6961 goto unlock;
6962 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006963 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006964 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006965 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6966 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07006967 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006968 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006969 } else if (mddev->recovery_cp < MaxSector) {
6970 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006971 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006972 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6973 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006974 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006975
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006977 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6978 /* We are adding a device or devices to an array
6979 * which has the bitmap stored on all devices.
6980 * So make sure all bitmap pages get written
6981 */
6982 bitmap_write_all(mddev->bitmap);
6983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984 mddev->sync_thread = md_register_thread(md_do_sync,
6985 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006986 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987 if (!mddev->sync_thread) {
6988 printk(KERN_ERR "%s: could not start resync"
6989 " thread...\n",
6990 mdname(mddev));
6991 /* leave the spares where they are, it shouldn't hurt */
6992 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006993 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11006995 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006996 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997 }
6998 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006999 if (!mddev->sync_thread) {
7000 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7001 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7002 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007003 if (mddev->sysfs_action)
7004 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006 mddev_unlock(mddev);
7007 }
7008}
7009
Dan Williams6bfe0b42008-04-30 00:52:32 -07007010void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7011{
NeilBrown3c0ee632008-10-21 13:25:28 +11007012 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007013 wait_event_timeout(rdev->blocked_wait,
7014 !test_bit(Blocked, &rdev->flags),
7015 msecs_to_jiffies(5000));
7016 rdev_dec_pending(rdev, mddev);
7017}
7018EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7019
Adrian Bunk75c96f82005-05-05 16:16:09 -07007020static int md_notify_reboot(struct notifier_block *this,
7021 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007022{
7023 struct list_head *tmp;
7024 mddev_t *mddev;
7025
7026 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7027
7028 printk(KERN_INFO "md: stopping all md devices.\n");
7029
NeilBrown29ac4aa2008-02-06 01:39:58 -08007030 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007031 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007032 /* Force a switch to readonly even array
7033 * appears to still be in use. Hence
7034 * the '100'.
7035 */
NeilBrownd710e132008-10-13 11:55:12 +11007036 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007037 mddev_unlock(mddev);
7038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039 /*
7040 * certain more exotic SCSI devices are known to be
7041 * volatile wrt too early system reboots. While the
7042 * right place to handle this issue is the given
7043 * driver, we do want to have a safe RAID driver ...
7044 */
7045 mdelay(1000*1);
7046 }
7047 return NOTIFY_DONE;
7048}
7049
Adrian Bunk75c96f82005-05-05 16:16:09 -07007050static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051 .notifier_call = md_notify_reboot,
7052 .next = NULL,
7053 .priority = INT_MAX, /* before any real devices */
7054};
7055
7056static void md_geninit(void)
7057{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7059
Denis V. Lunevc7705f32008-04-29 01:02:35 -07007060 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061}
7062
Adrian Bunk75c96f82005-05-05 16:16:09 -07007063static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007065 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066 return -1;
7067 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007068 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069 return -1;
7070 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007071 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007072 md_probe, NULL, NULL);
7073 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074 md_probe, NULL, NULL);
7075
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007077 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007078
7079 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007080 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081}
7082
7083
7084#ifndef MODULE
7085
7086/*
7087 * Searches all registered partitions for autorun RAID arrays
7088 * at boot time.
7089 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007090
7091static LIST_HEAD(all_detected_devices);
7092struct detected_devices_node {
7093 struct list_head list;
7094 dev_t dev;
7095};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096
7097void md_autodetect_dev(dev_t dev)
7098{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007099 struct detected_devices_node *node_detected_dev;
7100
7101 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7102 if (node_detected_dev) {
7103 node_detected_dev->dev = dev;
7104 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7105 } else {
7106 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7107 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109}
7110
7111
7112static void autostart_arrays(int part)
7113{
7114 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007115 struct detected_devices_node *node_detected_dev;
7116 dev_t dev;
7117 int i_scanned, i_passed;
7118
7119 i_scanned = 0;
7120 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121
7122 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7123
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007124 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7125 i_scanned++;
7126 node_detected_dev = list_entry(all_detected_devices.next,
7127 struct detected_devices_node, list);
7128 list_del(&node_detected_dev->list);
7129 dev = node_detected_dev->dev;
7130 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007131 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132 if (IS_ERR(rdev))
7133 continue;
7134
NeilBrownb2d444d2005-11-08 21:39:31 -08007135 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 MD_BUG();
7137 continue;
7138 }
NeilBrownd0fae182008-03-04 14:29:31 -08007139 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007141 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007143
7144 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7145 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146
7147 autorun_devices(part);
7148}
7149
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007150#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151
7152static __exit void md_exit(void)
7153{
7154 mddev_t *mddev;
7155 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007156
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007157 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007158 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007160 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161 unregister_blkdev(mdp_major, "mdp");
7162 unregister_reboot_notifier(&md_notifier);
7163 unregister_sysctl_table(raid_table_header);
7164 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007165 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007167 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168 }
7169}
7170
Dan Williams685784a2007-07-09 11:56:42 -07007171subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007172module_exit(md_exit)
7173
NeilBrownf91de922005-11-08 21:39:36 -08007174static int get_ro(char *buffer, struct kernel_param *kp)
7175{
7176 return sprintf(buffer, "%d", start_readonly);
7177}
7178static int set_ro(const char *val, struct kernel_param *kp)
7179{
7180 char *e;
7181 int num = simple_strtoul(val, &e, 10);
7182 if (*val && (*e == '\0' || *e == '\n')) {
7183 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007184 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007185 }
7186 return -EINVAL;
7187}
7188
NeilBrown80ca3a42006-07-10 04:44:18 -07007189module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7190module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8e2006-01-06 00:20:15 -08007191
NeilBrownefeb53c2009-01-09 08:31:10 +11007192module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007193
Linus Torvalds1da177e2005-04-16 15:20:36 -07007194EXPORT_SYMBOL(register_md_personality);
7195EXPORT_SYMBOL(unregister_md_personality);
7196EXPORT_SYMBOL(md_error);
7197EXPORT_SYMBOL(md_done_sync);
7198EXPORT_SYMBOL(md_write_start);
7199EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200EXPORT_SYMBOL(md_register_thread);
7201EXPORT_SYMBOL(md_unregister_thread);
7202EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203EXPORT_SYMBOL(md_check_recovery);
7204MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007205MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007206MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007207MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);