blob: bcc66680952e71fb1295a68a1575b131c7c1900a [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>
NeilBrownfb4d8c72008-10-13 11:55:12 +110042#include <linux/hdreg.h>
43#include <linux/proc_fs.h>
44#include <linux/random.h>
45#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070046#include <linux/file.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110047#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110048#include <linux/raid/md_p.h>
49#include <linux/raid/md_u.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110050#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110051#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#define DEBUG 0
54#define dprintk(x...) ((void)(DEBUG && printk(x)))
55
56
57#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110058static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
NeilBrown2604b702006-01-06 00:20:36 -080061static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static DEFINE_SPINLOCK(pers_lock);
63
Adrian Bunk5e563412006-06-26 00:27:42 -070064static void md_print_devices(void);
65
Bernd Schubert90b08712008-05-23 13:04:38 -070066static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
67
Adrian Bunk5e563412006-06-26 00:27:42 -070068#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*
Robert Becker1e509152009-12-14 12:49:58 +110071 * Default number of read corrections we'll attempt on an rdev
72 * before ejecting it from the array. We divide the read error
73 * count by 2 for every hour elapsed between read errors.
74 */
75#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
76/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
78 * is 1000 KB/sec, so the extra system load does not show up that much.
79 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070080 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * subsystem is idle. There is also an 'absolute maximum' reconstruction
82 * speed limit - in case reconstruction slows down your system despite
83 * idle IO detection.
84 *
85 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080086 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 */
88
89static int sysctl_speed_limit_min = 1000;
90static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080091static inline int speed_min(mddev_t *mddev)
92{
93 return mddev->sync_speed_min ?
94 mddev->sync_speed_min : sysctl_speed_limit_min;
95}
96
97static inline int speed_max(mddev_t *mddev)
98{
99 return mddev->sync_speed_max ?
100 mddev->sync_speed_max : sysctl_speed_limit_max;
101}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103static struct ctl_table_header *raid_table_header;
104
105static ctl_table raid_table[] = {
106 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 .procname = "speed_limit_min",
108 .data = &sysctl_speed_limit_min,
109 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700110 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800111 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 },
113 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .procname = "speed_limit_max",
115 .data = &sysctl_speed_limit_max,
116 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700117 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800118 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800120 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
123static ctl_table raid_dir_table[] = {
124 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .procname = "raid",
126 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700127 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 .child = raid_table,
129 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800130 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131};
132
133static ctl_table raid_root_table[] = {
134 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .procname = "dev",
136 .maxlen = 0,
137 .mode = 0555,
138 .child = raid_dir_table,
139 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800140 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700143static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
NeilBrownf91de922005-11-08 21:39:36 -0800145static int start_readonly;
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/*
NeilBrownd7603b72006-01-06 00:20:30 -0800148 * We have a system wide 'event count' that is incremented
149 * on any 'interesting' event, and readers of /proc/mdstat
150 * can use 'poll' or 'select' to find out when the event
151 * count increases.
152 *
153 * Events are:
154 * start array, stop array, error, add device, remove device,
155 * start build, activate spare
156 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800157static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800158static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800159void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800160{
161 atomic_inc(&md_event_count);
162 wake_up(&md_event_waiters);
163}
NeilBrown29269552006-03-27 01:18:10 -0800164EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800165
NeilBrownc331eb02006-05-30 21:27:13 -0700166/* Alternate version that can be called from interrupts
167 * when calling sysfs_notify isn't needed.
168 */
Adrian Bunk05381952006-06-26 00:28:01 -0700169static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700170{
171 atomic_inc(&md_event_count);
172 wake_up(&md_event_waiters);
173}
174
NeilBrownd7603b72006-01-06 00:20:30 -0800175/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 * Enables to iterate over all existing md arrays
177 * all_mddevs_lock protects this list.
178 */
179static LIST_HEAD(all_mddevs);
180static DEFINE_SPINLOCK(all_mddevs_lock);
181
182
183/*
184 * iterates through all used mddevs in the system.
185 * We take care to grab the all_mddevs_lock whenever navigating
186 * the list, and to always hold a refcount when unlocked.
187 * Any code which breaks out of this loop while own
188 * a reference to the current mddev and must mddev_put it.
189 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800190#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 \
192 for (({ spin_lock(&all_mddevs_lock); \
193 tmp = all_mddevs.next; \
194 mddev = NULL;}); \
195 ({ if (tmp != &all_mddevs) \
196 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
197 spin_unlock(&all_mddevs_lock); \
198 if (mddev) mddev_put(mddev); \
199 mddev = list_entry(tmp, mddev_t, all_mddevs); \
200 tmp != &all_mddevs;}); \
201 ({ spin_lock(&all_mddevs_lock); \
202 tmp = tmp->next;}) \
203 )
204
205
NeilBrown409c57f2009-03-31 14:39:39 +1100206/* Rather than calling directly into the personality make_request function,
207 * IO requests come here first so that we can check if the device is
208 * being suspended pending a reconfiguration.
209 * We hold a refcount over the call to ->make_request. By the time that
210 * call has finished, the bio has been linked into some internal structure
211 * and so is visible to ->quiesce(), so we don't need the refcount any more.
212 */
213static int md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
NeilBrown409c57f2009-03-31 14:39:39 +1100215 mddev_t *mddev = q->queuedata;
216 int rv;
217 if (mddev == NULL || mddev->pers == NULL) {
218 bio_io_error(bio);
219 return 0;
220 }
221 rcu_read_lock();
NeilBrowna2826aa2009-12-14 12:49:49 +1100222 if (mddev->suspended || mddev->barrier) {
NeilBrown409c57f2009-03-31 14:39:39 +1100223 DEFINE_WAIT(__wait);
224 for (;;) {
225 prepare_to_wait(&mddev->sb_wait, &__wait,
226 TASK_UNINTERRUPTIBLE);
NeilBrowna2826aa2009-12-14 12:49:49 +1100227 if (!mddev->suspended && !mddev->barrier)
NeilBrown409c57f2009-03-31 14:39:39 +1100228 break;
229 rcu_read_unlock();
230 schedule();
231 rcu_read_lock();
232 }
233 finish_wait(&mddev->sb_wait, &__wait);
234 }
235 atomic_inc(&mddev->active_io);
236 rcu_read_unlock();
237 rv = mddev->pers->make_request(q, bio);
238 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
239 wake_up(&mddev->sb_wait);
240
241 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
NeilBrown409c57f2009-03-31 14:39:39 +1100244static void mddev_suspend(mddev_t *mddev)
245{
246 BUG_ON(mddev->suspended);
247 mddev->suspended = 1;
248 synchronize_rcu();
249 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
250 mddev->pers->quiesce(mddev, 1);
251 md_unregister_thread(mddev->thread);
252 mddev->thread = NULL;
253 /* we now know that no code is executing in the personality module,
254 * except possibly the tail end of a ->bi_end_io function, but that
255 * is certain to complete before the module has a chance to get
256 * unloaded
257 */
258}
259
260static void mddev_resume(mddev_t *mddev)
261{
262 mddev->suspended = 0;
263 wake_up(&mddev->sb_wait);
264 mddev->pers->quiesce(mddev, 0);
265}
266
NeilBrown3fa841d2009-09-23 18:10:29 +1000267int mddev_congested(mddev_t *mddev, int bits)
268{
NeilBrowna2826aa2009-12-14 12:49:49 +1100269 if (mddev->barrier)
270 return 1;
NeilBrown3fa841d2009-09-23 18:10:29 +1000271 return mddev->suspended;
272}
273EXPORT_SYMBOL(mddev_congested);
274
NeilBrowna2826aa2009-12-14 12:49:49 +1100275/*
276 * Generic barrier handling for md
277 */
278
279#define POST_REQUEST_BARRIER ((void*)1)
280
281static void md_end_barrier(struct bio *bio, int err)
282{
283 mdk_rdev_t *rdev = bio->bi_private;
284 mddev_t *mddev = rdev->mddev;
285 if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER)
286 set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags);
287
288 rdev_dec_pending(rdev, mddev);
289
290 if (atomic_dec_and_test(&mddev->flush_pending)) {
291 if (mddev->barrier == POST_REQUEST_BARRIER) {
292 /* This was a post-request barrier */
293 mddev->barrier = NULL;
294 wake_up(&mddev->sb_wait);
295 } else
296 /* The pre-request barrier has finished */
297 schedule_work(&mddev->barrier_work);
298 }
299 bio_put(bio);
300}
301
302static void submit_barriers(mddev_t *mddev)
303{
304 mdk_rdev_t *rdev;
305
306 rcu_read_lock();
307 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
308 if (rdev->raid_disk >= 0 &&
309 !test_bit(Faulty, &rdev->flags)) {
310 /* Take two references, one is dropped
311 * when request finishes, one after
312 * we reclaim rcu_read_lock
313 */
314 struct bio *bi;
315 atomic_inc(&rdev->nr_pending);
316 atomic_inc(&rdev->nr_pending);
317 rcu_read_unlock();
318 bi = bio_alloc(GFP_KERNEL, 0);
319 bi->bi_end_io = md_end_barrier;
320 bi->bi_private = rdev;
321 bi->bi_bdev = rdev->bdev;
322 atomic_inc(&mddev->flush_pending);
323 submit_bio(WRITE_BARRIER, bi);
324 rcu_read_lock();
325 rdev_dec_pending(rdev, mddev);
326 }
327 rcu_read_unlock();
328}
329
330static void md_submit_barrier(struct work_struct *ws)
331{
332 mddev_t *mddev = container_of(ws, mddev_t, barrier_work);
333 struct bio *bio = mddev->barrier;
334
335 atomic_set(&mddev->flush_pending, 1);
336
337 if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags))
338 bio_endio(bio, -EOPNOTSUPP);
339 else if (bio->bi_size == 0)
340 /* an empty barrier - all done */
341 bio_endio(bio, 0);
342 else {
343 bio->bi_rw &= ~(1<<BIO_RW_BARRIER);
344 if (mddev->pers->make_request(mddev->queue, bio))
345 generic_make_request(bio);
346 mddev->barrier = POST_REQUEST_BARRIER;
347 submit_barriers(mddev);
348 }
349 if (atomic_dec_and_test(&mddev->flush_pending)) {
350 mddev->barrier = NULL;
351 wake_up(&mddev->sb_wait);
352 }
353}
354
355void md_barrier_request(mddev_t *mddev, struct bio *bio)
356{
357 spin_lock_irq(&mddev->write_lock);
358 wait_event_lock_irq(mddev->sb_wait,
359 !mddev->barrier,
360 mddev->write_lock, /*nothing*/);
361 mddev->barrier = bio;
362 spin_unlock_irq(&mddev->write_lock);
363
364 atomic_set(&mddev->flush_pending, 1);
365 INIT_WORK(&mddev->barrier_work, md_submit_barrier);
366
367 submit_barriers(mddev);
368
369 if (atomic_dec_and_test(&mddev->flush_pending))
370 schedule_work(&mddev->barrier_work);
371}
372EXPORT_SYMBOL(md_barrier_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374static inline mddev_t *mddev_get(mddev_t *mddev)
375{
376 atomic_inc(&mddev->active);
377 return mddev;
378}
379
Dan Williams5fd3a172009-03-04 00:57:25 -0700380static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382static void mddev_put(mddev_t *mddev)
383{
384 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
385 return;
NeilBrownd3374822009-01-09 08:31:10 +1100386 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
387 !mddev->hold_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 list_del(&mddev->all_mddevs);
NeilBrownd3374822009-01-09 08:31:10 +1100389 if (mddev->gendisk) {
390 /* we did a probe so need to clean up.
391 * Call schedule_work inside the spinlock
392 * so that flush_scheduled_work() after
393 * mddev_find will succeed in waiting for the
394 * work to be done.
395 */
396 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
397 schedule_work(&mddev->del_work);
398 } else
399 kfree(mddev);
400 }
401 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
404static mddev_t * mddev_find(dev_t unit)
405{
406 mddev_t *mddev, *new = NULL;
407
408 retry:
409 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
NeilBrownefeb53c2009-01-09 08:31:10 +1100411 if (unit) {
412 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
413 if (mddev->unit == unit) {
414 mddev_get(mddev);
415 spin_unlock(&all_mddevs_lock);
416 kfree(new);
417 return mddev;
418 }
419
420 if (new) {
421 list_add(&new->all_mddevs, &all_mddevs);
422 spin_unlock(&all_mddevs_lock);
423 new->hold_active = UNTIL_IOCTL;
424 return new;
425 }
426 } else if (new) {
427 /* find an unused unit number */
428 static int next_minor = 512;
429 int start = next_minor;
430 int is_free = 0;
431 int dev = 0;
432 while (!is_free) {
433 dev = MKDEV(MD_MAJOR, next_minor);
434 next_minor++;
435 if (next_minor > MINORMASK)
436 next_minor = 0;
437 if (next_minor == start) {
438 /* Oh dear, all in use. */
439 spin_unlock(&all_mddevs_lock);
440 kfree(new);
441 return NULL;
442 }
443
444 is_free = 1;
445 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
446 if (mddev->unit == dev) {
447 is_free = 0;
448 break;
449 }
450 }
451 new->unit = dev;
452 new->md_minor = MINOR(dev);
453 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 list_add(&new->all_mddevs, &all_mddevs);
455 spin_unlock(&all_mddevs_lock);
456 return new;
457 }
458 spin_unlock(&all_mddevs_lock);
459
NeilBrown9ffae0c2006-01-06 00:20:32 -0800460 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (!new)
462 return NULL;
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 new->unit = unit;
465 if (MAJOR(unit) == MD_MAJOR)
466 new->md_minor = MINOR(unit);
467 else
468 new->md_minor = MINOR(unit) >> MdpMinorShift;
469
NeilBrownc8c00a62009-08-10 12:50:52 +1000470 mutex_init(&new->open_mutex);
NeilBrowndf5b89b2006-03-27 01:18:20 -0800471 mutex_init(&new->reconfig_mutex);
NeilBrownc3d97142009-12-14 12:49:52 +1100472 mutex_init(&new->bitmap_info.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 INIT_LIST_HEAD(&new->disks);
474 INIT_LIST_HEAD(&new->all_mddevs);
475 init_timer(&new->safemode_timer);
476 atomic_set(&new->active, 1);
NeilBrownf2ea68c2008-07-21 17:05:25 +1000477 atomic_set(&new->openers, 0);
NeilBrown409c57f2009-03-31 14:39:39 +1100478 atomic_set(&new->active_io, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700479 spin_lock_init(&new->write_lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100480 atomic_set(&new->flush_pending, 0);
NeilBrown3d310eb2005-06-21 17:17:26 -0700481 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700482 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700483 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000484 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800485 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700486 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 goto retry;
489}
490
491static inline int mddev_lock(mddev_t * mddev)
492{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800493 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Dan Williamsb522adc2009-03-31 15:00:31 +1100496static inline int mddev_is_locked(mddev_t *mddev)
497{
498 return mutex_is_locked(&mddev->reconfig_mutex);
499}
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501static inline int mddev_trylock(mddev_t * mddev)
502{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800503 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
506static inline void mddev_unlock(mddev_t * mddev)
507{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800508 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
NeilBrown005eca52005-08-22 13:11:08 -0700510 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
NeilBrown2989ddb2006-01-06 00:20:43 -0800513static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100515 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100517 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (rdev->desc_nr == nr)
519 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return NULL;
522}
523
524static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
525{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 mdk_rdev_t *rdev;
527
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100528 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (rdev->bdev->bd_dev == dev)
530 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return NULL;
533}
534
NeilBrownd9d166c2006-01-06 00:20:51 -0800535static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800536{
537 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800538 list_for_each_entry(pers, &pers_list, list) {
539 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800540 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800541 if (strcmp(pers->name, clevel)==0)
542 return pers;
543 }
NeilBrown2604b702006-01-06 00:20:36 -0800544 return NULL;
545}
546
Andre Nollb73df2d2008-07-11 22:02:23 +1000547/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700548static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Andre Nollb73df2d2008-07-11 22:02:23 +1000550 sector_t num_sectors = bdev->bd_inode->i_size / 512;
551 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554static int alloc_disk_sb(mdk_rdev_t * rdev)
555{
556 if (rdev->sb_page)
557 MD_BUG();
558
559 rdev->sb_page = alloc_page(GFP_KERNEL);
560 if (!rdev->sb_page) {
561 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000562 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564
565 return 0;
566}
567
568static void free_disk_sb(mdk_rdev_t * rdev)
569{
570 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800571 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 rdev->sb_loaded = 0;
573 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000574 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100575 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577}
578
579
NeilBrown6712ecf2007-09-27 12:47:43 +0200580static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700581{
582 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800583 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700584
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700585 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
586 printk("md: super_written gets error=%d, uptodate=%d\n",
587 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
588 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800589 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700590 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700591
NeilBrowna9701a32005-11-08 21:39:34 -0800592 if (atomic_dec_and_test(&mddev->pending_writes))
593 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700594 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700595}
596
NeilBrown6712ecf2007-09-27 12:47:43 +0200597static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800598{
599 struct bio *bio2 = bio->bi_private;
600 mdk_rdev_t *rdev = bio2->bi_private;
601 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800602
603 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
604 error == -EOPNOTSUPP) {
605 unsigned long flags;
606 /* barriers don't appear to be supported :-( */
607 set_bit(BarriersNotsupp, &rdev->flags);
608 mddev->barriers_work = 0;
609 spin_lock_irqsave(&mddev->write_lock, flags);
610 bio2->bi_next = mddev->biolist;
611 mddev->biolist = bio2;
612 spin_unlock_irqrestore(&mddev->write_lock, flags);
613 wake_up(&mddev->sb_wait);
614 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200615 } else {
616 bio_put(bio2);
617 bio->bi_private = rdev;
618 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800619 }
NeilBrowna9701a32005-11-08 21:39:34 -0800620}
621
NeilBrown7bfa19f2005-06-21 17:17:28 -0700622void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
623 sector_t sector, int size, struct page *page)
624{
625 /* write first size bytes of page to sector of rdev
626 * Increment mddev->pending_writes before returning
627 * and decrement it on completion, waking up sb_wait
628 * if zero is reached.
629 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800630 *
631 * As we might need to resubmit the request if BIO_RW_BARRIER
632 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700633 */
634 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Jens Axboe93dbb392009-02-16 10:25:40 +0100635 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700636
637 bio->bi_bdev = rdev->bdev;
638 bio->bi_sector = sector;
639 bio_add_page(bio, page, size, 0);
640 bio->bi_private = rdev;
641 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800642 bio->bi_rw = rw;
643
NeilBrown7bfa19f2005-06-21 17:17:28 -0700644 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800645 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
646 struct bio *rbio;
647 rw |= (1<<BIO_RW_BARRIER);
648 rbio = bio_clone(bio, GFP_NOIO);
649 rbio->bi_private = bio;
650 rbio->bi_end_io = super_written_barrier;
651 submit_bio(rw, rbio);
652 } else
653 submit_bio(rw, bio);
654}
655
656void md_super_wait(mddev_t *mddev)
657{
658 /* wait for all superblock writes that were scheduled to complete.
659 * if any had to be retried (due to BARRIER problems), retry them
660 */
661 DEFINE_WAIT(wq);
662 for(;;) {
663 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
664 if (atomic_read(&mddev->pending_writes)==0)
665 break;
666 while (mddev->biolist) {
667 struct bio *bio;
668 spin_lock_irq(&mddev->write_lock);
669 bio = mddev->biolist;
670 mddev->biolist = bio->bi_next ;
671 bio->bi_next = NULL;
672 spin_unlock_irq(&mddev->write_lock);
673 submit_bio(bio->bi_rw, bio);
674 }
675 schedule();
676 }
677 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700678}
679
NeilBrown6712ecf2007-09-27 12:47:43 +0200680static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
NeilBrowna654b9d82005-06-21 17:17:27 -0700685int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 struct page *page, int rw)
687{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700688 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 struct completion event;
690 int ret;
691
Jens Axboe93dbb392009-02-16 10:25:40 +0100692 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 bio->bi_bdev = bdev;
695 bio->bi_sector = sector;
696 bio_add_page(bio, page, size, 0);
697 init_completion(&event);
698 bio->bi_private = &event;
699 bio->bi_end_io = bi_complete;
700 submit_bio(rw, bio);
701 wait_for_completion(&event);
702
703 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
704 bio_put(bio);
705 return ret;
706}
NeilBrowna8745db2006-01-06 00:20:34 -0800707EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
NeilBrown0002b272005-09-09 16:23:53 -0700709static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 char b[BDEVNAME_SIZE];
712 if (!rdev->sb_page) {
713 MD_BUG();
714 return -EINVAL;
715 }
716 if (rdev->sb_loaded)
717 return 0;
718
719
Andre Noll0f420352008-07-11 22:02:23 +1000720 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 goto fail;
722 rdev->sb_loaded = 1;
723 return 0;
724
725fail:
726 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
727 bdevname(rdev->bdev,b));
728 return -EINVAL;
729}
730
731static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
732{
Andre Noll05710462008-07-11 22:02:20 +1000733 return sb1->set_uuid0 == sb2->set_uuid0 &&
734 sb1->set_uuid1 == sb2->set_uuid1 &&
735 sb1->set_uuid2 == sb2->set_uuid2 &&
736 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
740{
741 int ret;
742 mdp_super_t *tmp1, *tmp2;
743
744 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
745 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
746
747 if (!tmp1 || !tmp2) {
748 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100749 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 goto abort;
751 }
752
753 *tmp1 = *sb1;
754 *tmp2 = *sb2;
755
756 /*
757 * nr_disks is not constant
758 */
759 tmp1->nr_disks = 0;
760 tmp2->nr_disks = 0;
761
Andre Nollce0c8e02008-07-11 22:02:20 +1000762 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700764 kfree(tmp1);
765 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return ret;
767}
768
NeilBrown4d167f02007-05-09 02:35:37 -0700769
770static u32 md_csum_fold(u32 csum)
771{
772 csum = (csum & 0xffff) + (csum >> 16);
773 return (csum & 0xffff) + (csum >> 16);
774}
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776static unsigned int calc_sb_csum(mdp_super_t * sb)
777{
NeilBrown4d167f02007-05-09 02:35:37 -0700778 u64 newcsum = 0;
779 u32 *sb32 = (u32*)sb;
780 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 unsigned int disk_csum, csum;
782
783 disk_csum = sb->sb_csum;
784 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700785
786 for (i = 0; i < MD_SB_BYTES/4 ; i++)
787 newcsum += sb32[i];
788 csum = (newcsum & 0xffffffff) + (newcsum>>32);
789
790
791#ifdef CONFIG_ALPHA
792 /* This used to use csum_partial, which was wrong for several
793 * reasons including that different results are returned on
794 * different architectures. It isn't critical that we get exactly
795 * the same return value as before (we always csum_fold before
796 * testing, and that removes any differences). However as we
797 * know that csum_partial always returned a 16bit value on
798 * alphas, do a fold to maximise conformity to previous behaviour.
799 */
800 sb->sb_csum = md_csum_fold(disk_csum);
801#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700803#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return csum;
805}
806
807
808/*
809 * Handle superblock details.
810 * We want to be able to handle multiple superblock formats
811 * so we have a common interface to them all, and an array of
812 * different handlers.
813 * We rely on user-space to write the initial superblock, and support
814 * reading and updating of superblocks.
815 * Interface methods are:
816 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
817 * loads and validates a superblock on dev.
818 * if refdev != NULL, compare superblocks on both devices
819 * Return:
820 * 0 - dev has a superblock that is compatible with refdev
821 * 1 - dev has a superblock that is compatible and newer than refdev
822 * so dev should be used as the refdev in future
823 * -EINVAL superblock incompatible or invalid
824 * -othererror e.g. -EIO
825 *
826 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
827 * Verify that dev is acceptable into mddev.
828 * The first time, mddev->raid_disks will be 0, and data from
829 * dev should be merged in. Subsequent calls check that dev
830 * is new enough. Return 0 or -EINVAL
831 *
832 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
833 * Update the superblock for rdev with data in mddev
834 * This does not write to disc.
835 *
836 */
837
838struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000839 char *name;
840 struct module *owner;
841 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
842 int minor_version);
843 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
844 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
845 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000846 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847};
848
849/*
Andre Noll0894cc32009-06-18 08:49:23 +1000850 * Check that the given mddev has no bitmap.
851 *
852 * This function is called from the run method of all personalities that do not
853 * support bitmaps. It prints an error message and returns non-zero if mddev
854 * has a bitmap. Otherwise, it returns 0.
855 *
856 */
857int md_check_no_bitmap(mddev_t *mddev)
858{
NeilBrownc3d97142009-12-14 12:49:52 +1100859 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000860 return 0;
861 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
862 mdname(mddev), mddev->pers->name);
863 return 1;
864}
865EXPORT_SYMBOL(md_check_no_bitmap);
866
867/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 * load_super for 0.90.0
869 */
870static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
871{
872 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
873 mdp_super_t *sb;
874 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 /*
Andre Noll0f420352008-07-11 22:02:23 +1000877 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 * it's at the end of the disk.
879 *
880 * It also happens to be a multiple of 4Kb.
881 */
Andre Noll0f420352008-07-11 22:02:23 +1000882 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
NeilBrown0002b272005-09-09 16:23:53 -0700884 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (ret) return ret;
886
887 ret = -EINVAL;
888
889 bdevname(rdev->bdev, b);
890 sb = (mdp_super_t*)page_address(rdev->sb_page);
891
892 if (sb->md_magic != MD_SB_MAGIC) {
893 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
894 b);
895 goto abort;
896 }
897
898 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800899 sb->minor_version < 90 ||
900 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
902 sb->major_version, sb->minor_version,
903 b);
904 goto abort;
905 }
906
907 if (sb->raid_disks <= 0)
908 goto abort;
909
NeilBrown4d167f02007-05-09 02:35:37 -0700910 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
912 b);
913 goto abort;
914 }
915
916 rdev->preferred_minor = sb->md_minor;
917 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700918 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 if (sb->level == LEVEL_MULTIPATH)
921 rdev->desc_nr = -1;
922 else
923 rdev->desc_nr = sb->this_disk.number;
924
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700925 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700927 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 __u64 ev1, ev2;
929 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
930 if (!uuid_equal(refsb, sb)) {
931 printk(KERN_WARNING "md: %s has different UUID to %s\n",
932 b, bdevname(refdev->bdev,b2));
933 goto abort;
934 }
935 if (!sb_equal(refsb, sb)) {
936 printk(KERN_WARNING "md: %s has same UUID"
937 " but different superblock to %s\n",
938 b, bdevname(refdev->bdev, b2));
939 goto abort;
940 }
941 ev1 = md_event(sb);
942 ev2 = md_event(refsb);
943 if (ev1 > ev2)
944 ret = 1;
945 else
946 ret = 0;
947 }
NeilBrown8190e752009-06-18 08:48:58 +1000948 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Andre Nolldd8ac332009-03-31 14:33:13 +1100950 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -0800951 /* "this cannot possibly happen" ... */
952 ret = -EINVAL;
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 abort:
955 return ret;
956}
957
958/*
959 * validate_super for 0.90.0
960 */
961static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
962{
963 mdp_disk_t *desc;
964 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700965 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
NeilBrown41158c72005-06-21 17:17:25 -0700967 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800968 clear_bit(Faulty, &rdev->flags);
969 clear_bit(In_sync, &rdev->flags);
970 clear_bit(WriteMostly, &rdev->flags);
971 clear_bit(BarriersNotsupp, &rdev->flags);
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (mddev->raid_disks == 0) {
974 mddev->major_version = 0;
975 mddev->minor_version = sb->minor_version;
976 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800977 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +1000978 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 mddev->ctime = sb->ctime;
980 mddev->utime = sb->utime;
981 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800982 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 mddev->layout = sb->layout;
984 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +1100985 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -0700986 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +1100987 mddev->bitmap_info.offset = 0;
988 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
NeilBrownf6705572006-03-27 01:18:11 -0800990 if (mddev->minor_version >= 91) {
991 mddev->reshape_position = sb->reshape_position;
992 mddev->delta_disks = sb->delta_disks;
993 mddev->new_level = sb->new_level;
994 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +1000995 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -0800996 } else {
997 mddev->reshape_position = MaxSector;
998 mddev->delta_disks = 0;
999 mddev->new_level = mddev->level;
1000 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001001 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001002 }
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (sb->state & (1<<MD_SB_CLEAN))
1005 mddev->recovery_cp = MaxSector;
1006 else {
1007 if (sb->events_hi == sb->cp_events_hi &&
1008 sb->events_lo == sb->cp_events_lo) {
1009 mddev->recovery_cp = sb->recovery_cp;
1010 } else
1011 mddev->recovery_cp = 0;
1012 }
1013
1014 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1015 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1016 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1017 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1018
1019 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001020
1021 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001022 mddev->bitmap_info.file == NULL)
1023 mddev->bitmap_info.offset =
1024 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001025
NeilBrown41158c72005-06-21 17:17:25 -07001026 } else if (mddev->pers == NULL) {
1027 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 ++ev1;
1029 if (ev1 < mddev->events)
1030 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001031 } else if (mddev->bitmap) {
1032 /* if adding to array with a bitmap, then we can accept an
1033 * older device ... but not too old.
1034 */
NeilBrown41158c72005-06-21 17:17:25 -07001035 if (ev1 < mddev->bitmap->events_cleared)
1036 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001037 } else {
1038 if (ev1 < mddev->events)
1039 /* just a hot-add of a new device, leave raid_disk at -1 */
1040 return 0;
1041 }
NeilBrown41158c72005-06-21 17:17:25 -07001042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 desc = sb->disks + rdev->desc_nr;
1045
1046 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001047 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001048 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1049 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001050 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001052 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1053 /* active but not in sync implies recovery up to
1054 * reshape position. We don't know exactly where
1055 * that is, so set to zero for now */
1056 if (mddev->minor_version >= 91) {
1057 rdev->recovery_offset = 0;
1058 rdev->raid_disk = desc->raid_disk;
1059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001061 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1062 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001063 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001064 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 return 0;
1066}
1067
1068/*
1069 * sync_super for 0.90.0
1070 */
1071static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1072{
1073 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 mdk_rdev_t *rdev2;
1075 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078 /* make rdev->sb match mddev data..
1079 *
1080 * 1/ zero out disks
1081 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1082 * 3/ any empty disks < next_spare become removed
1083 *
1084 * disks[0] gets initialised to REMOVED because
1085 * we cannot be sure from other fields if it has
1086 * been initialised or not.
1087 */
1088 int i;
1089 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1090
NeilBrown61181562005-09-09 16:24:02 -07001091 rdev->sb_size = MD_SB_BYTES;
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 sb = (mdp_super_t*)page_address(rdev->sb_page);
1094
1095 memset(sb, 0, sizeof(*sb));
1096
1097 sb->md_magic = MD_SB_MAGIC;
1098 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 sb->patch_version = mddev->patch_version;
1100 sb->gvalid_words = 0; /* ignored */
1101 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1102 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1103 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1104 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1105
1106 sb->ctime = mddev->ctime;
1107 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001108 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 sb->raid_disks = mddev->raid_disks;
1110 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001111 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 sb->utime = mddev->utime;
1113 sb->state = 0;
1114 sb->events_hi = (mddev->events>>32);
1115 sb->events_lo = (u32)mddev->events;
1116
NeilBrownf6705572006-03-27 01:18:11 -08001117 if (mddev->reshape_position == MaxSector)
1118 sb->minor_version = 90;
1119 else {
1120 sb->minor_version = 91;
1121 sb->reshape_position = mddev->reshape_position;
1122 sb->new_level = mddev->new_level;
1123 sb->delta_disks = mddev->delta_disks;
1124 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001125 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001126 }
1127 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 if (mddev->in_sync)
1129 {
1130 sb->recovery_cp = mddev->recovery_cp;
1131 sb->cp_events_hi = (mddev->events>>32);
1132 sb->cp_events_lo = (u32)mddev->events;
1133 if (mddev->recovery_cp == MaxSector)
1134 sb->state = (1<< MD_SB_CLEAN);
1135 } else
1136 sb->recovery_cp = 0;
1137
1138 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001139 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
NeilBrownc3d97142009-12-14 12:49:52 +11001141 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001142 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001145 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001147 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001148 int is_active = test_bit(In_sync, &rdev2->flags);
1149
1150 if (rdev2->raid_disk >= 0 &&
1151 sb->minor_version >= 91)
1152 /* we have nowhere to store the recovery_offset,
1153 * but if it is not below the reshape_position,
1154 * we can piggy-back on that.
1155 */
1156 is_active = 1;
1157 if (rdev2->raid_disk < 0 ||
1158 test_bit(Faulty, &rdev2->flags))
1159 is_active = 0;
1160 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001161 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001163 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001164 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 d = &sb->disks[rdev2->desc_nr];
1166 nr_disks++;
1167 d->number = rdev2->desc_nr;
1168 d->major = MAJOR(rdev2->bdev->bd_dev);
1169 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001170 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 d->raid_disk = rdev2->raid_disk;
1172 else
1173 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001174 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001176 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001178 if (test_bit(In_sync, &rdev2->flags))
1179 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 active++;
1181 working++;
1182 } else {
1183 d->state = 0;
1184 spare++;
1185 working++;
1186 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001187 if (test_bit(WriteMostly, &rdev2->flags))
1188 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 /* now set the "removed" and "faulty" bits on any missing devices */
1191 for (i=0 ; i < mddev->raid_disks ; i++) {
1192 mdp_disk_t *d = &sb->disks[i];
1193 if (d->state == 0 && d->number == 0) {
1194 d->number = i;
1195 d->raid_disk = i;
1196 d->state = (1<<MD_DISK_REMOVED);
1197 d->state |= (1<<MD_DISK_FAULTY);
1198 failed++;
1199 }
1200 }
1201 sb->nr_disks = nr_disks;
1202 sb->active_disks = active;
1203 sb->working_disks = working;
1204 sb->failed_disks = failed;
1205 sb->spare_disks = spare;
1206
1207 sb->this_disk = sb->disks[rdev->desc_nr];
1208 sb->sb_csum = calc_sb_csum(sb);
1209}
1210
1211/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001212 * rdev_size_change for 0.90.0
1213 */
1214static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001215super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001216{
Andre Noll58c0fed2009-03-31 14:33:13 +11001217 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001218 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001219 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001220 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001221 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001222 if (!num_sectors || num_sectors > rdev->sb_start)
1223 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001224 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001225 rdev->sb_page);
1226 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001227 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001228}
1229
1230
1231/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 * version 1 superblock
1233 */
1234
NeilBrown1c05b4b2006-10-21 10:24:08 -07001235static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001237 __le32 disk_csum;
1238 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 unsigned long long newcsum;
1240 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001241 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 int i;
1243
1244 disk_csum = sb->sb_csum;
1245 sb->sb_csum = 0;
1246 newcsum = 0;
1247 for (i=0; size>=4; size -= 4 )
1248 newcsum += le32_to_cpu(*isuper++);
1249
1250 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001251 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1254 sb->sb_csum = disk_csum;
1255 return cpu_to_le32(csum);
1256}
1257
1258static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1259{
1260 struct mdp_superblock_1 *sb;
1261 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001262 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001264 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 /*
Andre Noll0f420352008-07-11 22:02:23 +10001267 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 * It is always aligned to a 4K boundary and
1269 * depeding on minor_version, it can be:
1270 * 0: At least 8K, but less than 12K, from end of device
1271 * 1: At start of device
1272 * 2: 4K from start of device.
1273 */
1274 switch(minor_version) {
1275 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001276 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1277 sb_start -= 8*2;
1278 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 break;
1280 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001281 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 break;
1283 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001284 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 break;
1286 default:
1287 return -EINVAL;
1288 }
Andre Noll0f420352008-07-11 22:02:23 +10001289 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
NeilBrown0002b272005-09-09 16:23:53 -07001291 /* superblock is rarely larger than 1K, but it can be larger,
1292 * and it is safe to read 4k, so we do that
1293 */
1294 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (ret) return ret;
1296
1297
1298 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1299
1300 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1301 sb->major_version != cpu_to_le32(1) ||
1302 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001303 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001304 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return -EINVAL;
1306
1307 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1308 printk("md: invalid superblock checksum on %s\n",
1309 bdevname(rdev->bdev,b));
1310 return -EINVAL;
1311 }
1312 if (le64_to_cpu(sb->data_size) < 10) {
1313 printk("md: data_size too small on %s\n",
1314 bdevname(rdev->bdev,b));
1315 return -EINVAL;
1316 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 rdev->preferred_minor = 0xffff;
1319 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001320 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
NeilBrown0002b272005-09-09 16:23:53 -07001322 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001323 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001324 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001325 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1326
1327 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001328 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001329 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001330
NeilBrown31b65a02006-07-10 04:44:14 -07001331 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1332 rdev->desc_nr = -1;
1333 else
1334 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1335
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001336 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001337 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001338 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 __u64 ev1, ev2;
1340 struct mdp_superblock_1 *refsb =
1341 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1342
1343 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1344 sb->level != refsb->level ||
1345 sb->layout != refsb->layout ||
1346 sb->chunksize != refsb->chunksize) {
1347 printk(KERN_WARNING "md: %s has strangely different"
1348 " superblock to %s\n",
1349 bdevname(rdev->bdev,b),
1350 bdevname(refdev->bdev,b2));
1351 return -EINVAL;
1352 }
1353 ev1 = le64_to_cpu(sb->events);
1354 ev2 = le64_to_cpu(refsb->events);
1355
1356 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001357 ret = 1;
1358 else
1359 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
NeilBrowna1801f82008-03-04 14:29:31 -08001361 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001362 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1363 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001365 rdev->sectors = rdev->sb_start;
1366 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001368 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001369 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001370 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001371 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372}
1373
1374static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1375{
1376 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001377 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
NeilBrown41158c72005-06-21 17:17:25 -07001379 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001380 clear_bit(Faulty, &rdev->flags);
1381 clear_bit(In_sync, &rdev->flags);
1382 clear_bit(WriteMostly, &rdev->flags);
1383 clear_bit(BarriersNotsupp, &rdev->flags);
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 if (mddev->raid_disks == 0) {
1386 mddev->major_version = 1;
1387 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001388 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001389 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1391 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1392 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001393 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 mddev->layout = le32_to_cpu(sb->layout);
1395 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001396 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001397 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001398 mddev->bitmap_info.offset = 0;
1399 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1402 memcpy(mddev->uuid, sb->set_uuid, 16);
1403
1404 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001405
NeilBrown71c08052005-09-09 16:23:51 -07001406 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001407 mddev->bitmap_info.file == NULL )
1408 mddev->bitmap_info.offset =
1409 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001410
NeilBrownf6705572006-03-27 01:18:11 -08001411 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1412 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1413 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1414 mddev->new_level = le32_to_cpu(sb->new_level);
1415 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001416 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001417 } else {
1418 mddev->reshape_position = MaxSector;
1419 mddev->delta_disks = 0;
1420 mddev->new_level = mddev->level;
1421 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001422 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001423 }
1424
NeilBrown41158c72005-06-21 17:17:25 -07001425 } else if (mddev->pers == NULL) {
1426 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 ++ev1;
1428 if (ev1 < mddev->events)
1429 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001430 } else if (mddev->bitmap) {
1431 /* If adding to array with a bitmap, then we can accept an
1432 * older device, but not too old.
1433 */
NeilBrown41158c72005-06-21 17:17:25 -07001434 if (ev1 < mddev->bitmap->events_cleared)
1435 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001436 } else {
1437 if (ev1 < mddev->events)
1438 /* just a hot-add of a new device, leave raid_disk at -1 */
1439 return 0;
1440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (mddev->level != LEVEL_MULTIPATH) {
1442 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001443 if (rdev->desc_nr < 0 ||
1444 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1445 role = 0xffff;
1446 rdev->desc_nr = -1;
1447 } else
1448 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 switch(role) {
1450 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 break;
1452 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001453 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 break;
1455 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001456 if ((le32_to_cpu(sb->feature_map) &
1457 MD_FEATURE_RECOVERY_OFFSET))
1458 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1459 else
1460 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 rdev->raid_disk = role;
1462 break;
1463 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001464 if (sb->devflags & WriteMostly1)
1465 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001466 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001467 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return 0;
1470}
1471
1472static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1473{
1474 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 mdk_rdev_t *rdev2;
1476 int max_dev, i;
1477 /* make rdev->sb match mddev and rdev data. */
1478
1479 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1480
1481 sb->feature_map = 0;
1482 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001483 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 memset(sb->pad1, 0, sizeof(sb->pad1));
1485 memset(sb->pad2, 0, sizeof(sb->pad2));
1486 memset(sb->pad3, 0, sizeof(sb->pad3));
1487
1488 sb->utime = cpu_to_le64((__u64)mddev->utime);
1489 sb->events = cpu_to_le64(mddev->events);
1490 if (mddev->in_sync)
1491 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1492 else
1493 sb->resync_offset = cpu_to_le64(0);
1494
NeilBrown1c05b4b2006-10-21 10:24:08 -07001495 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001496
NeilBrownf0ca3402006-02-02 14:28:04 -08001497 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001498 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001499 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001500 sb->level = cpu_to_le32(mddev->level);
1501 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001502
NeilBrownc3d97142009-12-14 12:49:52 +11001503 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1504 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001505 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001506 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001507
1508 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001509 !test_bit(In_sync, &rdev->flags)) {
NeilBrown97e4f422009-03-31 14:33:13 +11001510 if (rdev->recovery_offset > 0) {
1511 sb->feature_map |=
1512 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1513 sb->recovery_offset =
1514 cpu_to_le64(rdev->recovery_offset);
1515 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001516 }
1517
NeilBrownf6705572006-03-27 01:18:11 -08001518 if (mddev->reshape_position != MaxSector) {
1519 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1520 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1521 sb->new_layout = cpu_to_le32(mddev->new_layout);
1522 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1523 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001524 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001525 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001528 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if (rdev2->desc_nr+1 > max_dev)
1530 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001531
NeilBrown70471da2009-08-03 10:59:57 +10001532 if (max_dev > le32_to_cpu(sb->max_dev)) {
1533 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001534 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001535 rdev->sb_size = max_dev * 2 + 256;
1536 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1537 if (rdev->sb_size & bmask)
1538 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 for (i=0; i<max_dev;i++)
1541 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1542
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001543 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001545 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001547 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001549 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1550 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 else
1552 sb->dev_roles[i] = cpu_to_le16(0xffff);
1553 }
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 sb->sb_csum = calc_sb_1_csum(sb);
1556}
1557
Chris Webb0cd17fe2008-06-28 08:31:46 +10001558static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001559super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001560{
1561 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001562 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001563 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001564 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001565 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001566 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001567 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1568 max_sectors -= rdev->data_offset;
1569 if (!num_sectors || num_sectors > max_sectors)
1570 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001571 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001572 /* minor version 0 with bitmap we can't move */
1573 return 0;
1574 } else {
1575 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001576 sector_t sb_start;
1577 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1578 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001579 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001580 if (!num_sectors || num_sectors > max_sectors)
1581 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001582 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001583 }
1584 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001585 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001586 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001587 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001588 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001589 rdev->sb_page);
1590 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001591 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001592}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Adrian Bunk75c96f82005-05-05 16:16:09 -07001594static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 [0] = {
1596 .name = "0.90.0",
1597 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001598 .load_super = super_90_load,
1599 .validate_super = super_90_validate,
1600 .sync_super = super_90_sync,
1601 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 },
1603 [1] = {
1604 .name = "md-1",
1605 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001606 .load_super = super_1_load,
1607 .validate_super = super_1_validate,
1608 .sync_super = super_1_sync,
1609 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 },
1611};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1614{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001615 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
NeilBrown4b809912008-07-21 17:05:25 +10001617 rcu_read_lock();
1618 rdev_for_each_rcu(rdev, mddev1)
1619 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001620 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001621 rdev2->bdev->bd_contains) {
1622 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001623 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001624 }
1625 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return 0;
1627}
1628
1629static LIST_HEAD(pending_raid_disks);
1630
Andre Nollac5e7112009-08-03 10:59:47 +10001631/*
1632 * Try to register data integrity profile for an mddev
1633 *
1634 * This is called when an array is started and after a disk has been kicked
1635 * from the array. It only succeeds if all working and active component devices
1636 * are integrity capable with matching profiles.
1637 */
1638int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001639{
Andre Nollac5e7112009-08-03 10:59:47 +10001640 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001641
Andre Nollac5e7112009-08-03 10:59:47 +10001642 if (list_empty(&mddev->disks))
1643 return 0; /* nothing to do */
1644 if (blk_get_integrity(mddev->gendisk))
1645 return 0; /* already registered */
1646 list_for_each_entry(rdev, &mddev->disks, same_set) {
1647 /* skip spares and non-functional disks */
1648 if (test_bit(Faulty, &rdev->flags))
1649 continue;
1650 if (rdev->raid_disk < 0)
1651 continue;
1652 /*
1653 * If at least one rdev is not integrity capable, we can not
1654 * enable data integrity for the md device.
1655 */
1656 if (!bdev_get_integrity(rdev->bdev))
1657 return -EINVAL;
1658 if (!reference) {
1659 /* Use the first rdev as the reference */
1660 reference = rdev;
1661 continue;
1662 }
1663 /* does this rdev's profile match the reference profile? */
1664 if (blk_integrity_compare(reference->bdev->bd_disk,
1665 rdev->bdev->bd_disk) < 0)
1666 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001667 }
Andre Nollac5e7112009-08-03 10:59:47 +10001668 /*
1669 * All component devices are integrity capable and have matching
1670 * profiles, register the common profile for the md device.
1671 */
1672 if (blk_integrity_register(mddev->gendisk,
1673 bdev_get_integrity(reference->bdev)) != 0) {
1674 printk(KERN_ERR "md: failed to register integrity for %s\n",
1675 mdname(mddev));
1676 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001677 }
Andre Nollac5e7112009-08-03 10:59:47 +10001678 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1679 mdname(mddev));
1680 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001681}
Andre Nollac5e7112009-08-03 10:59:47 +10001682EXPORT_SYMBOL(md_integrity_register);
1683
1684/* Disable data integrity if non-capable/non-matching disk is being added */
1685void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1686{
1687 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1688 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1689
1690 if (!bi_mddev) /* nothing to do */
1691 return;
1692 if (rdev->raid_disk < 0) /* skip spares */
1693 return;
1694 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1695 rdev->bdev->bd_disk) >= 0)
1696 return;
1697 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1698 blk_integrity_unregister(mddev->gendisk);
1699}
1700EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1703{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001704 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001705 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001706 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001707 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709 if (rdev->mddev) {
1710 MD_BUG();
1711 return -EINVAL;
1712 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001713
1714 /* prevent duplicates */
1715 if (find_rdev(mddev, rdev->bdev->bd_dev))
1716 return -EEXIST;
1717
Andre Nolldd8ac332009-03-31 14:33:13 +11001718 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1719 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1720 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001721 if (mddev->pers) {
1722 /* Cannot change size, so fail
1723 * If mddev->level <= 0, then we don't care
1724 * about aligning sizes (e.g. linear)
1725 */
1726 if (mddev->level > 0)
1727 return -ENOSPC;
1728 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001729 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 /* Verify rdev->desc_nr is unique.
1733 * If it is -1, assign a free number, else
1734 * check number is not in use
1735 */
1736 if (rdev->desc_nr < 0) {
1737 int choice = 0;
1738 if (mddev->pers) choice = mddev->raid_disks;
1739 while (find_rdev_nr(mddev, choice))
1740 choice++;
1741 rdev->desc_nr = choice;
1742 } else {
1743 if (find_rdev_nr(mddev, rdev->desc_nr))
1744 return -EBUSY;
1745 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001746 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1747 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1748 mdname(mddev), mddev->max_disks);
1749 return -EBUSY;
1750 }
NeilBrown19133a42005-11-08 21:39:35 -08001751 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001752 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001753 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001756 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001757
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001758 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001759 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001760
Tejun Heo0762b8b2008-08-25 19:56:12 +09001761 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001762 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1763 kobject_del(&rdev->kobj);
1764 goto fail;
1765 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001766 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1767
NeilBrown4b809912008-07-21 17:05:25 +10001768 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001769 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001770
1771 /* May as well allow recovery to be retried once */
1772 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001775
1776 fail:
1777 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1778 b, mdname(mddev));
1779 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
NeilBrown177a99b2008-02-06 01:39:56 -08001782static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001783{
1784 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1785 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001786 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001787}
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1790{
1791 char b[BDEVNAME_SIZE];
1792 if (!rdev->mddev) {
1793 MD_BUG();
1794 return;
1795 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001796 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001797 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1799 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001800 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001801 sysfs_put(rdev->sysfs_state);
1802 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001803 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001804 * writing to 'remove' to "dev/state". We also need
1805 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001806 */
NeilBrown4b809912008-07-21 17:05:25 +10001807 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001808 INIT_WORK(&rdev->del_work, md_delayed_delete);
1809 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001810 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
1813/*
1814 * prevent the device from being mounted, repartitioned or
1815 * otherwise reused by a RAID array (or any other kernel
1816 * subsystem), by bd_claiming the device.
1817 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001818static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
1820 int err = 0;
1821 struct block_device *bdev;
1822 char b[BDEVNAME_SIZE];
1823
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001824 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (IS_ERR(bdev)) {
1826 printk(KERN_ERR "md: could not open %s.\n",
1827 __bdevname(dev, b));
1828 return PTR_ERR(bdev);
1829 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001830 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (err) {
1832 printk(KERN_ERR "md: could not bd_claim %s.\n",
1833 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001834 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return err;
1836 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001837 if (!shared)
1838 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 rdev->bdev = bdev;
1840 return err;
1841}
1842
1843static void unlock_rdev(mdk_rdev_t *rdev)
1844{
1845 struct block_device *bdev = rdev->bdev;
1846 rdev->bdev = NULL;
1847 if (!bdev)
1848 MD_BUG();
1849 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001850 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
1853void md_autodetect_dev(dev_t dev);
1854
1855static void export_rdev(mdk_rdev_t * rdev)
1856{
1857 char b[BDEVNAME_SIZE];
1858 printk(KERN_INFO "md: export_rdev(%s)\n",
1859 bdevname(rdev->bdev,b));
1860 if (rdev->mddev)
1861 MD_BUG();
1862 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001864 if (test_bit(AutoDetected, &rdev->flags))
1865 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866#endif
1867 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001868 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869}
1870
1871static void kick_rdev_from_array(mdk_rdev_t * rdev)
1872{
1873 unbind_rdev_from_array(rdev);
1874 export_rdev(rdev);
1875}
1876
1877static void export_array(mddev_t *mddev)
1878{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001879 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
NeilBrownd089c6a2008-02-06 01:39:59 -08001881 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (!rdev->mddev) {
1883 MD_BUG();
1884 continue;
1885 }
1886 kick_rdev_from_array(rdev);
1887 }
1888 if (!list_empty(&mddev->disks))
1889 MD_BUG();
1890 mddev->raid_disks = 0;
1891 mddev->major_version = 0;
1892}
1893
1894static void print_desc(mdp_disk_t *desc)
1895{
1896 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1897 desc->major,desc->minor,desc->raid_disk,desc->state);
1898}
1899
Cheng Renquancd2ac932009-01-09 08:31:08 +11001900static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
1902 int i;
1903
1904 printk(KERN_INFO
1905 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1906 sb->major_version, sb->minor_version, sb->patch_version,
1907 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1908 sb->ctime);
1909 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1910 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1911 sb->md_minor, sb->layout, sb->chunk_size);
1912 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1913 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1914 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1915 sb->failed_disks, sb->spare_disks,
1916 sb->sb_csum, (unsigned long)sb->events_lo);
1917
1918 printk(KERN_INFO);
1919 for (i = 0; i < MD_SB_DISKS; i++) {
1920 mdp_disk_t *desc;
1921
1922 desc = sb->disks + i;
1923 if (desc->number || desc->major || desc->minor ||
1924 desc->raid_disk || (desc->state && (desc->state != 4))) {
1925 printk(" D %2d: ", i);
1926 print_desc(desc);
1927 }
1928 }
1929 printk(KERN_INFO "md: THIS: ");
1930 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
Cheng Renquancd2ac932009-01-09 08:31:08 +11001933static void print_sb_1(struct mdp_superblock_1 *sb)
1934{
1935 __u8 *uuid;
1936
1937 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001938 printk(KERN_INFO
1939 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1940 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1941 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001942 le32_to_cpu(sb->major_version),
1943 le32_to_cpu(sb->feature_map),
1944 uuid[0], uuid[1], uuid[2], uuid[3],
1945 uuid[4], uuid[5], uuid[6], uuid[7],
1946 uuid[8], uuid[9], uuid[10], uuid[11],
1947 uuid[12], uuid[13], uuid[14], uuid[15],
1948 sb->set_name,
1949 (unsigned long long)le64_to_cpu(sb->ctime)
1950 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1951
1952 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001953 printk(KERN_INFO
1954 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11001955 " RO:%llu\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001956 "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1957 ":%02x%02x%02x%02x%02x%02x\n"
1958 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1959 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001960 le32_to_cpu(sb->level),
1961 (unsigned long long)le64_to_cpu(sb->size),
1962 le32_to_cpu(sb->raid_disks),
1963 le32_to_cpu(sb->layout),
1964 le32_to_cpu(sb->chunksize),
1965 (unsigned long long)le64_to_cpu(sb->data_offset),
1966 (unsigned long long)le64_to_cpu(sb->data_size),
1967 (unsigned long long)le64_to_cpu(sb->super_offset),
1968 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1969 le32_to_cpu(sb->dev_number),
1970 uuid[0], uuid[1], uuid[2], uuid[3],
1971 uuid[4], uuid[5], uuid[6], uuid[7],
1972 uuid[8], uuid[9], uuid[10], uuid[11],
1973 uuid[12], uuid[13], uuid[14], uuid[15],
1974 sb->devflags,
1975 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1976 (unsigned long long)le64_to_cpu(sb->events),
1977 (unsigned long long)le64_to_cpu(sb->resync_offset),
1978 le32_to_cpu(sb->sb_csum),
1979 le32_to_cpu(sb->max_dev)
1980 );
1981}
1982
1983static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
1985 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11001986 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1987 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08001988 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1989 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11001991 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1992 switch (major_version) {
1993 case 0:
1994 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1995 break;
1996 case 1:
1997 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1998 break;
1999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 } else
2001 printk(KERN_INFO "md: no rdev superblock!\n");
2002}
2003
Adrian Bunk5e563412006-06-26 00:27:42 -07002004static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002006 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 mdk_rdev_t *rdev;
2008 mddev_t *mddev;
2009 char b[BDEVNAME_SIZE];
2010
2011 printk("\n");
2012 printk("md: **********************************\n");
2013 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2014 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002015 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
NeilBrown32a76272005-06-21 17:17:14 -07002017 if (mddev->bitmap)
2018 bitmap_print_sb(mddev->bitmap);
2019 else
2020 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002021 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 printk("<%s>", bdevname(rdev->bdev,b));
2023 printk("\n");
2024
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002025 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002026 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 }
2028 printk("md: **********************************\n");
2029 printk("\n");
2030}
2031
2032
NeilBrown42543762006-06-26 00:27:57 -07002033static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
NeilBrown42543762006-06-26 00:27:57 -07002035 /* Update each superblock (in-memory image), but
2036 * if we are allowed to, skip spares which already
2037 * have the right event counter, or have one earlier
2038 * (which would mean they aren't being marked as dirty
2039 * with the rest of the array)
2040 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
NeilBrown5e865102009-11-12 12:08:04 +11002043 /* First make sure individual recovery_offsets are correct */
2044 list_for_each_entry(rdev, &mddev->disks, same_set) {
2045 if (rdev->raid_disk >= 0 &&
2046 !test_bit(In_sync, &rdev->flags) &&
2047 mddev->curr_resync_completed > rdev->recovery_offset)
2048 rdev->recovery_offset = mddev->curr_resync_completed;
2049
2050 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002051 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002052 if (rdev->sb_events == mddev->events ||
2053 (nospares &&
2054 rdev->raid_disk < 0 &&
2055 (rdev->sb_events&1)==0 &&
2056 rdev->sb_events+1 == mddev->events)) {
2057 /* Don't update this superblock */
2058 rdev->sb_loaded = 2;
2059 } else {
2060 super_types[mddev->major_version].
2061 sync_super(mddev, rdev);
2062 rdev->sb_loaded = 1;
2063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
2065}
2066
NeilBrown850b2b42006-10-03 01:15:46 -07002067static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002070 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002071 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
NeilBrown1b57f132009-06-18 08:48:19 +10002073 mddev->utime = get_seconds();
NeilBrown8377bc82008-04-30 00:52:28 -07002074 if (mddev->external)
2075 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08002077 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002078
NeilBrown850b2b42006-10-03 01:15:46 -07002079 set_bit(MD_CHANGE_PENDING, &mddev->flags);
2080 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2081 force_change = 1;
2082 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2083 /* just a clean<-> dirty transition, possibly leave spares alone,
2084 * though if events isn't the right even/odd, we will have to do
2085 * spares after all
2086 */
2087 nospares = 1;
2088 if (force_change)
2089 nospares = 0;
2090 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002091 /* If the array is degraded, then skipping spares is both
2092 * dangerous and fairly pointless.
2093 * Dangerous because a device that was removed from the array
2094 * might have a event_count that still looks up-to-date,
2095 * so it can be re-added without a resync.
2096 * Pointless because if there are any spares to skip,
2097 * then a recovery will happen and soon that array won't
2098 * be degraded any more and the spare can go back to sleep then.
2099 */
NeilBrown850b2b42006-10-03 01:15:46 -07002100 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002101
NeilBrown06d91a52005-06-21 17:17:12 -07002102 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002103
2104 /* If this is just a dirty<->clean transition, and the array is clean
2105 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002106 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002107 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08002108 && (mddev->events & 1)
2109 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07002110 mddev->events--;
2111 else {
2112 /* otherwise we have to go forward and ... */
2113 mddev->events ++;
2114 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
NeilBrown51d56682009-08-13 09:54:02 +10002115 /* .. if the array isn't clean, an 'even' event must also go
2116 * to spares. */
2117 if ((mddev->events&1)==0)
NeilBrown42543762006-06-26 00:27:57 -07002118 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002119 } else {
NeilBrown51d56682009-08-13 09:54:02 +10002120 /* otherwise an 'odd' event must go to spares */
2121 if ((mddev->events&1))
NeilBrown42543762006-06-26 00:27:57 -07002122 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002123 }
2124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126 if (!mddev->events) {
2127 /*
2128 * oops, this 64-bit counter should never wrap.
2129 * Either we are in around ~1 trillion A.C., assuming
2130 * 1 reboot per second, or we have a bug:
2131 */
2132 MD_BUG();
2133 mddev->events --;
2134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 /*
2137 * do not write anything to disk if using
2138 * nonpersistent superblocks
2139 */
NeilBrown06d91a52005-06-21 17:17:12 -07002140 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08002141 if (!mddev->external)
2142 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2143
NeilBrowna9701a32005-11-08 21:39:34 -08002144 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002145 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 return;
NeilBrown06d91a52005-06-21 17:17:12 -07002147 }
NeilBrowne6910632008-02-06 01:39:51 -08002148 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002149 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 dprintk(KERN_INFO
2152 "md: updating %s RAID superblock on device (in sync %d)\n",
2153 mdname(mddev),mddev->in_sync);
2154
NeilBrown4ad13662007-07-17 04:06:13 -07002155 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002156 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 char b[BDEVNAME_SIZE];
2158 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002159 if (rdev->sb_loaded != 1)
2160 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002161 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 dprintk("(skipping faulty ");
2163
2164 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002165 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002166 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002167 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002168 rdev->sb_page);
2169 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2170 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002171 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002172 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 } else
2175 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002176 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 /* only need to write one superblock... */
2178 break;
2179 }
NeilBrowna9701a32005-11-08 21:39:34 -08002180 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002181 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002182
NeilBrowna9701a32005-11-08 21:39:34 -08002183 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002184 if (mddev->in_sync != sync_req ||
2185 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002186 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002187 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002188 goto repeat;
2189 }
NeilBrown850b2b42006-10-03 01:15:46 -07002190 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002191 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002192 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002193 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2194 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196}
2197
Andre Noll7f6ce762008-03-23 18:34:54 +01002198/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002199 * We want to accept with case. For this we use cmd_match.
2200 */
2201static int cmd_match(const char *cmd, const char *str)
2202{
2203 /* See if cmd, written into a sysfs file, matches
2204 * str. They must either be the same, or cmd can
2205 * have a trailing newline
2206 */
2207 while (*cmd && *str && *cmd == *str) {
2208 cmd++;
2209 str++;
2210 }
2211 if (*cmd == '\n')
2212 cmd++;
2213 if (*str || *cmd)
2214 return 0;
2215 return 1;
2216}
2217
NeilBrown86e6ffd2005-11-08 21:39:24 -08002218struct rdev_sysfs_entry {
2219 struct attribute attr;
2220 ssize_t (*show)(mdk_rdev_t *, char *);
2221 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2222};
2223
2224static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002225state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002226{
2227 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002228 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002229
NeilBrownb2d444d2005-11-08 21:39:31 -08002230 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002231 len+= sprintf(page+len, "%sfaulty",sep);
2232 sep = ",";
2233 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002234 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002235 len += sprintf(page+len, "%sin_sync",sep);
2236 sep = ",";
2237 }
NeilBrownf6556752006-06-26 00:28:01 -07002238 if (test_bit(WriteMostly, &rdev->flags)) {
2239 len += sprintf(page+len, "%swrite_mostly",sep);
2240 sep = ",";
2241 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002242 if (test_bit(Blocked, &rdev->flags)) {
2243 len += sprintf(page+len, "%sblocked", sep);
2244 sep = ",";
2245 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002246 if (!test_bit(Faulty, &rdev->flags) &&
2247 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002248 len += sprintf(page+len, "%sspare", sep);
2249 sep = ",";
2250 }
2251 return len+sprintf(page+len, "\n");
2252}
2253
NeilBrown45dc2de2006-06-26 00:27:58 -07002254static ssize_t
2255state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2256{
2257 /* can write
2258 * faulty - simulates and error
2259 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002260 * writemostly - sets write_mostly
2261 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002262 * blocked - sets the Blocked flag
2263 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002264 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002265 */
2266 int err = -EINVAL;
2267 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2268 md_error(rdev->mddev, rdev);
2269 err = 0;
2270 } else if (cmd_match(buf, "remove")) {
2271 if (rdev->raid_disk >= 0)
2272 err = -EBUSY;
2273 else {
2274 mddev_t *mddev = rdev->mddev;
2275 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002276 if (mddev->pers)
2277 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002278 md_new_event(mddev);
2279 err = 0;
2280 }
NeilBrownf6556752006-06-26 00:28:01 -07002281 } else if (cmd_match(buf, "writemostly")) {
2282 set_bit(WriteMostly, &rdev->flags);
2283 err = 0;
2284 } else if (cmd_match(buf, "-writemostly")) {
2285 clear_bit(WriteMostly, &rdev->flags);
2286 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002287 } else if (cmd_match(buf, "blocked")) {
2288 set_bit(Blocked, &rdev->flags);
2289 err = 0;
2290 } else if (cmd_match(buf, "-blocked")) {
2291 clear_bit(Blocked, &rdev->flags);
2292 wake_up(&rdev->blocked_wait);
2293 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2294 md_wakeup_thread(rdev->mddev->thread);
2295
2296 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002297 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2298 set_bit(In_sync, &rdev->flags);
2299 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002300 }
NeilBrown3c0ee632008-10-21 13:25:28 +11002301 if (!err && rdev->sysfs_state)
2302 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002303 return err ? err : len;
2304}
NeilBrown80ca3a42006-07-10 04:44:18 -07002305static struct rdev_sysfs_entry rdev_state =
2306__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002307
2308static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002309errors_show(mdk_rdev_t *rdev, char *page)
2310{
2311 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2312}
2313
2314static ssize_t
2315errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2316{
2317 char *e;
2318 unsigned long n = simple_strtoul(buf, &e, 10);
2319 if (*buf && (*e == 0 || *e == '\n')) {
2320 atomic_set(&rdev->corrected_errors, n);
2321 return len;
2322 }
2323 return -EINVAL;
2324}
2325static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002326__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002327
NeilBrown014236d2006-01-06 00:20:55 -08002328static ssize_t
2329slot_show(mdk_rdev_t *rdev, char *page)
2330{
2331 if (rdev->raid_disk < 0)
2332 return sprintf(page, "none\n");
2333 else
2334 return sprintf(page, "%d\n", rdev->raid_disk);
2335}
2336
2337static ssize_t
2338slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2339{
2340 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002341 int err;
2342 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002343 int slot = simple_strtoul(buf, &e, 10);
2344 if (strncmp(buf, "none", 4)==0)
2345 slot = -1;
2346 else if (e==buf || (*e && *e!= '\n'))
2347 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002348 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002349 /* Setting 'slot' on an active array requires also
2350 * updating the 'rd%d' link, and communicating
2351 * with the personality with ->hot_*_disk.
2352 * For now we only support removing
2353 * failed/spare devices. This normally happens automatically,
2354 * but not when the metadata is externally managed.
2355 */
NeilBrownc303da62008-02-06 01:39:51 -08002356 if (rdev->raid_disk == -1)
2357 return -EEXIST;
2358 /* personality does all needed checks */
2359 if (rdev->mddev->pers->hot_add_disk == NULL)
2360 return -EINVAL;
2361 err = rdev->mddev->pers->
2362 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2363 if (err)
2364 return err;
2365 sprintf(nm, "rd%d", rdev->raid_disk);
2366 sysfs_remove_link(&rdev->mddev->kobj, nm);
2367 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2368 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002369 } else if (rdev->mddev->pers) {
2370 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002371 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002372 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002373 */
2374
2375 if (rdev->raid_disk != -1)
2376 return -EBUSY;
2377
2378 if (rdev->mddev->pers->hot_add_disk == NULL)
2379 return -EINVAL;
2380
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002381 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002382 if (rdev2->raid_disk == slot)
2383 return -EEXIST;
2384
2385 rdev->raid_disk = slot;
2386 if (test_bit(In_sync, &rdev->flags))
2387 rdev->saved_raid_disk = slot;
2388 else
2389 rdev->saved_raid_disk = -1;
2390 err = rdev->mddev->pers->
2391 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002392 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002393 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002394 return err;
Neil Brown52664732008-06-28 08:31:44 +10002395 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002396 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002397 sprintf(nm, "rd%d", rdev->raid_disk);
2398 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2399 printk(KERN_WARNING
2400 "md: cannot register "
2401 "%s for %s\n",
2402 nm, mdname(rdev->mddev));
2403
2404 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002405 } else {
2406 if (slot >= rdev->mddev->raid_disks)
2407 return -ENOSPC;
2408 rdev->raid_disk = slot;
2409 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002410 clear_bit(Faulty, &rdev->flags);
2411 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002412 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002413 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002414 }
NeilBrown014236d2006-01-06 00:20:55 -08002415 return len;
2416}
2417
2418
2419static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002420__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002421
NeilBrown93c8cad2006-01-06 00:20:56 -08002422static ssize_t
2423offset_show(mdk_rdev_t *rdev, char *page)
2424{
Andrew Morton6961ece2006-01-06 00:20:59 -08002425 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002426}
2427
2428static ssize_t
2429offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2430{
2431 char *e;
2432 unsigned long long offset = simple_strtoull(buf, &e, 10);
2433 if (e==buf || (*e && *e != '\n'))
2434 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002435 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002436 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002437 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002438 /* Must set offset before size, so overlap checks
2439 * can be sane */
2440 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002441 rdev->data_offset = offset;
2442 return len;
2443}
2444
2445static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002446__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002447
NeilBrown83303b62006-01-06 00:21:06 -08002448static ssize_t
2449rdev_size_show(mdk_rdev_t *rdev, char *page)
2450{
Andre Nolldd8ac332009-03-31 14:33:13 +11002451 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002452}
2453
NeilBrownc5d79ad2008-02-06 01:39:54 -08002454static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2455{
2456 /* check if two start/length pairs overlap */
2457 if (s1+l1 <= s2)
2458 return 0;
2459 if (s2+l2 <= s1)
2460 return 0;
2461 return 1;
2462}
2463
Dan Williamsb522adc2009-03-31 15:00:31 +11002464static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2465{
2466 unsigned long long blocks;
2467 sector_t new;
2468
2469 if (strict_strtoull(buf, 10, &blocks) < 0)
2470 return -EINVAL;
2471
2472 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2473 return -EINVAL; /* sector conversion overflow */
2474
2475 new = blocks * 2;
2476 if (new != blocks * 2)
2477 return -EINVAL; /* unsigned long long to sector_t overflow */
2478
2479 *sectors = new;
2480 return 0;
2481}
2482
NeilBrown83303b62006-01-06 00:21:06 -08002483static ssize_t
2484rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2485{
NeilBrown27c529b2008-03-04 14:29:33 -08002486 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002487 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002488 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002489
Dan Williamsb522adc2009-03-31 15:00:31 +11002490 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002491 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002492 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002493 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002494 sectors = super_types[my_mddev->major_version].
2495 rdev_size_change(rdev, sectors);
2496 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002497 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002498 } else if (!sectors)
2499 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2500 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002501 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002502 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002503 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002504
Andre Nolldd8ac332009-03-31 14:33:13 +11002505 rdev->sectors = sectors;
2506 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002507 /* need to check that all other rdevs with the same ->bdev
2508 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002509 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002510 * we have to change it back, we will have the lock again.
2511 */
2512 mddev_t *mddev;
2513 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002514 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002515
NeilBrown27c529b2008-03-04 14:29:33 -08002516 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002517 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002518 mdk_rdev_t *rdev2;
2519
2520 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002521 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002522 if (test_bit(AllReserved, &rdev2->flags) ||
2523 (rdev->bdev == rdev2->bdev &&
2524 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002525 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002526 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002527 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002528 overlap = 1;
2529 break;
2530 }
2531 mddev_unlock(mddev);
2532 if (overlap) {
2533 mddev_put(mddev);
2534 break;
2535 }
2536 }
NeilBrown27c529b2008-03-04 14:29:33 -08002537 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002538 if (overlap) {
2539 /* Someone else could have slipped in a size
2540 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002541 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002542 * safe, and trust userspace not to race with
2543 * itself
2544 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002545 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002546 return -EBUSY;
2547 }
2548 }
NeilBrown83303b62006-01-06 00:21:06 -08002549 return len;
2550}
2551
2552static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002553__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002554
NeilBrown86e6ffd2005-11-08 21:39:24 -08002555static struct attribute *rdev_default_attrs[] = {
2556 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002557 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002558 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002559 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002560 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002561 NULL,
2562};
2563static ssize_t
2564rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2565{
2566 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2567 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002568 mddev_t *mddev = rdev->mddev;
2569 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002570
2571 if (!entry->show)
2572 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002573
2574 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2575 if (!rv) {
2576 if (rdev->mddev == NULL)
2577 rv = -EBUSY;
2578 else
2579 rv = entry->show(rdev, page);
2580 mddev_unlock(mddev);
2581 }
2582 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002583}
2584
2585static ssize_t
2586rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2587 const char *page, size_t length)
2588{
2589 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2590 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002591 ssize_t rv;
2592 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002593
2594 if (!entry->store)
2595 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002596 if (!capable(CAP_SYS_ADMIN))
2597 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002598 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002599 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002600 if (rdev->mddev == NULL)
2601 rv = -EBUSY;
2602 else
2603 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002604 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002605 }
2606 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002607}
2608
2609static void rdev_free(struct kobject *ko)
2610{
2611 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2612 kfree(rdev);
2613}
2614static struct sysfs_ops rdev_sysfs_ops = {
2615 .show = rdev_attr_show,
2616 .store = rdev_attr_store,
2617};
2618static struct kobj_type rdev_ktype = {
2619 .release = rdev_free,
2620 .sysfs_ops = &rdev_sysfs_ops,
2621 .default_attrs = rdev_default_attrs,
2622};
2623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624/*
2625 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2626 *
2627 * mark the device faulty if:
2628 *
2629 * - the device is nonexistent (zero size)
2630 * - the device has no valid superblock
2631 *
2632 * a faulty rdev _never_ has rdev->sb set.
2633 */
2634static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2635{
2636 char b[BDEVNAME_SIZE];
2637 int err;
2638 mdk_rdev_t *rdev;
2639 sector_t size;
2640
NeilBrown9ffae0c2006-01-06 00:20:32 -08002641 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 if (!rdev) {
2643 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2644 return ERR_PTR(-ENOMEM);
2645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
2647 if ((err = alloc_disk_sb(rdev)))
2648 goto abort_free;
2649
NeilBrownc5d79ad2008-02-06 01:39:54 -08002650 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 if (err)
2652 goto abort_free;
2653
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002654 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002657 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002658 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002659 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002661 rdev->sb_events = 0;
Robert Becker1e509152009-12-14 12:49:58 +11002662 rdev->last_read_error.tv_sec = 0;
2663 rdev->last_read_error.tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002665 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002666 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
2668 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2669 if (!size) {
2670 printk(KERN_WARNING
2671 "md: %s has zero or unknown size, marking faulty!\n",
2672 bdevname(rdev->bdev,b));
2673 err = -EINVAL;
2674 goto abort_free;
2675 }
2676
2677 if (super_format >= 0) {
2678 err = super_types[super_format].
2679 load_super(rdev, NULL, super_minor);
2680 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002681 printk(KERN_WARNING
2682 "md: %s does not have a valid v%d.%d "
2683 "superblock, not importing!\n",
2684 bdevname(rdev->bdev,b),
2685 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 goto abort_free;
2687 }
2688 if (err < 0) {
2689 printk(KERN_WARNING
2690 "md: could not read %s's sb, not importing!\n",
2691 bdevname(rdev->bdev,b));
2692 goto abort_free;
2693 }
2694 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002697 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
2699 return rdev;
2700
2701abort_free:
2702 if (rdev->sb_page) {
2703 if (rdev->bdev)
2704 unlock_rdev(rdev);
2705 free_disk_sb(rdev);
2706 }
2707 kfree(rdev);
2708 return ERR_PTR(err);
2709}
2710
2711/*
2712 * Check a full RAID array for plausibility
2713 */
2714
2715
NeilBrowna757e642005-04-16 15:26:42 -07002716static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
2718 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002719 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 char b[BDEVNAME_SIZE];
2721
2722 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002723 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 switch (super_types[mddev->major_version].
2725 load_super(rdev, freshest, mddev->minor_version)) {
2726 case 1:
2727 freshest = rdev;
2728 break;
2729 case 0:
2730 break;
2731 default:
2732 printk( KERN_ERR \
2733 "md: fatal superblock inconsistency in %s"
2734 " -- removing from array\n",
2735 bdevname(rdev->bdev,b));
2736 kick_rdev_from_array(rdev);
2737 }
2738
2739
2740 super_types[mddev->major_version].
2741 validate_super(mddev, freshest);
2742
2743 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002744 rdev_for_each(rdev, tmp, mddev) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002745 if (rdev->desc_nr >= mddev->max_disks ||
2746 i > mddev->max_disks) {
2747 printk(KERN_WARNING
2748 "md: %s: %s: only %d devices permitted\n",
2749 mdname(mddev), bdevname(rdev->bdev, b),
2750 mddev->max_disks);
2751 kick_rdev_from_array(rdev);
2752 continue;
2753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 if (rdev != freshest)
2755 if (super_types[mddev->major_version].
2756 validate_super(mddev, rdev)) {
2757 printk(KERN_WARNING "md: kicking non-fresh %s"
2758 " from array!\n",
2759 bdevname(rdev->bdev,b));
2760 kick_rdev_from_array(rdev);
2761 continue;
2762 }
2763 if (mddev->level == LEVEL_MULTIPATH) {
2764 rdev->desc_nr = i++;
2765 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002766 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002767 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002768 rdev->raid_disk = -1;
2769 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 }
2771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772}
2773
NeilBrown72e02072009-12-14 12:49:55 +11002774/* Read a fixed-point number.
2775 * Numbers in sysfs attributes should be in "standard" units where
2776 * possible, so time should be in seconds.
2777 * However we internally use a a much smaller unit such as
2778 * milliseconds or jiffies.
2779 * This function takes a decimal number with a possible fractional
2780 * component, and produces an integer which is the result of
2781 * multiplying that number by 10^'scale'.
2782 * all without any floating-point arithmetic.
2783 */
2784int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2785{
2786 unsigned long result = 0;
2787 long decimals = -1;
2788 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2789 if (*cp == '.')
2790 decimals = 0;
2791 else if (decimals < scale) {
2792 unsigned int value;
2793 value = *cp - '0';
2794 result = result * 10 + value;
2795 if (decimals >= 0)
2796 decimals++;
2797 }
2798 cp++;
2799 }
2800 if (*cp == '\n')
2801 cp++;
2802 if (*cp)
2803 return -EINVAL;
2804 if (decimals < 0)
2805 decimals = 0;
2806 while (decimals < scale) {
2807 result *= 10;
2808 decimals ++;
2809 }
2810 *res = result;
2811 return 0;
2812}
2813
2814
NeilBrown19052c02008-08-05 15:54:13 +10002815static void md_safemode_timeout(unsigned long data);
2816
NeilBrowneae17012005-11-08 21:39:23 -08002817static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002818safe_delay_show(mddev_t *mddev, char *page)
2819{
2820 int msec = (mddev->safemode_delay*1000)/HZ;
2821 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2822}
2823static ssize_t
2824safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2825{
NeilBrown16f17b32006-06-26 00:27:37 -07002826 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07002827
NeilBrown72e02072009-12-14 12:49:55 +11002828 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002829 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07002830 if (msec == 0)
2831 mddev->safemode_delay = 0;
2832 else {
NeilBrown19052c02008-08-05 15:54:13 +10002833 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002834 mddev->safemode_delay = (msec*HZ)/1000;
2835 if (mddev->safemode_delay == 0)
2836 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002837 if (mddev->safemode_delay < old_delay)
2838 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002839 }
2840 return len;
2841}
2842static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002843__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002844
2845static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002846level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002847{
NeilBrown2604b702006-01-06 00:20:36 -08002848 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002849 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002850 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002851 else if (mddev->clevel[0])
2852 return sprintf(page, "%s\n", mddev->clevel);
2853 else if (mddev->level != LEVEL_NONE)
2854 return sprintf(page, "%d\n", mddev->level);
2855 else
2856 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002857}
2858
NeilBrownd9d166c2006-01-06 00:20:51 -08002859static ssize_t
2860level_store(mddev_t *mddev, const char *buf, size_t len)
2861{
NeilBrown245f46c2009-03-31 14:39:39 +11002862 char level[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002863 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002864 struct mdk_personality *pers;
2865 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002866 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002867
2868 if (mddev->pers == NULL) {
2869 if (len == 0)
2870 return 0;
2871 if (len >= sizeof(mddev->clevel))
2872 return -ENOSPC;
2873 strncpy(mddev->clevel, buf, len);
2874 if (mddev->clevel[len-1] == '\n')
2875 len--;
2876 mddev->clevel[len] = 0;
2877 mddev->level = LEVEL_NONE;
2878 return rv;
2879 }
2880
2881 /* request to change the personality. Need to ensure:
2882 * - array is not engaged in resync/recovery/reshape
2883 * - old personality can be suspended
2884 * - new personality will access other array.
2885 */
2886
2887 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownd9d166c2006-01-06 00:20:51 -08002888 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002889
2890 if (!mddev->pers->quiesce) {
2891 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2892 mdname(mddev), mddev->pers->name);
2893 return -EINVAL;
2894 }
2895
2896 /* Now find the new personality */
2897 if (len == 0 || len >= sizeof(level))
2898 return -EINVAL;
2899 strncpy(level, buf, len);
2900 if (level[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002901 len--;
NeilBrown245f46c2009-03-31 14:39:39 +11002902 level[len] = 0;
2903
2904 request_module("md-%s", level);
2905 spin_lock(&pers_lock);
2906 pers = find_pers(LEVEL_NONE, level);
2907 if (!pers || !try_module_get(pers->owner)) {
2908 spin_unlock(&pers_lock);
2909 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2910 return -EINVAL;
2911 }
2912 spin_unlock(&pers_lock);
2913
2914 if (pers == mddev->pers) {
2915 /* Nothing to do! */
2916 module_put(pers->owner);
2917 return rv;
2918 }
2919 if (!pers->takeover) {
2920 module_put(pers->owner);
2921 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2922 mdname(mddev), level);
2923 return -EINVAL;
2924 }
2925
2926 /* ->takeover must set new_* and/or delta_disks
2927 * if it succeeds, and may set them when it fails.
2928 */
2929 priv = pers->takeover(mddev);
2930 if (IS_ERR(priv)) {
2931 mddev->new_level = mddev->level;
2932 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002933 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002934 mddev->raid_disks -= mddev->delta_disks;
2935 mddev->delta_disks = 0;
2936 module_put(pers->owner);
2937 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2938 mdname(mddev), level);
2939 return PTR_ERR(priv);
2940 }
2941
2942 /* Looks like we have a winner */
2943 mddev_suspend(mddev);
2944 mddev->pers->stop(mddev);
2945 module_put(mddev->pers->owner);
NeilBrown3a981b02009-08-03 10:59:55 +10002946 /* Invalidate devices that are now superfluous */
2947 list_for_each_entry(rdev, &mddev->disks, same_set)
2948 if (rdev->raid_disk >= mddev->raid_disks) {
2949 rdev->raid_disk = -1;
2950 clear_bit(In_sync, &rdev->flags);
2951 }
NeilBrown245f46c2009-03-31 14:39:39 +11002952 mddev->pers = pers;
2953 mddev->private = priv;
2954 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2955 mddev->level = mddev->new_level;
2956 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002957 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002958 mddev->delta_disks = 0;
2959 pers->run(mddev);
2960 mddev_resume(mddev);
2961 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2962 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2963 md_wakeup_thread(mddev->thread);
NeilBrownd9d166c2006-01-06 00:20:51 -08002964 return rv;
2965}
2966
2967static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002968__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002969
NeilBrownd4dbd022006-06-26 00:27:59 -07002970
2971static ssize_t
2972layout_show(mddev_t *mddev, char *page)
2973{
2974 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002975 if (mddev->reshape_position != MaxSector &&
2976 mddev->layout != mddev->new_layout)
2977 return sprintf(page, "%d (%d)\n",
2978 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002979 return sprintf(page, "%d\n", mddev->layout);
2980}
2981
2982static ssize_t
2983layout_store(mddev_t *mddev, const char *buf, size_t len)
2984{
2985 char *e;
2986 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002987
2988 if (!*buf || (*e && *e != '\n'))
2989 return -EINVAL;
2990
NeilBrownb3546032009-03-31 14:56:41 +11002991 if (mddev->pers) {
2992 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10002993 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11002994 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10002995 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10002996 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10002997 if (err) {
2998 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11002999 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003000 }
NeilBrownb3546032009-03-31 14:56:41 +11003001 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003002 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003003 if (mddev->reshape_position == MaxSector)
3004 mddev->layout = n;
3005 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003006 return len;
3007}
3008static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003009__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003010
3011
NeilBrowneae17012005-11-08 21:39:23 -08003012static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003013raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003014{
NeilBrownbb636542005-11-08 21:39:45 -08003015 if (mddev->raid_disks == 0)
3016 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003017 if (mddev->reshape_position != MaxSector &&
3018 mddev->delta_disks != 0)
3019 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3020 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003021 return sprintf(page, "%d\n", mddev->raid_disks);
3022}
3023
NeilBrownda943b992006-01-06 00:20:54 -08003024static int update_raid_disks(mddev_t *mddev, int raid_disks);
3025
3026static ssize_t
3027raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3028{
NeilBrownda943b992006-01-06 00:20:54 -08003029 char *e;
3030 int rv = 0;
3031 unsigned long n = simple_strtoul(buf, &e, 10);
3032
3033 if (!*buf || (*e && *e != '\n'))
3034 return -EINVAL;
3035
3036 if (mddev->pers)
3037 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003038 else if (mddev->reshape_position != MaxSector) {
3039 int olddisks = mddev->raid_disks - mddev->delta_disks;
3040 mddev->delta_disks = n - olddisks;
3041 mddev->raid_disks = n;
3042 } else
NeilBrownda943b992006-01-06 00:20:54 -08003043 mddev->raid_disks = n;
3044 return rv ? rv : len;
3045}
3046static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003047__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003048
NeilBrown24dd4692005-11-08 21:39:26 -08003049static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003050chunk_size_show(mddev_t *mddev, char *page)
3051{
NeilBrown08a02ec2007-05-09 02:35:38 -07003052 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003053 mddev->chunk_sectors != mddev->new_chunk_sectors)
3054 return sprintf(page, "%d (%d)\n",
3055 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003056 mddev->chunk_sectors << 9);
3057 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003058}
3059
3060static ssize_t
3061chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3062{
NeilBrown3b343802006-01-06 00:20:47 -08003063 char *e;
3064 unsigned long n = simple_strtoul(buf, &e, 10);
3065
NeilBrown3b343802006-01-06 00:20:47 -08003066 if (!*buf || (*e && *e != '\n'))
3067 return -EINVAL;
3068
NeilBrownb3546032009-03-31 14:56:41 +11003069 if (mddev->pers) {
3070 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003071 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003072 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003073 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003074 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003075 if (err) {
3076 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003077 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003078 }
NeilBrownb3546032009-03-31 14:56:41 +11003079 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003080 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003081 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003082 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003083 }
NeilBrown3b343802006-01-06 00:20:47 -08003084 return len;
3085}
3086static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003087__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003088
NeilBrowna94213b2006-06-26 00:28:00 -07003089static ssize_t
3090resync_start_show(mddev_t *mddev, char *page)
3091{
NeilBrownd1a7c502009-03-31 15:24:32 +11003092 if (mddev->recovery_cp == MaxSector)
3093 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003094 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3095}
3096
3097static ssize_t
3098resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3099{
NeilBrowna94213b2006-06-26 00:28:00 -07003100 char *e;
3101 unsigned long long n = simple_strtoull(buf, &e, 10);
3102
3103 if (mddev->pers)
3104 return -EBUSY;
3105 if (!*buf || (*e && *e != '\n'))
3106 return -EINVAL;
3107
3108 mddev->recovery_cp = n;
3109 return len;
3110}
3111static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003112__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003113
NeilBrown9e653b62006-06-26 00:27:58 -07003114/*
3115 * The array state can be:
3116 *
3117 * clear
3118 * No devices, no size, no level
3119 * Equivalent to STOP_ARRAY ioctl
3120 * inactive
3121 * May have some settings, but array is not active
3122 * all IO results in error
3123 * When written, doesn't tear down array, but just stops it
3124 * suspended (not supported yet)
3125 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003126 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003127 * readonly
3128 * no resync can happen. no superblocks get written.
3129 * write requests fail
3130 * read-auto
3131 * like readonly, but behaves like 'clean' on a write request.
3132 *
3133 * clean - no pending writes, but otherwise active.
3134 * When written to inactive array, starts without resync
3135 * If a write request arrives then
3136 * if metadata is known, mark 'dirty' and switch to 'active'.
3137 * if not known, block and switch to write-pending
3138 * If written to an active array that has pending writes, then fails.
3139 * active
3140 * fully active: IO and resync can be happening.
3141 * When written to inactive array, starts with resync
3142 *
3143 * write-pending
3144 * clean, but writes are blocked waiting for 'active' to be written.
3145 *
3146 * active-idle
3147 * like active, but no writes have been seen for a while (100msec).
3148 *
3149 */
3150enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3151 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003152static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003153 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3154 "write-pending", "active-idle", NULL };
3155
3156static int match_word(const char *word, char **list)
3157{
3158 int n;
3159 for (n=0; list[n]; n++)
3160 if (cmd_match(word, list[n]))
3161 break;
3162 return n;
3163}
3164
3165static ssize_t
3166array_state_show(mddev_t *mddev, char *page)
3167{
3168 enum array_state st = inactive;
3169
3170 if (mddev->pers)
3171 switch(mddev->ro) {
3172 case 1:
3173 st = readonly;
3174 break;
3175 case 2:
3176 st = read_auto;
3177 break;
3178 case 0:
3179 if (mddev->in_sync)
3180 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08003181 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3182 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003183 else if (mddev->safemode)
3184 st = active_idle;
3185 else
3186 st = active;
3187 }
3188 else {
3189 if (list_empty(&mddev->disks) &&
3190 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003191 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003192 st = clear;
3193 else
3194 st = inactive;
3195 }
3196 return sprintf(page, "%s\n", array_states[st]);
3197}
3198
Neil Browndf5b20c2008-07-11 22:02:22 +10003199static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003200static int do_md_run(mddev_t * mddev);
3201static int restart_array(mddev_t *mddev);
3202
3203static ssize_t
3204array_state_store(mddev_t *mddev, const char *buf, size_t len)
3205{
3206 int err = -EINVAL;
3207 enum array_state st = match_word(buf, array_states);
3208 switch(st) {
3209 case bad_word:
3210 break;
3211 case clear:
3212 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003213 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003214 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003215 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003216 break;
3217 case inactive:
3218 /* stopping an active array */
3219 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003220 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003221 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003222 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003223 } else
3224 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003225 break;
3226 case suspended:
3227 break; /* not supported yet */
3228 case readonly:
3229 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10003230 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003231 else {
3232 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003233 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003234 err = do_md_run(mddev);
3235 }
3236 break;
3237 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003238 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003239 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10003240 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003241 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003242 err = restart_array(mddev);
3243 if (err == 0) {
3244 mddev->ro = 2;
3245 set_disk_ro(mddev->gendisk, 0);
3246 }
NeilBrown9e653b62006-06-26 00:27:58 -07003247 } else {
3248 mddev->ro = 2;
3249 err = do_md_run(mddev);
3250 }
3251 break;
3252 case clean:
3253 if (mddev->pers) {
3254 restart_array(mddev);
3255 spin_lock_irq(&mddev->write_lock);
3256 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003257 if (mddev->in_sync == 0) {
3258 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003259 if (mddev->safemode == 1)
3260 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003261 if (mddev->persistent)
3262 set_bit(MD_CHANGE_CLEAN,
3263 &mddev->flags);
3264 }
3265 err = 0;
3266 } else
3267 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003268 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003269 } else
3270 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003271 break;
3272 case active:
3273 if (mddev->pers) {
3274 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003275 if (mddev->external)
3276 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003277 wake_up(&mddev->sb_wait);
3278 err = 0;
3279 } else {
3280 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003281 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003282 err = do_md_run(mddev);
3283 }
3284 break;
3285 case write_pending:
3286 case active_idle:
3287 /* these cannot be set */
3288 break;
3289 }
3290 if (err)
3291 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003292 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003293 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003294 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003295 }
NeilBrown9e653b62006-06-26 00:27:58 -07003296}
NeilBrown80ca3a42006-07-10 04:44:18 -07003297static struct md_sysfs_entry md_array_state =
3298__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003299
NeilBrown6d7ff732006-01-06 00:21:16 -08003300static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003301max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3302 return sprintf(page, "%d\n",
3303 atomic_read(&mddev->max_corr_read_errors));
3304}
3305
3306static ssize_t
3307max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3308{
3309 char *e;
3310 unsigned long n = simple_strtoul(buf, &e, 10);
3311
3312 if (*buf && (*e == 0 || *e == '\n')) {
3313 atomic_set(&mddev->max_corr_read_errors, n);
3314 return len;
3315 }
3316 return -EINVAL;
3317}
3318
3319static struct md_sysfs_entry max_corr_read_errors =
3320__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3321 max_corrected_read_errors_store);
3322
3323static ssize_t
NeilBrown6d7ff732006-01-06 00:21:16 -08003324null_show(mddev_t *mddev, char *page)
3325{
3326 return -EINVAL;
3327}
3328
3329static ssize_t
3330new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3331{
3332 /* buf must be %d:%d\n? giving major and minor numbers */
3333 /* The new device is added to the array.
3334 * If the array has a persistent superblock, we read the
3335 * superblock to initialise info and check validity.
3336 * Otherwise, only checking done is that in bind_rdev_to_array,
3337 * which mainly checks size.
3338 */
3339 char *e;
3340 int major = simple_strtoul(buf, &e, 10);
3341 int minor;
3342 dev_t dev;
3343 mdk_rdev_t *rdev;
3344 int err;
3345
3346 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3347 return -EINVAL;
3348 minor = simple_strtoul(e+1, &e, 10);
3349 if (*e && *e != '\n')
3350 return -EINVAL;
3351 dev = MKDEV(major, minor);
3352 if (major != MAJOR(dev) ||
3353 minor != MINOR(dev))
3354 return -EOVERFLOW;
3355
3356
3357 if (mddev->persistent) {
3358 rdev = md_import_device(dev, mddev->major_version,
3359 mddev->minor_version);
3360 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3361 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3362 mdk_rdev_t, same_set);
3363 err = super_types[mddev->major_version]
3364 .load_super(rdev, rdev0, mddev->minor_version);
3365 if (err < 0)
3366 goto out;
3367 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003368 } else if (mddev->external)
3369 rdev = md_import_device(dev, -2, -1);
3370 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003371 rdev = md_import_device(dev, -1, -1);
3372
3373 if (IS_ERR(rdev))
3374 return PTR_ERR(rdev);
3375 err = bind_rdev_to_array(rdev, mddev);
3376 out:
3377 if (err)
3378 export_rdev(rdev);
3379 return err ? err : len;
3380}
3381
3382static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003383__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003384
3385static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003386bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3387{
3388 char *end;
3389 unsigned long chunk, end_chunk;
3390
3391 if (!mddev->bitmap)
3392 goto out;
3393 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3394 while (*buf) {
3395 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3396 if (buf == end) break;
3397 if (*end == '-') { /* range */
3398 buf = end + 1;
3399 end_chunk = simple_strtoul(buf, &end, 0);
3400 if (buf == end) break;
3401 }
3402 if (*end && !isspace(*end)) break;
3403 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3404 buf = end;
3405 while (isspace(*buf)) buf++;
3406 }
3407 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3408out:
3409 return len;
3410}
3411
3412static struct md_sysfs_entry md_bitmap =
3413__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3414
3415static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003416size_show(mddev_t *mddev, char *page)
3417{
Andre Noll58c0fed2009-03-31 14:33:13 +11003418 return sprintf(page, "%llu\n",
3419 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003420}
3421
Andre Nolld71f9f82008-07-11 22:02:22 +10003422static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003423
3424static ssize_t
3425size_store(mddev_t *mddev, const char *buf, size_t len)
3426{
3427 /* If array is inactive, we can reduce the component size, but
3428 * not increase it (except from 0).
3429 * If array is active, we can try an on-line resize
3430 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003431 sector_t sectors;
3432 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003433
Andre Noll58c0fed2009-03-31 14:33:13 +11003434 if (err < 0)
3435 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003436 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003437 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003438 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003439 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003440 if (mddev->dev_sectors == 0 ||
3441 mddev->dev_sectors > sectors)
3442 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003443 else
3444 err = -ENOSPC;
3445 }
3446 return err ? err : len;
3447}
3448
3449static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003450__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003451
NeilBrown8bb93aa2006-01-06 00:20:50 -08003452
3453/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003454 * This is one of
3455 * 'none' for arrays with no metadata (good luck...)
3456 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003457 * or N.M for internally known formats
3458 */
3459static ssize_t
3460metadata_show(mddev_t *mddev, char *page)
3461{
3462 if (mddev->persistent)
3463 return sprintf(page, "%d.%d\n",
3464 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003465 else if (mddev->external)
3466 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003467 else
3468 return sprintf(page, "none\n");
3469}
3470
3471static ssize_t
3472metadata_store(mddev_t *mddev, const char *buf, size_t len)
3473{
3474 int major, minor;
3475 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003476 /* Changing the details of 'external' metadata is
3477 * always permitted. Otherwise there must be
3478 * no devices attached to the array.
3479 */
3480 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3481 ;
3482 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003483 return -EBUSY;
3484
3485 if (cmd_match(buf, "none")) {
3486 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003487 mddev->external = 0;
3488 mddev->major_version = 0;
3489 mddev->minor_version = 90;
3490 return len;
3491 }
3492 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003493 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003494 if (namelen >= sizeof(mddev->metadata_type))
3495 namelen = sizeof(mddev->metadata_type)-1;
3496 strncpy(mddev->metadata_type, buf+9, namelen);
3497 mddev->metadata_type[namelen] = 0;
3498 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3499 mddev->metadata_type[--namelen] = 0;
3500 mddev->persistent = 0;
3501 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003502 mddev->major_version = 0;
3503 mddev->minor_version = 90;
3504 return len;
3505 }
3506 major = simple_strtoul(buf, &e, 10);
3507 if (e==buf || *e != '.')
3508 return -EINVAL;
3509 buf = e+1;
3510 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003511 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003512 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003513 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003514 return -ENOENT;
3515 mddev->major_version = major;
3516 mddev->minor_version = minor;
3517 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003518 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003519 return len;
3520}
3521
3522static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003523__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003524
NeilBrowna35b0d62006-01-06 00:20:49 -08003525static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003526action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003527{
NeilBrown7eec3142005-11-08 21:39:44 -08003528 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003529 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3530 type = "frozen";
3531 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003532 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003533 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3534 type = "reshape";
3535 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003536 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3537 type = "resync";
3538 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3539 type = "check";
3540 else
3541 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003542 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003543 type = "recover";
3544 }
3545 return sprintf(page, "%s\n", type);
3546}
3547
3548static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003549action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003550{
NeilBrown7eec3142005-11-08 21:39:44 -08003551 if (!mddev->pers || !mddev->pers->sync_request)
3552 return -EINVAL;
3553
NeilBrownb6a9ce62009-05-26 09:41:17 +10003554 if (cmd_match(page, "frozen"))
3555 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3556 else
3557 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3558
3559 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003560 if (mddev->sync_thread) {
3561 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3562 md_unregister_thread(mddev->sync_thread);
3563 mddev->sync_thread = NULL;
3564 mddev->recovery = 0;
3565 }
NeilBrown03c902e2006-01-06 00:20:46 -08003566 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3567 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003568 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003569 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003570 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003571 else if (cmd_match(page, "recover")) {
3572 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3573 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3574 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003575 int err;
3576 if (mddev->pers->start_reshape == NULL)
3577 return -EINVAL;
3578 err = mddev->pers->start_reshape(mddev);
3579 if (err)
3580 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003581 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003582 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003583 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003584 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003585 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003586 return -EINVAL;
3587 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3588 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003589 }
NeilBrown03c902e2006-01-06 00:20:46 -08003590 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003591 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003592 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003593 return len;
3594}
3595
NeilBrown9d888832005-11-08 21:39:26 -08003596static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003597mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003598{
3599 return sprintf(page, "%llu\n",
3600 (unsigned long long) mddev->resync_mismatches);
3601}
3602
NeilBrown80ca3a42006-07-10 04:44:18 -07003603static struct md_sysfs_entry md_scan_mode =
3604__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003605
NeilBrown96de1e62005-11-08 21:39:39 -08003606
NeilBrown80ca3a42006-07-10 04:44:18 -07003607static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003608
NeilBrown88202a02006-01-06 00:21:36 -08003609static ssize_t
3610sync_min_show(mddev_t *mddev, char *page)
3611{
3612 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3613 mddev->sync_speed_min ? "local": "system");
3614}
3615
3616static ssize_t
3617sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3618{
3619 int min;
3620 char *e;
3621 if (strncmp(buf, "system", 6)==0) {
3622 mddev->sync_speed_min = 0;
3623 return len;
3624 }
3625 min = simple_strtoul(buf, &e, 10);
3626 if (buf == e || (*e && *e != '\n') || min <= 0)
3627 return -EINVAL;
3628 mddev->sync_speed_min = min;
3629 return len;
3630}
3631
3632static struct md_sysfs_entry md_sync_min =
3633__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3634
3635static ssize_t
3636sync_max_show(mddev_t *mddev, char *page)
3637{
3638 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3639 mddev->sync_speed_max ? "local": "system");
3640}
3641
3642static ssize_t
3643sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3644{
3645 int max;
3646 char *e;
3647 if (strncmp(buf, "system", 6)==0) {
3648 mddev->sync_speed_max = 0;
3649 return len;
3650 }
3651 max = simple_strtoul(buf, &e, 10);
3652 if (buf == e || (*e && *e != '\n') || max <= 0)
3653 return -EINVAL;
3654 mddev->sync_speed_max = max;
3655 return len;
3656}
3657
3658static struct md_sysfs_entry md_sync_max =
3659__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3660
Iustin Popd7f3d292007-10-16 23:30:54 -07003661static ssize_t
3662degraded_show(mddev_t *mddev, char *page)
3663{
3664 return sprintf(page, "%d\n", mddev->degraded);
3665}
3666static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003667
3668static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003669sync_force_parallel_show(mddev_t *mddev, char *page)
3670{
3671 return sprintf(page, "%d\n", mddev->parallel_resync);
3672}
3673
3674static ssize_t
3675sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3676{
3677 long n;
3678
3679 if (strict_strtol(buf, 10, &n))
3680 return -EINVAL;
3681
3682 if (n != 0 && n != 1)
3683 return -EINVAL;
3684
3685 mddev->parallel_resync = n;
3686
3687 if (mddev->sync_thread)
3688 wake_up(&resync_wait);
3689
3690 return len;
3691}
3692
3693/* force parallel resync, even with shared block devices */
3694static struct md_sysfs_entry md_sync_force_parallel =
3695__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3696 sync_force_parallel_show, sync_force_parallel_store);
3697
3698static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003699sync_speed_show(mddev_t *mddev, char *page)
3700{
3701 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003702 if (mddev->curr_resync == 0)
3703 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003704 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3705 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003706 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003707 db = resync - mddev->resync_mark_cnt;
3708 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003709}
3710
NeilBrown80ca3a42006-07-10 04:44:18 -07003711static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003712
3713static ssize_t
3714sync_completed_show(mddev_t *mddev, char *page)
3715{
Andre Noll58c0fed2009-03-31 14:33:13 +11003716 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003717
NeilBrownacb180b2009-04-14 16:28:34 +10003718 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3719 return sprintf(page, "none\n");
3720
NeilBrown88202a02006-01-06 00:21:36 -08003721 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003722 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003723 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003724 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003725
NeilBrownacb180b2009-04-14 16:28:34 +10003726 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003727 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003728}
3729
NeilBrown80ca3a42006-07-10 04:44:18 -07003730static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003731
NeilBrowne464eaf2006-03-27 01:18:14 -08003732static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003733min_sync_show(mddev_t *mddev, char *page)
3734{
3735 return sprintf(page, "%llu\n",
3736 (unsigned long long)mddev->resync_min);
3737}
3738static ssize_t
3739min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3740{
3741 unsigned long long min;
3742 if (strict_strtoull(buf, 10, &min))
3743 return -EINVAL;
3744 if (min > mddev->resync_max)
3745 return -EINVAL;
3746 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3747 return -EBUSY;
3748
3749 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003750 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003751 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003752 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003753 return -EINVAL;
3754 }
3755 mddev->resync_min = min;
3756
3757 return len;
3758}
3759
3760static struct md_sysfs_entry md_min_sync =
3761__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3762
3763static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003764max_sync_show(mddev_t *mddev, char *page)
3765{
3766 if (mddev->resync_max == MaxSector)
3767 return sprintf(page, "max\n");
3768 else
3769 return sprintf(page, "%llu\n",
3770 (unsigned long long)mddev->resync_max);
3771}
3772static ssize_t
3773max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3774{
3775 if (strncmp(buf, "max", 3) == 0)
3776 mddev->resync_max = MaxSector;
3777 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003778 unsigned long long max;
3779 if (strict_strtoull(buf, 10, &max))
3780 return -EINVAL;
3781 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003782 return -EINVAL;
3783 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003784 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003785 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3786 return -EBUSY;
3787
3788 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003789 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003790 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003791 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003792 return -EINVAL;
3793 }
3794 mddev->resync_max = max;
3795 }
3796 wake_up(&mddev->recovery_wait);
3797 return len;
3798}
3799
3800static struct md_sysfs_entry md_max_sync =
3801__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3802
3803static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003804suspend_lo_show(mddev_t *mddev, char *page)
3805{
3806 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3807}
3808
3809static ssize_t
3810suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3811{
3812 char *e;
3813 unsigned long long new = simple_strtoull(buf, &e, 10);
3814
NeilBrownb8d966e2009-07-01 11:14:04 +10003815 if (mddev->pers == NULL ||
3816 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003817 return -EINVAL;
3818 if (buf == e || (*e && *e != '\n'))
3819 return -EINVAL;
3820 if (new >= mddev->suspend_hi ||
3821 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3822 mddev->suspend_lo = new;
3823 mddev->pers->quiesce(mddev, 2);
3824 return len;
3825 } else
3826 return -EINVAL;
3827}
3828static struct md_sysfs_entry md_suspend_lo =
3829__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3830
3831
3832static ssize_t
3833suspend_hi_show(mddev_t *mddev, char *page)
3834{
3835 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3836}
3837
3838static ssize_t
3839suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3840{
3841 char *e;
3842 unsigned long long new = simple_strtoull(buf, &e, 10);
3843
NeilBrownb8d966e2009-07-01 11:14:04 +10003844 if (mddev->pers == NULL ||
3845 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003846 return -EINVAL;
3847 if (buf == e || (*e && *e != '\n'))
3848 return -EINVAL;
3849 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3850 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3851 mddev->suspend_hi = new;
3852 mddev->pers->quiesce(mddev, 1);
3853 mddev->pers->quiesce(mddev, 0);
3854 return len;
3855 } else
3856 return -EINVAL;
3857}
3858static struct md_sysfs_entry md_suspend_hi =
3859__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3860
NeilBrown08a02ec2007-05-09 02:35:38 -07003861static ssize_t
3862reshape_position_show(mddev_t *mddev, char *page)
3863{
3864 if (mddev->reshape_position != MaxSector)
3865 return sprintf(page, "%llu\n",
3866 (unsigned long long)mddev->reshape_position);
3867 strcpy(page, "none\n");
3868 return 5;
3869}
3870
3871static ssize_t
3872reshape_position_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 if (mddev->pers)
3877 return -EBUSY;
3878 if (buf == e || (*e && *e != '\n'))
3879 return -EINVAL;
3880 mddev->reshape_position = new;
3881 mddev->delta_disks = 0;
3882 mddev->new_level = mddev->level;
3883 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003884 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07003885 return len;
3886}
3887
3888static struct md_sysfs_entry md_reshape_position =
3889__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3890 reshape_position_store);
3891
Dan Williamsb522adc2009-03-31 15:00:31 +11003892static ssize_t
3893array_size_show(mddev_t *mddev, char *page)
3894{
3895 if (mddev->external_size)
3896 return sprintf(page, "%llu\n",
3897 (unsigned long long)mddev->array_sectors/2);
3898 else
3899 return sprintf(page, "default\n");
3900}
3901
3902static ssize_t
3903array_size_store(mddev_t *mddev, const char *buf, size_t len)
3904{
3905 sector_t sectors;
3906
3907 if (strncmp(buf, "default", 7) == 0) {
3908 if (mddev->pers)
3909 sectors = mddev->pers->size(mddev, 0, 0);
3910 else
3911 sectors = mddev->array_sectors;
3912
3913 mddev->external_size = 0;
3914 } else {
3915 if (strict_blocks_to_sectors(buf, &sectors) < 0)
3916 return -EINVAL;
3917 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10003918 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11003919
3920 mddev->external_size = 1;
3921 }
3922
3923 mddev->array_sectors = sectors;
3924 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10003925 if (mddev->pers)
3926 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11003927
3928 return len;
3929}
3930
3931static struct md_sysfs_entry md_array_size =
3932__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
3933 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08003934
NeilBrowneae17012005-11-08 21:39:23 -08003935static struct attribute *md_default_attrs[] = {
3936 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003937 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003938 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003939 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003940 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003941 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003942 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08003943 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003944 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003945 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003946 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11003947 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11003948 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003949 NULL,
3950};
3951
3952static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003953 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003954 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003955 &md_sync_min.attr,
3956 &md_sync_max.attr,
3957 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003958 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003959 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003960 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003961 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003962 &md_suspend_lo.attr,
3963 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003964 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003965 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003966 NULL,
3967};
NeilBrown411036f2005-11-08 21:39:40 -08003968static struct attribute_group md_redundancy_group = {
3969 .name = NULL,
3970 .attrs = md_redundancy_attrs,
3971};
3972
NeilBrowneae17012005-11-08 21:39:23 -08003973
3974static ssize_t
3975md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3976{
3977 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3978 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003979 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003980
3981 if (!entry->show)
3982 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003983 rv = mddev_lock(mddev);
3984 if (!rv) {
3985 rv = entry->show(mddev, page);
3986 mddev_unlock(mddev);
3987 }
NeilBrown96de1e62005-11-08 21:39:39 -08003988 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003989}
3990
3991static ssize_t
3992md_attr_store(struct kobject *kobj, struct attribute *attr,
3993 const char *page, size_t length)
3994{
3995 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3996 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003997 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003998
3999 if (!entry->store)
4000 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004001 if (!capable(CAP_SYS_ADMIN))
4002 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004003 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004004 if (mddev->hold_active == UNTIL_IOCTL)
4005 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004006 if (!rv) {
4007 rv = entry->store(mddev, page, length);
4008 mddev_unlock(mddev);
4009 }
NeilBrown96de1e62005-11-08 21:39:39 -08004010 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004011}
4012
4013static void md_free(struct kobject *ko)
4014{
4015 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004016
4017 if (mddev->sysfs_state)
4018 sysfs_put(mddev->sysfs_state);
4019
4020 if (mddev->gendisk) {
4021 del_gendisk(mddev->gendisk);
4022 put_disk(mddev->gendisk);
4023 }
4024 if (mddev->queue)
4025 blk_cleanup_queue(mddev->queue);
4026
NeilBrowneae17012005-11-08 21:39:23 -08004027 kfree(mddev);
4028}
4029
4030static struct sysfs_ops md_sysfs_ops = {
4031 .show = md_attr_show,
4032 .store = md_attr_store,
4033};
4034static struct kobj_type md_ktype = {
4035 .release = md_free,
4036 .sysfs_ops = &md_sysfs_ops,
4037 .default_attrs = md_default_attrs,
4038};
4039
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040int mdp_major = 0;
4041
Dan Williams5fd3a172009-03-04 00:57:25 -07004042static void mddev_delayed_delete(struct work_struct *ws)
4043{
4044 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4045
4046 if (mddev->private == &md_redundancy_group) {
4047 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
4048 if (mddev->sysfs_action)
4049 sysfs_put(mddev->sysfs_action);
4050 mddev->sysfs_action = NULL;
4051 mddev->private = NULL;
4052 }
NeilBrown43a70502009-12-14 12:49:55 +11004053 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004054 kobject_del(&mddev->kobj);
4055 kobject_put(&mddev->kobj);
4056}
4057
NeilBrownefeb53c2009-01-09 08:31:10 +11004058static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004060 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 mddev_t *mddev = mddev_find(dev);
4062 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004063 int partitioned;
4064 int shift;
4065 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004066 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067
4068 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004069 return -ENODEV;
4070
4071 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4072 shift = partitioned ? MdpMinorShift : 0;
4073 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
NeilBrownd3374822009-01-09 08:31:10 +11004075 /* wait for any previous instance if this device
4076 * to be completed removed (mddev_delayed_delete).
4077 */
4078 flush_scheduled_work();
4079
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004080 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004081 error = -EEXIST;
4082 if (mddev->gendisk)
4083 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004084
4085 if (name) {
4086 /* Need to ensure that 'name' is not a duplicate.
4087 */
4088 mddev_t *mddev2;
4089 spin_lock(&all_mddevs_lock);
4090
4091 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4092 if (mddev2->gendisk &&
4093 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4094 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004095 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004096 }
4097 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 }
NeilBrown8b765392009-01-09 08:31:08 +11004099
NeilBrown0909dc42009-07-01 12:27:21 +10004100 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004101 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004102 if (!mddev->queue)
4103 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004104 mddev->queue->queuedata = mddev;
4105
NeilBrown8b765392009-01-09 08:31:08 +11004106 /* Can be unlocked because the queue is new: no concurrency */
4107 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4108
NeilBrown409c57f2009-03-31 14:39:39 +11004109 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004110
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 disk = alloc_disk(1 << shift);
4112 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004113 blk_cleanup_queue(mddev->queue);
4114 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004115 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004117 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004119 if (name)
4120 strcpy(disk->disk_name, name);
4121 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004123 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 disk->fops = &md_fops;
4126 disk->private_data = mddev;
4127 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004128 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004129 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004130 * remove it now.
4131 */
4132 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 add_disk(disk);
4134 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004135 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4136 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004137 if (error) {
4138 /* This isn't possible, but as kobject_init_and_add is marked
4139 * __must_check, we must do something with the result
4140 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004141 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4142 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004143 error = 0;
4144 }
NeilBrown43a70502009-12-14 12:49:55 +11004145 if (sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4146 printk(KERN_DEBUG "pointless warning\n");
NeilBrown0909dc42009-07-01 12:27:21 +10004147 abort:
4148 mutex_unlock(&disks_mutex);
4149 if (!error) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004150 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11004151 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
4152 }
NeilBrownd3374822009-01-09 08:31:10 +11004153 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004154 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004155}
4156
4157static struct kobject *md_probe(dev_t dev, int *part, void *data)
4158{
4159 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 return NULL;
4161}
4162
NeilBrownefeb53c2009-01-09 08:31:10 +11004163static int add_named_array(const char *val, struct kernel_param *kp)
4164{
4165 /* val must be "md_*" where * is not all digits.
4166 * We allocate an array with a large free minor number, and
4167 * set the name to val. val must not already be an active name.
4168 */
4169 int len = strlen(val);
4170 char buf[DISK_NAME_LEN];
4171
4172 while (len && val[len-1] == '\n')
4173 len--;
4174 if (len >= DISK_NAME_LEN)
4175 return -E2BIG;
4176 strlcpy(buf, val, len+1);
4177 if (strncmp(buf, "md_", 3) != 0)
4178 return -EINVAL;
4179 return md_alloc(0, buf);
4180}
4181
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182static void md_safemode_timeout(unsigned long data)
4183{
4184 mddev_t *mddev = (mddev_t *) data;
4185
Neil Brown0fd62b82008-06-28 08:31:36 +10004186 if (!atomic_read(&mddev->writes_pending)) {
4187 mddev->safemode = 1;
4188 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11004189 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 md_wakeup_thread(mddev->thread);
4192}
4193
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004194static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
4196static int do_md_run(mddev_t * mddev)
4197{
NeilBrown2604b702006-01-06 00:20:36 -08004198 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 mdk_rdev_t *rdev;
4200 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08004201 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
NeilBrowna757e642005-04-16 15:26:42 -07004203 if (list_empty(&mddev->disks))
4204 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206
4207 if (mddev->pers)
4208 return -EBUSY;
4209
4210 /*
4211 * Analyze all RAID superblock(s)
4212 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004213 if (!mddev->raid_disks) {
4214 if (!mddev->persistent)
4215 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004216 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
NeilBrownd9d166c2006-01-06 00:20:51 -08004219 if (mddev->level != LEVEL_NONE)
4220 request_module("md-level-%d", mddev->level);
4221 else if (mddev->clevel[0])
4222 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223
4224 /*
4225 * Drop all container device buffers, from now on
4226 * the only valid external interface is through the md
4227 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004229 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004230 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 continue;
4232 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004233 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004234
4235 /* perform some consistency tests on the device.
4236 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004237 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004238 */
Andre Noll0f420352008-07-11 22:02:23 +10004239 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004240 if (mddev->dev_sectors &&
4241 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004242 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004243 printk("md: %s: data overlaps metadata\n",
4244 mdname(mddev));
4245 return -EINVAL;
4246 }
4247 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004248 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004249 > rdev->data_offset) {
4250 printk("md: %s: metadata overlaps data\n",
4251 mdname(mddev));
4252 return -EINVAL;
4253 }
4254 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004255 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 }
4257
4258 md_probe(mddev->unit, NULL, NULL);
4259 disk = mddev->gendisk;
4260 if (!disk)
4261 return -ENOMEM;
4262
4263 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004264 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004265 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004267 if (mddev->level != LEVEL_NONE)
4268 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4269 mddev->level);
4270 else
4271 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4272 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 return -EINVAL;
4274 }
NeilBrown2604b702006-01-06 00:20:36 -08004275 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004277 if (mddev->level != pers->level) {
4278 mddev->level = pers->level;
4279 mddev->new_level = pers->level;
4280 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004281 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282
NeilBrownf6705572006-03-27 01:18:11 -08004283 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004284 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004285 /* This personality cannot handle reshaping... */
4286 mddev->pers = NULL;
4287 module_put(pers->owner);
4288 return -EINVAL;
4289 }
4290
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004291 if (pers->sync_request) {
4292 /* Warn if this is a potentially silly
4293 * configuration.
4294 */
4295 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4296 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004297 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004298
4299 list_for_each_entry(rdev, &mddev->disks, same_set)
4300 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004301 if (rdev < rdev2 &&
4302 rdev->bdev->bd_contains ==
4303 rdev2->bdev->bd_contains) {
4304 printk(KERN_WARNING
4305 "%s: WARNING: %s appears to be"
4306 " on the same physical disk as"
4307 " %s.\n",
4308 mdname(mddev),
4309 bdevname(rdev->bdev,b),
4310 bdevname(rdev2->bdev,b2));
4311 warned = 1;
4312 }
4313 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004314
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004315 if (warned)
4316 printk(KERN_WARNING
4317 "True protection against single-disk"
4318 " failure might be compromised.\n");
4319 }
4320
NeilBrown657390d2005-08-26 18:34:16 -07004321 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004322 /* may be over-ridden by personality */
4323 mddev->resync_max_sectors = mddev->dev_sectors;
4324
NeilBrowna9701a32005-11-08 21:39:34 -08004325 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004326 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
NeilBrownf91de922005-11-08 21:39:36 -08004328 if (start_readonly)
4329 mddev->ro = 2; /* read-only, but switch on first write */
4330
NeilBrownb15c2e52006-01-06 00:20:16 -08004331 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004332 if (err)
4333 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004334 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4335 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4336 " but 'external_size' not in effect?\n", __func__);
4337 printk(KERN_ERR
4338 "md: invalid array_size %llu > default size %llu\n",
4339 (unsigned long long)mddev->array_sectors / 2,
4340 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4341 err = -EINVAL;
4342 mddev->pers->stop(mddev);
4343 }
4344 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004345 err = bitmap_create(mddev);
4346 if (err) {
4347 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4348 mdname(mddev), err);
4349 mddev->pers->stop(mddev);
4350 }
4351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 module_put(mddev->pers->owner);
4354 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004355 bitmap_destroy(mddev);
4356 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004358 if (mddev->pers->sync_request) {
4359 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4360 printk(KERN_WARNING
4361 "md: cannot register extra attributes for %s\n",
4362 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004363 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004364 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004365 mddev->ro = 0;
4366
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004368 atomic_set(&mddev->max_corr_read_errors,
4369 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 mddev->safemode = 0;
4371 mddev->safemode_timer.function = md_safemode_timeout;
4372 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004373 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004375
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004376 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004377 if (rdev->raid_disk >= 0) {
4378 char nm[20];
4379 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004380 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4381 printk("md: cannot register %s for %s\n",
4382 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
4385 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4386
NeilBrown850b2b42006-10-03 01:15:46 -07004387 if (mddev->flags)
4388 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389
Andre Nollf233ea52008-07-21 17:05:22 +10004390 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004392 /* If there is a partially-recovered drive we need to
4393 * start recovery here. If we leave it to md_check_recovery,
4394 * it will remove the drives and not do the right thing
4395 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07004396 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004397 int spares = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004398 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004399 if (rdev->raid_disk >= 0 &&
4400 !test_bit(In_sync, &rdev->flags) &&
4401 !test_bit(Faulty, &rdev->flags))
4402 /* complete an interrupted recovery */
4403 spares++;
4404 if (spares && mddev->pers->sync_request) {
4405 mddev->recovery = 0;
4406 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4407 mddev->sync_thread = md_register_thread(md_do_sync,
4408 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10004409 "resync");
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004410 if (!mddev->sync_thread) {
4411 printk(KERN_ERR "%s: could not start resync"
4412 " thread...\n",
4413 mdname(mddev));
4414 /* leave the spares where they are, it shouldn't hurt */
4415 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07004416 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004417 }
4418 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07004419 md_wakeup_thread(mddev->thread);
4420 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004421
NeilBrown449aad32009-08-03 10:59:58 +10004422 revalidate_disk(mddev->gendisk);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004423 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08004424 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004425 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004426 if (mddev->sysfs_action)
4427 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004428 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09004429 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 return 0;
4431}
4432
4433static int restart_array(mddev_t *mddev)
4434{
4435 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436
Andre Noll80fab1d2008-07-11 22:02:21 +10004437 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004439 return -ENXIO;
4440 if (!mddev->pers)
4441 return -EINVAL;
4442 if (!mddev->ro)
4443 return -EBUSY;
4444 mddev->safemode = 0;
4445 mddev->ro = 0;
4446 set_disk_ro(disk, 0);
4447 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4448 mdname(mddev));
4449 /* Kick recovery or resync if necessary */
4450 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4451 md_wakeup_thread(mddev->thread);
4452 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004453 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455}
4456
NeilBrownacc55e22006-06-26 00:27:47 -07004457/* similar to deny_write_access, but accounts for our holding a reference
4458 * to the file ourselves */
4459static int deny_bitmap_write_access(struct file * file)
4460{
4461 struct inode *inode = file->f_mapping->host;
4462
4463 spin_lock(&inode->i_lock);
4464 if (atomic_read(&inode->i_writecount) > 1) {
4465 spin_unlock(&inode->i_lock);
4466 return -ETXTBSY;
4467 }
4468 atomic_set(&inode->i_writecount, -1);
4469 spin_unlock(&inode->i_lock);
4470
4471 return 0;
4472}
4473
NeilBrown43a70502009-12-14 12:49:55 +11004474void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004475{
4476 struct inode *inode = file->f_mapping->host;
4477
4478 spin_lock(&inode->i_lock);
4479 atomic_set(&inode->i_writecount, 1);
4480 spin_unlock(&inode->i_lock);
4481}
4482
NeilBrown9e653b62006-06-26 00:27:58 -07004483/* mode:
4484 * 0 - completely stop and dis-assemble array
4485 * 1 - switch to readonly
4486 * 2 - stop but do not disassemble array
4487 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004488static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489{
4490 int err = 0;
4491 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004492 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493
NeilBrownc8c00a62009-08-10 12:50:52 +10004494 mutex_lock(&mddev->open_mutex);
NeilBrownf2ea68c2008-07-21 17:05:25 +10004495 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004496 printk("md: %s still in use.\n",mdname(mddev));
NeilBrownc8c00a62009-08-10 12:50:52 +10004497 err = -EBUSY;
4498 } else if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499
4500 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004501 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4503 md_unregister_thread(mddev->sync_thread);
4504 mddev->sync_thread = NULL;
4505 }
4506
4507 del_timer_sync(&mddev->safemode_timer);
4508
NeilBrown9e653b62006-06-26 00:27:58 -07004509 switch(mode) {
4510 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004512 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 goto out;
4514 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004515 break;
4516 case 0: /* disassemble */
4517 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004518 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004519 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 if (mddev->ro)
4521 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004522
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004524 mddev->queue->merge_bvec_fn = NULL;
4525 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004526 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 module_put(mddev->pers->owner);
Dan Williams5fd3a172009-03-04 00:57:25 -07004528 if (mddev->pers->sync_request)
4529 mddev->private = &md_redundancy_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004531 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004532 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004533
NeilBrownc4647292009-05-07 12:51:06 +10004534 list_for_each_entry(rdev, &mddev->disks, same_set)
4535 if (rdev->raid_disk >= 0) {
4536 char nm[20];
4537 sprintf(nm, "rd%d", rdev->raid_disk);
4538 sysfs_remove_link(&mddev->kobj, nm);
4539 }
4540
NeilBrown0d4ca602006-12-10 02:20:44 -08004541 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004542 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08004543
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 if (mddev->ro)
4545 mddev->ro = 0;
4546 }
NeilBrown850b2b42006-10-03 01:15:46 -07004547 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 /* mark array as shutdown cleanly */
4549 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07004550 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 }
NeilBrown9e653b62006-06-26 00:27:58 -07004552 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004554 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown80ffb3c2009-08-18 10:35:26 +10004555 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 }
NeilBrownc8c00a62009-08-10 12:50:52 +10004557out:
4558 mutex_unlock(&mddev->open_mutex);
4559 if (err)
4560 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 /*
4562 * Free resources if final stop
4563 */
NeilBrown9e653b62006-06-26 00:27:58 -07004564 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004565
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4567
NeilBrown978f9462006-02-02 14:28:05 -08004568 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004569 if (mddev->bitmap_info.file) {
4570 restore_bitmap_write_access(mddev->bitmap_info.file);
4571 fput(mddev->bitmap_info.file);
4572 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004573 }
NeilBrownc3d97142009-12-14 12:49:52 +11004574 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004575
NeilBrown177a99b2008-02-06 01:39:56 -08004576 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07004577 flush_scheduled_work();
4578
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 export_array(mddev);
4580
Andre Nollf233ea52008-07-21 17:05:22 +10004581 mddev->array_sectors = 0;
Dan Williamsb522adc2009-03-31 15:00:31 +11004582 mddev->external_size = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004583 mddev->dev_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004584 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004585 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004586 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004587 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004588 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004589 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004590 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004591 mddev->level = LEVEL_NONE;
4592 mddev->clevel[0] = 0;
4593 mddev->flags = 0;
4594 mddev->ro = 0;
4595 mddev->metadata_type[0] = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10004596 mddev->chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004597 mddev->ctime = mddev->utime = 0;
4598 mddev->layout = 0;
4599 mddev->max_disks = 0;
4600 mddev->events = 0;
4601 mddev->delta_disks = 0;
4602 mddev->new_level = LEVEL_NONE;
4603 mddev->new_layout = 0;
Andre Noll664e7c42009-06-18 08:45:27 +10004604 mddev->new_chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004605 mddev->curr_resync = 0;
4606 mddev->resync_mismatches = 0;
4607 mddev->suspend_lo = mddev->suspend_hi = 0;
4608 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4609 mddev->recovery = 0;
4610 mddev->in_sync = 0;
4611 mddev->changed = 0;
4612 mddev->degraded = 0;
4613 mddev->barriers_work = 0;
4614 mddev->safemode = 0;
NeilBrownc3d97142009-12-14 12:49:52 +11004615 mddev->bitmap_info.offset = 0;
4616 mddev->bitmap_info.default_offset = 0;
NeilBrown42a04b52009-12-14 12:49:53 +11004617 mddev->bitmap_info.chunksize = 0;
4618 mddev->bitmap_info.daemon_sleep = 0;
4619 mddev->bitmap_info.max_write_behind = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004620 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004621 if (mddev->hold_active == UNTIL_STOP)
4622 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004623
NeilBrowna8a55c32006-06-26 00:27:39 -07004624 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4626 mdname(mddev));
4627 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004628 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004629 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004630 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 return err;
4632}
4633
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004634#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635static void autorun_array(mddev_t *mddev)
4636{
4637 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 int err;
4639
NeilBrowna757e642005-04-16 15:26:42 -07004640 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
4643 printk(KERN_INFO "md: running: ");
4644
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004645 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 char b[BDEVNAME_SIZE];
4647 printk("<%s>", bdevname(rdev->bdev,b));
4648 }
4649 printk("\n");
4650
NeilBrownd710e132008-10-13 11:55:12 +11004651 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 if (err) {
4653 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004654 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 }
4656}
4657
4658/*
4659 * lets try to run arrays based on all disks that have arrived
4660 * until now. (those are in pending_raid_disks)
4661 *
4662 * the method: pick the first pending disk, collect all disks with
4663 * the same UUID, remove all from the pending list and put them into
4664 * the 'same_array' list. Then order this list based on superblock
4665 * update time (freshest comes first), kick out 'old' disks and
4666 * compare superblocks. If everything's fine then run it.
4667 *
4668 * If "unit" is allocated, then bump its reference count
4669 */
4670static void autorun_devices(int part)
4671{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004672 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 mddev_t *mddev;
4674 char b[BDEVNAME_SIZE];
4675
4676 printk(KERN_INFO "md: autorun ...\n");
4677 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004678 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004680 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 rdev0 = list_entry(pending_raid_disks.next,
4682 mdk_rdev_t, same_set);
4683
4684 printk(KERN_INFO "md: considering %s ...\n",
4685 bdevname(rdev0->bdev,b));
4686 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004687 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 if (super_90_load(rdev, rdev0, 0) >= 0) {
4689 printk(KERN_INFO "md: adding %s ...\n",
4690 bdevname(rdev->bdev,b));
4691 list_move(&rdev->same_set, &candidates);
4692 }
4693 /*
4694 * now we have a set of devices, with all of them having
4695 * mostly sane superblocks. It's time to allocate the
4696 * mddev.
4697 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004698 if (part) {
4699 dev = MKDEV(mdp_major,
4700 rdev0->preferred_minor << MdpMinorShift);
4701 unit = MINOR(dev) >> MdpMinorShift;
4702 } else {
4703 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4704 unit = MINOR(dev);
4705 }
4706 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4708 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4709 break;
4710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711
4712 md_probe(dev, NULL, NULL);
4713 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004714 if (!mddev || !mddev->gendisk) {
4715 if (mddev)
4716 mddev_put(mddev);
4717 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 "md: cannot allocate memory for md drive.\n");
4719 break;
4720 }
4721 if (mddev_lock(mddev))
4722 printk(KERN_WARNING "md: %s locked, cannot run\n",
4723 mdname(mddev));
4724 else if (mddev->raid_disks || mddev->major_version
4725 || !list_empty(&mddev->disks)) {
4726 printk(KERN_WARNING
4727 "md: %s already running, cannot run %s\n",
4728 mdname(mddev), bdevname(rdev0->bdev,b));
4729 mddev_unlock(mddev);
4730 } else {
4731 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004732 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004733 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 list_del_init(&rdev->same_set);
4735 if (bind_rdev_to_array(rdev, mddev))
4736 export_rdev(rdev);
4737 }
4738 autorun_array(mddev);
4739 mddev_unlock(mddev);
4740 }
4741 /* on success, candidates will be empty, on error
4742 * it won't...
4743 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004744 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004745 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 mddev_put(mddev);
4749 }
4750 printk(KERN_INFO "md: ... autorun DONE.\n");
4751}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004752#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754static int get_version(void __user * arg)
4755{
4756 mdu_version_t ver;
4757
4758 ver.major = MD_MAJOR_VERSION;
4759 ver.minor = MD_MINOR_VERSION;
4760 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4761
4762 if (copy_to_user(arg, &ver, sizeof(ver)))
4763 return -EFAULT;
4764
4765 return 0;
4766}
4767
4768static int get_array_info(mddev_t * mddev, void __user * arg)
4769{
4770 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004771 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773
NeilBrowna9f326e2009-09-23 18:06:41 +10004774 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004775 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004777 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 failed++;
4779 else {
4780 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004781 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004782 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 else
4784 spare++;
4785 }
4786 }
4787
4788 info.major_version = mddev->major_version;
4789 info.minor_version = mddev->minor_version;
4790 info.patch_version = MD_PATCHLEVEL_VERSION;
4791 info.ctime = mddev->ctime;
4792 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004793 info.size = mddev->dev_sectors / 2;
4794 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004795 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 info.nr_disks = nr;
4797 info.raid_disks = mddev->raid_disks;
4798 info.md_minor = mddev->md_minor;
4799 info.not_persistent= !mddev->persistent;
4800
4801 info.utime = mddev->utime;
4802 info.state = 0;
4803 if (mddev->in_sync)
4804 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11004805 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07004806 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004807 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 info.working_disks = working;
4809 info.failed_disks = failed;
4810 info.spare_disks = spare;
4811
4812 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004813 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814
4815 if (copy_to_user(arg, &info, sizeof(info)))
4816 return -EFAULT;
4817
4818 return 0;
4819}
4820
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004821static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004822{
4823 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4824 char *ptr, *buf = NULL;
4825 int err = -ENOMEM;
4826
Dan Williamsb5470dc2008-06-27 21:44:04 -07004827 if (md_allow_write(mddev))
4828 file = kmalloc(sizeof(*file), GFP_NOIO);
4829 else
4830 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004831
NeilBrown32a76272005-06-21 17:17:14 -07004832 if (!file)
4833 goto out;
4834
4835 /* bitmap disabled, zero the first byte and copy out */
4836 if (!mddev->bitmap || !mddev->bitmap->file) {
4837 file->pathname[0] = '\0';
4838 goto copy_out;
4839 }
4840
4841 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4842 if (!buf)
4843 goto out;
4844
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004845 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4846 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004847 goto out;
4848
4849 strcpy(file->pathname, ptr);
4850
4851copy_out:
4852 err = 0;
4853 if (copy_to_user(arg, file, sizeof(*file)))
4854 err = -EFAULT;
4855out:
4856 kfree(buf);
4857 kfree(file);
4858 return err;
4859}
4860
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861static int get_disk_info(mddev_t * mddev, void __user * arg)
4862{
4863 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 mdk_rdev_t *rdev;
4865
4866 if (copy_from_user(&info, arg, sizeof(info)))
4867 return -EFAULT;
4868
Andre Noll26ef3792008-07-11 22:02:21 +10004869 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 if (rdev) {
4871 info.major = MAJOR(rdev->bdev->bd_dev);
4872 info.minor = MINOR(rdev->bdev->bd_dev);
4873 info.raid_disk = rdev->raid_disk;
4874 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004875 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004877 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 info.state |= (1<<MD_DISK_ACTIVE);
4879 info.state |= (1<<MD_DISK_SYNC);
4880 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004881 if (test_bit(WriteMostly, &rdev->flags))
4882 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 } else {
4884 info.major = info.minor = 0;
4885 info.raid_disk = -1;
4886 info.state = (1<<MD_DISK_REMOVED);
4887 }
4888
4889 if (copy_to_user(arg, &info, sizeof(info)))
4890 return -EFAULT;
4891
4892 return 0;
4893}
4894
4895static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4896{
4897 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4898 mdk_rdev_t *rdev;
4899 dev_t dev = MKDEV(info->major,info->minor);
4900
4901 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4902 return -EOVERFLOW;
4903
4904 if (!mddev->raid_disks) {
4905 int err;
4906 /* expecting a device which has a superblock */
4907 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4908 if (IS_ERR(rdev)) {
4909 printk(KERN_WARNING
4910 "md: md_import_device returned %ld\n",
4911 PTR_ERR(rdev));
4912 return PTR_ERR(rdev);
4913 }
4914 if (!list_empty(&mddev->disks)) {
4915 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4916 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10004917 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 .load_super(rdev, rdev0, mddev->minor_version);
4919 if (err < 0) {
4920 printk(KERN_WARNING
4921 "md: %s has different UUID to %s\n",
4922 bdevname(rdev->bdev,b),
4923 bdevname(rdev0->bdev,b2));
4924 export_rdev(rdev);
4925 return -EINVAL;
4926 }
4927 }
4928 err = bind_rdev_to_array(rdev, mddev);
4929 if (err)
4930 export_rdev(rdev);
4931 return err;
4932 }
4933
4934 /*
4935 * add_new_disk can be used once the array is assembled
4936 * to add "hot spares". They must already have a superblock
4937 * written
4938 */
4939 if (mddev->pers) {
4940 int err;
4941 if (!mddev->pers->hot_add_disk) {
4942 printk(KERN_WARNING
4943 "%s: personality does not support diskops!\n",
4944 mdname(mddev));
4945 return -EINVAL;
4946 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004947 if (mddev->persistent)
4948 rdev = md_import_device(dev, mddev->major_version,
4949 mddev->minor_version);
4950 else
4951 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 if (IS_ERR(rdev)) {
4953 printk(KERN_WARNING
4954 "md: md_import_device returned %ld\n",
4955 PTR_ERR(rdev));
4956 return PTR_ERR(rdev);
4957 }
NeilBrown41158c72005-06-21 17:17:25 -07004958 /* set save_raid_disk if appropriate */
4959 if (!mddev->persistent) {
4960 if (info->state & (1<<MD_DISK_SYNC) &&
4961 info->raid_disk < mddev->raid_disks)
4962 rdev->raid_disk = info->raid_disk;
4963 else
4964 rdev->raid_disk = -1;
4965 } else
4966 super_types[mddev->major_version].
4967 validate_super(mddev, rdev);
4968 rdev->saved_raid_disk = rdev->raid_disk;
4969
NeilBrownb2d444d2005-11-08 21:39:31 -08004970 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004971 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4972 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11004973 else
4974 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004975
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 rdev->raid_disk = -1;
4977 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004978 if (!err && !mddev->pers->hot_remove_disk) {
4979 /* If there is hot_add_disk but no hot_remove_disk
4980 * then added disks for geometry changes,
4981 * and should be added immediately.
4982 */
4983 super_types[mddev->major_version].
4984 validate_super(mddev, rdev);
4985 err = mddev->pers->hot_add_disk(mddev, rdev);
4986 if (err)
4987 unbind_rdev_from_array(rdev);
4988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 if (err)
4990 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004991 else
NeilBrown3c0ee632008-10-21 13:25:28 +11004992 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07004993
NeilBrown17571282006-12-10 02:20:52 -08004994 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004995 if (mddev->degraded)
4996 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004997 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004998 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 return err;
5000 }
5001
5002 /* otherwise, add_new_disk is only allowed
5003 * for major_version==0 superblocks
5004 */
5005 if (mddev->major_version != 0) {
5006 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5007 mdname(mddev));
5008 return -EINVAL;
5009 }
5010
5011 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5012 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005013 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 if (IS_ERR(rdev)) {
5015 printk(KERN_WARNING
5016 "md: error, md_import_device() returned %ld\n",
5017 PTR_ERR(rdev));
5018 return PTR_ERR(rdev);
5019 }
5020 rdev->desc_nr = info->number;
5021 if (info->raid_disk < mddev->raid_disks)
5022 rdev->raid_disk = info->raid_disk;
5023 else
5024 rdev->raid_disk = -1;
5025
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005027 if (info->state & (1<<MD_DISK_SYNC))
5028 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005030 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5031 set_bit(WriteMostly, &rdev->flags);
5032
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 if (!mddev->persistent) {
5034 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10005035 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 } else
Andre Noll0f420352008-07-11 22:02:23 +10005037 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10005038 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039
NeilBrown2bf071b2006-01-06 00:20:55 -08005040 err = bind_rdev_to_array(rdev, mddev);
5041 if (err) {
5042 export_rdev(rdev);
5043 return err;
5044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 }
5046
5047 return 0;
5048}
5049
5050static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5051{
5052 char b[BDEVNAME_SIZE];
5053 mdk_rdev_t *rdev;
5054
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 rdev = find_rdev(mddev, dev);
5056 if (!rdev)
5057 return -ENXIO;
5058
5059 if (rdev->raid_disk >= 0)
5060 goto busy;
5061
5062 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005063 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005064 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065
5066 return 0;
5067busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005068 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 bdevname(rdev->bdev,b), mdname(mddev));
5070 return -EBUSY;
5071}
5072
5073static int hot_add_disk(mddev_t * mddev, dev_t dev)
5074{
5075 char b[BDEVNAME_SIZE];
5076 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 mdk_rdev_t *rdev;
5078
5079 if (!mddev->pers)
5080 return -ENODEV;
5081
5082 if (mddev->major_version != 0) {
5083 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5084 " version-0 superblocks.\n",
5085 mdname(mddev));
5086 return -EINVAL;
5087 }
5088 if (!mddev->pers->hot_add_disk) {
5089 printk(KERN_WARNING
5090 "%s: personality does not support diskops!\n",
5091 mdname(mddev));
5092 return -EINVAL;
5093 }
5094
NeilBrownd710e132008-10-13 11:55:12 +11005095 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 if (IS_ERR(rdev)) {
5097 printk(KERN_WARNING
5098 "md: error, md_import_device() returned %ld\n",
5099 PTR_ERR(rdev));
5100 return -EINVAL;
5101 }
5102
5103 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10005104 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 else
Andre Noll0f420352008-07-11 22:02:23 +10005106 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
NeilBrown8190e752009-06-18 08:48:58 +10005108 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109
NeilBrownb2d444d2005-11-08 21:39:31 -08005110 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 printk(KERN_WARNING
5112 "md: can not hot-add faulty %s disk to %s!\n",
5113 bdevname(rdev->bdev,b), mdname(mddev));
5114 err = -EINVAL;
5115 goto abort_export;
5116 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005117 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005119 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005120 err = bind_rdev_to_array(rdev, mddev);
5121 if (err)
5122 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123
5124 /*
5125 * The rest should better be atomic, we can have disk failures
5126 * noticed in interrupt contexts ...
5127 */
5128
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 rdev->raid_disk = -1;
5130
NeilBrown850b2b42006-10-03 01:15:46 -07005131 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132
5133 /*
5134 * Kick recovery, maybe this spare has to be added to the
5135 * array immediately.
5136 */
5137 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5138 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005139 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 return 0;
5141
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142abort_export:
5143 export_rdev(rdev);
5144 return err;
5145}
5146
NeilBrown32a76272005-06-21 17:17:14 -07005147static int set_bitmap_file(mddev_t *mddev, int fd)
5148{
5149 int err;
5150
NeilBrown36fa3062005-09-09 16:23:45 -07005151 if (mddev->pers) {
5152 if (!mddev->pers->quiesce)
5153 return -EBUSY;
5154 if (mddev->recovery || mddev->sync_thread)
5155 return -EBUSY;
5156 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005157 }
5158
NeilBrown36fa3062005-09-09 16:23:45 -07005159
5160 if (fd >= 0) {
5161 if (mddev->bitmap)
5162 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005163 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005164
NeilBrownc3d97142009-12-14 12:49:52 +11005165 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005166 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5167 mdname(mddev));
5168 return -EBADF;
5169 }
5170
NeilBrownc3d97142009-12-14 12:49:52 +11005171 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005172 if (err) {
5173 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5174 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005175 fput(mddev->bitmap_info.file);
5176 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005177 return err;
5178 }
NeilBrownc3d97142009-12-14 12:49:52 +11005179 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005180 } else if (mddev->bitmap == NULL)
5181 return -ENOENT; /* cannot remove what isn't there */
5182 err = 0;
5183 if (mddev->pers) {
5184 mddev->pers->quiesce(mddev, 1);
5185 if (fd >= 0)
5186 err = bitmap_create(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07005187 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005188 bitmap_destroy(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07005189 fd = -1; /* make sure to put the file */
5190 }
NeilBrown36fa3062005-09-09 16:23:45 -07005191 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab322006-06-26 00:27:43 -07005192 }
5193 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005194 if (mddev->bitmap_info.file) {
5195 restore_bitmap_write_access(mddev->bitmap_info.file);
5196 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005197 }
NeilBrownc3d97142009-12-14 12:49:52 +11005198 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005199 }
5200
NeilBrown32a76272005-06-21 17:17:14 -07005201 return err;
5202}
5203
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204/*
5205 * set_array_info is used two different ways
5206 * The original usage is when creating a new array.
5207 * In this usage, raid_disks is > 0 and it together with
5208 * level, size, not_persistent,layout,chunksize determine the
5209 * shape of the array.
5210 * This will always create an array with a type-0.90.0 superblock.
5211 * The newer usage is when assembling an array.
5212 * In this case raid_disks will be 0, and the major_version field is
5213 * use to determine which style super-blocks are to be found on the devices.
5214 * The minor and patch _version numbers are also kept incase the
5215 * super_block handler wishes to interpret them.
5216 */
5217static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5218{
5219
5220 if (info->raid_disks == 0) {
5221 /* just setting version number for superblock loading */
5222 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005223 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 super_types[info->major_version].name == NULL) {
5225 /* maybe try to auto-load a module? */
5226 printk(KERN_INFO
5227 "md: superblock version %d not known\n",
5228 info->major_version);
5229 return -EINVAL;
5230 }
5231 mddev->major_version = info->major_version;
5232 mddev->minor_version = info->minor_version;
5233 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005234 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 return 0;
5236 }
5237 mddev->major_version = MD_MAJOR_VERSION;
5238 mddev->minor_version = MD_MINOR_VERSION;
5239 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5240 mddev->ctime = get_seconds();
5241
5242 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005243 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005244 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 mddev->raid_disks = info->raid_disks;
5246 /* don't set md_minor, it is determined by which /dev/md* was
5247 * openned
5248 */
5249 if (info->state & (1<<MD_SB_CLEAN))
5250 mddev->recovery_cp = MaxSector;
5251 else
5252 mddev->recovery_cp = 0;
5253 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005254 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255
5256 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005257 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258
5259 mddev->max_disks = MD_SB_DISKS;
5260
NeilBrowne6910632008-02-06 01:39:51 -08005261 if (mddev->persistent)
5262 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005263 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264
NeilBrownc3d97142009-12-14 12:49:52 +11005265 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5266 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005267
NeilBrownf6705572006-03-27 01:18:11 -08005268 mddev->reshape_position = MaxSector;
5269
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 /*
5271 * Generate a 128 bit UUID
5272 */
5273 get_random_bytes(mddev->uuid, 16);
5274
NeilBrownf6705572006-03-27 01:18:11 -08005275 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005276 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005277 mddev->new_layout = mddev->layout;
5278 mddev->delta_disks = 0;
5279
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 return 0;
5281}
5282
Dan Williams1f403622009-03-31 14:59:03 +11005283void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5284{
Dan Williamsb522adc2009-03-31 15:00:31 +11005285 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5286
5287 if (mddev->external_size)
5288 return;
5289
Dan Williams1f403622009-03-31 14:59:03 +11005290 mddev->array_sectors = array_sectors;
5291}
5292EXPORT_SYMBOL(md_set_array_sectors);
5293
Andre Nolld71f9f82008-07-11 22:02:22 +10005294static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005295{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005296 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005297 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005298 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005299
5300 if (mddev->pers->resize == NULL)
5301 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005302 /* The "num_sectors" is the number of sectors of each device that
5303 * is used. This can only make sense for arrays with redundancy.
5304 * linear and raid0 always use whatever space is available. We can only
5305 * consider changing this number if no resync or reconstruction is
5306 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005307 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005308 * of each device. If num_sectors is zero, we find the largest size
5309 * that fits.
5310
NeilBrowna35b0d62006-01-06 00:20:49 -08005311 */
5312 if (mddev->sync_thread)
5313 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005314 if (mddev->bitmap)
5315 /* Sorry, cannot grow a bitmap yet, just remove it,
5316 * grow, and re-add.
5317 */
5318 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005319 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005320 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005321
Andre Nolld71f9f82008-07-11 22:02:22 +10005322 if (fit && (num_sectors == 0 || num_sectors > avail))
5323 num_sectors = avail;
5324 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005325 return -ENOSPC;
5326 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005327 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005328 if (!rv)
5329 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005330 return rv;
5331}
5332
NeilBrownda943b992006-01-06 00:20:54 -08005333static int update_raid_disks(mddev_t *mddev, int raid_disks)
5334{
5335 int rv;
5336 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005337 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005338 return -EINVAL;
5339 if (raid_disks <= 0 ||
5340 raid_disks >= mddev->max_disks)
5341 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005342 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005343 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005344 mddev->delta_disks = raid_disks - mddev->raid_disks;
5345
5346 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005347 return rv;
5348}
5349
5350
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351/*
5352 * update_array_info is used to change the configuration of an
5353 * on-line array.
5354 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5355 * fields in the info are checked against the array.
5356 * Any differences that cannot be handled will cause an error.
5357 * Normally, only one change can be managed at a time.
5358 */
5359static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5360{
5361 int rv = 0;
5362 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005363 int state = 0;
5364
5365 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005366 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005367 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368
5369 if (mddev->major_version != info->major_version ||
5370 mddev->minor_version != info->minor_version ||
5371/* mddev->patch_version != info->patch_version || */
5372 mddev->ctime != info->ctime ||
5373 mddev->level != info->level ||
5374/* mddev->layout != info->layout || */
5375 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005376 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005377 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5378 ((state^info->state) & 0xfffffe00)
5379 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 return -EINVAL;
5381 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005382 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5383 cnt++;
5384 if (mddev->raid_disks != info->raid_disks)
5385 cnt++;
5386 if (mddev->layout != info->layout)
5387 cnt++;
5388 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5389 cnt++;
5390 if (cnt == 0)
5391 return 0;
5392 if (cnt > 1)
5393 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394
5395 if (mddev->layout != info->layout) {
5396 /* Change layout
5397 * we don't need to do anything at the md level, the
5398 * personality will take care of it all.
5399 */
NeilBrown50ac1682009-06-18 08:47:55 +10005400 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005402 else {
5403 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005404 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005405 if (rv)
5406 mddev->new_layout = mddev->layout;
5407 return rv;
5408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005410 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005411 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412
NeilBrownda943b992006-01-06 00:20:54 -08005413 if (mddev->raid_disks != info->raid_disks)
5414 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415
NeilBrown36fa3062005-09-09 16:23:45 -07005416 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5417 if (mddev->pers->quiesce == NULL)
5418 return -EINVAL;
5419 if (mddev->recovery || mddev->sync_thread)
5420 return -EBUSY;
5421 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5422 /* add the bitmap */
5423 if (mddev->bitmap)
5424 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005425 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005426 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005427 mddev->bitmap_info.offset =
5428 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005429 mddev->pers->quiesce(mddev, 1);
5430 rv = bitmap_create(mddev);
5431 if (rv)
5432 bitmap_destroy(mddev);
5433 mddev->pers->quiesce(mddev, 0);
5434 } else {
5435 /* remove the bitmap */
5436 if (!mddev->bitmap)
5437 return -ENOENT;
5438 if (mddev->bitmap->file)
5439 return -EINVAL;
5440 mddev->pers->quiesce(mddev, 1);
5441 bitmap_destroy(mddev);
5442 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005443 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005444 }
5445 }
NeilBrown850b2b42006-10-03 01:15:46 -07005446 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 return rv;
5448}
5449
5450static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5451{
5452 mdk_rdev_t *rdev;
5453
5454 if (mddev->pers == NULL)
5455 return -ENODEV;
5456
5457 rdev = find_rdev(mddev, dev);
5458 if (!rdev)
5459 return -ENODEV;
5460
5461 md_error(mddev, rdev);
5462 return 0;
5463}
5464
Andre Noll2f9618c2008-04-25 18:57:58 +02005465/*
5466 * We have a problem here : there is no easy way to give a CHS
5467 * virtual geometry. We currently pretend that we have a 2 heads
5468 * 4 sectors (with a BIG number of cylinders...). This drives
5469 * dosfs just mad... ;-)
5470 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005471static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5472{
5473 mddev_t *mddev = bdev->bd_disk->private_data;
5474
5475 geo->heads = 2;
5476 geo->sectors = 4;
5477 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5478 return 0;
5479}
5480
Al Viroa39907f2008-03-02 10:31:15 -05005481static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 unsigned int cmd, unsigned long arg)
5483{
5484 int err = 0;
5485 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 mddev_t *mddev = NULL;
5487
5488 if (!capable(CAP_SYS_ADMIN))
5489 return -EACCES;
5490
5491 /*
5492 * Commands dealing with the RAID driver but not any
5493 * particular array:
5494 */
5495 switch (cmd)
5496 {
5497 case RAID_VERSION:
5498 err = get_version(argp);
5499 goto done;
5500
5501 case PRINT_RAID_DEBUG:
5502 err = 0;
5503 md_print_devices();
5504 goto done;
5505
5506#ifndef MODULE
5507 case RAID_AUTORUN:
5508 err = 0;
5509 autostart_arrays(arg);
5510 goto done;
5511#endif
5512 default:;
5513 }
5514
5515 /*
5516 * Commands creating/starting a new array:
5517 */
5518
Al Viroa39907f2008-03-02 10:31:15 -05005519 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520
5521 if (!mddev) {
5522 BUG();
5523 goto abort;
5524 }
5525
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 err = mddev_lock(mddev);
5527 if (err) {
5528 printk(KERN_INFO
5529 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5530 err, cmd);
5531 goto abort;
5532 }
5533
5534 switch (cmd)
5535 {
5536 case SET_ARRAY_INFO:
5537 {
5538 mdu_array_info_t info;
5539 if (!arg)
5540 memset(&info, 0, sizeof(info));
5541 else if (copy_from_user(&info, argp, sizeof(info))) {
5542 err = -EFAULT;
5543 goto abort_unlock;
5544 }
5545 if (mddev->pers) {
5546 err = update_array_info(mddev, &info);
5547 if (err) {
5548 printk(KERN_WARNING "md: couldn't update"
5549 " array info. %d\n", err);
5550 goto abort_unlock;
5551 }
5552 goto done_unlock;
5553 }
5554 if (!list_empty(&mddev->disks)) {
5555 printk(KERN_WARNING
5556 "md: array %s already has disks!\n",
5557 mdname(mddev));
5558 err = -EBUSY;
5559 goto abort_unlock;
5560 }
5561 if (mddev->raid_disks) {
5562 printk(KERN_WARNING
5563 "md: array %s already initialised!\n",
5564 mdname(mddev));
5565 err = -EBUSY;
5566 goto abort_unlock;
5567 }
5568 err = set_array_info(mddev, &info);
5569 if (err) {
5570 printk(KERN_WARNING "md: couldn't set"
5571 " array info. %d\n", err);
5572 goto abort_unlock;
5573 }
5574 }
5575 goto done_unlock;
5576
5577 default:;
5578 }
5579
5580 /*
5581 * Commands querying/configuring an existing array:
5582 */
NeilBrown32a76272005-06-21 17:17:14 -07005583 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005584 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005585 if ((!mddev->raid_disks && !mddev->external)
5586 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5587 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5588 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 err = -ENODEV;
5590 goto abort_unlock;
5591 }
5592
5593 /*
5594 * Commands even a read-only array can execute:
5595 */
5596 switch (cmd)
5597 {
5598 case GET_ARRAY_INFO:
5599 err = get_array_info(mddev, argp);
5600 goto done_unlock;
5601
NeilBrown32a76272005-06-21 17:17:14 -07005602 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005603 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005604 goto done_unlock;
5605
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 case GET_DISK_INFO:
5607 err = get_disk_info(mddev, argp);
5608 goto done_unlock;
5609
5610 case RESTART_ARRAY_RW:
5611 err = restart_array(mddev);
5612 goto done_unlock;
5613
5614 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005615 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 goto done_unlock;
5617
5618 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005619 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 goto done_unlock;
5621
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 }
5623
5624 /*
5625 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005626 * superblock, so we do not allow them on read-only arrays.
5627 * However non-MD ioctls (e.g. get-size) will still come through
5628 * here and hit the 'default' below, so only disallow
5629 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005631 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005632 if (mddev->ro == 2) {
5633 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005634 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005635 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5636 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005637 } else {
5638 err = -EROFS;
5639 goto abort_unlock;
5640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 }
5642
5643 switch (cmd)
5644 {
5645 case ADD_NEW_DISK:
5646 {
5647 mdu_disk_info_t info;
5648 if (copy_from_user(&info, argp, sizeof(info)))
5649 err = -EFAULT;
5650 else
5651 err = add_new_disk(mddev, &info);
5652 goto done_unlock;
5653 }
5654
5655 case HOT_REMOVE_DISK:
5656 err = hot_remove_disk(mddev, new_decode_dev(arg));
5657 goto done_unlock;
5658
5659 case HOT_ADD_DISK:
5660 err = hot_add_disk(mddev, new_decode_dev(arg));
5661 goto done_unlock;
5662
5663 case SET_DISK_FAULTY:
5664 err = set_disk_faulty(mddev, new_decode_dev(arg));
5665 goto done_unlock;
5666
5667 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005668 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 goto done_unlock;
5670
NeilBrown32a76272005-06-21 17:17:14 -07005671 case SET_BITMAP_FILE:
5672 err = set_bitmap_file(mddev, (int)arg);
5673 goto done_unlock;
5674
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 err = -EINVAL;
5677 goto abort_unlock;
5678 }
5679
5680done_unlock:
5681abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005682 if (mddev->hold_active == UNTIL_IOCTL &&
5683 err != -EINVAL)
5684 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 mddev_unlock(mddev);
5686
5687 return err;
5688done:
5689 if (err)
5690 MD_BUG();
5691abort:
5692 return err;
5693}
5694
Al Viroa39907f2008-03-02 10:31:15 -05005695static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696{
5697 /*
5698 * Succeed if we can lock the mddev, which confirms that
5699 * it isn't being stopped right now.
5700 */
NeilBrownd3374822009-01-09 08:31:10 +11005701 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 int err;
5703
NeilBrownd3374822009-01-09 08:31:10 +11005704 if (mddev->gendisk != bdev->bd_disk) {
5705 /* we are racing with mddev_put which is discarding this
5706 * bd_disk.
5707 */
5708 mddev_put(mddev);
5709 /* Wait until bdev->bd_disk is definitely gone */
5710 flush_scheduled_work();
5711 /* Then retry the open from the top */
5712 return -ERESTARTSYS;
5713 }
5714 BUG_ON(mddev != bdev->bd_disk->private_data);
5715
NeilBrownc8c00a62009-08-10 12:50:52 +10005716 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 goto out;
5718
5719 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005720 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005721 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722
Al Viroa39907f2008-03-02 10:31:15 -05005723 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 out:
5725 return err;
5726}
5727
Al Viroa39907f2008-03-02 10:31:15 -05005728static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729{
Al Viroa39907f2008-03-02 10:31:15 -05005730 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005732 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005733 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 mddev_put(mddev);
5735
5736 return 0;
5737}
5738
Linus Torvalds44ce62942007-05-09 18:51:36 -07005739static int md_media_changed(struct gendisk *disk)
5740{
5741 mddev_t *mddev = disk->private_data;
5742
5743 return mddev->changed;
5744}
5745
5746static int md_revalidate(struct gendisk *disk)
5747{
5748 mddev_t *mddev = disk->private_data;
5749
5750 mddev->changed = 0;
5751 return 0;
5752}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005753static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754{
5755 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005756 .open = md_open,
5757 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005758 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005759 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07005760 .media_changed = md_media_changed,
5761 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762};
5763
Adrian Bunk75c96f82005-05-05 16:16:09 -07005764static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765{
5766 mdk_thread_t *thread = arg;
5767
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 /*
5769 * md_thread is a 'system-thread', it's priority should be very
5770 * high. We avoid resource deadlocks individually in each
5771 * raid personality. (RAID5 does preallocation) We also use RR and
5772 * the very same RT priority as kswapd, thus we will never get
5773 * into a priority inversion deadlock.
5774 *
5775 * we definitely have to have equal or higher priority than
5776 * bdflush, otherwise bdflush will deadlock if there are too
5777 * many dirty RAID5 blocks.
5778 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779
NeilBrown6985c432005-10-19 21:23:47 -07005780 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005781 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782
NeilBrown93588e22005-11-15 00:09:12 -08005783 /* We need to wait INTERRUPTIBLE so that
5784 * we don't add to the load-average.
5785 * That means we need to be sure no signals are
5786 * pending
5787 */
5788 if (signal_pending(current))
5789 flush_signals(current);
5790
5791 wait_event_interruptible_timeout
5792 (thread->wqueue,
5793 test_bit(THREAD_WAKEUP, &thread->flags)
5794 || kthread_should_stop(),
5795 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796
5797 clear_bit(THREAD_WAKEUP, &thread->flags);
5798
NeilBrown787453c2005-11-08 21:39:43 -08005799 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005801
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 return 0;
5803}
5804
5805void md_wakeup_thread(mdk_thread_t *thread)
5806{
5807 if (thread) {
5808 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5809 set_bit(THREAD_WAKEUP, &thread->flags);
5810 wake_up(&thread->wqueue);
5811 }
5812}
5813
5814mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5815 const char *name)
5816{
5817 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818
NeilBrown9ffae0c2006-01-06 00:20:32 -08005819 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 if (!thread)
5821 return NULL;
5822
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 init_waitqueue_head(&thread->wqueue);
5824
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 thread->run = run;
5826 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005827 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10005828 thread->tsk = kthread_run(md_thread, thread,
5829 "%s_%s",
5830 mdname(thread->mddev),
5831 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07005832 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 kfree(thread);
5834 return NULL;
5835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 return thread;
5837}
5838
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839void md_unregister_thread(mdk_thread_t *thread)
5840{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005841 if (!thread)
5842 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005843 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005844
5845 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 kfree(thread);
5847}
5848
5849void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5850{
5851 if (!mddev) {
5852 MD_BUG();
5853 return;
5854 }
5855
NeilBrownb2d444d2005-11-08 21:39:31 -08005856 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005858
5859 if (mddev->external)
5860 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005861/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5863 mdname(mddev),
5864 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5865 __builtin_return_address(0),__builtin_return_address(1),
5866 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005867*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005868 if (!mddev->pers)
5869 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 if (!mddev->pers->error_handler)
5871 return;
5872 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005873 if (mddev->degraded)
5874 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005875 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5877 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5878 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005879 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880}
5881
5882/* seq_file implementation /proc/mdstat */
5883
5884static void status_unused(struct seq_file *seq)
5885{
5886 int i = 0;
5887 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888
5889 seq_printf(seq, "unused devices: ");
5890
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005891 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 char b[BDEVNAME_SIZE];
5893 i++;
5894 seq_printf(seq, "%s ",
5895 bdevname(rdev->bdev,b));
5896 }
5897 if (!i)
5898 seq_printf(seq, "<none>");
5899
5900 seq_printf(seq, "\n");
5901}
5902
5903
5904static void status_resync(struct seq_file *seq, mddev_t * mddev)
5905{
NeilBrowndd71cf62009-05-07 12:49:35 +10005906 sector_t max_sectors, resync, res;
5907 unsigned long dt, db;
5908 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08005909 int scale;
5910 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911
NeilBrowndd71cf62009-05-07 12:49:35 +10005912 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913
5914 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10005915 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 else
NeilBrowndd71cf62009-05-07 12:49:35 +10005917 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918
5919 /*
5920 * Should not happen.
5921 */
NeilBrowndd71cf62009-05-07 12:49:35 +10005922 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 MD_BUG();
5924 return;
5925 }
NeilBrown4588b422006-03-27 01:18:04 -08005926 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10005927 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08005928 * u32, as those are the requirements for sector_div.
5929 * Thus 'scale' must be at least 10
5930 */
5931 scale = 10;
5932 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10005933 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08005934 scale++;
5935 }
5936 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10005937 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08005938
5939 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940 {
NeilBrown4588b422006-03-27 01:18:04 -08005941 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 seq_printf(seq, "[");
5943 for (i = 0; i < x; i++)
5944 seq_printf(seq, "=");
5945 seq_printf(seq, ">");
5946 for (i = 0; i < y; i++)
5947 seq_printf(seq, ".");
5948 seq_printf(seq, "] ");
5949 }
NeilBrown4588b422006-03-27 01:18:04 -08005950 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005951 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5952 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005953 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5954 "check" :
5955 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5956 "resync" : "recovery"))),
5957 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10005958 (unsigned long long) resync/2,
5959 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960
5961 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962 * dt: time from mark until now
5963 * db: blocks written from mark until now
5964 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10005965 *
5966 * rt is a sector_t, so could be 32bit or 64bit.
5967 * So we divide before multiply in case it is 32bit and close
5968 * to the limit.
5969 * We scale the divisor (db) by 32 to avoid loosing precision
5970 * near the end of resync when the number of remaining sectors
5971 * is close to 'db'.
5972 * We then divide rt by 32 after multiplying by db to compensate.
5973 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 */
5975 dt = ((jiffies - mddev->resync_mark) / HZ);
5976 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005977 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5978 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979
NeilBrowndd71cf62009-05-07 12:49:35 +10005980 rt = max_sectors - resync; /* number of remaining sectors */
5981 sector_div(rt, db/32+1);
5982 rt *= dt;
5983 rt >>= 5;
5984
5985 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
5986 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987
NeilBrownff4e8d92006-07-10 04:44:16 -07005988 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989}
5990
5991static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5992{
5993 struct list_head *tmp;
5994 loff_t l = *pos;
5995 mddev_t *mddev;
5996
5997 if (l >= 0x10000)
5998 return NULL;
5999 if (!l--)
6000 /* header */
6001 return (void*)1;
6002
6003 spin_lock(&all_mddevs_lock);
6004 list_for_each(tmp,&all_mddevs)
6005 if (!l--) {
6006 mddev = list_entry(tmp, mddev_t, all_mddevs);
6007 mddev_get(mddev);
6008 spin_unlock(&all_mddevs_lock);
6009 return mddev;
6010 }
6011 spin_unlock(&all_mddevs_lock);
6012 if (!l--)
6013 return (void*)2;/* tail */
6014 return NULL;
6015}
6016
6017static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6018{
6019 struct list_head *tmp;
6020 mddev_t *next_mddev, *mddev = v;
6021
6022 ++*pos;
6023 if (v == (void*)2)
6024 return NULL;
6025
6026 spin_lock(&all_mddevs_lock);
6027 if (v == (void*)1)
6028 tmp = all_mddevs.next;
6029 else
6030 tmp = mddev->all_mddevs.next;
6031 if (tmp != &all_mddevs)
6032 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6033 else {
6034 next_mddev = (void*)2;
6035 *pos = 0x10000;
6036 }
6037 spin_unlock(&all_mddevs_lock);
6038
6039 if (v != (void*)1)
6040 mddev_put(mddev);
6041 return next_mddev;
6042
6043}
6044
6045static void md_seq_stop(struct seq_file *seq, void *v)
6046{
6047 mddev_t *mddev = v;
6048
6049 if (mddev && v != (void*)1 && v != (void*)2)
6050 mddev_put(mddev);
6051}
6052
NeilBrownd7603b72006-01-06 00:20:30 -08006053struct mdstat_info {
6054 int event;
6055};
6056
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057static int md_seq_show(struct seq_file *seq, void *v)
6058{
6059 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006060 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006062 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006063 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064
6065 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006066 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 seq_printf(seq, "Personalities : ");
6068 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006069 list_for_each_entry(pers, &pers_list, list)
6070 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071
6072 spin_unlock(&pers_lock);
6073 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006074 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 return 0;
6076 }
6077 if (v == (void*)2) {
6078 status_unused(seq);
6079 return 0;
6080 }
6081
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006082 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006084
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6086 seq_printf(seq, "%s : %sactive", mdname(mddev),
6087 mddev->pers ? "" : "in");
6088 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006089 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006091 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006092 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 seq_printf(seq, " %s", mddev->pers->name);
6094 }
6095
Andre Nolldd8ac332009-03-31 14:33:13 +11006096 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006097 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 char b[BDEVNAME_SIZE];
6099 seq_printf(seq, " %s[%d]",
6100 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006101 if (test_bit(WriteMostly, &rdev->flags))
6102 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006103 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 seq_printf(seq, "(F)");
6105 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006106 } else if (rdev->raid_disk < 0)
6107 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006108 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109 }
6110
6111 if (!list_empty(&mddev->disks)) {
6112 if (mddev->pers)
6113 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006114 (unsigned long long)
6115 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 else
6117 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006118 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006120 if (mddev->persistent) {
6121 if (mddev->major_version != 0 ||
6122 mddev->minor_version != 90) {
6123 seq_printf(seq," super %d.%d",
6124 mddev->major_version,
6125 mddev->minor_version);
6126 }
NeilBrowne6910632008-02-06 01:39:51 -08006127 } else if (mddev->external)
6128 seq_printf(seq, " super external:%s",
6129 mddev->metadata_type);
6130 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006131 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132
6133 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006134 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006136 if (mddev->pers->sync_request) {
6137 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006138 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006139 seq_printf(seq, "\n ");
6140 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6141 seq_printf(seq, "\tresync=DELAYED\n ");
6142 else if (mddev->recovery_cp < MaxSector)
6143 seq_printf(seq, "\tresync=PENDING\n ");
6144 }
NeilBrown32a76272005-06-21 17:17:14 -07006145 } else
6146 seq_printf(seq, "\n ");
6147
6148 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006149 unsigned long chunk_kb;
6150 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006151 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006152 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006153 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6154 "%lu%s chunk",
6155 bitmap->pages - bitmap->missing_pages,
6156 bitmap->pages,
6157 (bitmap->pages - bitmap->missing_pages)
6158 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006159 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006160 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006161 if (bitmap->file) {
6162 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006163 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006164 }
NeilBrown78d742d2005-06-21 17:17:15 -07006165
NeilBrown32a76272005-06-21 17:17:14 -07006166 seq_printf(seq, "\n");
6167 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 }
6169
6170 seq_printf(seq, "\n");
6171 }
6172 mddev_unlock(mddev);
6173
6174 return 0;
6175}
6176
Jan Engelhardt110518b2009-05-07 12:49:37 +10006177static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178 .start = md_seq_start,
6179 .next = md_seq_next,
6180 .stop = md_seq_stop,
6181 .show = md_seq_show,
6182};
6183
6184static int md_seq_open(struct inode *inode, struct file *file)
6185{
6186 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006187 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6188 if (mi == NULL)
6189 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190
6191 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006192 if (error)
6193 kfree(mi);
6194 else {
6195 struct seq_file *p = file->private_data;
6196 p->private = mi;
6197 mi->event = atomic_read(&md_event_count);
6198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 return error;
6200}
6201
NeilBrownd7603b72006-01-06 00:20:30 -08006202static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6203{
6204 struct seq_file *m = filp->private_data;
6205 struct mdstat_info *mi = m->private;
6206 int mask;
6207
6208 poll_wait(filp, &md_event_waiters, wait);
6209
6210 /* always allow read */
6211 mask = POLLIN | POLLRDNORM;
6212
6213 if (mi->event != atomic_read(&md_event_count))
6214 mask |= POLLERR | POLLPRI;
6215 return mask;
6216}
6217
Arjan van de Venfa027c22007-02-12 00:55:33 -08006218static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006219 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 .open = md_seq_open,
6221 .read = seq_read,
6222 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006223 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006224 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225};
6226
NeilBrown2604b702006-01-06 00:20:36 -08006227int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006230 list_add_tail(&p->list, &pers_list);
6231 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 spin_unlock(&pers_lock);
6233 return 0;
6234}
6235
NeilBrown2604b702006-01-06 00:20:36 -08006236int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237{
NeilBrown2604b702006-01-06 00:20:36 -08006238 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006240 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 spin_unlock(&pers_lock);
6242 return 0;
6243}
6244
NeilBrowneea1bf32009-03-31 14:27:02 +11006245static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246{
6247 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006249 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250
6251 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006252 rcu_read_lock();
6253 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006255 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6256 (int)part_stat_read(&disk->part0, sectors[1]) -
6257 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006258 /* sync IO will cause sync_io to increase before the disk_stats
6259 * as sync_io is counted when a request starts, and
6260 * disk_stats is counted when it completes.
6261 * So resync activity will cause curr_events to be smaller than
6262 * when there was no such activity.
6263 * non-sync IO will cause disk_stat to increase without
6264 * increasing sync_io so curr_events will (eventually)
6265 * be larger than it was before. Once it becomes
6266 * substantially larger, the test below will cause
6267 * the array to appear non-idle, and resync will slow
6268 * down.
6269 * If there is a lot of outstanding resync activity when
6270 * we set last_event to curr_events, then all that activity
6271 * completing might cause the array to appear non-idle
6272 * and resync will be slowed down even though there might
6273 * not have been non-resync activity. This will only
6274 * happen once though. 'last_events' will soon reflect
6275 * the state where there is little or no outstanding
6276 * resync requests, and further resync activity will
6277 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006278 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006280 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281 rdev->last_events = curr_events;
6282 idle = 0;
6283 }
6284 }
NeilBrown4b809912008-07-21 17:05:25 +10006285 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286 return idle;
6287}
6288
6289void md_done_sync(mddev_t *mddev, int blocks, int ok)
6290{
6291 /* another "blocks" (512byte) blocks have been synced */
6292 atomic_sub(blocks, &mddev->recovery_active);
6293 wake_up(&mddev->recovery_wait);
6294 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006295 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 md_wakeup_thread(mddev->thread);
6297 // stop recovery, signal do_sync ....
6298 }
6299}
6300
6301
NeilBrown06d91a52005-06-21 17:17:12 -07006302/* md_write_start(mddev, bi)
6303 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006304 * in superblock) before writing, schedule a superblock update
6305 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006306 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006307void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308{
Neil Brown0fd62b82008-06-28 08:31:36 +10006309 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006310 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006311 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006312
NeilBrownf91de922005-11-08 21:39:36 -08006313 BUG_ON(mddev->ro == 1);
6314 if (mddev->ro == 2) {
6315 /* need to switch to read/write */
6316 mddev->ro = 0;
6317 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6318 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006319 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006320 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006321 }
NeilBrown06d91a52005-06-21 17:17:12 -07006322 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006323 if (mddev->safemode == 1)
6324 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006325 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006326 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006327 if (mddev->in_sync) {
6328 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006329 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006330 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006331 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006332 }
NeilBrowna9701a32005-11-08 21:39:34 -08006333 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006334 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006335 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006336 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006337 wait_event(mddev->sb_wait,
6338 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6339 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340}
6341
6342void md_write_end(mddev_t *mddev)
6343{
6344 if (atomic_dec_and_test(&mddev->writes_pending)) {
6345 if (mddev->safemode == 2)
6346 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006347 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6349 }
6350}
6351
NeilBrown2a2275d2007-01-26 00:57:11 -08006352/* md_allow_write(mddev)
6353 * Calling this ensures that the array is marked 'active' so that writes
6354 * may proceed without blocking. It is important to call this before
6355 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6356 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006357 *
6358 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6359 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006360 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006361int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006362{
6363 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006364 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006365 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006366 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006367 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006368 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006369
6370 spin_lock_irq(&mddev->write_lock);
6371 if (mddev->in_sync) {
6372 mddev->in_sync = 0;
6373 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6374 if (mddev->safemode_delay &&
6375 mddev->safemode == 0)
6376 mddev->safemode = 1;
6377 spin_unlock_irq(&mddev->write_lock);
6378 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006379 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006380 } else
6381 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006382
6383 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6384 return -EAGAIN;
6385 else
6386 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006387}
6388EXPORT_SYMBOL_GPL(md_allow_write);
6389
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390#define SYNC_MARKS 10
6391#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006392void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393{
6394 mddev_t *mddev2;
6395 unsigned int currspeed = 0,
6396 window;
NeilBrown57afd892005-06-21 17:17:13 -07006397 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 unsigned long mark[SYNC_MARKS];
6399 sector_t mark_cnt[SYNC_MARKS];
6400 int last_mark,m;
6401 struct list_head *tmp;
6402 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006403 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006404 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006405 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406
6407 /* just incase thread restarts... */
6408 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6409 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006410 if (mddev->ro) /* never try to sync a read-only array */
6411 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412
NeilBrown61df9d92006-10-03 01:15:57 -07006413 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6414 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6415 desc = "data-check";
6416 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6417 desc = "requested-resync";
6418 else
6419 desc = "resync";
6420 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6421 desc = "reshape";
6422 else
6423 desc = "recovery";
6424
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425 /* we overload curr_resync somewhat here.
6426 * 0 == not engaged in resync at all
6427 * 2 == checking that there is no conflict with another sync
6428 * 1 == like 2, but have yielded to allow conflicting resync to
6429 * commense
6430 * other == active in resync - this many blocks
6431 *
6432 * Before starting a resync we must have set curr_resync to
6433 * 2, and then checked that every "conflicting" array has curr_resync
6434 * less than ours. When we find one that is the same or higher
6435 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6436 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6437 * This will mean we have to start checking from the beginning again.
6438 *
6439 */
6440
6441 do {
6442 mddev->curr_resync = 2;
6443
6444 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08006445 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07006446 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447 goto skip;
6448 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08006449 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 if (mddev2 == mddev)
6451 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006452 if (!mddev->parallel_resync
6453 && mddev2->curr_resync
6454 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455 DEFINE_WAIT(wq);
6456 if (mddev < mddev2 && mddev->curr_resync == 2) {
6457 /* arbitrarily yield */
6458 mddev->curr_resync = 1;
6459 wake_up(&resync_wait);
6460 }
6461 if (mddev > mddev2 && mddev->curr_resync == 1)
6462 /* no need to wait here, we can wait the next
6463 * time 'round when curr_resync == 2
6464 */
6465 continue;
NeilBrown97441972008-09-19 11:49:54 +10006466 /* We need to wait 'interruptible' so as not to
6467 * contribute to the load average, and not to
6468 * be caught by 'softlockup'
6469 */
6470 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006471 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006472 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006473 printk(KERN_INFO "md: delaying %s of %s"
6474 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006476 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006478 if (signal_pending(current))
6479 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480 schedule();
6481 finish_wait(&resync_wait, &wq);
6482 goto try_again;
6483 }
6484 finish_wait(&resync_wait, &wq);
6485 }
6486 }
6487 } while (mddev->curr_resync < 2);
6488
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006489 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006490 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006492 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493 */
6494 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006495 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006496 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006497 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6498 j = mddev->resync_min;
6499 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006500 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006501
NeilBrownccfcc3c2006-03-27 01:18:09 -08006502 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006503 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006504 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006506 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006507 j = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006508 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006509 if (rdev->raid_disk >= 0 &&
6510 !test_bit(Faulty, &rdev->flags) &&
6511 !test_bit(In_sync, &rdev->flags) &&
6512 rdev->recovery_offset < j)
6513 j = rdev->recovery_offset;
6514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515
NeilBrown61df9d92006-10-03 01:15:57 -07006516 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6517 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6518 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006519 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006520 "(but not more than %d KB/sec) for %s.\n",
6521 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522
NeilBrowneea1bf32009-03-31 14:27:02 +11006523 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006524
NeilBrown57afd892005-06-21 17:17:13 -07006525 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526 for (m = 0; m < SYNC_MARKS; m++) {
6527 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006528 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529 }
6530 last_mark = 0;
6531 mddev->resync_mark = mark[last_mark];
6532 mddev->resync_mark_cnt = mark_cnt[last_mark];
6533
6534 /*
6535 * Tune reconstruction:
6536 */
6537 window = 32*(PAGE_SIZE/512);
6538 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6539 window/2,(unsigned long long) max_sectors/2);
6540
6541 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006542 last_check = 0;
6543
6544 if (j>2) {
6545 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006546 "md: resuming %s of %s from checkpoint.\n",
6547 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548 mddev->curr_resync = j;
6549 }
NeilBrownefa59332009-12-14 12:49:49 +11006550 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006551
6552 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006553 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554
NeilBrown57afd892005-06-21 17:17:13 -07006555 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006556
NeilBrown7a91ee12009-05-26 12:57:21 +10006557 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6558 ((mddev->curr_resync > mddev->curr_resync_completed &&
6559 (mddev->curr_resync - mddev->curr_resync_completed)
6560 > (max_sectors >> 4)) ||
6561 (j - mddev->curr_resync_completed)*2
6562 >= mddev->resync_max - mddev->curr_resync_completed
6563 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006564 /* time to update curr_resync_completed */
6565 blk_unplug(mddev->queue);
6566 wait_event(mddev->recovery_wait,
6567 atomic_read(&mddev->recovery_active) == 0);
6568 mddev->curr_resync_completed =
6569 mddev->curr_resync;
6570 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006571 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006572 }
NeilBrownacb180b2009-04-14 16:28:34 +10006573
NeilBrowne62e58a2009-07-01 13:15:35 +10006574 while (j >= mddev->resync_max && !kthread_should_stop()) {
6575 /* As this condition is controlled by user-space,
6576 * we can block indefinitely, so use '_interruptible'
6577 * to avoid triggering warnings.
6578 */
6579 flush_signals(current); /* just in case */
6580 wait_event_interruptible(mddev->recovery_wait,
6581 mddev->resync_max > j
6582 || kthread_should_stop());
6583 }
NeilBrownacb180b2009-04-14 16:28:34 +10006584
6585 if (kthread_should_stop())
6586 goto interrupted;
6587
NeilBrown57afd892005-06-21 17:17:13 -07006588 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006589 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006590 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006591 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592 goto out;
6593 }
NeilBrown57afd892005-06-21 17:17:13 -07006594
6595 if (!skipped) { /* actual IO requested */
6596 io_sectors += sectors;
6597 atomic_add(sectors, &mddev->recovery_active);
6598 }
6599
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600 j += sectors;
6601 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006602 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006603 if (last_check == 0)
6604 /* this is the earliers that rebuilt will be
6605 * visible in /proc/mdstat
6606 */
6607 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006608
6609 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610 continue;
6611
NeilBrown57afd892005-06-21 17:17:13 -07006612 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613
NeilBrowndfc70642008-05-23 13:04:39 -07006614 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615 break;
6616
6617 repeat:
6618 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6619 /* step marks */
6620 int next = (last_mark+1) % SYNC_MARKS;
6621
6622 mddev->resync_mark = mark[next];
6623 mddev->resync_mark_cnt = mark_cnt[next];
6624 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006625 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 last_mark = next;
6627 }
6628
6629
NeilBrownc6207272008-02-06 01:39:52 -08006630 if (kthread_should_stop())
6631 goto interrupted;
6632
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633
6634 /*
6635 * this loop exits only if either when we are slower than
6636 * the 'hard' speed limit, or the system was IO-idle for
6637 * a jiffy.
6638 * the system might be non-idle CPU-wise, but we only care
6639 * about not overloading the IO subsystem. (things like an
6640 * e2fsck being done on the RAID array should execute fast)
6641 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006642 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643 cond_resched();
6644
NeilBrown57afd892005-06-21 17:17:13 -07006645 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6646 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647
NeilBrown88202a02006-01-06 00:21:36 -08006648 if (currspeed > speed_min(mddev)) {
6649 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006650 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006651 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652 goto repeat;
6653 }
6654 }
6655 }
NeilBrown61df9d92006-10-03 01:15:57 -07006656 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657 /*
6658 * this also signals 'finished resyncing' to md_stop
6659 */
6660 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006661 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662
6663 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6664
6665 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006666 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667
NeilBrowndfc70642008-05-23 13:04:39 -07006668 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006669 mddev->curr_resync > 2) {
6670 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6671 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6672 if (mddev->curr_resync >= mddev->recovery_cp) {
6673 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006674 "md: checkpointing %s of %s.\n",
6675 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006676 mddev->recovery_cp = mddev->curr_resync;
6677 }
6678 } else
6679 mddev->recovery_cp = MaxSector;
6680 } else {
6681 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6682 mddev->curr_resync = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006683 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006684 if (rdev->raid_disk >= 0 &&
6685 !test_bit(Faulty, &rdev->flags) &&
6686 !test_bit(In_sync, &rdev->flags) &&
6687 rdev->recovery_offset < mddev->curr_resync)
6688 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690 }
NeilBrown17571282006-12-10 02:20:52 -08006691 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006694 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6695 /* We completed so min/max setting can be forgotten if used. */
6696 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6697 mddev->resync_min = 0;
6698 mddev->resync_max = MaxSector;
6699 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6700 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006702 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6703 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006704 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705 wake_up(&resync_wait);
6706 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6707 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006708 return;
6709
6710 interrupted:
6711 /*
6712 * got a signal, exit.
6713 */
6714 printk(KERN_INFO
6715 "md: md_do_sync() got signal ... exiting\n");
6716 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6717 goto out;
6718
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719}
NeilBrown29269552006-03-27 01:18:10 -08006720EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006721
6722
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006723static int remove_and_add_spares(mddev_t *mddev)
6724{
6725 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006726 int spares = 0;
6727
NeilBrown97e4f422009-03-31 14:33:13 +11006728 mddev->curr_resync_completed = 0;
6729
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006730 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006731 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006732 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006733 (test_bit(Faulty, &rdev->flags) ||
6734 ! test_bit(In_sync, &rdev->flags)) &&
6735 atomic_read(&rdev->nr_pending)==0) {
6736 if (mddev->pers->hot_remove_disk(
6737 mddev, rdev->raid_disk)==0) {
6738 char nm[20];
6739 sprintf(nm,"rd%d", rdev->raid_disk);
6740 sysfs_remove_link(&mddev->kobj, nm);
6741 rdev->raid_disk = -1;
6742 }
6743 }
6744
NeilBrown4044ba52009-01-09 08:31:11 +11006745 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006746 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006747 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006748 !test_bit(In_sync, &rdev->flags) &&
6749 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006750 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006751 if (rdev->raid_disk < 0
6752 && !test_bit(Faulty, &rdev->flags)) {
6753 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006754 if (mddev->pers->
6755 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006756 char nm[20];
6757 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006758 if (sysfs_create_link(&mddev->kobj,
6759 &rdev->kobj, nm))
6760 printk(KERN_WARNING
6761 "md: cannot register "
6762 "%s for %s\n",
6763 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006764 spares++;
6765 md_new_event(mddev);
6766 } else
6767 break;
6768 }
NeilBrowndfc70642008-05-23 13:04:39 -07006769 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006770 }
6771 return spares;
6772}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773/*
6774 * This routine is regularly called by all per-raid-array threads to
6775 * deal with generic issues like resync and super-block update.
6776 * Raid personalities that don't have a thread (linear/raid0) do not
6777 * need this as they never do any recovery or update the superblock.
6778 *
6779 * It does not do any resync itself, but rather "forks" off other threads
6780 * to do that as needed.
6781 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6782 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006783 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784 * and wakeups up this thread which will reap the thread and finish up.
6785 * This thread also removes any faulty devices (with nr_pending == 0).
6786 *
6787 * The overall approach is:
6788 * 1/ if the superblock needs updating, update it.
6789 * 2/ If a recovery thread is running, don't do anything else.
6790 * 3/ If recovery has finished, clean up, possibly marking spares active.
6791 * 4/ If there are any faulty devices, remove them.
6792 * 5/ If array is degraded, try to add spares devices
6793 * 6/ If array has spares or is not in-sync, start a resync thread.
6794 */
6795void md_check_recovery(mddev_t *mddev)
6796{
6797 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798
6799
NeilBrown5f404022005-06-21 17:17:16 -07006800 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11006801 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802
6803 if (mddev->ro)
6804 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006805
6806 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006807 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006808 printk(KERN_INFO "md: %s in immediate safe mode\n",
6809 mdname(mddev));
6810 mddev->safemode = 2;
6811 }
6812 flush_signals(current);
6813 }
6814
NeilBrownc89a8ee2008-08-05 15:54:13 +10006815 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6816 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006818 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006820 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006821 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006822 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6823 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824 ))
6825 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006826
NeilBrowndf5b89b2006-03-27 01:18:20 -08006827 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006828 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006829
NeilBrownc89a8ee2008-08-05 15:54:13 +10006830 if (mddev->ro) {
6831 /* Only thing we do on a ro array is remove
6832 * failed devices.
6833 */
6834 remove_and_add_spares(mddev);
6835 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6836 goto unlock;
6837 }
6838
NeilBrown31a59e32008-04-30 00:52:30 -07006839 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006840 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006841 spin_lock_irq(&mddev->write_lock);
6842 if (mddev->safemode &&
6843 !atomic_read(&mddev->writes_pending) &&
6844 !mddev->in_sync &&
6845 mddev->recovery_cp == MaxSector) {
6846 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006847 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006848 if (mddev->persistent)
6849 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6850 }
6851 if (mddev->safemode == 1)
6852 mddev->safemode = 0;
6853 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006854 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006855 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006856 }
NeilBrownfca4d842005-06-21 17:17:11 -07006857
NeilBrown850b2b42006-10-03 01:15:46 -07006858 if (mddev->flags)
6859 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006860
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006861 list_for_each_entry(rdev, &mddev->disks, same_set)
Neil Brown52664732008-06-28 08:31:44 +10006862 if (test_and_clear_bit(StateChanged, &rdev->flags))
NeilBrown3c0ee632008-10-21 13:25:28 +11006863 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown52664732008-06-28 08:31:44 +10006864
NeilBrown06d91a52005-06-21 17:17:12 -07006865
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6867 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6868 /* resync/recovery still happening */
6869 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6870 goto unlock;
6871 }
6872 if (mddev->sync_thread) {
6873 /* resync has finished, collect result */
6874 md_unregister_thread(mddev->sync_thread);
6875 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006876 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6877 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 /* success...*/
6879 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006880 if (mddev->pers->spare_active(mddev))
6881 sysfs_notify(&mddev->kobj, NULL,
6882 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883 }
NeilBrowncea9c222009-03-31 15:15:05 +11006884 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6885 mddev->pers->finish_reshape)
6886 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07006887 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006888
6889 /* if array is no-longer degraded, then any saved_raid_disk
6890 * information must be scrapped
6891 */
6892 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006893 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07006894 rdev->saved_raid_disk = -1;
6895
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896 mddev->recovery = 0;
6897 /* flag recovery needed just to double check */
6898 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11006899 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006900 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901 goto unlock;
6902 }
Neil Brown72a23c22008-06-28 08:31:41 +10006903 /* Set RUNNING before clearing NEEDED to avoid
6904 * any transients in the value of "sync_action".
6905 */
6906 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6907 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006908 /* Clear some bits that don't mean anything, but
6909 * might be left set
6910 */
NeilBrown24dd4692005-11-08 21:39:26 -08006911 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6912 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006914 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6915 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916 /* no recovery is running.
6917 * remove any failed drives, then
6918 * add spares if possible.
6919 * Spare are also removed and re-added, to allow
6920 * the personality to fail the re-add.
6921 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006922
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006923 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10006924 if (mddev->pers->check_reshape == NULL ||
6925 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006926 /* Cannot proceed */
6927 goto unlock;
6928 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006929 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006930 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006931 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6932 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07006933 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006934 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006935 } else if (mddev->recovery_cp < MaxSector) {
6936 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006937 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006938 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6939 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006941
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006943 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6944 /* We are adding a device or devices to an array
6945 * which has the bitmap stored on all devices.
6946 * So make sure all bitmap pages get written
6947 */
6948 bitmap_write_all(mddev->bitmap);
6949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950 mddev->sync_thread = md_register_thread(md_do_sync,
6951 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006952 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953 if (!mddev->sync_thread) {
6954 printk(KERN_ERR "%s: could not start resync"
6955 " thread...\n",
6956 mdname(mddev));
6957 /* leave the spares where they are, it shouldn't hurt */
6958 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006959 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11006961 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006962 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963 }
6964 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006965 if (!mddev->sync_thread) {
6966 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6967 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6968 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11006969 if (mddev->sysfs_action)
6970 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10006971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972 mddev_unlock(mddev);
6973 }
6974}
6975
Dan Williams6bfe0b42008-04-30 00:52:32 -07006976void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6977{
NeilBrown3c0ee632008-10-21 13:25:28 +11006978 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07006979 wait_event_timeout(rdev->blocked_wait,
6980 !test_bit(Blocked, &rdev->flags),
6981 msecs_to_jiffies(5000));
6982 rdev_dec_pending(rdev, mddev);
6983}
6984EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6985
Adrian Bunk75c96f82005-05-05 16:16:09 -07006986static int md_notify_reboot(struct notifier_block *this,
6987 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006988{
6989 struct list_head *tmp;
6990 mddev_t *mddev;
6991
6992 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6993
6994 printk(KERN_INFO "md: stopping all md devices.\n");
6995
NeilBrown29ac4aa2008-02-06 01:39:58 -08006996 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006997 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10006998 /* Force a switch to readonly even array
6999 * appears to still be in use. Hence
7000 * the '100'.
7001 */
NeilBrownd710e132008-10-13 11:55:12 +11007002 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007003 mddev_unlock(mddev);
7004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005 /*
7006 * certain more exotic SCSI devices are known to be
7007 * volatile wrt too early system reboots. While the
7008 * right place to handle this issue is the given
7009 * driver, we do want to have a safe RAID driver ...
7010 */
7011 mdelay(1000*1);
7012 }
7013 return NOTIFY_DONE;
7014}
7015
Adrian Bunk75c96f82005-05-05 16:16:09 -07007016static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017 .notifier_call = md_notify_reboot,
7018 .next = NULL,
7019 .priority = INT_MAX, /* before any real devices */
7020};
7021
7022static void md_geninit(void)
7023{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7025
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07007026 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027}
7028
Adrian Bunk75c96f82005-05-05 16:16:09 -07007029static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007031 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032 return -1;
7033 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007034 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035 return -1;
7036 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007037 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007038 md_probe, NULL, NULL);
7039 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040 md_probe, NULL, NULL);
7041
Linus Torvalds1da177e2005-04-16 15:20:36 -07007042 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007043 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044
7045 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007046 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047}
7048
7049
7050#ifndef MODULE
7051
7052/*
7053 * Searches all registered partitions for autorun RAID arrays
7054 * at boot time.
7055 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007056
7057static LIST_HEAD(all_detected_devices);
7058struct detected_devices_node {
7059 struct list_head list;
7060 dev_t dev;
7061};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007062
7063void md_autodetect_dev(dev_t dev)
7064{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007065 struct detected_devices_node *node_detected_dev;
7066
7067 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7068 if (node_detected_dev) {
7069 node_detected_dev->dev = dev;
7070 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7071 } else {
7072 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7073 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075}
7076
7077
7078static void autostart_arrays(int part)
7079{
7080 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007081 struct detected_devices_node *node_detected_dev;
7082 dev_t dev;
7083 int i_scanned, i_passed;
7084
7085 i_scanned = 0;
7086 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087
7088 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7089
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007090 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7091 i_scanned++;
7092 node_detected_dev = list_entry(all_detected_devices.next,
7093 struct detected_devices_node, list);
7094 list_del(&node_detected_dev->list);
7095 dev = node_detected_dev->dev;
7096 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007097 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098 if (IS_ERR(rdev))
7099 continue;
7100
NeilBrownb2d444d2005-11-08 21:39:31 -08007101 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102 MD_BUG();
7103 continue;
7104 }
NeilBrownd0fae182008-03-04 14:29:31 -08007105 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007106 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007107 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007109
7110 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7111 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112
7113 autorun_devices(part);
7114}
7115
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007116#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007117
7118static __exit void md_exit(void)
7119{
7120 mddev_t *mddev;
7121 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007122
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007123 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007124 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007126 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127 unregister_blkdev(mdp_major, "mdp");
7128 unregister_reboot_notifier(&md_notifier);
7129 unregister_sysctl_table(raid_table_header);
7130 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007131 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007133 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134 }
7135}
7136
Dan Williams685784a2007-07-09 11:56:42 -07007137subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138module_exit(md_exit)
7139
NeilBrownf91de922005-11-08 21:39:36 -08007140static int get_ro(char *buffer, struct kernel_param *kp)
7141{
7142 return sprintf(buffer, "%d", start_readonly);
7143}
7144static int set_ro(const char *val, struct kernel_param *kp)
7145{
7146 char *e;
7147 int num = simple_strtoul(val, &e, 10);
7148 if (*val && (*e == '\0' || *e == '\n')) {
7149 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007150 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007151 }
7152 return -EINVAL;
7153}
7154
NeilBrown80ca3a42006-07-10 04:44:18 -07007155module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7156module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007157
NeilBrownefeb53c2009-01-09 08:31:10 +11007158module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007159
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160EXPORT_SYMBOL(register_md_personality);
7161EXPORT_SYMBOL(unregister_md_personality);
7162EXPORT_SYMBOL(md_error);
7163EXPORT_SYMBOL(md_done_sync);
7164EXPORT_SYMBOL(md_write_start);
7165EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166EXPORT_SYMBOL(md_register_thread);
7167EXPORT_SYMBOL(md_unregister_thread);
7168EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169EXPORT_SYMBOL(md_check_recovery);
7170MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007171MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007172MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007173MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);