blob: 859edbf8c9b0a72679f86ff035c23e5e2949cb6c [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/*
71 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
72 * is 1000 KB/sec, so the extra system load does not show up that much.
73 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070074 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * subsystem is idle. There is also an 'absolute maximum' reconstruction
76 * speed limit - in case reconstruction slows down your system despite
77 * idle IO detection.
78 *
79 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080080 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
82
83static int sysctl_speed_limit_min = 1000;
84static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080085static inline int speed_min(mddev_t *mddev)
86{
87 return mddev->sync_speed_min ?
88 mddev->sync_speed_min : sysctl_speed_limit_min;
89}
90
91static inline int speed_max(mddev_t *mddev)
92{
93 return mddev->sync_speed_max ?
94 mddev->sync_speed_max : sysctl_speed_limit_max;
95}
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97static struct ctl_table_header *raid_table_header;
98
99static ctl_table raid_table[] = {
100 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 .procname = "speed_limit_min",
102 .data = &sysctl_speed_limit_min,
103 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700104 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800105 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 },
107 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 .procname = "speed_limit_max",
109 .data = &sysctl_speed_limit_max,
110 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700111 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800112 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800114 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};
116
117static ctl_table raid_dir_table[] = {
118 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .procname = "raid",
120 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700121 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 .child = raid_table,
123 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800124 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
127static ctl_table raid_root_table[] = {
128 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .procname = "dev",
130 .maxlen = 0,
131 .mode = 0555,
132 .child = raid_dir_table,
133 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800134 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700137static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
NeilBrownf91de922005-11-08 21:39:36 -0800139static int start_readonly;
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/*
NeilBrownd7603b72006-01-06 00:20:30 -0800142 * We have a system wide 'event count' that is incremented
143 * on any 'interesting' event, and readers of /proc/mdstat
144 * can use 'poll' or 'select' to find out when the event
145 * count increases.
146 *
147 * Events are:
148 * start array, stop array, error, add device, remove device,
149 * start build, activate spare
150 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800151static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800152static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800153void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800154{
155 atomic_inc(&md_event_count);
156 wake_up(&md_event_waiters);
157}
NeilBrown29269552006-03-27 01:18:10 -0800158EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800159
NeilBrownc331eb02006-05-30 21:27:13 -0700160/* Alternate version that can be called from interrupts
161 * when calling sysfs_notify isn't needed.
162 */
Adrian Bunk05381952006-06-26 00:28:01 -0700163static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700164{
165 atomic_inc(&md_event_count);
166 wake_up(&md_event_waiters);
167}
168
NeilBrownd7603b72006-01-06 00:20:30 -0800169/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * Enables to iterate over all existing md arrays
171 * all_mddevs_lock protects this list.
172 */
173static LIST_HEAD(all_mddevs);
174static DEFINE_SPINLOCK(all_mddevs_lock);
175
176
177/*
178 * iterates through all used mddevs in the system.
179 * We take care to grab the all_mddevs_lock whenever navigating
180 * the list, and to always hold a refcount when unlocked.
181 * Any code which breaks out of this loop while own
182 * a reference to the current mddev and must mddev_put it.
183 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800184#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 \
186 for (({ spin_lock(&all_mddevs_lock); \
187 tmp = all_mddevs.next; \
188 mddev = NULL;}); \
189 ({ if (tmp != &all_mddevs) \
190 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
191 spin_unlock(&all_mddevs_lock); \
192 if (mddev) mddev_put(mddev); \
193 mddev = list_entry(tmp, mddev_t, all_mddevs); \
194 tmp != &all_mddevs;}); \
195 ({ spin_lock(&all_mddevs_lock); \
196 tmp = tmp->next;}) \
197 )
198
199
NeilBrown409c57f2009-03-31 14:39:39 +1100200/* Rather than calling directly into the personality make_request function,
201 * IO requests come here first so that we can check if the device is
202 * being suspended pending a reconfiguration.
203 * We hold a refcount over the call to ->make_request. By the time that
204 * call has finished, the bio has been linked into some internal structure
205 * and so is visible to ->quiesce(), so we don't need the refcount any more.
206 */
207static int md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
NeilBrown409c57f2009-03-31 14:39:39 +1100209 mddev_t *mddev = q->queuedata;
210 int rv;
211 if (mddev == NULL || mddev->pers == NULL) {
212 bio_io_error(bio);
213 return 0;
214 }
215 rcu_read_lock();
NeilBrowna2826aa2009-12-14 12:49:49 +1100216 if (mddev->suspended || mddev->barrier) {
NeilBrown409c57f2009-03-31 14:39:39 +1100217 DEFINE_WAIT(__wait);
218 for (;;) {
219 prepare_to_wait(&mddev->sb_wait, &__wait,
220 TASK_UNINTERRUPTIBLE);
NeilBrowna2826aa2009-12-14 12:49:49 +1100221 if (!mddev->suspended && !mddev->barrier)
NeilBrown409c57f2009-03-31 14:39:39 +1100222 break;
223 rcu_read_unlock();
224 schedule();
225 rcu_read_lock();
226 }
227 finish_wait(&mddev->sb_wait, &__wait);
228 }
229 atomic_inc(&mddev->active_io);
230 rcu_read_unlock();
231 rv = mddev->pers->make_request(q, bio);
232 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
233 wake_up(&mddev->sb_wait);
234
235 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
NeilBrown409c57f2009-03-31 14:39:39 +1100238static void mddev_suspend(mddev_t *mddev)
239{
240 BUG_ON(mddev->suspended);
241 mddev->suspended = 1;
242 synchronize_rcu();
243 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
244 mddev->pers->quiesce(mddev, 1);
245 md_unregister_thread(mddev->thread);
246 mddev->thread = NULL;
247 /* we now know that no code is executing in the personality module,
248 * except possibly the tail end of a ->bi_end_io function, but that
249 * is certain to complete before the module has a chance to get
250 * unloaded
251 */
252}
253
254static void mddev_resume(mddev_t *mddev)
255{
256 mddev->suspended = 0;
257 wake_up(&mddev->sb_wait);
258 mddev->pers->quiesce(mddev, 0);
259}
260
NeilBrown3fa841d2009-09-23 18:10:29 +1000261int mddev_congested(mddev_t *mddev, int bits)
262{
NeilBrowna2826aa2009-12-14 12:49:49 +1100263 if (mddev->barrier)
264 return 1;
NeilBrown3fa841d2009-09-23 18:10:29 +1000265 return mddev->suspended;
266}
267EXPORT_SYMBOL(mddev_congested);
268
NeilBrowna2826aa2009-12-14 12:49:49 +1100269/*
270 * Generic barrier handling for md
271 */
272
273#define POST_REQUEST_BARRIER ((void*)1)
274
275static void md_end_barrier(struct bio *bio, int err)
276{
277 mdk_rdev_t *rdev = bio->bi_private;
278 mddev_t *mddev = rdev->mddev;
279 if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER)
280 set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags);
281
282 rdev_dec_pending(rdev, mddev);
283
284 if (atomic_dec_and_test(&mddev->flush_pending)) {
285 if (mddev->barrier == POST_REQUEST_BARRIER) {
286 /* This was a post-request barrier */
287 mddev->barrier = NULL;
288 wake_up(&mddev->sb_wait);
289 } else
290 /* The pre-request barrier has finished */
291 schedule_work(&mddev->barrier_work);
292 }
293 bio_put(bio);
294}
295
296static void submit_barriers(mddev_t *mddev)
297{
298 mdk_rdev_t *rdev;
299
300 rcu_read_lock();
301 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
302 if (rdev->raid_disk >= 0 &&
303 !test_bit(Faulty, &rdev->flags)) {
304 /* Take two references, one is dropped
305 * when request finishes, one after
306 * we reclaim rcu_read_lock
307 */
308 struct bio *bi;
309 atomic_inc(&rdev->nr_pending);
310 atomic_inc(&rdev->nr_pending);
311 rcu_read_unlock();
312 bi = bio_alloc(GFP_KERNEL, 0);
313 bi->bi_end_io = md_end_barrier;
314 bi->bi_private = rdev;
315 bi->bi_bdev = rdev->bdev;
316 atomic_inc(&mddev->flush_pending);
317 submit_bio(WRITE_BARRIER, bi);
318 rcu_read_lock();
319 rdev_dec_pending(rdev, mddev);
320 }
321 rcu_read_unlock();
322}
323
324static void md_submit_barrier(struct work_struct *ws)
325{
326 mddev_t *mddev = container_of(ws, mddev_t, barrier_work);
327 struct bio *bio = mddev->barrier;
328
329 atomic_set(&mddev->flush_pending, 1);
330
331 if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags))
332 bio_endio(bio, -EOPNOTSUPP);
333 else if (bio->bi_size == 0)
334 /* an empty barrier - all done */
335 bio_endio(bio, 0);
336 else {
337 bio->bi_rw &= ~(1<<BIO_RW_BARRIER);
338 if (mddev->pers->make_request(mddev->queue, bio))
339 generic_make_request(bio);
340 mddev->barrier = POST_REQUEST_BARRIER;
341 submit_barriers(mddev);
342 }
343 if (atomic_dec_and_test(&mddev->flush_pending)) {
344 mddev->barrier = NULL;
345 wake_up(&mddev->sb_wait);
346 }
347}
348
349void md_barrier_request(mddev_t *mddev, struct bio *bio)
350{
351 spin_lock_irq(&mddev->write_lock);
352 wait_event_lock_irq(mddev->sb_wait,
353 !mddev->barrier,
354 mddev->write_lock, /*nothing*/);
355 mddev->barrier = bio;
356 spin_unlock_irq(&mddev->write_lock);
357
358 atomic_set(&mddev->flush_pending, 1);
359 INIT_WORK(&mddev->barrier_work, md_submit_barrier);
360
361 submit_barriers(mddev);
362
363 if (atomic_dec_and_test(&mddev->flush_pending))
364 schedule_work(&mddev->barrier_work);
365}
366EXPORT_SYMBOL(md_barrier_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368static inline mddev_t *mddev_get(mddev_t *mddev)
369{
370 atomic_inc(&mddev->active);
371 return mddev;
372}
373
Dan Williams5fd3a172009-03-04 00:57:25 -0700374static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376static void mddev_put(mddev_t *mddev)
377{
378 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
379 return;
NeilBrownd3374822009-01-09 08:31:10 +1100380 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
381 !mddev->hold_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 list_del(&mddev->all_mddevs);
NeilBrownd3374822009-01-09 08:31:10 +1100383 if (mddev->gendisk) {
384 /* we did a probe so need to clean up.
385 * Call schedule_work inside the spinlock
386 * so that flush_scheduled_work() after
387 * mddev_find will succeed in waiting for the
388 * work to be done.
389 */
390 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
391 schedule_work(&mddev->del_work);
392 } else
393 kfree(mddev);
394 }
395 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
398static mddev_t * mddev_find(dev_t unit)
399{
400 mddev_t *mddev, *new = NULL;
401
402 retry:
403 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
NeilBrownefeb53c2009-01-09 08:31:10 +1100405 if (unit) {
406 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
407 if (mddev->unit == unit) {
408 mddev_get(mddev);
409 spin_unlock(&all_mddevs_lock);
410 kfree(new);
411 return mddev;
412 }
413
414 if (new) {
415 list_add(&new->all_mddevs, &all_mddevs);
416 spin_unlock(&all_mddevs_lock);
417 new->hold_active = UNTIL_IOCTL;
418 return new;
419 }
420 } else if (new) {
421 /* find an unused unit number */
422 static int next_minor = 512;
423 int start = next_minor;
424 int is_free = 0;
425 int dev = 0;
426 while (!is_free) {
427 dev = MKDEV(MD_MAJOR, next_minor);
428 next_minor++;
429 if (next_minor > MINORMASK)
430 next_minor = 0;
431 if (next_minor == start) {
432 /* Oh dear, all in use. */
433 spin_unlock(&all_mddevs_lock);
434 kfree(new);
435 return NULL;
436 }
437
438 is_free = 1;
439 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
440 if (mddev->unit == dev) {
441 is_free = 0;
442 break;
443 }
444 }
445 new->unit = dev;
446 new->md_minor = MINOR(dev);
447 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 list_add(&new->all_mddevs, &all_mddevs);
449 spin_unlock(&all_mddevs_lock);
450 return new;
451 }
452 spin_unlock(&all_mddevs_lock);
453
NeilBrown9ffae0c2006-01-06 00:20:32 -0800454 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (!new)
456 return NULL;
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 new->unit = unit;
459 if (MAJOR(unit) == MD_MAJOR)
460 new->md_minor = MINOR(unit);
461 else
462 new->md_minor = MINOR(unit) >> MdpMinorShift;
463
NeilBrownc8c00a62009-08-10 12:50:52 +1000464 mutex_init(&new->open_mutex);
NeilBrowndf5b89b2006-03-27 01:18:20 -0800465 mutex_init(&new->reconfig_mutex);
NeilBrownc3d97142009-12-14 12:49:52 +1100466 mutex_init(&new->bitmap_info.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 INIT_LIST_HEAD(&new->disks);
468 INIT_LIST_HEAD(&new->all_mddevs);
469 init_timer(&new->safemode_timer);
470 atomic_set(&new->active, 1);
NeilBrownf2ea68c2008-07-21 17:05:25 +1000471 atomic_set(&new->openers, 0);
NeilBrown409c57f2009-03-31 14:39:39 +1100472 atomic_set(&new->active_io, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700473 spin_lock_init(&new->write_lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100474 atomic_set(&new->flush_pending, 0);
NeilBrown3d310eb2005-06-21 17:17:26 -0700475 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700476 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700477 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000478 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800479 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700480 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 goto retry;
483}
484
485static inline int mddev_lock(mddev_t * mddev)
486{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800487 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
Dan Williamsb522adc2009-03-31 15:00:31 +1100490static inline int mddev_is_locked(mddev_t *mddev)
491{
492 return mutex_is_locked(&mddev->reconfig_mutex);
493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495static inline int mddev_trylock(mddev_t * mddev)
496{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800497 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
500static inline void mddev_unlock(mddev_t * mddev)
501{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800502 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
NeilBrown005eca52005-08-22 13:11:08 -0700504 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
NeilBrown2989ddb2006-01-06 00:20:43 -0800507static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100509 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100511 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (rdev->desc_nr == nr)
513 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return NULL;
516}
517
518static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
519{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 mdk_rdev_t *rdev;
521
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100522 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (rdev->bdev->bd_dev == dev)
524 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return NULL;
527}
528
NeilBrownd9d166c2006-01-06 00:20:51 -0800529static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800530{
531 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800532 list_for_each_entry(pers, &pers_list, list) {
533 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800534 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800535 if (strcmp(pers->name, clevel)==0)
536 return pers;
537 }
NeilBrown2604b702006-01-06 00:20:36 -0800538 return NULL;
539}
540
Andre Nollb73df2d2008-07-11 22:02:23 +1000541/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700542static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Andre Nollb73df2d2008-07-11 22:02:23 +1000544 sector_t num_sectors = bdev->bd_inode->i_size / 512;
545 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548static int alloc_disk_sb(mdk_rdev_t * rdev)
549{
550 if (rdev->sb_page)
551 MD_BUG();
552
553 rdev->sb_page = alloc_page(GFP_KERNEL);
554 if (!rdev->sb_page) {
555 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000556 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558
559 return 0;
560}
561
562static void free_disk_sb(mdk_rdev_t * rdev)
563{
564 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800565 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 rdev->sb_loaded = 0;
567 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000568 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100569 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571}
572
573
NeilBrown6712ecf2007-09-27 12:47:43 +0200574static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700575{
576 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800577 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700578
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700579 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
580 printk("md: super_written gets error=%d, uptodate=%d\n",
581 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
582 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800583 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700584 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700585
NeilBrowna9701a32005-11-08 21:39:34 -0800586 if (atomic_dec_and_test(&mddev->pending_writes))
587 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700588 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700589}
590
NeilBrown6712ecf2007-09-27 12:47:43 +0200591static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800592{
593 struct bio *bio2 = bio->bi_private;
594 mdk_rdev_t *rdev = bio2->bi_private;
595 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800596
597 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
598 error == -EOPNOTSUPP) {
599 unsigned long flags;
600 /* barriers don't appear to be supported :-( */
601 set_bit(BarriersNotsupp, &rdev->flags);
602 mddev->barriers_work = 0;
603 spin_lock_irqsave(&mddev->write_lock, flags);
604 bio2->bi_next = mddev->biolist;
605 mddev->biolist = bio2;
606 spin_unlock_irqrestore(&mddev->write_lock, flags);
607 wake_up(&mddev->sb_wait);
608 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200609 } else {
610 bio_put(bio2);
611 bio->bi_private = rdev;
612 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800613 }
NeilBrowna9701a32005-11-08 21:39:34 -0800614}
615
NeilBrown7bfa19f2005-06-21 17:17:28 -0700616void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
617 sector_t sector, int size, struct page *page)
618{
619 /* write first size bytes of page to sector of rdev
620 * Increment mddev->pending_writes before returning
621 * and decrement it on completion, waking up sb_wait
622 * if zero is reached.
623 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800624 *
625 * As we might need to resubmit the request if BIO_RW_BARRIER
626 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700627 */
628 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Jens Axboe93dbb392009-02-16 10:25:40 +0100629 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700630
631 bio->bi_bdev = rdev->bdev;
632 bio->bi_sector = sector;
633 bio_add_page(bio, page, size, 0);
634 bio->bi_private = rdev;
635 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800636 bio->bi_rw = rw;
637
NeilBrown7bfa19f2005-06-21 17:17:28 -0700638 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800639 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
640 struct bio *rbio;
641 rw |= (1<<BIO_RW_BARRIER);
642 rbio = bio_clone(bio, GFP_NOIO);
643 rbio->bi_private = bio;
644 rbio->bi_end_io = super_written_barrier;
645 submit_bio(rw, rbio);
646 } else
647 submit_bio(rw, bio);
648}
649
650void md_super_wait(mddev_t *mddev)
651{
652 /* wait for all superblock writes that were scheduled to complete.
653 * if any had to be retried (due to BARRIER problems), retry them
654 */
655 DEFINE_WAIT(wq);
656 for(;;) {
657 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
658 if (atomic_read(&mddev->pending_writes)==0)
659 break;
660 while (mddev->biolist) {
661 struct bio *bio;
662 spin_lock_irq(&mddev->write_lock);
663 bio = mddev->biolist;
664 mddev->biolist = bio->bi_next ;
665 bio->bi_next = NULL;
666 spin_unlock_irq(&mddev->write_lock);
667 submit_bio(bio->bi_rw, bio);
668 }
669 schedule();
670 }
671 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700672}
673
NeilBrown6712ecf2007-09-27 12:47:43 +0200674static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
NeilBrowna654b9d82005-06-21 17:17:27 -0700679int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 struct page *page, int rw)
681{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700682 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 struct completion event;
684 int ret;
685
Jens Axboe93dbb392009-02-16 10:25:40 +0100686 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 bio->bi_bdev = bdev;
689 bio->bi_sector = sector;
690 bio_add_page(bio, page, size, 0);
691 init_completion(&event);
692 bio->bi_private = &event;
693 bio->bi_end_io = bi_complete;
694 submit_bio(rw, bio);
695 wait_for_completion(&event);
696
697 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
698 bio_put(bio);
699 return ret;
700}
NeilBrowna8745db2006-01-06 00:20:34 -0800701EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
NeilBrown0002b272005-09-09 16:23:53 -0700703static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 char b[BDEVNAME_SIZE];
706 if (!rdev->sb_page) {
707 MD_BUG();
708 return -EINVAL;
709 }
710 if (rdev->sb_loaded)
711 return 0;
712
713
Andre Noll0f420352008-07-11 22:02:23 +1000714 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 goto fail;
716 rdev->sb_loaded = 1;
717 return 0;
718
719fail:
720 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
721 bdevname(rdev->bdev,b));
722 return -EINVAL;
723}
724
725static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
726{
Andre Noll05710462008-07-11 22:02:20 +1000727 return sb1->set_uuid0 == sb2->set_uuid0 &&
728 sb1->set_uuid1 == sb2->set_uuid1 &&
729 sb1->set_uuid2 == sb2->set_uuid2 &&
730 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
734{
735 int ret;
736 mdp_super_t *tmp1, *tmp2;
737
738 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
739 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
740
741 if (!tmp1 || !tmp2) {
742 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100743 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 goto abort;
745 }
746
747 *tmp1 = *sb1;
748 *tmp2 = *sb2;
749
750 /*
751 * nr_disks is not constant
752 */
753 tmp1->nr_disks = 0;
754 tmp2->nr_disks = 0;
755
Andre Nollce0c8e02008-07-11 22:02:20 +1000756 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700758 kfree(tmp1);
759 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return ret;
761}
762
NeilBrown4d167f02007-05-09 02:35:37 -0700763
764static u32 md_csum_fold(u32 csum)
765{
766 csum = (csum & 0xffff) + (csum >> 16);
767 return (csum & 0xffff) + (csum >> 16);
768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770static unsigned int calc_sb_csum(mdp_super_t * sb)
771{
NeilBrown4d167f02007-05-09 02:35:37 -0700772 u64 newcsum = 0;
773 u32 *sb32 = (u32*)sb;
774 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 unsigned int disk_csum, csum;
776
777 disk_csum = sb->sb_csum;
778 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700779
780 for (i = 0; i < MD_SB_BYTES/4 ; i++)
781 newcsum += sb32[i];
782 csum = (newcsum & 0xffffffff) + (newcsum>>32);
783
784
785#ifdef CONFIG_ALPHA
786 /* This used to use csum_partial, which was wrong for several
787 * reasons including that different results are returned on
788 * different architectures. It isn't critical that we get exactly
789 * the same return value as before (we always csum_fold before
790 * testing, and that removes any differences). However as we
791 * know that csum_partial always returned a 16bit value on
792 * alphas, do a fold to maximise conformity to previous behaviour.
793 */
794 sb->sb_csum = md_csum_fold(disk_csum);
795#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700797#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return csum;
799}
800
801
802/*
803 * Handle superblock details.
804 * We want to be able to handle multiple superblock formats
805 * so we have a common interface to them all, and an array of
806 * different handlers.
807 * We rely on user-space to write the initial superblock, and support
808 * reading and updating of superblocks.
809 * Interface methods are:
810 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
811 * loads and validates a superblock on dev.
812 * if refdev != NULL, compare superblocks on both devices
813 * Return:
814 * 0 - dev has a superblock that is compatible with refdev
815 * 1 - dev has a superblock that is compatible and newer than refdev
816 * so dev should be used as the refdev in future
817 * -EINVAL superblock incompatible or invalid
818 * -othererror e.g. -EIO
819 *
820 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
821 * Verify that dev is acceptable into mddev.
822 * The first time, mddev->raid_disks will be 0, and data from
823 * dev should be merged in. Subsequent calls check that dev
824 * is new enough. Return 0 or -EINVAL
825 *
826 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
827 * Update the superblock for rdev with data in mddev
828 * This does not write to disc.
829 *
830 */
831
832struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000833 char *name;
834 struct module *owner;
835 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
836 int minor_version);
837 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
838 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
839 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000840 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841};
842
843/*
Andre Noll0894cc32009-06-18 08:49:23 +1000844 * Check that the given mddev has no bitmap.
845 *
846 * This function is called from the run method of all personalities that do not
847 * support bitmaps. It prints an error message and returns non-zero if mddev
848 * has a bitmap. Otherwise, it returns 0.
849 *
850 */
851int md_check_no_bitmap(mddev_t *mddev)
852{
NeilBrownc3d97142009-12-14 12:49:52 +1100853 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000854 return 0;
855 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
856 mdname(mddev), mddev->pers->name);
857 return 1;
858}
859EXPORT_SYMBOL(md_check_no_bitmap);
860
861/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 * load_super for 0.90.0
863 */
864static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
865{
866 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
867 mdp_super_t *sb;
868 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 /*
Andre Noll0f420352008-07-11 22:02:23 +1000871 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 * it's at the end of the disk.
873 *
874 * It also happens to be a multiple of 4Kb.
875 */
Andre Noll0f420352008-07-11 22:02:23 +1000876 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
NeilBrown0002b272005-09-09 16:23:53 -0700878 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (ret) return ret;
880
881 ret = -EINVAL;
882
883 bdevname(rdev->bdev, b);
884 sb = (mdp_super_t*)page_address(rdev->sb_page);
885
886 if (sb->md_magic != MD_SB_MAGIC) {
887 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
888 b);
889 goto abort;
890 }
891
892 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800893 sb->minor_version < 90 ||
894 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
896 sb->major_version, sb->minor_version,
897 b);
898 goto abort;
899 }
900
901 if (sb->raid_disks <= 0)
902 goto abort;
903
NeilBrown4d167f02007-05-09 02:35:37 -0700904 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
906 b);
907 goto abort;
908 }
909
910 rdev->preferred_minor = sb->md_minor;
911 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700912 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 if (sb->level == LEVEL_MULTIPATH)
915 rdev->desc_nr = -1;
916 else
917 rdev->desc_nr = sb->this_disk.number;
918
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700919 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700921 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 __u64 ev1, ev2;
923 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
924 if (!uuid_equal(refsb, sb)) {
925 printk(KERN_WARNING "md: %s has different UUID to %s\n",
926 b, bdevname(refdev->bdev,b2));
927 goto abort;
928 }
929 if (!sb_equal(refsb, sb)) {
930 printk(KERN_WARNING "md: %s has same UUID"
931 " but different superblock to %s\n",
932 b, bdevname(refdev->bdev, b2));
933 goto abort;
934 }
935 ev1 = md_event(sb);
936 ev2 = md_event(refsb);
937 if (ev1 > ev2)
938 ret = 1;
939 else
940 ret = 0;
941 }
NeilBrown8190e752009-06-18 08:48:58 +1000942 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Andre Nolldd8ac332009-03-31 14:33:13 +1100944 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -0800945 /* "this cannot possibly happen" ... */
946 ret = -EINVAL;
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 abort:
949 return ret;
950}
951
952/*
953 * validate_super for 0.90.0
954 */
955static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
956{
957 mdp_disk_t *desc;
958 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700959 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
NeilBrown41158c72005-06-21 17:17:25 -0700961 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800962 clear_bit(Faulty, &rdev->flags);
963 clear_bit(In_sync, &rdev->flags);
964 clear_bit(WriteMostly, &rdev->flags);
965 clear_bit(BarriersNotsupp, &rdev->flags);
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (mddev->raid_disks == 0) {
968 mddev->major_version = 0;
969 mddev->minor_version = sb->minor_version;
970 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800971 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +1000972 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 mddev->ctime = sb->ctime;
974 mddev->utime = sb->utime;
975 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800976 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 mddev->layout = sb->layout;
978 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +1100979 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -0700980 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +1100981 mddev->bitmap_info.offset = 0;
982 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
NeilBrownf6705572006-03-27 01:18:11 -0800984 if (mddev->minor_version >= 91) {
985 mddev->reshape_position = sb->reshape_position;
986 mddev->delta_disks = sb->delta_disks;
987 mddev->new_level = sb->new_level;
988 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +1000989 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -0800990 } else {
991 mddev->reshape_position = MaxSector;
992 mddev->delta_disks = 0;
993 mddev->new_level = mddev->level;
994 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +1000995 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -0800996 }
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (sb->state & (1<<MD_SB_CLEAN))
999 mddev->recovery_cp = MaxSector;
1000 else {
1001 if (sb->events_hi == sb->cp_events_hi &&
1002 sb->events_lo == sb->cp_events_lo) {
1003 mddev->recovery_cp = sb->recovery_cp;
1004 } else
1005 mddev->recovery_cp = 0;
1006 }
1007
1008 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1009 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1010 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1011 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1012
1013 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001014
1015 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001016 mddev->bitmap_info.file == NULL)
1017 mddev->bitmap_info.offset =
1018 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001019
NeilBrown41158c72005-06-21 17:17:25 -07001020 } else if (mddev->pers == NULL) {
1021 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 ++ev1;
1023 if (ev1 < mddev->events)
1024 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001025 } else if (mddev->bitmap) {
1026 /* if adding to array with a bitmap, then we can accept an
1027 * older device ... but not too old.
1028 */
NeilBrown41158c72005-06-21 17:17:25 -07001029 if (ev1 < mddev->bitmap->events_cleared)
1030 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001031 } else {
1032 if (ev1 < mddev->events)
1033 /* just a hot-add of a new device, leave raid_disk at -1 */
1034 return 0;
1035 }
NeilBrown41158c72005-06-21 17:17:25 -07001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 desc = sb->disks + rdev->desc_nr;
1039
1040 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001041 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001042 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1043 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001044 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001046 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1047 /* active but not in sync implies recovery up to
1048 * reshape position. We don't know exactly where
1049 * that is, so set to zero for now */
1050 if (mddev->minor_version >= 91) {
1051 rdev->recovery_offset = 0;
1052 rdev->raid_disk = desc->raid_disk;
1053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001055 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1056 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001057 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001058 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return 0;
1060}
1061
1062/*
1063 * sync_super for 0.90.0
1064 */
1065static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1066{
1067 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 mdk_rdev_t *rdev2;
1069 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 /* make rdev->sb match mddev data..
1073 *
1074 * 1/ zero out disks
1075 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1076 * 3/ any empty disks < next_spare become removed
1077 *
1078 * disks[0] gets initialised to REMOVED because
1079 * we cannot be sure from other fields if it has
1080 * been initialised or not.
1081 */
1082 int i;
1083 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1084
NeilBrown61181562005-09-09 16:24:02 -07001085 rdev->sb_size = MD_SB_BYTES;
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 sb = (mdp_super_t*)page_address(rdev->sb_page);
1088
1089 memset(sb, 0, sizeof(*sb));
1090
1091 sb->md_magic = MD_SB_MAGIC;
1092 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 sb->patch_version = mddev->patch_version;
1094 sb->gvalid_words = 0; /* ignored */
1095 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1096 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1097 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1098 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1099
1100 sb->ctime = mddev->ctime;
1101 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001102 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 sb->raid_disks = mddev->raid_disks;
1104 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001105 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 sb->utime = mddev->utime;
1107 sb->state = 0;
1108 sb->events_hi = (mddev->events>>32);
1109 sb->events_lo = (u32)mddev->events;
1110
NeilBrownf6705572006-03-27 01:18:11 -08001111 if (mddev->reshape_position == MaxSector)
1112 sb->minor_version = 90;
1113 else {
1114 sb->minor_version = 91;
1115 sb->reshape_position = mddev->reshape_position;
1116 sb->new_level = mddev->new_level;
1117 sb->delta_disks = mddev->delta_disks;
1118 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001119 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001120 }
1121 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (mddev->in_sync)
1123 {
1124 sb->recovery_cp = mddev->recovery_cp;
1125 sb->cp_events_hi = (mddev->events>>32);
1126 sb->cp_events_lo = (u32)mddev->events;
1127 if (mddev->recovery_cp == MaxSector)
1128 sb->state = (1<< MD_SB_CLEAN);
1129 } else
1130 sb->recovery_cp = 0;
1131
1132 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001133 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
NeilBrownc3d97142009-12-14 12:49:52 +11001135 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001136 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001139 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001141 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001142 int is_active = test_bit(In_sync, &rdev2->flags);
1143
1144 if (rdev2->raid_disk >= 0 &&
1145 sb->minor_version >= 91)
1146 /* we have nowhere to store the recovery_offset,
1147 * but if it is not below the reshape_position,
1148 * we can piggy-back on that.
1149 */
1150 is_active = 1;
1151 if (rdev2->raid_disk < 0 ||
1152 test_bit(Faulty, &rdev2->flags))
1153 is_active = 0;
1154 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001155 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001157 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001158 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 d = &sb->disks[rdev2->desc_nr];
1160 nr_disks++;
1161 d->number = rdev2->desc_nr;
1162 d->major = MAJOR(rdev2->bdev->bd_dev);
1163 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001164 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 d->raid_disk = rdev2->raid_disk;
1166 else
1167 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001168 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001170 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001172 if (test_bit(In_sync, &rdev2->flags))
1173 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 active++;
1175 working++;
1176 } else {
1177 d->state = 0;
1178 spare++;
1179 working++;
1180 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001181 if (test_bit(WriteMostly, &rdev2->flags))
1182 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 /* now set the "removed" and "faulty" bits on any missing devices */
1185 for (i=0 ; i < mddev->raid_disks ; i++) {
1186 mdp_disk_t *d = &sb->disks[i];
1187 if (d->state == 0 && d->number == 0) {
1188 d->number = i;
1189 d->raid_disk = i;
1190 d->state = (1<<MD_DISK_REMOVED);
1191 d->state |= (1<<MD_DISK_FAULTY);
1192 failed++;
1193 }
1194 }
1195 sb->nr_disks = nr_disks;
1196 sb->active_disks = active;
1197 sb->working_disks = working;
1198 sb->failed_disks = failed;
1199 sb->spare_disks = spare;
1200
1201 sb->this_disk = sb->disks[rdev->desc_nr];
1202 sb->sb_csum = calc_sb_csum(sb);
1203}
1204
1205/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001206 * rdev_size_change for 0.90.0
1207 */
1208static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001209super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001210{
Andre Noll58c0fed2009-03-31 14:33:13 +11001211 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001212 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001213 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001214 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001215 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001216 if (!num_sectors || num_sectors > rdev->sb_start)
1217 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001218 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001219 rdev->sb_page);
1220 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001221 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001222}
1223
1224
1225/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 * version 1 superblock
1227 */
1228
NeilBrown1c05b4b2006-10-21 10:24:08 -07001229static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001231 __le32 disk_csum;
1232 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 unsigned long long newcsum;
1234 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001235 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 int i;
1237
1238 disk_csum = sb->sb_csum;
1239 sb->sb_csum = 0;
1240 newcsum = 0;
1241 for (i=0; size>=4; size -= 4 )
1242 newcsum += le32_to_cpu(*isuper++);
1243
1244 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001245 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1248 sb->sb_csum = disk_csum;
1249 return cpu_to_le32(csum);
1250}
1251
1252static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1253{
1254 struct mdp_superblock_1 *sb;
1255 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001256 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001258 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 /*
Andre Noll0f420352008-07-11 22:02:23 +10001261 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 * It is always aligned to a 4K boundary and
1263 * depeding on minor_version, it can be:
1264 * 0: At least 8K, but less than 12K, from end of device
1265 * 1: At start of device
1266 * 2: 4K from start of device.
1267 */
1268 switch(minor_version) {
1269 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001270 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1271 sb_start -= 8*2;
1272 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 break;
1274 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001275 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 break;
1277 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001278 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 break;
1280 default:
1281 return -EINVAL;
1282 }
Andre Noll0f420352008-07-11 22:02:23 +10001283 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
NeilBrown0002b272005-09-09 16:23:53 -07001285 /* superblock is rarely larger than 1K, but it can be larger,
1286 * and it is safe to read 4k, so we do that
1287 */
1288 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (ret) return ret;
1290
1291
1292 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1293
1294 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1295 sb->major_version != cpu_to_le32(1) ||
1296 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001297 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001298 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return -EINVAL;
1300
1301 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1302 printk("md: invalid superblock checksum on %s\n",
1303 bdevname(rdev->bdev,b));
1304 return -EINVAL;
1305 }
1306 if (le64_to_cpu(sb->data_size) < 10) {
1307 printk("md: data_size too small on %s\n",
1308 bdevname(rdev->bdev,b));
1309 return -EINVAL;
1310 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 rdev->preferred_minor = 0xffff;
1313 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001314 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
NeilBrown0002b272005-09-09 16:23:53 -07001316 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001317 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001318 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001319 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1320
1321 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001322 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001323 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001324
NeilBrown31b65a02006-07-10 04:44:14 -07001325 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1326 rdev->desc_nr = -1;
1327 else
1328 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1329
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001330 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001331 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001332 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 __u64 ev1, ev2;
1334 struct mdp_superblock_1 *refsb =
1335 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1336
1337 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1338 sb->level != refsb->level ||
1339 sb->layout != refsb->layout ||
1340 sb->chunksize != refsb->chunksize) {
1341 printk(KERN_WARNING "md: %s has strangely different"
1342 " superblock to %s\n",
1343 bdevname(rdev->bdev,b),
1344 bdevname(refdev->bdev,b2));
1345 return -EINVAL;
1346 }
1347 ev1 = le64_to_cpu(sb->events);
1348 ev2 = le64_to_cpu(refsb->events);
1349
1350 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001351 ret = 1;
1352 else
1353 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
NeilBrowna1801f82008-03-04 14:29:31 -08001355 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001356 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1357 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001359 rdev->sectors = rdev->sb_start;
1360 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001362 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001363 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001364 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001365 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366}
1367
1368static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1369{
1370 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001371 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
NeilBrown41158c72005-06-21 17:17:25 -07001373 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001374 clear_bit(Faulty, &rdev->flags);
1375 clear_bit(In_sync, &rdev->flags);
1376 clear_bit(WriteMostly, &rdev->flags);
1377 clear_bit(BarriersNotsupp, &rdev->flags);
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (mddev->raid_disks == 0) {
1380 mddev->major_version = 1;
1381 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001382 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001383 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1385 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1386 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001387 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 mddev->layout = le32_to_cpu(sb->layout);
1389 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001390 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001391 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001392 mddev->bitmap_info.offset = 0;
1393 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1396 memcpy(mddev->uuid, sb->set_uuid, 16);
1397
1398 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001399
NeilBrown71c08052005-09-09 16:23:51 -07001400 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001401 mddev->bitmap_info.file == NULL )
1402 mddev->bitmap_info.offset =
1403 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001404
NeilBrownf6705572006-03-27 01:18:11 -08001405 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1406 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1407 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1408 mddev->new_level = le32_to_cpu(sb->new_level);
1409 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001410 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001411 } else {
1412 mddev->reshape_position = MaxSector;
1413 mddev->delta_disks = 0;
1414 mddev->new_level = mddev->level;
1415 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001416 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001417 }
1418
NeilBrown41158c72005-06-21 17:17:25 -07001419 } else if (mddev->pers == NULL) {
1420 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 ++ev1;
1422 if (ev1 < mddev->events)
1423 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001424 } else if (mddev->bitmap) {
1425 /* If adding to array with a bitmap, then we can accept an
1426 * older device, but not too old.
1427 */
NeilBrown41158c72005-06-21 17:17:25 -07001428 if (ev1 < mddev->bitmap->events_cleared)
1429 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001430 } else {
1431 if (ev1 < mddev->events)
1432 /* just a hot-add of a new device, leave raid_disk at -1 */
1433 return 0;
1434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (mddev->level != LEVEL_MULTIPATH) {
1436 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001437 if (rdev->desc_nr < 0 ||
1438 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1439 role = 0xffff;
1440 rdev->desc_nr = -1;
1441 } else
1442 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 switch(role) {
1444 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 break;
1446 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001447 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 break;
1449 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001450 if ((le32_to_cpu(sb->feature_map) &
1451 MD_FEATURE_RECOVERY_OFFSET))
1452 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1453 else
1454 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 rdev->raid_disk = role;
1456 break;
1457 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001458 if (sb->devflags & WriteMostly1)
1459 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001460 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001461 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return 0;
1464}
1465
1466static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1467{
1468 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 mdk_rdev_t *rdev2;
1470 int max_dev, i;
1471 /* make rdev->sb match mddev and rdev data. */
1472
1473 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1474
1475 sb->feature_map = 0;
1476 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001477 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 memset(sb->pad1, 0, sizeof(sb->pad1));
1479 memset(sb->pad2, 0, sizeof(sb->pad2));
1480 memset(sb->pad3, 0, sizeof(sb->pad3));
1481
1482 sb->utime = cpu_to_le64((__u64)mddev->utime);
1483 sb->events = cpu_to_le64(mddev->events);
1484 if (mddev->in_sync)
1485 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1486 else
1487 sb->resync_offset = cpu_to_le64(0);
1488
NeilBrown1c05b4b2006-10-21 10:24:08 -07001489 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001490
NeilBrownf0ca3402006-02-02 14:28:04 -08001491 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001492 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001493 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001494 sb->level = cpu_to_le32(mddev->level);
1495 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001496
NeilBrownc3d97142009-12-14 12:49:52 +11001497 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1498 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001499 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001500 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001501
1502 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001503 !test_bit(In_sync, &rdev->flags)) {
NeilBrown97e4f422009-03-31 14:33:13 +11001504 if (rdev->recovery_offset > 0) {
1505 sb->feature_map |=
1506 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1507 sb->recovery_offset =
1508 cpu_to_le64(rdev->recovery_offset);
1509 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001510 }
1511
NeilBrownf6705572006-03-27 01:18:11 -08001512 if (mddev->reshape_position != MaxSector) {
1513 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1514 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1515 sb->new_layout = cpu_to_le32(mddev->new_layout);
1516 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1517 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001518 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001519 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001522 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (rdev2->desc_nr+1 > max_dev)
1524 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001525
NeilBrown70471da2009-08-03 10:59:57 +10001526 if (max_dev > le32_to_cpu(sb->max_dev)) {
1527 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001528 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001529 rdev->sb_size = max_dev * 2 + 256;
1530 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1531 if (rdev->sb_size & bmask)
1532 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 for (i=0; i<max_dev;i++)
1535 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1536
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001537 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001539 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001541 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001543 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1544 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 else
1546 sb->dev_roles[i] = cpu_to_le16(0xffff);
1547 }
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 sb->sb_csum = calc_sb_1_csum(sb);
1550}
1551
Chris Webb0cd17fe2008-06-28 08:31:46 +10001552static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001553super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001554{
1555 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001556 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001557 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001558 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001559 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001560 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001561 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1562 max_sectors -= rdev->data_offset;
1563 if (!num_sectors || num_sectors > max_sectors)
1564 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001565 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001566 /* minor version 0 with bitmap we can't move */
1567 return 0;
1568 } else {
1569 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001570 sector_t sb_start;
1571 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1572 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001573 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001574 if (!num_sectors || num_sectors > max_sectors)
1575 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001576 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001577 }
1578 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001579 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001580 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001581 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001582 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001583 rdev->sb_page);
1584 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001585 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001586}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Adrian Bunk75c96f82005-05-05 16:16:09 -07001588static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 [0] = {
1590 .name = "0.90.0",
1591 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001592 .load_super = super_90_load,
1593 .validate_super = super_90_validate,
1594 .sync_super = super_90_sync,
1595 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 },
1597 [1] = {
1598 .name = "md-1",
1599 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001600 .load_super = super_1_load,
1601 .validate_super = super_1_validate,
1602 .sync_super = super_1_sync,
1603 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 },
1605};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1608{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001609 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
NeilBrown4b809912008-07-21 17:05:25 +10001611 rcu_read_lock();
1612 rdev_for_each_rcu(rdev, mddev1)
1613 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001614 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001615 rdev2->bdev->bd_contains) {
1616 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001617 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001618 }
1619 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 return 0;
1621}
1622
1623static LIST_HEAD(pending_raid_disks);
1624
Andre Nollac5e7112009-08-03 10:59:47 +10001625/*
1626 * Try to register data integrity profile for an mddev
1627 *
1628 * This is called when an array is started and after a disk has been kicked
1629 * from the array. It only succeeds if all working and active component devices
1630 * are integrity capable with matching profiles.
1631 */
1632int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001633{
Andre Nollac5e7112009-08-03 10:59:47 +10001634 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001635
Andre Nollac5e7112009-08-03 10:59:47 +10001636 if (list_empty(&mddev->disks))
1637 return 0; /* nothing to do */
1638 if (blk_get_integrity(mddev->gendisk))
1639 return 0; /* already registered */
1640 list_for_each_entry(rdev, &mddev->disks, same_set) {
1641 /* skip spares and non-functional disks */
1642 if (test_bit(Faulty, &rdev->flags))
1643 continue;
1644 if (rdev->raid_disk < 0)
1645 continue;
1646 /*
1647 * If at least one rdev is not integrity capable, we can not
1648 * enable data integrity for the md device.
1649 */
1650 if (!bdev_get_integrity(rdev->bdev))
1651 return -EINVAL;
1652 if (!reference) {
1653 /* Use the first rdev as the reference */
1654 reference = rdev;
1655 continue;
1656 }
1657 /* does this rdev's profile match the reference profile? */
1658 if (blk_integrity_compare(reference->bdev->bd_disk,
1659 rdev->bdev->bd_disk) < 0)
1660 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001661 }
Andre Nollac5e7112009-08-03 10:59:47 +10001662 /*
1663 * All component devices are integrity capable and have matching
1664 * profiles, register the common profile for the md device.
1665 */
1666 if (blk_integrity_register(mddev->gendisk,
1667 bdev_get_integrity(reference->bdev)) != 0) {
1668 printk(KERN_ERR "md: failed to register integrity for %s\n",
1669 mdname(mddev));
1670 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001671 }
Andre Nollac5e7112009-08-03 10:59:47 +10001672 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1673 mdname(mddev));
1674 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001675}
Andre Nollac5e7112009-08-03 10:59:47 +10001676EXPORT_SYMBOL(md_integrity_register);
1677
1678/* Disable data integrity if non-capable/non-matching disk is being added */
1679void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1680{
1681 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1682 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1683
1684 if (!bi_mddev) /* nothing to do */
1685 return;
1686 if (rdev->raid_disk < 0) /* skip spares */
1687 return;
1688 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1689 rdev->bdev->bd_disk) >= 0)
1690 return;
1691 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1692 blk_integrity_unregister(mddev->gendisk);
1693}
1694EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1697{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001698 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001699 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001700 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001701 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703 if (rdev->mddev) {
1704 MD_BUG();
1705 return -EINVAL;
1706 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001707
1708 /* prevent duplicates */
1709 if (find_rdev(mddev, rdev->bdev->bd_dev))
1710 return -EEXIST;
1711
Andre Nolldd8ac332009-03-31 14:33:13 +11001712 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1713 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1714 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001715 if (mddev->pers) {
1716 /* Cannot change size, so fail
1717 * If mddev->level <= 0, then we don't care
1718 * about aligning sizes (e.g. linear)
1719 */
1720 if (mddev->level > 0)
1721 return -ENOSPC;
1722 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001723 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 /* Verify rdev->desc_nr is unique.
1727 * If it is -1, assign a free number, else
1728 * check number is not in use
1729 */
1730 if (rdev->desc_nr < 0) {
1731 int choice = 0;
1732 if (mddev->pers) choice = mddev->raid_disks;
1733 while (find_rdev_nr(mddev, choice))
1734 choice++;
1735 rdev->desc_nr = choice;
1736 } else {
1737 if (find_rdev_nr(mddev, rdev->desc_nr))
1738 return -EBUSY;
1739 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001740 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1741 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1742 mdname(mddev), mddev->max_disks);
1743 return -EBUSY;
1744 }
NeilBrown19133a42005-11-08 21:39:35 -08001745 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001746 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001747 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001750 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001751
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001752 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001753 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001754
Tejun Heo0762b8b2008-08-25 19:56:12 +09001755 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001756 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1757 kobject_del(&rdev->kobj);
1758 goto fail;
1759 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001760 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1761
NeilBrown4b809912008-07-21 17:05:25 +10001762 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001763 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001764
1765 /* May as well allow recovery to be retried once */
1766 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001769
1770 fail:
1771 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1772 b, mdname(mddev));
1773 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
NeilBrown177a99b2008-02-06 01:39:56 -08001776static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001777{
1778 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1779 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001780 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001781}
1782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1784{
1785 char b[BDEVNAME_SIZE];
1786 if (!rdev->mddev) {
1787 MD_BUG();
1788 return;
1789 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001790 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001791 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1793 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001794 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001795 sysfs_put(rdev->sysfs_state);
1796 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001797 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001798 * writing to 'remove' to "dev/state". We also need
1799 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001800 */
NeilBrown4b809912008-07-21 17:05:25 +10001801 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001802 INIT_WORK(&rdev->del_work, md_delayed_delete);
1803 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001804 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
1807/*
1808 * prevent the device from being mounted, repartitioned or
1809 * otherwise reused by a RAID array (or any other kernel
1810 * subsystem), by bd_claiming the device.
1811 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001812static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
1814 int err = 0;
1815 struct block_device *bdev;
1816 char b[BDEVNAME_SIZE];
1817
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001818 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (IS_ERR(bdev)) {
1820 printk(KERN_ERR "md: could not open %s.\n",
1821 __bdevname(dev, b));
1822 return PTR_ERR(bdev);
1823 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001824 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (err) {
1826 printk(KERN_ERR "md: could not bd_claim %s.\n",
1827 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001828 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 return err;
1830 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001831 if (!shared)
1832 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 rdev->bdev = bdev;
1834 return err;
1835}
1836
1837static void unlock_rdev(mdk_rdev_t *rdev)
1838{
1839 struct block_device *bdev = rdev->bdev;
1840 rdev->bdev = NULL;
1841 if (!bdev)
1842 MD_BUG();
1843 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001844 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845}
1846
1847void md_autodetect_dev(dev_t dev);
1848
1849static void export_rdev(mdk_rdev_t * rdev)
1850{
1851 char b[BDEVNAME_SIZE];
1852 printk(KERN_INFO "md: export_rdev(%s)\n",
1853 bdevname(rdev->bdev,b));
1854 if (rdev->mddev)
1855 MD_BUG();
1856 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001858 if (test_bit(AutoDetected, &rdev->flags))
1859 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860#endif
1861 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001862 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863}
1864
1865static void kick_rdev_from_array(mdk_rdev_t * rdev)
1866{
1867 unbind_rdev_from_array(rdev);
1868 export_rdev(rdev);
1869}
1870
1871static void export_array(mddev_t *mddev)
1872{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001873 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
NeilBrownd089c6a2008-02-06 01:39:59 -08001875 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 if (!rdev->mddev) {
1877 MD_BUG();
1878 continue;
1879 }
1880 kick_rdev_from_array(rdev);
1881 }
1882 if (!list_empty(&mddev->disks))
1883 MD_BUG();
1884 mddev->raid_disks = 0;
1885 mddev->major_version = 0;
1886}
1887
1888static void print_desc(mdp_disk_t *desc)
1889{
1890 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1891 desc->major,desc->minor,desc->raid_disk,desc->state);
1892}
1893
Cheng Renquancd2ac932009-01-09 08:31:08 +11001894static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
1896 int i;
1897
1898 printk(KERN_INFO
1899 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1900 sb->major_version, sb->minor_version, sb->patch_version,
1901 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1902 sb->ctime);
1903 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1904 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1905 sb->md_minor, sb->layout, sb->chunk_size);
1906 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1907 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1908 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1909 sb->failed_disks, sb->spare_disks,
1910 sb->sb_csum, (unsigned long)sb->events_lo);
1911
1912 printk(KERN_INFO);
1913 for (i = 0; i < MD_SB_DISKS; i++) {
1914 mdp_disk_t *desc;
1915
1916 desc = sb->disks + i;
1917 if (desc->number || desc->major || desc->minor ||
1918 desc->raid_disk || (desc->state && (desc->state != 4))) {
1919 printk(" D %2d: ", i);
1920 print_desc(desc);
1921 }
1922 }
1923 printk(KERN_INFO "md: THIS: ");
1924 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
1926
Cheng Renquancd2ac932009-01-09 08:31:08 +11001927static void print_sb_1(struct mdp_superblock_1 *sb)
1928{
1929 __u8 *uuid;
1930
1931 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001932 printk(KERN_INFO
1933 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1934 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1935 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001936 le32_to_cpu(sb->major_version),
1937 le32_to_cpu(sb->feature_map),
1938 uuid[0], uuid[1], uuid[2], uuid[3],
1939 uuid[4], uuid[5], uuid[6], uuid[7],
1940 uuid[8], uuid[9], uuid[10], uuid[11],
1941 uuid[12], uuid[13], uuid[14], uuid[15],
1942 sb->set_name,
1943 (unsigned long long)le64_to_cpu(sb->ctime)
1944 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1945
1946 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001947 printk(KERN_INFO
1948 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11001949 " RO:%llu\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001950 "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1951 ":%02x%02x%02x%02x%02x%02x\n"
1952 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1953 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001954 le32_to_cpu(sb->level),
1955 (unsigned long long)le64_to_cpu(sb->size),
1956 le32_to_cpu(sb->raid_disks),
1957 le32_to_cpu(sb->layout),
1958 le32_to_cpu(sb->chunksize),
1959 (unsigned long long)le64_to_cpu(sb->data_offset),
1960 (unsigned long long)le64_to_cpu(sb->data_size),
1961 (unsigned long long)le64_to_cpu(sb->super_offset),
1962 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1963 le32_to_cpu(sb->dev_number),
1964 uuid[0], uuid[1], uuid[2], uuid[3],
1965 uuid[4], uuid[5], uuid[6], uuid[7],
1966 uuid[8], uuid[9], uuid[10], uuid[11],
1967 uuid[12], uuid[13], uuid[14], uuid[15],
1968 sb->devflags,
1969 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1970 (unsigned long long)le64_to_cpu(sb->events),
1971 (unsigned long long)le64_to_cpu(sb->resync_offset),
1972 le32_to_cpu(sb->sb_csum),
1973 le32_to_cpu(sb->max_dev)
1974 );
1975}
1976
1977static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
1979 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11001980 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1981 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08001982 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1983 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11001985 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1986 switch (major_version) {
1987 case 0:
1988 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1989 break;
1990 case 1:
1991 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1992 break;
1993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 } else
1995 printk(KERN_INFO "md: no rdev superblock!\n");
1996}
1997
Adrian Bunk5e563412006-06-26 00:27:42 -07001998static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002000 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 mdk_rdev_t *rdev;
2002 mddev_t *mddev;
2003 char b[BDEVNAME_SIZE];
2004
2005 printk("\n");
2006 printk("md: **********************************\n");
2007 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2008 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002009 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
NeilBrown32a76272005-06-21 17:17:14 -07002011 if (mddev->bitmap)
2012 bitmap_print_sb(mddev->bitmap);
2013 else
2014 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002015 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 printk("<%s>", bdevname(rdev->bdev,b));
2017 printk("\n");
2018
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002019 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002020 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
2022 printk("md: **********************************\n");
2023 printk("\n");
2024}
2025
2026
NeilBrown42543762006-06-26 00:27:57 -07002027static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
NeilBrown42543762006-06-26 00:27:57 -07002029 /* Update each superblock (in-memory image), but
2030 * if we are allowed to, skip spares which already
2031 * have the right event counter, or have one earlier
2032 * (which would mean they aren't being marked as dirty
2033 * with the rest of the array)
2034 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
NeilBrown5e865102009-11-12 12:08:04 +11002037 /* First make sure individual recovery_offsets are correct */
2038 list_for_each_entry(rdev, &mddev->disks, same_set) {
2039 if (rdev->raid_disk >= 0 &&
2040 !test_bit(In_sync, &rdev->flags) &&
2041 mddev->curr_resync_completed > rdev->recovery_offset)
2042 rdev->recovery_offset = mddev->curr_resync_completed;
2043
2044 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002045 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002046 if (rdev->sb_events == mddev->events ||
2047 (nospares &&
2048 rdev->raid_disk < 0 &&
2049 (rdev->sb_events&1)==0 &&
2050 rdev->sb_events+1 == mddev->events)) {
2051 /* Don't update this superblock */
2052 rdev->sb_loaded = 2;
2053 } else {
2054 super_types[mddev->major_version].
2055 sync_super(mddev, rdev);
2056 rdev->sb_loaded = 1;
2057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 }
2059}
2060
NeilBrown850b2b42006-10-03 01:15:46 -07002061static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002064 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002065 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
NeilBrown1b57f132009-06-18 08:48:19 +10002067 mddev->utime = get_seconds();
NeilBrown8377bc82008-04-30 00:52:28 -07002068 if (mddev->external)
2069 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08002071 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002072
NeilBrown850b2b42006-10-03 01:15:46 -07002073 set_bit(MD_CHANGE_PENDING, &mddev->flags);
2074 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2075 force_change = 1;
2076 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2077 /* just a clean<-> dirty transition, possibly leave spares alone,
2078 * though if events isn't the right even/odd, we will have to do
2079 * spares after all
2080 */
2081 nospares = 1;
2082 if (force_change)
2083 nospares = 0;
2084 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002085 /* If the array is degraded, then skipping spares is both
2086 * dangerous and fairly pointless.
2087 * Dangerous because a device that was removed from the array
2088 * might have a event_count that still looks up-to-date,
2089 * so it can be re-added without a resync.
2090 * Pointless because if there are any spares to skip,
2091 * then a recovery will happen and soon that array won't
2092 * be degraded any more and the spare can go back to sleep then.
2093 */
NeilBrown850b2b42006-10-03 01:15:46 -07002094 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002095
NeilBrown06d91a52005-06-21 17:17:12 -07002096 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002097
2098 /* If this is just a dirty<->clean transition, and the array is clean
2099 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002100 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002101 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08002102 && (mddev->events & 1)
2103 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07002104 mddev->events--;
2105 else {
2106 /* otherwise we have to go forward and ... */
2107 mddev->events ++;
2108 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
NeilBrown51d56682009-08-13 09:54:02 +10002109 /* .. if the array isn't clean, an 'even' event must also go
2110 * to spares. */
2111 if ((mddev->events&1)==0)
NeilBrown42543762006-06-26 00:27:57 -07002112 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002113 } else {
NeilBrown51d56682009-08-13 09:54:02 +10002114 /* otherwise an 'odd' event must go to spares */
2115 if ((mddev->events&1))
NeilBrown42543762006-06-26 00:27:57 -07002116 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002117 }
2118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
2120 if (!mddev->events) {
2121 /*
2122 * oops, this 64-bit counter should never wrap.
2123 * Either we are in around ~1 trillion A.C., assuming
2124 * 1 reboot per second, or we have a bug:
2125 */
2126 MD_BUG();
2127 mddev->events --;
2128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 /*
2131 * do not write anything to disk if using
2132 * nonpersistent superblocks
2133 */
NeilBrown06d91a52005-06-21 17:17:12 -07002134 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08002135 if (!mddev->external)
2136 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2137
NeilBrowna9701a32005-11-08 21:39:34 -08002138 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002139 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return;
NeilBrown06d91a52005-06-21 17:17:12 -07002141 }
NeilBrowne6910632008-02-06 01:39:51 -08002142 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002143 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 dprintk(KERN_INFO
2146 "md: updating %s RAID superblock on device (in sync %d)\n",
2147 mdname(mddev),mddev->in_sync);
2148
NeilBrown4ad13662007-07-17 04:06:13 -07002149 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002150 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 char b[BDEVNAME_SIZE];
2152 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002153 if (rdev->sb_loaded != 1)
2154 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002155 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 dprintk("(skipping faulty ");
2157
2158 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002159 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002160 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002161 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002162 rdev->sb_page);
2163 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2164 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002165 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002166 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 } else
2169 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002170 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 /* only need to write one superblock... */
2172 break;
2173 }
NeilBrowna9701a32005-11-08 21:39:34 -08002174 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002175 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002176
NeilBrowna9701a32005-11-08 21:39:34 -08002177 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002178 if (mddev->in_sync != sync_req ||
2179 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002180 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002181 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002182 goto repeat;
2183 }
NeilBrown850b2b42006-10-03 01:15:46 -07002184 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002185 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002186 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002187 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2188 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190}
2191
Andre Noll7f6ce762008-03-23 18:34:54 +01002192/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002193 * We want to accept with case. For this we use cmd_match.
2194 */
2195static int cmd_match(const char *cmd, const char *str)
2196{
2197 /* See if cmd, written into a sysfs file, matches
2198 * str. They must either be the same, or cmd can
2199 * have a trailing newline
2200 */
2201 while (*cmd && *str && *cmd == *str) {
2202 cmd++;
2203 str++;
2204 }
2205 if (*cmd == '\n')
2206 cmd++;
2207 if (*str || *cmd)
2208 return 0;
2209 return 1;
2210}
2211
NeilBrown86e6ffd2005-11-08 21:39:24 -08002212struct rdev_sysfs_entry {
2213 struct attribute attr;
2214 ssize_t (*show)(mdk_rdev_t *, char *);
2215 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2216};
2217
2218static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002219state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002220{
2221 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002222 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002223
NeilBrownb2d444d2005-11-08 21:39:31 -08002224 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002225 len+= sprintf(page+len, "%sfaulty",sep);
2226 sep = ",";
2227 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002228 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002229 len += sprintf(page+len, "%sin_sync",sep);
2230 sep = ",";
2231 }
NeilBrownf6556752006-06-26 00:28:01 -07002232 if (test_bit(WriteMostly, &rdev->flags)) {
2233 len += sprintf(page+len, "%swrite_mostly",sep);
2234 sep = ",";
2235 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002236 if (test_bit(Blocked, &rdev->flags)) {
2237 len += sprintf(page+len, "%sblocked", sep);
2238 sep = ",";
2239 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002240 if (!test_bit(Faulty, &rdev->flags) &&
2241 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002242 len += sprintf(page+len, "%sspare", sep);
2243 sep = ",";
2244 }
2245 return len+sprintf(page+len, "\n");
2246}
2247
NeilBrown45dc2de2006-06-26 00:27:58 -07002248static ssize_t
2249state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2250{
2251 /* can write
2252 * faulty - simulates and error
2253 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002254 * writemostly - sets write_mostly
2255 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002256 * blocked - sets the Blocked flag
2257 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002258 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002259 */
2260 int err = -EINVAL;
2261 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2262 md_error(rdev->mddev, rdev);
2263 err = 0;
2264 } else if (cmd_match(buf, "remove")) {
2265 if (rdev->raid_disk >= 0)
2266 err = -EBUSY;
2267 else {
2268 mddev_t *mddev = rdev->mddev;
2269 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002270 if (mddev->pers)
2271 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002272 md_new_event(mddev);
2273 err = 0;
2274 }
NeilBrownf6556752006-06-26 00:28:01 -07002275 } else if (cmd_match(buf, "writemostly")) {
2276 set_bit(WriteMostly, &rdev->flags);
2277 err = 0;
2278 } else if (cmd_match(buf, "-writemostly")) {
2279 clear_bit(WriteMostly, &rdev->flags);
2280 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002281 } else if (cmd_match(buf, "blocked")) {
2282 set_bit(Blocked, &rdev->flags);
2283 err = 0;
2284 } else if (cmd_match(buf, "-blocked")) {
2285 clear_bit(Blocked, &rdev->flags);
2286 wake_up(&rdev->blocked_wait);
2287 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2288 md_wakeup_thread(rdev->mddev->thread);
2289
2290 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002291 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2292 set_bit(In_sync, &rdev->flags);
2293 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002294 }
NeilBrown3c0ee632008-10-21 13:25:28 +11002295 if (!err && rdev->sysfs_state)
2296 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002297 return err ? err : len;
2298}
NeilBrown80ca3a42006-07-10 04:44:18 -07002299static struct rdev_sysfs_entry rdev_state =
2300__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002301
2302static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002303errors_show(mdk_rdev_t *rdev, char *page)
2304{
2305 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2306}
2307
2308static ssize_t
2309errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2310{
2311 char *e;
2312 unsigned long n = simple_strtoul(buf, &e, 10);
2313 if (*buf && (*e == 0 || *e == '\n')) {
2314 atomic_set(&rdev->corrected_errors, n);
2315 return len;
2316 }
2317 return -EINVAL;
2318}
2319static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002320__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002321
NeilBrown014236d2006-01-06 00:20:55 -08002322static ssize_t
2323slot_show(mdk_rdev_t *rdev, char *page)
2324{
2325 if (rdev->raid_disk < 0)
2326 return sprintf(page, "none\n");
2327 else
2328 return sprintf(page, "%d\n", rdev->raid_disk);
2329}
2330
2331static ssize_t
2332slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2333{
2334 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002335 int err;
2336 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002337 int slot = simple_strtoul(buf, &e, 10);
2338 if (strncmp(buf, "none", 4)==0)
2339 slot = -1;
2340 else if (e==buf || (*e && *e!= '\n'))
2341 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002342 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002343 /* Setting 'slot' on an active array requires also
2344 * updating the 'rd%d' link, and communicating
2345 * with the personality with ->hot_*_disk.
2346 * For now we only support removing
2347 * failed/spare devices. This normally happens automatically,
2348 * but not when the metadata is externally managed.
2349 */
NeilBrownc303da62008-02-06 01:39:51 -08002350 if (rdev->raid_disk == -1)
2351 return -EEXIST;
2352 /* personality does all needed checks */
2353 if (rdev->mddev->pers->hot_add_disk == NULL)
2354 return -EINVAL;
2355 err = rdev->mddev->pers->
2356 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2357 if (err)
2358 return err;
2359 sprintf(nm, "rd%d", rdev->raid_disk);
2360 sysfs_remove_link(&rdev->mddev->kobj, nm);
2361 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2362 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002363 } else if (rdev->mddev->pers) {
2364 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002365 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002366 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002367 */
2368
2369 if (rdev->raid_disk != -1)
2370 return -EBUSY;
2371
2372 if (rdev->mddev->pers->hot_add_disk == NULL)
2373 return -EINVAL;
2374
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002375 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002376 if (rdev2->raid_disk == slot)
2377 return -EEXIST;
2378
2379 rdev->raid_disk = slot;
2380 if (test_bit(In_sync, &rdev->flags))
2381 rdev->saved_raid_disk = slot;
2382 else
2383 rdev->saved_raid_disk = -1;
2384 err = rdev->mddev->pers->
2385 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002386 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002387 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002388 return err;
Neil Brown52664732008-06-28 08:31:44 +10002389 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002390 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002391 sprintf(nm, "rd%d", rdev->raid_disk);
2392 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2393 printk(KERN_WARNING
2394 "md: cannot register "
2395 "%s for %s\n",
2396 nm, mdname(rdev->mddev));
2397
2398 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002399 } else {
2400 if (slot >= rdev->mddev->raid_disks)
2401 return -ENOSPC;
2402 rdev->raid_disk = slot;
2403 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002404 clear_bit(Faulty, &rdev->flags);
2405 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002406 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002407 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002408 }
NeilBrown014236d2006-01-06 00:20:55 -08002409 return len;
2410}
2411
2412
2413static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002414__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002415
NeilBrown93c8cad2006-01-06 00:20:56 -08002416static ssize_t
2417offset_show(mdk_rdev_t *rdev, char *page)
2418{
Andrew Morton6961ece2006-01-06 00:20:59 -08002419 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002420}
2421
2422static ssize_t
2423offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2424{
2425 char *e;
2426 unsigned long long offset = simple_strtoull(buf, &e, 10);
2427 if (e==buf || (*e && *e != '\n'))
2428 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002429 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002430 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002431 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002432 /* Must set offset before size, so overlap checks
2433 * can be sane */
2434 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002435 rdev->data_offset = offset;
2436 return len;
2437}
2438
2439static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002440__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002441
NeilBrown83303b62006-01-06 00:21:06 -08002442static ssize_t
2443rdev_size_show(mdk_rdev_t *rdev, char *page)
2444{
Andre Nolldd8ac332009-03-31 14:33:13 +11002445 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002446}
2447
NeilBrownc5d79ad2008-02-06 01:39:54 -08002448static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2449{
2450 /* check if two start/length pairs overlap */
2451 if (s1+l1 <= s2)
2452 return 0;
2453 if (s2+l2 <= s1)
2454 return 0;
2455 return 1;
2456}
2457
Dan Williamsb522adc2009-03-31 15:00:31 +11002458static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2459{
2460 unsigned long long blocks;
2461 sector_t new;
2462
2463 if (strict_strtoull(buf, 10, &blocks) < 0)
2464 return -EINVAL;
2465
2466 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2467 return -EINVAL; /* sector conversion overflow */
2468
2469 new = blocks * 2;
2470 if (new != blocks * 2)
2471 return -EINVAL; /* unsigned long long to sector_t overflow */
2472
2473 *sectors = new;
2474 return 0;
2475}
2476
NeilBrown83303b62006-01-06 00:21:06 -08002477static ssize_t
2478rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2479{
NeilBrown27c529b2008-03-04 14:29:33 -08002480 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002481 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002482 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002483
Dan Williamsb522adc2009-03-31 15:00:31 +11002484 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002485 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002486 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002487 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002488 sectors = super_types[my_mddev->major_version].
2489 rdev_size_change(rdev, sectors);
2490 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002491 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002492 } else if (!sectors)
2493 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2494 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002495 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002496 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002497 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002498
Andre Nolldd8ac332009-03-31 14:33:13 +11002499 rdev->sectors = sectors;
2500 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002501 /* need to check that all other rdevs with the same ->bdev
2502 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002503 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002504 * we have to change it back, we will have the lock again.
2505 */
2506 mddev_t *mddev;
2507 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002508 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002509
NeilBrown27c529b2008-03-04 14:29:33 -08002510 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002511 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002512 mdk_rdev_t *rdev2;
2513
2514 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002515 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002516 if (test_bit(AllReserved, &rdev2->flags) ||
2517 (rdev->bdev == rdev2->bdev &&
2518 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002519 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002520 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002521 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002522 overlap = 1;
2523 break;
2524 }
2525 mddev_unlock(mddev);
2526 if (overlap) {
2527 mddev_put(mddev);
2528 break;
2529 }
2530 }
NeilBrown27c529b2008-03-04 14:29:33 -08002531 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002532 if (overlap) {
2533 /* Someone else could have slipped in a size
2534 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002535 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002536 * safe, and trust userspace not to race with
2537 * itself
2538 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002539 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002540 return -EBUSY;
2541 }
2542 }
NeilBrown83303b62006-01-06 00:21:06 -08002543 return len;
2544}
2545
2546static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002547__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002548
NeilBrown86e6ffd2005-11-08 21:39:24 -08002549static struct attribute *rdev_default_attrs[] = {
2550 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002551 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002552 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002553 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002554 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002555 NULL,
2556};
2557static ssize_t
2558rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2559{
2560 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2561 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002562 mddev_t *mddev = rdev->mddev;
2563 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002564
2565 if (!entry->show)
2566 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002567
2568 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2569 if (!rv) {
2570 if (rdev->mddev == NULL)
2571 rv = -EBUSY;
2572 else
2573 rv = entry->show(rdev, page);
2574 mddev_unlock(mddev);
2575 }
2576 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002577}
2578
2579static ssize_t
2580rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2581 const char *page, size_t length)
2582{
2583 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2584 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002585 ssize_t rv;
2586 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002587
2588 if (!entry->store)
2589 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002590 if (!capable(CAP_SYS_ADMIN))
2591 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002592 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002593 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002594 if (rdev->mddev == NULL)
2595 rv = -EBUSY;
2596 else
2597 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002598 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002599 }
2600 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002601}
2602
2603static void rdev_free(struct kobject *ko)
2604{
2605 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2606 kfree(rdev);
2607}
2608static struct sysfs_ops rdev_sysfs_ops = {
2609 .show = rdev_attr_show,
2610 .store = rdev_attr_store,
2611};
2612static struct kobj_type rdev_ktype = {
2613 .release = rdev_free,
2614 .sysfs_ops = &rdev_sysfs_ops,
2615 .default_attrs = rdev_default_attrs,
2616};
2617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618/*
2619 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2620 *
2621 * mark the device faulty if:
2622 *
2623 * - the device is nonexistent (zero size)
2624 * - the device has no valid superblock
2625 *
2626 * a faulty rdev _never_ has rdev->sb set.
2627 */
2628static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2629{
2630 char b[BDEVNAME_SIZE];
2631 int err;
2632 mdk_rdev_t *rdev;
2633 sector_t size;
2634
NeilBrown9ffae0c2006-01-06 00:20:32 -08002635 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 if (!rdev) {
2637 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2638 return ERR_PTR(-ENOMEM);
2639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
2641 if ((err = alloc_disk_sb(rdev)))
2642 goto abort_free;
2643
NeilBrownc5d79ad2008-02-06 01:39:54 -08002644 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 if (err)
2646 goto abort_free;
2647
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002648 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002651 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002652 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002653 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002655 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002657 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002658 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
2660 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2661 if (!size) {
2662 printk(KERN_WARNING
2663 "md: %s has zero or unknown size, marking faulty!\n",
2664 bdevname(rdev->bdev,b));
2665 err = -EINVAL;
2666 goto abort_free;
2667 }
2668
2669 if (super_format >= 0) {
2670 err = super_types[super_format].
2671 load_super(rdev, NULL, super_minor);
2672 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002673 printk(KERN_WARNING
2674 "md: %s does not have a valid v%d.%d "
2675 "superblock, not importing!\n",
2676 bdevname(rdev->bdev,b),
2677 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 goto abort_free;
2679 }
2680 if (err < 0) {
2681 printk(KERN_WARNING
2682 "md: could not read %s's sb, not importing!\n",
2683 bdevname(rdev->bdev,b));
2684 goto abort_free;
2685 }
2686 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002689 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
2691 return rdev;
2692
2693abort_free:
2694 if (rdev->sb_page) {
2695 if (rdev->bdev)
2696 unlock_rdev(rdev);
2697 free_disk_sb(rdev);
2698 }
2699 kfree(rdev);
2700 return ERR_PTR(err);
2701}
2702
2703/*
2704 * Check a full RAID array for plausibility
2705 */
2706
2707
NeilBrowna757e642005-04-16 15:26:42 -07002708static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709{
2710 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002711 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 char b[BDEVNAME_SIZE];
2713
2714 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002715 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 switch (super_types[mddev->major_version].
2717 load_super(rdev, freshest, mddev->minor_version)) {
2718 case 1:
2719 freshest = rdev;
2720 break;
2721 case 0:
2722 break;
2723 default:
2724 printk( KERN_ERR \
2725 "md: fatal superblock inconsistency in %s"
2726 " -- removing from array\n",
2727 bdevname(rdev->bdev,b));
2728 kick_rdev_from_array(rdev);
2729 }
2730
2731
2732 super_types[mddev->major_version].
2733 validate_super(mddev, freshest);
2734
2735 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002736 rdev_for_each(rdev, tmp, mddev) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002737 if (rdev->desc_nr >= mddev->max_disks ||
2738 i > mddev->max_disks) {
2739 printk(KERN_WARNING
2740 "md: %s: %s: only %d devices permitted\n",
2741 mdname(mddev), bdevname(rdev->bdev, b),
2742 mddev->max_disks);
2743 kick_rdev_from_array(rdev);
2744 continue;
2745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 if (rdev != freshest)
2747 if (super_types[mddev->major_version].
2748 validate_super(mddev, rdev)) {
2749 printk(KERN_WARNING "md: kicking non-fresh %s"
2750 " from array!\n",
2751 bdevname(rdev->bdev,b));
2752 kick_rdev_from_array(rdev);
2753 continue;
2754 }
2755 if (mddev->level == LEVEL_MULTIPATH) {
2756 rdev->desc_nr = i++;
2757 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002758 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002759 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002760 rdev->raid_disk = -1;
2761 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 }
2763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764}
2765
NeilBrown72e02072009-12-14 12:49:55 +11002766/* Read a fixed-point number.
2767 * Numbers in sysfs attributes should be in "standard" units where
2768 * possible, so time should be in seconds.
2769 * However we internally use a a much smaller unit such as
2770 * milliseconds or jiffies.
2771 * This function takes a decimal number with a possible fractional
2772 * component, and produces an integer which is the result of
2773 * multiplying that number by 10^'scale'.
2774 * all without any floating-point arithmetic.
2775 */
2776int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2777{
2778 unsigned long result = 0;
2779 long decimals = -1;
2780 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2781 if (*cp == '.')
2782 decimals = 0;
2783 else if (decimals < scale) {
2784 unsigned int value;
2785 value = *cp - '0';
2786 result = result * 10 + value;
2787 if (decimals >= 0)
2788 decimals++;
2789 }
2790 cp++;
2791 }
2792 if (*cp == '\n')
2793 cp++;
2794 if (*cp)
2795 return -EINVAL;
2796 if (decimals < 0)
2797 decimals = 0;
2798 while (decimals < scale) {
2799 result *= 10;
2800 decimals ++;
2801 }
2802 *res = result;
2803 return 0;
2804}
2805
2806
NeilBrown19052c02008-08-05 15:54:13 +10002807static void md_safemode_timeout(unsigned long data);
2808
NeilBrowneae17012005-11-08 21:39:23 -08002809static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002810safe_delay_show(mddev_t *mddev, char *page)
2811{
2812 int msec = (mddev->safemode_delay*1000)/HZ;
2813 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2814}
2815static ssize_t
2816safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2817{
NeilBrown16f17b32006-06-26 00:27:37 -07002818 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07002819
NeilBrown72e02072009-12-14 12:49:55 +11002820 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002821 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07002822 if (msec == 0)
2823 mddev->safemode_delay = 0;
2824 else {
NeilBrown19052c02008-08-05 15:54:13 +10002825 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002826 mddev->safemode_delay = (msec*HZ)/1000;
2827 if (mddev->safemode_delay == 0)
2828 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002829 if (mddev->safemode_delay < old_delay)
2830 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002831 }
2832 return len;
2833}
2834static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002835__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002836
2837static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002838level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002839{
NeilBrown2604b702006-01-06 00:20:36 -08002840 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002841 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002842 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002843 else if (mddev->clevel[0])
2844 return sprintf(page, "%s\n", mddev->clevel);
2845 else if (mddev->level != LEVEL_NONE)
2846 return sprintf(page, "%d\n", mddev->level);
2847 else
2848 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002849}
2850
NeilBrownd9d166c2006-01-06 00:20:51 -08002851static ssize_t
2852level_store(mddev_t *mddev, const char *buf, size_t len)
2853{
NeilBrown245f46c2009-03-31 14:39:39 +11002854 char level[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002855 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002856 struct mdk_personality *pers;
2857 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002858 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002859
2860 if (mddev->pers == NULL) {
2861 if (len == 0)
2862 return 0;
2863 if (len >= sizeof(mddev->clevel))
2864 return -ENOSPC;
2865 strncpy(mddev->clevel, buf, len);
2866 if (mddev->clevel[len-1] == '\n')
2867 len--;
2868 mddev->clevel[len] = 0;
2869 mddev->level = LEVEL_NONE;
2870 return rv;
2871 }
2872
2873 /* request to change the personality. Need to ensure:
2874 * - array is not engaged in resync/recovery/reshape
2875 * - old personality can be suspended
2876 * - new personality will access other array.
2877 */
2878
2879 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownd9d166c2006-01-06 00:20:51 -08002880 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002881
2882 if (!mddev->pers->quiesce) {
2883 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2884 mdname(mddev), mddev->pers->name);
2885 return -EINVAL;
2886 }
2887
2888 /* Now find the new personality */
2889 if (len == 0 || len >= sizeof(level))
2890 return -EINVAL;
2891 strncpy(level, buf, len);
2892 if (level[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002893 len--;
NeilBrown245f46c2009-03-31 14:39:39 +11002894 level[len] = 0;
2895
2896 request_module("md-%s", level);
2897 spin_lock(&pers_lock);
2898 pers = find_pers(LEVEL_NONE, level);
2899 if (!pers || !try_module_get(pers->owner)) {
2900 spin_unlock(&pers_lock);
2901 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2902 return -EINVAL;
2903 }
2904 spin_unlock(&pers_lock);
2905
2906 if (pers == mddev->pers) {
2907 /* Nothing to do! */
2908 module_put(pers->owner);
2909 return rv;
2910 }
2911 if (!pers->takeover) {
2912 module_put(pers->owner);
2913 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2914 mdname(mddev), level);
2915 return -EINVAL;
2916 }
2917
2918 /* ->takeover must set new_* and/or delta_disks
2919 * if it succeeds, and may set them when it fails.
2920 */
2921 priv = pers->takeover(mddev);
2922 if (IS_ERR(priv)) {
2923 mddev->new_level = mddev->level;
2924 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002925 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002926 mddev->raid_disks -= mddev->delta_disks;
2927 mddev->delta_disks = 0;
2928 module_put(pers->owner);
2929 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2930 mdname(mddev), level);
2931 return PTR_ERR(priv);
2932 }
2933
2934 /* Looks like we have a winner */
2935 mddev_suspend(mddev);
2936 mddev->pers->stop(mddev);
2937 module_put(mddev->pers->owner);
NeilBrown3a981b02009-08-03 10:59:55 +10002938 /* Invalidate devices that are now superfluous */
2939 list_for_each_entry(rdev, &mddev->disks, same_set)
2940 if (rdev->raid_disk >= mddev->raid_disks) {
2941 rdev->raid_disk = -1;
2942 clear_bit(In_sync, &rdev->flags);
2943 }
NeilBrown245f46c2009-03-31 14:39:39 +11002944 mddev->pers = pers;
2945 mddev->private = priv;
2946 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2947 mddev->level = mddev->new_level;
2948 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002949 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002950 mddev->delta_disks = 0;
2951 pers->run(mddev);
2952 mddev_resume(mddev);
2953 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2954 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2955 md_wakeup_thread(mddev->thread);
NeilBrownd9d166c2006-01-06 00:20:51 -08002956 return rv;
2957}
2958
2959static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002960__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002961
NeilBrownd4dbd022006-06-26 00:27:59 -07002962
2963static ssize_t
2964layout_show(mddev_t *mddev, char *page)
2965{
2966 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002967 if (mddev->reshape_position != MaxSector &&
2968 mddev->layout != mddev->new_layout)
2969 return sprintf(page, "%d (%d)\n",
2970 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002971 return sprintf(page, "%d\n", mddev->layout);
2972}
2973
2974static ssize_t
2975layout_store(mddev_t *mddev, const char *buf, size_t len)
2976{
2977 char *e;
2978 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002979
2980 if (!*buf || (*e && *e != '\n'))
2981 return -EINVAL;
2982
NeilBrownb3546032009-03-31 14:56:41 +11002983 if (mddev->pers) {
2984 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10002985 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11002986 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10002987 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10002988 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10002989 if (err) {
2990 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11002991 return err;
NeilBrown597a7112009-06-18 08:47:42 +10002992 }
NeilBrownb3546032009-03-31 14:56:41 +11002993 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07002994 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11002995 if (mddev->reshape_position == MaxSector)
2996 mddev->layout = n;
2997 }
NeilBrownd4dbd022006-06-26 00:27:59 -07002998 return len;
2999}
3000static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003001__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003002
3003
NeilBrowneae17012005-11-08 21:39:23 -08003004static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003005raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003006{
NeilBrownbb636542005-11-08 21:39:45 -08003007 if (mddev->raid_disks == 0)
3008 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003009 if (mddev->reshape_position != MaxSector &&
3010 mddev->delta_disks != 0)
3011 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3012 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003013 return sprintf(page, "%d\n", mddev->raid_disks);
3014}
3015
NeilBrownda943b992006-01-06 00:20:54 -08003016static int update_raid_disks(mddev_t *mddev, int raid_disks);
3017
3018static ssize_t
3019raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3020{
NeilBrownda943b992006-01-06 00:20:54 -08003021 char *e;
3022 int rv = 0;
3023 unsigned long n = simple_strtoul(buf, &e, 10);
3024
3025 if (!*buf || (*e && *e != '\n'))
3026 return -EINVAL;
3027
3028 if (mddev->pers)
3029 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003030 else if (mddev->reshape_position != MaxSector) {
3031 int olddisks = mddev->raid_disks - mddev->delta_disks;
3032 mddev->delta_disks = n - olddisks;
3033 mddev->raid_disks = n;
3034 } else
NeilBrownda943b992006-01-06 00:20:54 -08003035 mddev->raid_disks = n;
3036 return rv ? rv : len;
3037}
3038static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003039__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003040
NeilBrown24dd4692005-11-08 21:39:26 -08003041static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003042chunk_size_show(mddev_t *mddev, char *page)
3043{
NeilBrown08a02ec2007-05-09 02:35:38 -07003044 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003045 mddev->chunk_sectors != mddev->new_chunk_sectors)
3046 return sprintf(page, "%d (%d)\n",
3047 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003048 mddev->chunk_sectors << 9);
3049 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003050}
3051
3052static ssize_t
3053chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3054{
NeilBrown3b343802006-01-06 00:20:47 -08003055 char *e;
3056 unsigned long n = simple_strtoul(buf, &e, 10);
3057
NeilBrown3b343802006-01-06 00:20:47 -08003058 if (!*buf || (*e && *e != '\n'))
3059 return -EINVAL;
3060
NeilBrownb3546032009-03-31 14:56:41 +11003061 if (mddev->pers) {
3062 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003063 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003064 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003065 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003066 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003067 if (err) {
3068 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003069 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003070 }
NeilBrownb3546032009-03-31 14:56:41 +11003071 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003072 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003073 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003074 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003075 }
NeilBrown3b343802006-01-06 00:20:47 -08003076 return len;
3077}
3078static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003079__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003080
NeilBrowna94213b2006-06-26 00:28:00 -07003081static ssize_t
3082resync_start_show(mddev_t *mddev, char *page)
3083{
NeilBrownd1a7c502009-03-31 15:24:32 +11003084 if (mddev->recovery_cp == MaxSector)
3085 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003086 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3087}
3088
3089static ssize_t
3090resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3091{
NeilBrowna94213b2006-06-26 00:28:00 -07003092 char *e;
3093 unsigned long long n = simple_strtoull(buf, &e, 10);
3094
3095 if (mddev->pers)
3096 return -EBUSY;
3097 if (!*buf || (*e && *e != '\n'))
3098 return -EINVAL;
3099
3100 mddev->recovery_cp = n;
3101 return len;
3102}
3103static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003104__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003105
NeilBrown9e653b62006-06-26 00:27:58 -07003106/*
3107 * The array state can be:
3108 *
3109 * clear
3110 * No devices, no size, no level
3111 * Equivalent to STOP_ARRAY ioctl
3112 * inactive
3113 * May have some settings, but array is not active
3114 * all IO results in error
3115 * When written, doesn't tear down array, but just stops it
3116 * suspended (not supported yet)
3117 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003118 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003119 * readonly
3120 * no resync can happen. no superblocks get written.
3121 * write requests fail
3122 * read-auto
3123 * like readonly, but behaves like 'clean' on a write request.
3124 *
3125 * clean - no pending writes, but otherwise active.
3126 * When written to inactive array, starts without resync
3127 * If a write request arrives then
3128 * if metadata is known, mark 'dirty' and switch to 'active'.
3129 * if not known, block and switch to write-pending
3130 * If written to an active array that has pending writes, then fails.
3131 * active
3132 * fully active: IO and resync can be happening.
3133 * When written to inactive array, starts with resync
3134 *
3135 * write-pending
3136 * clean, but writes are blocked waiting for 'active' to be written.
3137 *
3138 * active-idle
3139 * like active, but no writes have been seen for a while (100msec).
3140 *
3141 */
3142enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3143 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003144static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003145 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3146 "write-pending", "active-idle", NULL };
3147
3148static int match_word(const char *word, char **list)
3149{
3150 int n;
3151 for (n=0; list[n]; n++)
3152 if (cmd_match(word, list[n]))
3153 break;
3154 return n;
3155}
3156
3157static ssize_t
3158array_state_show(mddev_t *mddev, char *page)
3159{
3160 enum array_state st = inactive;
3161
3162 if (mddev->pers)
3163 switch(mddev->ro) {
3164 case 1:
3165 st = readonly;
3166 break;
3167 case 2:
3168 st = read_auto;
3169 break;
3170 case 0:
3171 if (mddev->in_sync)
3172 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08003173 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3174 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003175 else if (mddev->safemode)
3176 st = active_idle;
3177 else
3178 st = active;
3179 }
3180 else {
3181 if (list_empty(&mddev->disks) &&
3182 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003183 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003184 st = clear;
3185 else
3186 st = inactive;
3187 }
3188 return sprintf(page, "%s\n", array_states[st]);
3189}
3190
Neil Browndf5b20c2008-07-11 22:02:22 +10003191static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003192static int do_md_run(mddev_t * mddev);
3193static int restart_array(mddev_t *mddev);
3194
3195static ssize_t
3196array_state_store(mddev_t *mddev, const char *buf, size_t len)
3197{
3198 int err = -EINVAL;
3199 enum array_state st = match_word(buf, array_states);
3200 switch(st) {
3201 case bad_word:
3202 break;
3203 case clear:
3204 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003205 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003206 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003207 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003208 break;
3209 case inactive:
3210 /* stopping an active array */
3211 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003212 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003213 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003214 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003215 } else
3216 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003217 break;
3218 case suspended:
3219 break; /* not supported yet */
3220 case readonly:
3221 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10003222 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003223 else {
3224 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003225 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003226 err = do_md_run(mddev);
3227 }
3228 break;
3229 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003230 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003231 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10003232 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003233 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003234 err = restart_array(mddev);
3235 if (err == 0) {
3236 mddev->ro = 2;
3237 set_disk_ro(mddev->gendisk, 0);
3238 }
NeilBrown9e653b62006-06-26 00:27:58 -07003239 } else {
3240 mddev->ro = 2;
3241 err = do_md_run(mddev);
3242 }
3243 break;
3244 case clean:
3245 if (mddev->pers) {
3246 restart_array(mddev);
3247 spin_lock_irq(&mddev->write_lock);
3248 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003249 if (mddev->in_sync == 0) {
3250 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003251 if (mddev->safemode == 1)
3252 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003253 if (mddev->persistent)
3254 set_bit(MD_CHANGE_CLEAN,
3255 &mddev->flags);
3256 }
3257 err = 0;
3258 } else
3259 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003260 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003261 } else
3262 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003263 break;
3264 case active:
3265 if (mddev->pers) {
3266 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003267 if (mddev->external)
3268 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003269 wake_up(&mddev->sb_wait);
3270 err = 0;
3271 } else {
3272 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003273 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003274 err = do_md_run(mddev);
3275 }
3276 break;
3277 case write_pending:
3278 case active_idle:
3279 /* these cannot be set */
3280 break;
3281 }
3282 if (err)
3283 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003284 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003285 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003286 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003287 }
NeilBrown9e653b62006-06-26 00:27:58 -07003288}
NeilBrown80ca3a42006-07-10 04:44:18 -07003289static struct md_sysfs_entry md_array_state =
3290__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003291
NeilBrown6d7ff732006-01-06 00:21:16 -08003292static ssize_t
3293null_show(mddev_t *mddev, char *page)
3294{
3295 return -EINVAL;
3296}
3297
3298static ssize_t
3299new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3300{
3301 /* buf must be %d:%d\n? giving major and minor numbers */
3302 /* The new device is added to the array.
3303 * If the array has a persistent superblock, we read the
3304 * superblock to initialise info and check validity.
3305 * Otherwise, only checking done is that in bind_rdev_to_array,
3306 * which mainly checks size.
3307 */
3308 char *e;
3309 int major = simple_strtoul(buf, &e, 10);
3310 int minor;
3311 dev_t dev;
3312 mdk_rdev_t *rdev;
3313 int err;
3314
3315 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3316 return -EINVAL;
3317 minor = simple_strtoul(e+1, &e, 10);
3318 if (*e && *e != '\n')
3319 return -EINVAL;
3320 dev = MKDEV(major, minor);
3321 if (major != MAJOR(dev) ||
3322 minor != MINOR(dev))
3323 return -EOVERFLOW;
3324
3325
3326 if (mddev->persistent) {
3327 rdev = md_import_device(dev, mddev->major_version,
3328 mddev->minor_version);
3329 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3330 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3331 mdk_rdev_t, same_set);
3332 err = super_types[mddev->major_version]
3333 .load_super(rdev, rdev0, mddev->minor_version);
3334 if (err < 0)
3335 goto out;
3336 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003337 } else if (mddev->external)
3338 rdev = md_import_device(dev, -2, -1);
3339 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003340 rdev = md_import_device(dev, -1, -1);
3341
3342 if (IS_ERR(rdev))
3343 return PTR_ERR(rdev);
3344 err = bind_rdev_to_array(rdev, mddev);
3345 out:
3346 if (err)
3347 export_rdev(rdev);
3348 return err ? err : len;
3349}
3350
3351static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003352__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003353
3354static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003355bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3356{
3357 char *end;
3358 unsigned long chunk, end_chunk;
3359
3360 if (!mddev->bitmap)
3361 goto out;
3362 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3363 while (*buf) {
3364 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3365 if (buf == end) break;
3366 if (*end == '-') { /* range */
3367 buf = end + 1;
3368 end_chunk = simple_strtoul(buf, &end, 0);
3369 if (buf == end) break;
3370 }
3371 if (*end && !isspace(*end)) break;
3372 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3373 buf = end;
3374 while (isspace(*buf)) buf++;
3375 }
3376 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3377out:
3378 return len;
3379}
3380
3381static struct md_sysfs_entry md_bitmap =
3382__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3383
3384static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003385size_show(mddev_t *mddev, char *page)
3386{
Andre Noll58c0fed2009-03-31 14:33:13 +11003387 return sprintf(page, "%llu\n",
3388 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003389}
3390
Andre Nolld71f9f82008-07-11 22:02:22 +10003391static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003392
3393static ssize_t
3394size_store(mddev_t *mddev, const char *buf, size_t len)
3395{
3396 /* If array is inactive, we can reduce the component size, but
3397 * not increase it (except from 0).
3398 * If array is active, we can try an on-line resize
3399 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003400 sector_t sectors;
3401 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003402
Andre Noll58c0fed2009-03-31 14:33:13 +11003403 if (err < 0)
3404 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003405 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003406 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003407 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003408 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003409 if (mddev->dev_sectors == 0 ||
3410 mddev->dev_sectors > sectors)
3411 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003412 else
3413 err = -ENOSPC;
3414 }
3415 return err ? err : len;
3416}
3417
3418static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003419__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003420
NeilBrown8bb93aa2006-01-06 00:20:50 -08003421
3422/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003423 * This is one of
3424 * 'none' for arrays with no metadata (good luck...)
3425 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003426 * or N.M for internally known formats
3427 */
3428static ssize_t
3429metadata_show(mddev_t *mddev, char *page)
3430{
3431 if (mddev->persistent)
3432 return sprintf(page, "%d.%d\n",
3433 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003434 else if (mddev->external)
3435 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003436 else
3437 return sprintf(page, "none\n");
3438}
3439
3440static ssize_t
3441metadata_store(mddev_t *mddev, const char *buf, size_t len)
3442{
3443 int major, minor;
3444 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003445 /* Changing the details of 'external' metadata is
3446 * always permitted. Otherwise there must be
3447 * no devices attached to the array.
3448 */
3449 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3450 ;
3451 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003452 return -EBUSY;
3453
3454 if (cmd_match(buf, "none")) {
3455 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003456 mddev->external = 0;
3457 mddev->major_version = 0;
3458 mddev->minor_version = 90;
3459 return len;
3460 }
3461 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003462 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003463 if (namelen >= sizeof(mddev->metadata_type))
3464 namelen = sizeof(mddev->metadata_type)-1;
3465 strncpy(mddev->metadata_type, buf+9, namelen);
3466 mddev->metadata_type[namelen] = 0;
3467 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3468 mddev->metadata_type[--namelen] = 0;
3469 mddev->persistent = 0;
3470 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003471 mddev->major_version = 0;
3472 mddev->minor_version = 90;
3473 return len;
3474 }
3475 major = simple_strtoul(buf, &e, 10);
3476 if (e==buf || *e != '.')
3477 return -EINVAL;
3478 buf = e+1;
3479 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003480 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003481 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003482 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003483 return -ENOENT;
3484 mddev->major_version = major;
3485 mddev->minor_version = minor;
3486 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003487 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003488 return len;
3489}
3490
3491static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003492__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003493
NeilBrowna35b0d62006-01-06 00:20:49 -08003494static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003495action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003496{
NeilBrown7eec3142005-11-08 21:39:44 -08003497 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003498 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3499 type = "frozen";
3500 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003501 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003502 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3503 type = "reshape";
3504 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003505 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3506 type = "resync";
3507 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3508 type = "check";
3509 else
3510 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003511 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003512 type = "recover";
3513 }
3514 return sprintf(page, "%s\n", type);
3515}
3516
3517static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003518action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003519{
NeilBrown7eec3142005-11-08 21:39:44 -08003520 if (!mddev->pers || !mddev->pers->sync_request)
3521 return -EINVAL;
3522
NeilBrownb6a9ce62009-05-26 09:41:17 +10003523 if (cmd_match(page, "frozen"))
3524 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3525 else
3526 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3527
3528 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003529 if (mddev->sync_thread) {
3530 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3531 md_unregister_thread(mddev->sync_thread);
3532 mddev->sync_thread = NULL;
3533 mddev->recovery = 0;
3534 }
NeilBrown03c902e2006-01-06 00:20:46 -08003535 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3536 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003537 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003538 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003539 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003540 else if (cmd_match(page, "recover")) {
3541 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3542 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3543 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003544 int err;
3545 if (mddev->pers->start_reshape == NULL)
3546 return -EINVAL;
3547 err = mddev->pers->start_reshape(mddev);
3548 if (err)
3549 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003550 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003551 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003552 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003553 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003554 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003555 return -EINVAL;
3556 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3557 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003558 }
NeilBrown03c902e2006-01-06 00:20:46 -08003559 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003560 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003561 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003562 return len;
3563}
3564
NeilBrown9d888832005-11-08 21:39:26 -08003565static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003566mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003567{
3568 return sprintf(page, "%llu\n",
3569 (unsigned long long) mddev->resync_mismatches);
3570}
3571
NeilBrown80ca3a42006-07-10 04:44:18 -07003572static struct md_sysfs_entry md_scan_mode =
3573__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003574
NeilBrown96de1e62005-11-08 21:39:39 -08003575
NeilBrown80ca3a42006-07-10 04:44:18 -07003576static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003577
NeilBrown88202a02006-01-06 00:21:36 -08003578static ssize_t
3579sync_min_show(mddev_t *mddev, char *page)
3580{
3581 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3582 mddev->sync_speed_min ? "local": "system");
3583}
3584
3585static ssize_t
3586sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3587{
3588 int min;
3589 char *e;
3590 if (strncmp(buf, "system", 6)==0) {
3591 mddev->sync_speed_min = 0;
3592 return len;
3593 }
3594 min = simple_strtoul(buf, &e, 10);
3595 if (buf == e || (*e && *e != '\n') || min <= 0)
3596 return -EINVAL;
3597 mddev->sync_speed_min = min;
3598 return len;
3599}
3600
3601static struct md_sysfs_entry md_sync_min =
3602__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3603
3604static ssize_t
3605sync_max_show(mddev_t *mddev, char *page)
3606{
3607 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3608 mddev->sync_speed_max ? "local": "system");
3609}
3610
3611static ssize_t
3612sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3613{
3614 int max;
3615 char *e;
3616 if (strncmp(buf, "system", 6)==0) {
3617 mddev->sync_speed_max = 0;
3618 return len;
3619 }
3620 max = simple_strtoul(buf, &e, 10);
3621 if (buf == e || (*e && *e != '\n') || max <= 0)
3622 return -EINVAL;
3623 mddev->sync_speed_max = max;
3624 return len;
3625}
3626
3627static struct md_sysfs_entry md_sync_max =
3628__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3629
Iustin Popd7f3d292007-10-16 23:30:54 -07003630static ssize_t
3631degraded_show(mddev_t *mddev, char *page)
3632{
3633 return sprintf(page, "%d\n", mddev->degraded);
3634}
3635static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003636
3637static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003638sync_force_parallel_show(mddev_t *mddev, char *page)
3639{
3640 return sprintf(page, "%d\n", mddev->parallel_resync);
3641}
3642
3643static ssize_t
3644sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3645{
3646 long n;
3647
3648 if (strict_strtol(buf, 10, &n))
3649 return -EINVAL;
3650
3651 if (n != 0 && n != 1)
3652 return -EINVAL;
3653
3654 mddev->parallel_resync = n;
3655
3656 if (mddev->sync_thread)
3657 wake_up(&resync_wait);
3658
3659 return len;
3660}
3661
3662/* force parallel resync, even with shared block devices */
3663static struct md_sysfs_entry md_sync_force_parallel =
3664__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3665 sync_force_parallel_show, sync_force_parallel_store);
3666
3667static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003668sync_speed_show(mddev_t *mddev, char *page)
3669{
3670 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003671 if (mddev->curr_resync == 0)
3672 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003673 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3674 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003675 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003676 db = resync - mddev->resync_mark_cnt;
3677 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003678}
3679
NeilBrown80ca3a42006-07-10 04:44:18 -07003680static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003681
3682static ssize_t
3683sync_completed_show(mddev_t *mddev, char *page)
3684{
Andre Noll58c0fed2009-03-31 14:33:13 +11003685 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003686
NeilBrownacb180b2009-04-14 16:28:34 +10003687 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3688 return sprintf(page, "none\n");
3689
NeilBrown88202a02006-01-06 00:21:36 -08003690 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003691 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003692 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003693 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003694
NeilBrownacb180b2009-04-14 16:28:34 +10003695 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003696 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003697}
3698
NeilBrown80ca3a42006-07-10 04:44:18 -07003699static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003700
NeilBrowne464eaf2006-03-27 01:18:14 -08003701static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003702min_sync_show(mddev_t *mddev, char *page)
3703{
3704 return sprintf(page, "%llu\n",
3705 (unsigned long long)mddev->resync_min);
3706}
3707static ssize_t
3708min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3709{
3710 unsigned long long min;
3711 if (strict_strtoull(buf, 10, &min))
3712 return -EINVAL;
3713 if (min > mddev->resync_max)
3714 return -EINVAL;
3715 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3716 return -EBUSY;
3717
3718 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003719 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003720 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003721 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003722 return -EINVAL;
3723 }
3724 mddev->resync_min = min;
3725
3726 return len;
3727}
3728
3729static struct md_sysfs_entry md_min_sync =
3730__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3731
3732static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003733max_sync_show(mddev_t *mddev, char *page)
3734{
3735 if (mddev->resync_max == MaxSector)
3736 return sprintf(page, "max\n");
3737 else
3738 return sprintf(page, "%llu\n",
3739 (unsigned long long)mddev->resync_max);
3740}
3741static ssize_t
3742max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3743{
3744 if (strncmp(buf, "max", 3) == 0)
3745 mddev->resync_max = MaxSector;
3746 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003747 unsigned long long max;
3748 if (strict_strtoull(buf, 10, &max))
3749 return -EINVAL;
3750 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003751 return -EINVAL;
3752 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003753 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003754 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3755 return -EBUSY;
3756
3757 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003758 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003759 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003760 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003761 return -EINVAL;
3762 }
3763 mddev->resync_max = max;
3764 }
3765 wake_up(&mddev->recovery_wait);
3766 return len;
3767}
3768
3769static struct md_sysfs_entry md_max_sync =
3770__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3771
3772static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003773suspend_lo_show(mddev_t *mddev, char *page)
3774{
3775 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3776}
3777
3778static ssize_t
3779suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3780{
3781 char *e;
3782 unsigned long long new = simple_strtoull(buf, &e, 10);
3783
NeilBrownb8d966e2009-07-01 11:14:04 +10003784 if (mddev->pers == NULL ||
3785 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003786 return -EINVAL;
3787 if (buf == e || (*e && *e != '\n'))
3788 return -EINVAL;
3789 if (new >= mddev->suspend_hi ||
3790 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3791 mddev->suspend_lo = new;
3792 mddev->pers->quiesce(mddev, 2);
3793 return len;
3794 } else
3795 return -EINVAL;
3796}
3797static struct md_sysfs_entry md_suspend_lo =
3798__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3799
3800
3801static ssize_t
3802suspend_hi_show(mddev_t *mddev, char *page)
3803{
3804 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3805}
3806
3807static ssize_t
3808suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3809{
3810 char *e;
3811 unsigned long long new = simple_strtoull(buf, &e, 10);
3812
NeilBrownb8d966e2009-07-01 11:14:04 +10003813 if (mddev->pers == NULL ||
3814 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003815 return -EINVAL;
3816 if (buf == e || (*e && *e != '\n'))
3817 return -EINVAL;
3818 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3819 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3820 mddev->suspend_hi = new;
3821 mddev->pers->quiesce(mddev, 1);
3822 mddev->pers->quiesce(mddev, 0);
3823 return len;
3824 } else
3825 return -EINVAL;
3826}
3827static struct md_sysfs_entry md_suspend_hi =
3828__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3829
NeilBrown08a02ec2007-05-09 02:35:38 -07003830static ssize_t
3831reshape_position_show(mddev_t *mddev, char *page)
3832{
3833 if (mddev->reshape_position != MaxSector)
3834 return sprintf(page, "%llu\n",
3835 (unsigned long long)mddev->reshape_position);
3836 strcpy(page, "none\n");
3837 return 5;
3838}
3839
3840static ssize_t
3841reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3842{
3843 char *e;
3844 unsigned long long new = simple_strtoull(buf, &e, 10);
3845 if (mddev->pers)
3846 return -EBUSY;
3847 if (buf == e || (*e && *e != '\n'))
3848 return -EINVAL;
3849 mddev->reshape_position = new;
3850 mddev->delta_disks = 0;
3851 mddev->new_level = mddev->level;
3852 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003853 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07003854 return len;
3855}
3856
3857static struct md_sysfs_entry md_reshape_position =
3858__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3859 reshape_position_store);
3860
Dan Williamsb522adc2009-03-31 15:00:31 +11003861static ssize_t
3862array_size_show(mddev_t *mddev, char *page)
3863{
3864 if (mddev->external_size)
3865 return sprintf(page, "%llu\n",
3866 (unsigned long long)mddev->array_sectors/2);
3867 else
3868 return sprintf(page, "default\n");
3869}
3870
3871static ssize_t
3872array_size_store(mddev_t *mddev, const char *buf, size_t len)
3873{
3874 sector_t sectors;
3875
3876 if (strncmp(buf, "default", 7) == 0) {
3877 if (mddev->pers)
3878 sectors = mddev->pers->size(mddev, 0, 0);
3879 else
3880 sectors = mddev->array_sectors;
3881
3882 mddev->external_size = 0;
3883 } else {
3884 if (strict_blocks_to_sectors(buf, &sectors) < 0)
3885 return -EINVAL;
3886 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10003887 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11003888
3889 mddev->external_size = 1;
3890 }
3891
3892 mddev->array_sectors = sectors;
3893 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10003894 if (mddev->pers)
3895 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11003896
3897 return len;
3898}
3899
3900static struct md_sysfs_entry md_array_size =
3901__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
3902 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08003903
NeilBrowneae17012005-11-08 21:39:23 -08003904static struct attribute *md_default_attrs[] = {
3905 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003906 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003907 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003908 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003909 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003910 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003911 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08003912 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003913 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003914 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003915 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11003916 &md_array_size.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003917 NULL,
3918};
3919
3920static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003921 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003922 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003923 &md_sync_min.attr,
3924 &md_sync_max.attr,
3925 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003926 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003927 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003928 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003929 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003930 &md_suspend_lo.attr,
3931 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003932 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003933 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003934 NULL,
3935};
NeilBrown411036f2005-11-08 21:39:40 -08003936static struct attribute_group md_redundancy_group = {
3937 .name = NULL,
3938 .attrs = md_redundancy_attrs,
3939};
3940
NeilBrowneae17012005-11-08 21:39:23 -08003941
3942static ssize_t
3943md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3944{
3945 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3946 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003947 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003948
3949 if (!entry->show)
3950 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003951 rv = mddev_lock(mddev);
3952 if (!rv) {
3953 rv = entry->show(mddev, page);
3954 mddev_unlock(mddev);
3955 }
NeilBrown96de1e62005-11-08 21:39:39 -08003956 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003957}
3958
3959static ssize_t
3960md_attr_store(struct kobject *kobj, struct attribute *attr,
3961 const char *page, size_t length)
3962{
3963 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3964 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003965 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003966
3967 if (!entry->store)
3968 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003969 if (!capable(CAP_SYS_ADMIN))
3970 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003971 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11003972 if (mddev->hold_active == UNTIL_IOCTL)
3973 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003974 if (!rv) {
3975 rv = entry->store(mddev, page, length);
3976 mddev_unlock(mddev);
3977 }
NeilBrown96de1e62005-11-08 21:39:39 -08003978 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003979}
3980
3981static void md_free(struct kobject *ko)
3982{
3983 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11003984
3985 if (mddev->sysfs_state)
3986 sysfs_put(mddev->sysfs_state);
3987
3988 if (mddev->gendisk) {
3989 del_gendisk(mddev->gendisk);
3990 put_disk(mddev->gendisk);
3991 }
3992 if (mddev->queue)
3993 blk_cleanup_queue(mddev->queue);
3994
NeilBrowneae17012005-11-08 21:39:23 -08003995 kfree(mddev);
3996}
3997
3998static struct sysfs_ops md_sysfs_ops = {
3999 .show = md_attr_show,
4000 .store = md_attr_store,
4001};
4002static struct kobj_type md_ktype = {
4003 .release = md_free,
4004 .sysfs_ops = &md_sysfs_ops,
4005 .default_attrs = md_default_attrs,
4006};
4007
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008int mdp_major = 0;
4009
Dan Williams5fd3a172009-03-04 00:57:25 -07004010static void mddev_delayed_delete(struct work_struct *ws)
4011{
4012 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4013
4014 if (mddev->private == &md_redundancy_group) {
4015 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
4016 if (mddev->sysfs_action)
4017 sysfs_put(mddev->sysfs_action);
4018 mddev->sysfs_action = NULL;
4019 mddev->private = NULL;
4020 }
NeilBrown43a70502009-12-14 12:49:55 +11004021 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004022 kobject_del(&mddev->kobj);
4023 kobject_put(&mddev->kobj);
4024}
4025
NeilBrownefeb53c2009-01-09 08:31:10 +11004026static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004028 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 mddev_t *mddev = mddev_find(dev);
4030 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004031 int partitioned;
4032 int shift;
4033 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004034 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
4036 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004037 return -ENODEV;
4038
4039 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4040 shift = partitioned ? MdpMinorShift : 0;
4041 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
NeilBrownd3374822009-01-09 08:31:10 +11004043 /* wait for any previous instance if this device
4044 * to be completed removed (mddev_delayed_delete).
4045 */
4046 flush_scheduled_work();
4047
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004048 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004049 error = -EEXIST;
4050 if (mddev->gendisk)
4051 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004052
4053 if (name) {
4054 /* Need to ensure that 'name' is not a duplicate.
4055 */
4056 mddev_t *mddev2;
4057 spin_lock(&all_mddevs_lock);
4058
4059 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4060 if (mddev2->gendisk &&
4061 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4062 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004063 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004064 }
4065 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 }
NeilBrown8b765392009-01-09 08:31:08 +11004067
NeilBrown0909dc42009-07-01 12:27:21 +10004068 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004069 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004070 if (!mddev->queue)
4071 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004072 mddev->queue->queuedata = mddev;
4073
NeilBrown8b765392009-01-09 08:31:08 +11004074 /* Can be unlocked because the queue is new: no concurrency */
4075 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4076
NeilBrown409c57f2009-03-31 14:39:39 +11004077 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004078
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 disk = alloc_disk(1 << shift);
4080 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004081 blk_cleanup_queue(mddev->queue);
4082 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004083 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004085 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004087 if (name)
4088 strcpy(disk->disk_name, name);
4089 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004091 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 disk->fops = &md_fops;
4094 disk->private_data = mddev;
4095 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004096 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004097 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004098 * remove it now.
4099 */
4100 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 add_disk(disk);
4102 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004103 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4104 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004105 if (error) {
4106 /* This isn't possible, but as kobject_init_and_add is marked
4107 * __must_check, we must do something with the result
4108 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004109 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4110 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004111 error = 0;
4112 }
NeilBrown43a70502009-12-14 12:49:55 +11004113 if (sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4114 printk(KERN_DEBUG "pointless warning\n");
NeilBrown0909dc42009-07-01 12:27:21 +10004115 abort:
4116 mutex_unlock(&disks_mutex);
4117 if (!error) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004118 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11004119 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
4120 }
NeilBrownd3374822009-01-09 08:31:10 +11004121 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004122 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004123}
4124
4125static struct kobject *md_probe(dev_t dev, int *part, void *data)
4126{
4127 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 return NULL;
4129}
4130
NeilBrownefeb53c2009-01-09 08:31:10 +11004131static int add_named_array(const char *val, struct kernel_param *kp)
4132{
4133 /* val must be "md_*" where * is not all digits.
4134 * We allocate an array with a large free minor number, and
4135 * set the name to val. val must not already be an active name.
4136 */
4137 int len = strlen(val);
4138 char buf[DISK_NAME_LEN];
4139
4140 while (len && val[len-1] == '\n')
4141 len--;
4142 if (len >= DISK_NAME_LEN)
4143 return -E2BIG;
4144 strlcpy(buf, val, len+1);
4145 if (strncmp(buf, "md_", 3) != 0)
4146 return -EINVAL;
4147 return md_alloc(0, buf);
4148}
4149
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150static void md_safemode_timeout(unsigned long data)
4151{
4152 mddev_t *mddev = (mddev_t *) data;
4153
Neil Brown0fd62b82008-06-28 08:31:36 +10004154 if (!atomic_read(&mddev->writes_pending)) {
4155 mddev->safemode = 1;
4156 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11004157 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 md_wakeup_thread(mddev->thread);
4160}
4161
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004162static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
4164static int do_md_run(mddev_t * mddev)
4165{
NeilBrown2604b702006-01-06 00:20:36 -08004166 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 mdk_rdev_t *rdev;
4168 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08004169 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170
NeilBrowna757e642005-04-16 15:26:42 -07004171 if (list_empty(&mddev->disks))
4172 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
4175 if (mddev->pers)
4176 return -EBUSY;
4177
4178 /*
4179 * Analyze all RAID superblock(s)
4180 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004181 if (!mddev->raid_disks) {
4182 if (!mddev->persistent)
4183 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004184 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186
NeilBrownd9d166c2006-01-06 00:20:51 -08004187 if (mddev->level != LEVEL_NONE)
4188 request_module("md-level-%d", mddev->level);
4189 else if (mddev->clevel[0])
4190 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191
4192 /*
4193 * Drop all container device buffers, from now on
4194 * the only valid external interface is through the md
4195 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004197 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004198 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 continue;
4200 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004201 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004202
4203 /* perform some consistency tests on the device.
4204 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004205 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004206 */
Andre Noll0f420352008-07-11 22:02:23 +10004207 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004208 if (mddev->dev_sectors &&
4209 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004210 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004211 printk("md: %s: data overlaps metadata\n",
4212 mdname(mddev));
4213 return -EINVAL;
4214 }
4215 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004216 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004217 > rdev->data_offset) {
4218 printk("md: %s: metadata overlaps data\n",
4219 mdname(mddev));
4220 return -EINVAL;
4221 }
4222 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004223 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 }
4225
4226 md_probe(mddev->unit, NULL, NULL);
4227 disk = mddev->gendisk;
4228 if (!disk)
4229 return -ENOMEM;
4230
4231 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004232 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004233 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004235 if (mddev->level != LEVEL_NONE)
4236 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4237 mddev->level);
4238 else
4239 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4240 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 return -EINVAL;
4242 }
NeilBrown2604b702006-01-06 00:20:36 -08004243 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004245 if (mddev->level != pers->level) {
4246 mddev->level = pers->level;
4247 mddev->new_level = pers->level;
4248 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004249 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
NeilBrownf6705572006-03-27 01:18:11 -08004251 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004252 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004253 /* This personality cannot handle reshaping... */
4254 mddev->pers = NULL;
4255 module_put(pers->owner);
4256 return -EINVAL;
4257 }
4258
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004259 if (pers->sync_request) {
4260 /* Warn if this is a potentially silly
4261 * configuration.
4262 */
4263 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4264 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004265 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004266
4267 list_for_each_entry(rdev, &mddev->disks, same_set)
4268 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004269 if (rdev < rdev2 &&
4270 rdev->bdev->bd_contains ==
4271 rdev2->bdev->bd_contains) {
4272 printk(KERN_WARNING
4273 "%s: WARNING: %s appears to be"
4274 " on the same physical disk as"
4275 " %s.\n",
4276 mdname(mddev),
4277 bdevname(rdev->bdev,b),
4278 bdevname(rdev2->bdev,b2));
4279 warned = 1;
4280 }
4281 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004282
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004283 if (warned)
4284 printk(KERN_WARNING
4285 "True protection against single-disk"
4286 " failure might be compromised.\n");
4287 }
4288
NeilBrown657390d2005-08-26 18:34:16 -07004289 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004290 /* may be over-ridden by personality */
4291 mddev->resync_max_sectors = mddev->dev_sectors;
4292
NeilBrowna9701a32005-11-08 21:39:34 -08004293 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004294 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295
NeilBrownf91de922005-11-08 21:39:36 -08004296 if (start_readonly)
4297 mddev->ro = 2; /* read-only, but switch on first write */
4298
NeilBrownb15c2e52006-01-06 00:20:16 -08004299 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004300 if (err)
4301 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004302 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4303 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4304 " but 'external_size' not in effect?\n", __func__);
4305 printk(KERN_ERR
4306 "md: invalid array_size %llu > default size %llu\n",
4307 (unsigned long long)mddev->array_sectors / 2,
4308 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4309 err = -EINVAL;
4310 mddev->pers->stop(mddev);
4311 }
4312 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004313 err = bitmap_create(mddev);
4314 if (err) {
4315 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4316 mdname(mddev), err);
4317 mddev->pers->stop(mddev);
4318 }
4319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 module_put(mddev->pers->owner);
4322 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004323 bitmap_destroy(mddev);
4324 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004326 if (mddev->pers->sync_request) {
4327 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4328 printk(KERN_WARNING
4329 "md: cannot register extra attributes for %s\n",
4330 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004331 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004332 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004333 mddev->ro = 0;
4334
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 atomic_set(&mddev->writes_pending,0);
4336 mddev->safemode = 0;
4337 mddev->safemode_timer.function = md_safemode_timeout;
4338 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004339 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004341
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004342 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004343 if (rdev->raid_disk >= 0) {
4344 char nm[20];
4345 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004346 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4347 printk("md: cannot register %s for %s\n",
4348 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350
4351 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4352
NeilBrown850b2b42006-10-03 01:15:46 -07004353 if (mddev->flags)
4354 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355
Andre Nollf233ea52008-07-21 17:05:22 +10004356 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004358 /* If there is a partially-recovered drive we need to
4359 * start recovery here. If we leave it to md_check_recovery,
4360 * it will remove the drives and not do the right thing
4361 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07004362 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004363 int spares = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004364 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004365 if (rdev->raid_disk >= 0 &&
4366 !test_bit(In_sync, &rdev->flags) &&
4367 !test_bit(Faulty, &rdev->flags))
4368 /* complete an interrupted recovery */
4369 spares++;
4370 if (spares && mddev->pers->sync_request) {
4371 mddev->recovery = 0;
4372 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4373 mddev->sync_thread = md_register_thread(md_do_sync,
4374 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10004375 "resync");
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004376 if (!mddev->sync_thread) {
4377 printk(KERN_ERR "%s: could not start resync"
4378 " thread...\n",
4379 mdname(mddev));
4380 /* leave the spares where they are, it shouldn't hurt */
4381 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07004382 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004383 }
4384 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07004385 md_wakeup_thread(mddev->thread);
4386 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004387
NeilBrown449aad32009-08-03 10:59:58 +10004388 revalidate_disk(mddev->gendisk);
Linus Torvalds44ce6292007-05-09 18:51:36 -07004389 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08004390 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004391 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004392 if (mddev->sysfs_action)
4393 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004394 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09004395 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 return 0;
4397}
4398
4399static int restart_array(mddev_t *mddev)
4400{
4401 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402
Andre Noll80fab1d2008-07-11 22:02:21 +10004403 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004405 return -ENXIO;
4406 if (!mddev->pers)
4407 return -EINVAL;
4408 if (!mddev->ro)
4409 return -EBUSY;
4410 mddev->safemode = 0;
4411 mddev->ro = 0;
4412 set_disk_ro(disk, 0);
4413 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4414 mdname(mddev));
4415 /* Kick recovery or resync if necessary */
4416 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4417 md_wakeup_thread(mddev->thread);
4418 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004419 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421}
4422
NeilBrownacc55e22006-06-26 00:27:47 -07004423/* similar to deny_write_access, but accounts for our holding a reference
4424 * to the file ourselves */
4425static int deny_bitmap_write_access(struct file * file)
4426{
4427 struct inode *inode = file->f_mapping->host;
4428
4429 spin_lock(&inode->i_lock);
4430 if (atomic_read(&inode->i_writecount) > 1) {
4431 spin_unlock(&inode->i_lock);
4432 return -ETXTBSY;
4433 }
4434 atomic_set(&inode->i_writecount, -1);
4435 spin_unlock(&inode->i_lock);
4436
4437 return 0;
4438}
4439
NeilBrown43a70502009-12-14 12:49:55 +11004440void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004441{
4442 struct inode *inode = file->f_mapping->host;
4443
4444 spin_lock(&inode->i_lock);
4445 atomic_set(&inode->i_writecount, 1);
4446 spin_unlock(&inode->i_lock);
4447}
4448
NeilBrown9e653b62006-06-26 00:27:58 -07004449/* mode:
4450 * 0 - completely stop and dis-assemble array
4451 * 1 - switch to readonly
4452 * 2 - stop but do not disassemble array
4453 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004454static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455{
4456 int err = 0;
4457 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004458 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459
NeilBrownc8c00a62009-08-10 12:50:52 +10004460 mutex_lock(&mddev->open_mutex);
NeilBrownf2ea68c2008-07-21 17:05:25 +10004461 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004462 printk("md: %s still in use.\n",mdname(mddev));
NeilBrownc8c00a62009-08-10 12:50:52 +10004463 err = -EBUSY;
4464 } else if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465
4466 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004467 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4469 md_unregister_thread(mddev->sync_thread);
4470 mddev->sync_thread = NULL;
4471 }
4472
4473 del_timer_sync(&mddev->safemode_timer);
4474
NeilBrown9e653b62006-06-26 00:27:58 -07004475 switch(mode) {
4476 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004478 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 goto out;
4480 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004481 break;
4482 case 0: /* disassemble */
4483 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004484 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004485 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 if (mddev->ro)
4487 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004488
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004490 mddev->queue->merge_bvec_fn = NULL;
4491 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004492 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 module_put(mddev->pers->owner);
Dan Williams5fd3a172009-03-04 00:57:25 -07004494 if (mddev->pers->sync_request)
4495 mddev->private = &md_redundancy_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004497 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004498 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004499
NeilBrownc4647292009-05-07 12:51:06 +10004500 list_for_each_entry(rdev, &mddev->disks, same_set)
4501 if (rdev->raid_disk >= 0) {
4502 char nm[20];
4503 sprintf(nm, "rd%d", rdev->raid_disk);
4504 sysfs_remove_link(&mddev->kobj, nm);
4505 }
4506
NeilBrown0d4ca602006-12-10 02:20:44 -08004507 set_capacity(disk, 0);
Linus Torvalds44ce6292007-05-09 18:51:36 -07004508 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08004509
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 if (mddev->ro)
4511 mddev->ro = 0;
4512 }
NeilBrown850b2b42006-10-03 01:15:46 -07004513 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 /* mark array as shutdown cleanly */
4515 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07004516 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 }
NeilBrown9e653b62006-06-26 00:27:58 -07004518 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004520 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown80ffb3c2009-08-18 10:35:26 +10004521 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 }
NeilBrownc8c00a62009-08-10 12:50:52 +10004523out:
4524 mutex_unlock(&mddev->open_mutex);
4525 if (err)
4526 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 /*
4528 * Free resources if final stop
4529 */
NeilBrown9e653b62006-06-26 00:27:58 -07004530 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004531
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4533
NeilBrown978f9462006-02-02 14:28:05 -08004534 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004535 if (mddev->bitmap_info.file) {
4536 restore_bitmap_write_access(mddev->bitmap_info.file);
4537 fput(mddev->bitmap_info.file);
4538 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004539 }
NeilBrownc3d97142009-12-14 12:49:52 +11004540 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004541
NeilBrown177a99b2008-02-06 01:39:56 -08004542 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07004543 flush_scheduled_work();
4544
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 export_array(mddev);
4546
Andre Nollf233ea52008-07-21 17:05:22 +10004547 mddev->array_sectors = 0;
Dan Williamsb522adc2009-03-31 15:00:31 +11004548 mddev->external_size = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004549 mddev->dev_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004550 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004551 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004552 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004553 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004554 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004555 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004556 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004557 mddev->level = LEVEL_NONE;
4558 mddev->clevel[0] = 0;
4559 mddev->flags = 0;
4560 mddev->ro = 0;
4561 mddev->metadata_type[0] = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10004562 mddev->chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004563 mddev->ctime = mddev->utime = 0;
4564 mddev->layout = 0;
4565 mddev->max_disks = 0;
4566 mddev->events = 0;
4567 mddev->delta_disks = 0;
4568 mddev->new_level = LEVEL_NONE;
4569 mddev->new_layout = 0;
Andre Noll664e7c42009-06-18 08:45:27 +10004570 mddev->new_chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004571 mddev->curr_resync = 0;
4572 mddev->resync_mismatches = 0;
4573 mddev->suspend_lo = mddev->suspend_hi = 0;
4574 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4575 mddev->recovery = 0;
4576 mddev->in_sync = 0;
4577 mddev->changed = 0;
4578 mddev->degraded = 0;
4579 mddev->barriers_work = 0;
4580 mddev->safemode = 0;
NeilBrownc3d97142009-12-14 12:49:52 +11004581 mddev->bitmap_info.offset = 0;
4582 mddev->bitmap_info.default_offset = 0;
NeilBrown42a04b52009-12-14 12:49:53 +11004583 mddev->bitmap_info.chunksize = 0;
4584 mddev->bitmap_info.daemon_sleep = 0;
4585 mddev->bitmap_info.max_write_behind = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004586 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004587 if (mddev->hold_active == UNTIL_STOP)
4588 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004589
NeilBrowna8a55c32006-06-26 00:27:39 -07004590 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4592 mdname(mddev));
4593 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004594 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004595 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004596 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 return err;
4598}
4599
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004600#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601static void autorun_array(mddev_t *mddev)
4602{
4603 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 int err;
4605
NeilBrowna757e642005-04-16 15:26:42 -07004606 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
4609 printk(KERN_INFO "md: running: ");
4610
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004611 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 char b[BDEVNAME_SIZE];
4613 printk("<%s>", bdevname(rdev->bdev,b));
4614 }
4615 printk("\n");
4616
NeilBrownd710e132008-10-13 11:55:12 +11004617 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 if (err) {
4619 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004620 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 }
4622}
4623
4624/*
4625 * lets try to run arrays based on all disks that have arrived
4626 * until now. (those are in pending_raid_disks)
4627 *
4628 * the method: pick the first pending disk, collect all disks with
4629 * the same UUID, remove all from the pending list and put them into
4630 * the 'same_array' list. Then order this list based on superblock
4631 * update time (freshest comes first), kick out 'old' disks and
4632 * compare superblocks. If everything's fine then run it.
4633 *
4634 * If "unit" is allocated, then bump its reference count
4635 */
4636static void autorun_devices(int part)
4637{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004638 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 mddev_t *mddev;
4640 char b[BDEVNAME_SIZE];
4641
4642 printk(KERN_INFO "md: autorun ...\n");
4643 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004644 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004646 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 rdev0 = list_entry(pending_raid_disks.next,
4648 mdk_rdev_t, same_set);
4649
4650 printk(KERN_INFO "md: considering %s ...\n",
4651 bdevname(rdev0->bdev,b));
4652 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004653 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 if (super_90_load(rdev, rdev0, 0) >= 0) {
4655 printk(KERN_INFO "md: adding %s ...\n",
4656 bdevname(rdev->bdev,b));
4657 list_move(&rdev->same_set, &candidates);
4658 }
4659 /*
4660 * now we have a set of devices, with all of them having
4661 * mostly sane superblocks. It's time to allocate the
4662 * mddev.
4663 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004664 if (part) {
4665 dev = MKDEV(mdp_major,
4666 rdev0->preferred_minor << MdpMinorShift);
4667 unit = MINOR(dev) >> MdpMinorShift;
4668 } else {
4669 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4670 unit = MINOR(dev);
4671 }
4672 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4674 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4675 break;
4676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
4678 md_probe(dev, NULL, NULL);
4679 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004680 if (!mddev || !mddev->gendisk) {
4681 if (mddev)
4682 mddev_put(mddev);
4683 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 "md: cannot allocate memory for md drive.\n");
4685 break;
4686 }
4687 if (mddev_lock(mddev))
4688 printk(KERN_WARNING "md: %s locked, cannot run\n",
4689 mdname(mddev));
4690 else if (mddev->raid_disks || mddev->major_version
4691 || !list_empty(&mddev->disks)) {
4692 printk(KERN_WARNING
4693 "md: %s already running, cannot run %s\n",
4694 mdname(mddev), bdevname(rdev0->bdev,b));
4695 mddev_unlock(mddev);
4696 } else {
4697 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004698 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004699 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 list_del_init(&rdev->same_set);
4701 if (bind_rdev_to_array(rdev, mddev))
4702 export_rdev(rdev);
4703 }
4704 autorun_array(mddev);
4705 mddev_unlock(mddev);
4706 }
4707 /* on success, candidates will be empty, on error
4708 * it won't...
4709 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004710 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004711 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 mddev_put(mddev);
4715 }
4716 printk(KERN_INFO "md: ... autorun DONE.\n");
4717}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004718#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720static int get_version(void __user * arg)
4721{
4722 mdu_version_t ver;
4723
4724 ver.major = MD_MAJOR_VERSION;
4725 ver.minor = MD_MINOR_VERSION;
4726 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4727
4728 if (copy_to_user(arg, &ver, sizeof(ver)))
4729 return -EFAULT;
4730
4731 return 0;
4732}
4733
4734static int get_array_info(mddev_t * mddev, void __user * arg)
4735{
4736 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004737 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739
NeilBrowna9f326e2009-09-23 18:06:41 +10004740 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004741 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004743 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 failed++;
4745 else {
4746 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004747 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004748 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 else
4750 spare++;
4751 }
4752 }
4753
4754 info.major_version = mddev->major_version;
4755 info.minor_version = mddev->minor_version;
4756 info.patch_version = MD_PATCHLEVEL_VERSION;
4757 info.ctime = mddev->ctime;
4758 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004759 info.size = mddev->dev_sectors / 2;
4760 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004761 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 info.nr_disks = nr;
4763 info.raid_disks = mddev->raid_disks;
4764 info.md_minor = mddev->md_minor;
4765 info.not_persistent= !mddev->persistent;
4766
4767 info.utime = mddev->utime;
4768 info.state = 0;
4769 if (mddev->in_sync)
4770 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11004771 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07004772 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004773 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 info.working_disks = working;
4775 info.failed_disks = failed;
4776 info.spare_disks = spare;
4777
4778 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004779 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780
4781 if (copy_to_user(arg, &info, sizeof(info)))
4782 return -EFAULT;
4783
4784 return 0;
4785}
4786
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004787static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004788{
4789 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4790 char *ptr, *buf = NULL;
4791 int err = -ENOMEM;
4792
Dan Williamsb5470dc2008-06-27 21:44:04 -07004793 if (md_allow_write(mddev))
4794 file = kmalloc(sizeof(*file), GFP_NOIO);
4795 else
4796 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004797
NeilBrown32a76272005-06-21 17:17:14 -07004798 if (!file)
4799 goto out;
4800
4801 /* bitmap disabled, zero the first byte and copy out */
4802 if (!mddev->bitmap || !mddev->bitmap->file) {
4803 file->pathname[0] = '\0';
4804 goto copy_out;
4805 }
4806
4807 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4808 if (!buf)
4809 goto out;
4810
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004811 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4812 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004813 goto out;
4814
4815 strcpy(file->pathname, ptr);
4816
4817copy_out:
4818 err = 0;
4819 if (copy_to_user(arg, file, sizeof(*file)))
4820 err = -EFAULT;
4821out:
4822 kfree(buf);
4823 kfree(file);
4824 return err;
4825}
4826
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827static int get_disk_info(mddev_t * mddev, void __user * arg)
4828{
4829 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 mdk_rdev_t *rdev;
4831
4832 if (copy_from_user(&info, arg, sizeof(info)))
4833 return -EFAULT;
4834
Andre Noll26ef3792008-07-11 22:02:21 +10004835 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 if (rdev) {
4837 info.major = MAJOR(rdev->bdev->bd_dev);
4838 info.minor = MINOR(rdev->bdev->bd_dev);
4839 info.raid_disk = rdev->raid_disk;
4840 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004841 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004843 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 info.state |= (1<<MD_DISK_ACTIVE);
4845 info.state |= (1<<MD_DISK_SYNC);
4846 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004847 if (test_bit(WriteMostly, &rdev->flags))
4848 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 } else {
4850 info.major = info.minor = 0;
4851 info.raid_disk = -1;
4852 info.state = (1<<MD_DISK_REMOVED);
4853 }
4854
4855 if (copy_to_user(arg, &info, sizeof(info)))
4856 return -EFAULT;
4857
4858 return 0;
4859}
4860
4861static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4862{
4863 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4864 mdk_rdev_t *rdev;
4865 dev_t dev = MKDEV(info->major,info->minor);
4866
4867 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4868 return -EOVERFLOW;
4869
4870 if (!mddev->raid_disks) {
4871 int err;
4872 /* expecting a device which has a superblock */
4873 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4874 if (IS_ERR(rdev)) {
4875 printk(KERN_WARNING
4876 "md: md_import_device returned %ld\n",
4877 PTR_ERR(rdev));
4878 return PTR_ERR(rdev);
4879 }
4880 if (!list_empty(&mddev->disks)) {
4881 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4882 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10004883 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 .load_super(rdev, rdev0, mddev->minor_version);
4885 if (err < 0) {
4886 printk(KERN_WARNING
4887 "md: %s has different UUID to %s\n",
4888 bdevname(rdev->bdev,b),
4889 bdevname(rdev0->bdev,b2));
4890 export_rdev(rdev);
4891 return -EINVAL;
4892 }
4893 }
4894 err = bind_rdev_to_array(rdev, mddev);
4895 if (err)
4896 export_rdev(rdev);
4897 return err;
4898 }
4899
4900 /*
4901 * add_new_disk can be used once the array is assembled
4902 * to add "hot spares". They must already have a superblock
4903 * written
4904 */
4905 if (mddev->pers) {
4906 int err;
4907 if (!mddev->pers->hot_add_disk) {
4908 printk(KERN_WARNING
4909 "%s: personality does not support diskops!\n",
4910 mdname(mddev));
4911 return -EINVAL;
4912 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004913 if (mddev->persistent)
4914 rdev = md_import_device(dev, mddev->major_version,
4915 mddev->minor_version);
4916 else
4917 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 if (IS_ERR(rdev)) {
4919 printk(KERN_WARNING
4920 "md: md_import_device returned %ld\n",
4921 PTR_ERR(rdev));
4922 return PTR_ERR(rdev);
4923 }
NeilBrown41158c72005-06-21 17:17:25 -07004924 /* set save_raid_disk if appropriate */
4925 if (!mddev->persistent) {
4926 if (info->state & (1<<MD_DISK_SYNC) &&
4927 info->raid_disk < mddev->raid_disks)
4928 rdev->raid_disk = info->raid_disk;
4929 else
4930 rdev->raid_disk = -1;
4931 } else
4932 super_types[mddev->major_version].
4933 validate_super(mddev, rdev);
4934 rdev->saved_raid_disk = rdev->raid_disk;
4935
NeilBrownb2d444d2005-11-08 21:39:31 -08004936 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004937 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4938 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11004939 else
4940 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004941
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 rdev->raid_disk = -1;
4943 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004944 if (!err && !mddev->pers->hot_remove_disk) {
4945 /* If there is hot_add_disk but no hot_remove_disk
4946 * then added disks for geometry changes,
4947 * and should be added immediately.
4948 */
4949 super_types[mddev->major_version].
4950 validate_super(mddev, rdev);
4951 err = mddev->pers->hot_add_disk(mddev, rdev);
4952 if (err)
4953 unbind_rdev_from_array(rdev);
4954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 if (err)
4956 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004957 else
NeilBrown3c0ee632008-10-21 13:25:28 +11004958 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07004959
NeilBrown17571282006-12-10 02:20:52 -08004960 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004961 if (mddev->degraded)
4962 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004963 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004964 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 return err;
4966 }
4967
4968 /* otherwise, add_new_disk is only allowed
4969 * for major_version==0 superblocks
4970 */
4971 if (mddev->major_version != 0) {
4972 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4973 mdname(mddev));
4974 return -EINVAL;
4975 }
4976
4977 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4978 int err;
NeilBrownd710e132008-10-13 11:55:12 +11004979 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 if (IS_ERR(rdev)) {
4981 printk(KERN_WARNING
4982 "md: error, md_import_device() returned %ld\n",
4983 PTR_ERR(rdev));
4984 return PTR_ERR(rdev);
4985 }
4986 rdev->desc_nr = info->number;
4987 if (info->raid_disk < mddev->raid_disks)
4988 rdev->raid_disk = info->raid_disk;
4989 else
4990 rdev->raid_disk = -1;
4991
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004993 if (info->state & (1<<MD_DISK_SYNC))
4994 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004996 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4997 set_bit(WriteMostly, &rdev->flags);
4998
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 if (!mddev->persistent) {
5000 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10005001 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 } else
Andre Noll0f420352008-07-11 22:02:23 +10005003 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10005004 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005
NeilBrown2bf071b2006-01-06 00:20:55 -08005006 err = bind_rdev_to_array(rdev, mddev);
5007 if (err) {
5008 export_rdev(rdev);
5009 return err;
5010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 }
5012
5013 return 0;
5014}
5015
5016static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5017{
5018 char b[BDEVNAME_SIZE];
5019 mdk_rdev_t *rdev;
5020
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 rdev = find_rdev(mddev, dev);
5022 if (!rdev)
5023 return -ENXIO;
5024
5025 if (rdev->raid_disk >= 0)
5026 goto busy;
5027
5028 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005029 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005030 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031
5032 return 0;
5033busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005034 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 bdevname(rdev->bdev,b), mdname(mddev));
5036 return -EBUSY;
5037}
5038
5039static int hot_add_disk(mddev_t * mddev, dev_t dev)
5040{
5041 char b[BDEVNAME_SIZE];
5042 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 mdk_rdev_t *rdev;
5044
5045 if (!mddev->pers)
5046 return -ENODEV;
5047
5048 if (mddev->major_version != 0) {
5049 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5050 " version-0 superblocks.\n",
5051 mdname(mddev));
5052 return -EINVAL;
5053 }
5054 if (!mddev->pers->hot_add_disk) {
5055 printk(KERN_WARNING
5056 "%s: personality does not support diskops!\n",
5057 mdname(mddev));
5058 return -EINVAL;
5059 }
5060
NeilBrownd710e132008-10-13 11:55:12 +11005061 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 if (IS_ERR(rdev)) {
5063 printk(KERN_WARNING
5064 "md: error, md_import_device() returned %ld\n",
5065 PTR_ERR(rdev));
5066 return -EINVAL;
5067 }
5068
5069 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10005070 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 else
Andre Noll0f420352008-07-11 22:02:23 +10005072 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073
NeilBrown8190e752009-06-18 08:48:58 +10005074 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075
NeilBrownb2d444d2005-11-08 21:39:31 -08005076 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 printk(KERN_WARNING
5078 "md: can not hot-add faulty %s disk to %s!\n",
5079 bdevname(rdev->bdev,b), mdname(mddev));
5080 err = -EINVAL;
5081 goto abort_export;
5082 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005083 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005085 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005086 err = bind_rdev_to_array(rdev, mddev);
5087 if (err)
5088 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089
5090 /*
5091 * The rest should better be atomic, we can have disk failures
5092 * noticed in interrupt contexts ...
5093 */
5094
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 rdev->raid_disk = -1;
5096
NeilBrown850b2b42006-10-03 01:15:46 -07005097 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098
5099 /*
5100 * Kick recovery, maybe this spare has to be added to the
5101 * array immediately.
5102 */
5103 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5104 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005105 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 return 0;
5107
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108abort_export:
5109 export_rdev(rdev);
5110 return err;
5111}
5112
NeilBrown32a76272005-06-21 17:17:14 -07005113static int set_bitmap_file(mddev_t *mddev, int fd)
5114{
5115 int err;
5116
NeilBrown36fa3062005-09-09 16:23:45 -07005117 if (mddev->pers) {
5118 if (!mddev->pers->quiesce)
5119 return -EBUSY;
5120 if (mddev->recovery || mddev->sync_thread)
5121 return -EBUSY;
5122 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005123 }
5124
NeilBrown36fa3062005-09-09 16:23:45 -07005125
5126 if (fd >= 0) {
5127 if (mddev->bitmap)
5128 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005129 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005130
NeilBrownc3d97142009-12-14 12:49:52 +11005131 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005132 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5133 mdname(mddev));
5134 return -EBADF;
5135 }
5136
NeilBrownc3d97142009-12-14 12:49:52 +11005137 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005138 if (err) {
5139 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5140 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005141 fput(mddev->bitmap_info.file);
5142 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005143 return err;
5144 }
NeilBrownc3d97142009-12-14 12:49:52 +11005145 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005146 } else if (mddev->bitmap == NULL)
5147 return -ENOENT; /* cannot remove what isn't there */
5148 err = 0;
5149 if (mddev->pers) {
5150 mddev->pers->quiesce(mddev, 1);
5151 if (fd >= 0)
5152 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005153 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005154 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005155 fd = -1; /* make sure to put the file */
5156 }
NeilBrown36fa3062005-09-09 16:23:45 -07005157 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005158 }
5159 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005160 if (mddev->bitmap_info.file) {
5161 restore_bitmap_write_access(mddev->bitmap_info.file);
5162 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005163 }
NeilBrownc3d97142009-12-14 12:49:52 +11005164 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005165 }
5166
NeilBrown32a76272005-06-21 17:17:14 -07005167 return err;
5168}
5169
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170/*
5171 * set_array_info is used two different ways
5172 * The original usage is when creating a new array.
5173 * In this usage, raid_disks is > 0 and it together with
5174 * level, size, not_persistent,layout,chunksize determine the
5175 * shape of the array.
5176 * This will always create an array with a type-0.90.0 superblock.
5177 * The newer usage is when assembling an array.
5178 * In this case raid_disks will be 0, and the major_version field is
5179 * use to determine which style super-blocks are to be found on the devices.
5180 * The minor and patch _version numbers are also kept incase the
5181 * super_block handler wishes to interpret them.
5182 */
5183static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5184{
5185
5186 if (info->raid_disks == 0) {
5187 /* just setting version number for superblock loading */
5188 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005189 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 super_types[info->major_version].name == NULL) {
5191 /* maybe try to auto-load a module? */
5192 printk(KERN_INFO
5193 "md: superblock version %d not known\n",
5194 info->major_version);
5195 return -EINVAL;
5196 }
5197 mddev->major_version = info->major_version;
5198 mddev->minor_version = info->minor_version;
5199 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005200 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 return 0;
5202 }
5203 mddev->major_version = MD_MAJOR_VERSION;
5204 mddev->minor_version = MD_MINOR_VERSION;
5205 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5206 mddev->ctime = get_seconds();
5207
5208 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005209 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005210 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 mddev->raid_disks = info->raid_disks;
5212 /* don't set md_minor, it is determined by which /dev/md* was
5213 * openned
5214 */
5215 if (info->state & (1<<MD_SB_CLEAN))
5216 mddev->recovery_cp = MaxSector;
5217 else
5218 mddev->recovery_cp = 0;
5219 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005220 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221
5222 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005223 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224
5225 mddev->max_disks = MD_SB_DISKS;
5226
NeilBrowne6910632008-02-06 01:39:51 -08005227 if (mddev->persistent)
5228 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005229 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230
NeilBrownc3d97142009-12-14 12:49:52 +11005231 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5232 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005233
NeilBrownf6705572006-03-27 01:18:11 -08005234 mddev->reshape_position = MaxSector;
5235
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 /*
5237 * Generate a 128 bit UUID
5238 */
5239 get_random_bytes(mddev->uuid, 16);
5240
NeilBrownf6705572006-03-27 01:18:11 -08005241 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005242 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005243 mddev->new_layout = mddev->layout;
5244 mddev->delta_disks = 0;
5245
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 return 0;
5247}
5248
Dan Williams1f403622009-03-31 14:59:03 +11005249void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5250{
Dan Williamsb522adc2009-03-31 15:00:31 +11005251 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5252
5253 if (mddev->external_size)
5254 return;
5255
Dan Williams1f403622009-03-31 14:59:03 +11005256 mddev->array_sectors = array_sectors;
5257}
5258EXPORT_SYMBOL(md_set_array_sectors);
5259
Andre Nolld71f9f82008-07-11 22:02:22 +10005260static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005261{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005262 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005263 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005264 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005265
5266 if (mddev->pers->resize == NULL)
5267 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005268 /* The "num_sectors" is the number of sectors of each device that
5269 * is used. This can only make sense for arrays with redundancy.
5270 * linear and raid0 always use whatever space is available. We can only
5271 * consider changing this number if no resync or reconstruction is
5272 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005273 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005274 * of each device. If num_sectors is zero, we find the largest size
5275 * that fits.
5276
NeilBrowna35b0d62006-01-06 00:20:49 -08005277 */
5278 if (mddev->sync_thread)
5279 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005280 if (mddev->bitmap)
5281 /* Sorry, cannot grow a bitmap yet, just remove it,
5282 * grow, and re-add.
5283 */
5284 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005285 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005286 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005287
Andre Nolld71f9f82008-07-11 22:02:22 +10005288 if (fit && (num_sectors == 0 || num_sectors > avail))
5289 num_sectors = avail;
5290 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005291 return -ENOSPC;
5292 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005293 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005294 if (!rv)
5295 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005296 return rv;
5297}
5298
NeilBrownda943b992006-01-06 00:20:54 -08005299static int update_raid_disks(mddev_t *mddev, int raid_disks)
5300{
5301 int rv;
5302 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005303 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005304 return -EINVAL;
5305 if (raid_disks <= 0 ||
5306 raid_disks >= mddev->max_disks)
5307 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005308 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005309 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005310 mddev->delta_disks = raid_disks - mddev->raid_disks;
5311
5312 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005313 return rv;
5314}
5315
5316
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317/*
5318 * update_array_info is used to change the configuration of an
5319 * on-line array.
5320 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5321 * fields in the info are checked against the array.
5322 * Any differences that cannot be handled will cause an error.
5323 * Normally, only one change can be managed at a time.
5324 */
5325static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5326{
5327 int rv = 0;
5328 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005329 int state = 0;
5330
5331 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005332 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005333 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334
5335 if (mddev->major_version != info->major_version ||
5336 mddev->minor_version != info->minor_version ||
5337/* mddev->patch_version != info->patch_version || */
5338 mddev->ctime != info->ctime ||
5339 mddev->level != info->level ||
5340/* mddev->layout != info->layout || */
5341 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005342 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005343 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5344 ((state^info->state) & 0xfffffe00)
5345 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 return -EINVAL;
5347 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005348 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5349 cnt++;
5350 if (mddev->raid_disks != info->raid_disks)
5351 cnt++;
5352 if (mddev->layout != info->layout)
5353 cnt++;
5354 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5355 cnt++;
5356 if (cnt == 0)
5357 return 0;
5358 if (cnt > 1)
5359 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360
5361 if (mddev->layout != info->layout) {
5362 /* Change layout
5363 * we don't need to do anything at the md level, the
5364 * personality will take care of it all.
5365 */
NeilBrown50ac1682009-06-18 08:47:55 +10005366 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005368 else {
5369 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005370 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005371 if (rv)
5372 mddev->new_layout = mddev->layout;
5373 return rv;
5374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005376 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005377 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378
NeilBrownda943b992006-01-06 00:20:54 -08005379 if (mddev->raid_disks != info->raid_disks)
5380 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381
NeilBrown36fa3062005-09-09 16:23:45 -07005382 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5383 if (mddev->pers->quiesce == NULL)
5384 return -EINVAL;
5385 if (mddev->recovery || mddev->sync_thread)
5386 return -EBUSY;
5387 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5388 /* add the bitmap */
5389 if (mddev->bitmap)
5390 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005391 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005392 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005393 mddev->bitmap_info.offset =
5394 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005395 mddev->pers->quiesce(mddev, 1);
5396 rv = bitmap_create(mddev);
5397 if (rv)
5398 bitmap_destroy(mddev);
5399 mddev->pers->quiesce(mddev, 0);
5400 } else {
5401 /* remove the bitmap */
5402 if (!mddev->bitmap)
5403 return -ENOENT;
5404 if (mddev->bitmap->file)
5405 return -EINVAL;
5406 mddev->pers->quiesce(mddev, 1);
5407 bitmap_destroy(mddev);
5408 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005409 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005410 }
5411 }
NeilBrown850b2b42006-10-03 01:15:46 -07005412 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 return rv;
5414}
5415
5416static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5417{
5418 mdk_rdev_t *rdev;
5419
5420 if (mddev->pers == NULL)
5421 return -ENODEV;
5422
5423 rdev = find_rdev(mddev, dev);
5424 if (!rdev)
5425 return -ENODEV;
5426
5427 md_error(mddev, rdev);
5428 return 0;
5429}
5430
Andre Noll2f9618c2008-04-25 18:57:58 +02005431/*
5432 * We have a problem here : there is no easy way to give a CHS
5433 * virtual geometry. We currently pretend that we have a 2 heads
5434 * 4 sectors (with a BIG number of cylinders...). This drives
5435 * dosfs just mad... ;-)
5436 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005437static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5438{
5439 mddev_t *mddev = bdev->bd_disk->private_data;
5440
5441 geo->heads = 2;
5442 geo->sectors = 4;
5443 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5444 return 0;
5445}
5446
Al Viroa39907f2008-03-02 10:31:15 -05005447static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 unsigned int cmd, unsigned long arg)
5449{
5450 int err = 0;
5451 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 mddev_t *mddev = NULL;
5453
5454 if (!capable(CAP_SYS_ADMIN))
5455 return -EACCES;
5456
5457 /*
5458 * Commands dealing with the RAID driver but not any
5459 * particular array:
5460 */
5461 switch (cmd)
5462 {
5463 case RAID_VERSION:
5464 err = get_version(argp);
5465 goto done;
5466
5467 case PRINT_RAID_DEBUG:
5468 err = 0;
5469 md_print_devices();
5470 goto done;
5471
5472#ifndef MODULE
5473 case RAID_AUTORUN:
5474 err = 0;
5475 autostart_arrays(arg);
5476 goto done;
5477#endif
5478 default:;
5479 }
5480
5481 /*
5482 * Commands creating/starting a new array:
5483 */
5484
Al Viroa39907f2008-03-02 10:31:15 -05005485 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486
5487 if (!mddev) {
5488 BUG();
5489 goto abort;
5490 }
5491
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 err = mddev_lock(mddev);
5493 if (err) {
5494 printk(KERN_INFO
5495 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5496 err, cmd);
5497 goto abort;
5498 }
5499
5500 switch (cmd)
5501 {
5502 case SET_ARRAY_INFO:
5503 {
5504 mdu_array_info_t info;
5505 if (!arg)
5506 memset(&info, 0, sizeof(info));
5507 else if (copy_from_user(&info, argp, sizeof(info))) {
5508 err = -EFAULT;
5509 goto abort_unlock;
5510 }
5511 if (mddev->pers) {
5512 err = update_array_info(mddev, &info);
5513 if (err) {
5514 printk(KERN_WARNING "md: couldn't update"
5515 " array info. %d\n", err);
5516 goto abort_unlock;
5517 }
5518 goto done_unlock;
5519 }
5520 if (!list_empty(&mddev->disks)) {
5521 printk(KERN_WARNING
5522 "md: array %s already has disks!\n",
5523 mdname(mddev));
5524 err = -EBUSY;
5525 goto abort_unlock;
5526 }
5527 if (mddev->raid_disks) {
5528 printk(KERN_WARNING
5529 "md: array %s already initialised!\n",
5530 mdname(mddev));
5531 err = -EBUSY;
5532 goto abort_unlock;
5533 }
5534 err = set_array_info(mddev, &info);
5535 if (err) {
5536 printk(KERN_WARNING "md: couldn't set"
5537 " array info. %d\n", err);
5538 goto abort_unlock;
5539 }
5540 }
5541 goto done_unlock;
5542
5543 default:;
5544 }
5545
5546 /*
5547 * Commands querying/configuring an existing array:
5548 */
NeilBrown32a76272005-06-21 17:17:14 -07005549 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005550 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005551 if ((!mddev->raid_disks && !mddev->external)
5552 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5553 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5554 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 err = -ENODEV;
5556 goto abort_unlock;
5557 }
5558
5559 /*
5560 * Commands even a read-only array can execute:
5561 */
5562 switch (cmd)
5563 {
5564 case GET_ARRAY_INFO:
5565 err = get_array_info(mddev, argp);
5566 goto done_unlock;
5567
NeilBrown32a76272005-06-21 17:17:14 -07005568 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005569 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005570 goto done_unlock;
5571
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 case GET_DISK_INFO:
5573 err = get_disk_info(mddev, argp);
5574 goto done_unlock;
5575
5576 case RESTART_ARRAY_RW:
5577 err = restart_array(mddev);
5578 goto done_unlock;
5579
5580 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005581 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 goto done_unlock;
5583
5584 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005585 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 goto done_unlock;
5587
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 }
5589
5590 /*
5591 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005592 * superblock, so we do not allow them on read-only arrays.
5593 * However non-MD ioctls (e.g. get-size) will still come through
5594 * here and hit the 'default' below, so only disallow
5595 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005597 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005598 if (mddev->ro == 2) {
5599 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005600 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005601 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5602 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005603 } else {
5604 err = -EROFS;
5605 goto abort_unlock;
5606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 }
5608
5609 switch (cmd)
5610 {
5611 case ADD_NEW_DISK:
5612 {
5613 mdu_disk_info_t info;
5614 if (copy_from_user(&info, argp, sizeof(info)))
5615 err = -EFAULT;
5616 else
5617 err = add_new_disk(mddev, &info);
5618 goto done_unlock;
5619 }
5620
5621 case HOT_REMOVE_DISK:
5622 err = hot_remove_disk(mddev, new_decode_dev(arg));
5623 goto done_unlock;
5624
5625 case HOT_ADD_DISK:
5626 err = hot_add_disk(mddev, new_decode_dev(arg));
5627 goto done_unlock;
5628
5629 case SET_DISK_FAULTY:
5630 err = set_disk_faulty(mddev, new_decode_dev(arg));
5631 goto done_unlock;
5632
5633 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005634 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 goto done_unlock;
5636
NeilBrown32a76272005-06-21 17:17:14 -07005637 case SET_BITMAP_FILE:
5638 err = set_bitmap_file(mddev, (int)arg);
5639 goto done_unlock;
5640
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 err = -EINVAL;
5643 goto abort_unlock;
5644 }
5645
5646done_unlock:
5647abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005648 if (mddev->hold_active == UNTIL_IOCTL &&
5649 err != -EINVAL)
5650 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 mddev_unlock(mddev);
5652
5653 return err;
5654done:
5655 if (err)
5656 MD_BUG();
5657abort:
5658 return err;
5659}
5660
Al Viroa39907f2008-03-02 10:31:15 -05005661static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662{
5663 /*
5664 * Succeed if we can lock the mddev, which confirms that
5665 * it isn't being stopped right now.
5666 */
NeilBrownd3374822009-01-09 08:31:10 +11005667 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 int err;
5669
NeilBrownd3374822009-01-09 08:31:10 +11005670 if (mddev->gendisk != bdev->bd_disk) {
5671 /* we are racing with mddev_put which is discarding this
5672 * bd_disk.
5673 */
5674 mddev_put(mddev);
5675 /* Wait until bdev->bd_disk is definitely gone */
5676 flush_scheduled_work();
5677 /* Then retry the open from the top */
5678 return -ERESTARTSYS;
5679 }
5680 BUG_ON(mddev != bdev->bd_disk->private_data);
5681
NeilBrownc8c00a62009-08-10 12:50:52 +10005682 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 goto out;
5684
5685 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005686 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005687 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688
Al Viroa39907f2008-03-02 10:31:15 -05005689 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 out:
5691 return err;
5692}
5693
Al Viroa39907f2008-03-02 10:31:15 -05005694static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695{
Al Viroa39907f2008-03-02 10:31:15 -05005696 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005698 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005699 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 mddev_put(mddev);
5701
5702 return 0;
5703}
5704
Linus Torvalds44ce6292007-05-09 18:51:36 -07005705static int md_media_changed(struct gendisk *disk)
5706{
5707 mddev_t *mddev = disk->private_data;
5708
5709 return mddev->changed;
5710}
5711
5712static int md_revalidate(struct gendisk *disk)
5713{
5714 mddev_t *mddev = disk->private_data;
5715
5716 mddev->changed = 0;
5717 return 0;
5718}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005719static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720{
5721 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005722 .open = md_open,
5723 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005724 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005725 .getgeo = md_getgeo,
Linus Torvalds44ce6292007-05-09 18:51:36 -07005726 .media_changed = md_media_changed,
5727 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728};
5729
Adrian Bunk75c96f82005-05-05 16:16:09 -07005730static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731{
5732 mdk_thread_t *thread = arg;
5733
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 /*
5735 * md_thread is a 'system-thread', it's priority should be very
5736 * high. We avoid resource deadlocks individually in each
5737 * raid personality. (RAID5 does preallocation) We also use RR and
5738 * the very same RT priority as kswapd, thus we will never get
5739 * into a priority inversion deadlock.
5740 *
5741 * we definitely have to have equal or higher priority than
5742 * bdflush, otherwise bdflush will deadlock if there are too
5743 * many dirty RAID5 blocks.
5744 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745
NeilBrown6985c432005-10-19 21:23:47 -07005746 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005747 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748
NeilBrown93588e22005-11-15 00:09:12 -08005749 /* We need to wait INTERRUPTIBLE so that
5750 * we don't add to the load-average.
5751 * That means we need to be sure no signals are
5752 * pending
5753 */
5754 if (signal_pending(current))
5755 flush_signals(current);
5756
5757 wait_event_interruptible_timeout
5758 (thread->wqueue,
5759 test_bit(THREAD_WAKEUP, &thread->flags)
5760 || kthread_should_stop(),
5761 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762
5763 clear_bit(THREAD_WAKEUP, &thread->flags);
5764
NeilBrown787453c2005-11-08 21:39:43 -08005765 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005767
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 return 0;
5769}
5770
5771void md_wakeup_thread(mdk_thread_t *thread)
5772{
5773 if (thread) {
5774 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5775 set_bit(THREAD_WAKEUP, &thread->flags);
5776 wake_up(&thread->wqueue);
5777 }
5778}
5779
5780mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5781 const char *name)
5782{
5783 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784
NeilBrown9ffae0c2006-01-06 00:20:32 -08005785 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 if (!thread)
5787 return NULL;
5788
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 init_waitqueue_head(&thread->wqueue);
5790
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 thread->run = run;
5792 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005793 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10005794 thread->tsk = kthread_run(md_thread, thread,
5795 "%s_%s",
5796 mdname(thread->mddev),
5797 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07005798 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 kfree(thread);
5800 return NULL;
5801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 return thread;
5803}
5804
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805void md_unregister_thread(mdk_thread_t *thread)
5806{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005807 if (!thread)
5808 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005809 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005810
5811 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 kfree(thread);
5813}
5814
5815void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5816{
5817 if (!mddev) {
5818 MD_BUG();
5819 return;
5820 }
5821
NeilBrownb2d444d2005-11-08 21:39:31 -08005822 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005824
5825 if (mddev->external)
5826 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005827/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5829 mdname(mddev),
5830 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5831 __builtin_return_address(0),__builtin_return_address(1),
5832 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005833*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005834 if (!mddev->pers)
5835 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 if (!mddev->pers->error_handler)
5837 return;
5838 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005839 if (mddev->degraded)
5840 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005841 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5843 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5844 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005845 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846}
5847
5848/* seq_file implementation /proc/mdstat */
5849
5850static void status_unused(struct seq_file *seq)
5851{
5852 int i = 0;
5853 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854
5855 seq_printf(seq, "unused devices: ");
5856
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005857 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 char b[BDEVNAME_SIZE];
5859 i++;
5860 seq_printf(seq, "%s ",
5861 bdevname(rdev->bdev,b));
5862 }
5863 if (!i)
5864 seq_printf(seq, "<none>");
5865
5866 seq_printf(seq, "\n");
5867}
5868
5869
5870static void status_resync(struct seq_file *seq, mddev_t * mddev)
5871{
NeilBrowndd71cf62009-05-07 12:49:35 +10005872 sector_t max_sectors, resync, res;
5873 unsigned long dt, db;
5874 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08005875 int scale;
5876 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877
NeilBrowndd71cf62009-05-07 12:49:35 +10005878 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879
5880 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10005881 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 else
NeilBrowndd71cf62009-05-07 12:49:35 +10005883 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884
5885 /*
5886 * Should not happen.
5887 */
NeilBrowndd71cf62009-05-07 12:49:35 +10005888 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 MD_BUG();
5890 return;
5891 }
NeilBrown4588b422006-03-27 01:18:04 -08005892 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10005893 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08005894 * u32, as those are the requirements for sector_div.
5895 * Thus 'scale' must be at least 10
5896 */
5897 scale = 10;
5898 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10005899 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08005900 scale++;
5901 }
5902 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10005903 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08005904
5905 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 {
NeilBrown4588b422006-03-27 01:18:04 -08005907 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 seq_printf(seq, "[");
5909 for (i = 0; i < x; i++)
5910 seq_printf(seq, "=");
5911 seq_printf(seq, ">");
5912 for (i = 0; i < y; i++)
5913 seq_printf(seq, ".");
5914 seq_printf(seq, "] ");
5915 }
NeilBrown4588b422006-03-27 01:18:04 -08005916 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005917 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5918 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005919 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5920 "check" :
5921 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5922 "resync" : "recovery"))),
5923 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10005924 (unsigned long long) resync/2,
5925 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926
5927 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 * dt: time from mark until now
5929 * db: blocks written from mark until now
5930 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10005931 *
5932 * rt is a sector_t, so could be 32bit or 64bit.
5933 * So we divide before multiply in case it is 32bit and close
5934 * to the limit.
5935 * We scale the divisor (db) by 32 to avoid loosing precision
5936 * near the end of resync when the number of remaining sectors
5937 * is close to 'db'.
5938 * We then divide rt by 32 after multiplying by db to compensate.
5939 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940 */
5941 dt = ((jiffies - mddev->resync_mark) / HZ);
5942 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005943 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5944 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945
NeilBrowndd71cf62009-05-07 12:49:35 +10005946 rt = max_sectors - resync; /* number of remaining sectors */
5947 sector_div(rt, db/32+1);
5948 rt *= dt;
5949 rt >>= 5;
5950
5951 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
5952 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953
NeilBrownff4e8d92006-07-10 04:44:16 -07005954 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955}
5956
5957static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5958{
5959 struct list_head *tmp;
5960 loff_t l = *pos;
5961 mddev_t *mddev;
5962
5963 if (l >= 0x10000)
5964 return NULL;
5965 if (!l--)
5966 /* header */
5967 return (void*)1;
5968
5969 spin_lock(&all_mddevs_lock);
5970 list_for_each(tmp,&all_mddevs)
5971 if (!l--) {
5972 mddev = list_entry(tmp, mddev_t, all_mddevs);
5973 mddev_get(mddev);
5974 spin_unlock(&all_mddevs_lock);
5975 return mddev;
5976 }
5977 spin_unlock(&all_mddevs_lock);
5978 if (!l--)
5979 return (void*)2;/* tail */
5980 return NULL;
5981}
5982
5983static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5984{
5985 struct list_head *tmp;
5986 mddev_t *next_mddev, *mddev = v;
5987
5988 ++*pos;
5989 if (v == (void*)2)
5990 return NULL;
5991
5992 spin_lock(&all_mddevs_lock);
5993 if (v == (void*)1)
5994 tmp = all_mddevs.next;
5995 else
5996 tmp = mddev->all_mddevs.next;
5997 if (tmp != &all_mddevs)
5998 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5999 else {
6000 next_mddev = (void*)2;
6001 *pos = 0x10000;
6002 }
6003 spin_unlock(&all_mddevs_lock);
6004
6005 if (v != (void*)1)
6006 mddev_put(mddev);
6007 return next_mddev;
6008
6009}
6010
6011static void md_seq_stop(struct seq_file *seq, void *v)
6012{
6013 mddev_t *mddev = v;
6014
6015 if (mddev && v != (void*)1 && v != (void*)2)
6016 mddev_put(mddev);
6017}
6018
NeilBrownd7603b72006-01-06 00:20:30 -08006019struct mdstat_info {
6020 int event;
6021};
6022
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023static int md_seq_show(struct seq_file *seq, void *v)
6024{
6025 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006026 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006028 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006029 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030
6031 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006032 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 seq_printf(seq, "Personalities : ");
6034 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006035 list_for_each_entry(pers, &pers_list, list)
6036 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037
6038 spin_unlock(&pers_lock);
6039 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006040 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041 return 0;
6042 }
6043 if (v == (void*)2) {
6044 status_unused(seq);
6045 return 0;
6046 }
6047
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006048 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006050
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6052 seq_printf(seq, "%s : %sactive", mdname(mddev),
6053 mddev->pers ? "" : "in");
6054 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006055 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006057 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006058 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 seq_printf(seq, " %s", mddev->pers->name);
6060 }
6061
Andre Nolldd8ac332009-03-31 14:33:13 +11006062 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006063 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 char b[BDEVNAME_SIZE];
6065 seq_printf(seq, " %s[%d]",
6066 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006067 if (test_bit(WriteMostly, &rdev->flags))
6068 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006069 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 seq_printf(seq, "(F)");
6071 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006072 } else if (rdev->raid_disk < 0)
6073 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006074 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 }
6076
6077 if (!list_empty(&mddev->disks)) {
6078 if (mddev->pers)
6079 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006080 (unsigned long long)
6081 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 else
6083 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006084 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006086 if (mddev->persistent) {
6087 if (mddev->major_version != 0 ||
6088 mddev->minor_version != 90) {
6089 seq_printf(seq," super %d.%d",
6090 mddev->major_version,
6091 mddev->minor_version);
6092 }
NeilBrowne6910632008-02-06 01:39:51 -08006093 } else if (mddev->external)
6094 seq_printf(seq, " super external:%s",
6095 mddev->metadata_type);
6096 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006097 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098
6099 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006100 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006102 if (mddev->pers->sync_request) {
6103 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006104 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006105 seq_printf(seq, "\n ");
6106 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6107 seq_printf(seq, "\tresync=DELAYED\n ");
6108 else if (mddev->recovery_cp < MaxSector)
6109 seq_printf(seq, "\tresync=PENDING\n ");
6110 }
NeilBrown32a76272005-06-21 17:17:14 -07006111 } else
6112 seq_printf(seq, "\n ");
6113
6114 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006115 unsigned long chunk_kb;
6116 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006117 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006118 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006119 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6120 "%lu%s chunk",
6121 bitmap->pages - bitmap->missing_pages,
6122 bitmap->pages,
6123 (bitmap->pages - bitmap->missing_pages)
6124 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006125 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006126 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006127 if (bitmap->file) {
6128 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006129 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006130 }
NeilBrown78d742d2005-06-21 17:17:15 -07006131
NeilBrown32a76272005-06-21 17:17:14 -07006132 seq_printf(seq, "\n");
6133 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134 }
6135
6136 seq_printf(seq, "\n");
6137 }
6138 mddev_unlock(mddev);
6139
6140 return 0;
6141}
6142
Jan Engelhardt110518b2009-05-07 12:49:37 +10006143static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 .start = md_seq_start,
6145 .next = md_seq_next,
6146 .stop = md_seq_stop,
6147 .show = md_seq_show,
6148};
6149
6150static int md_seq_open(struct inode *inode, struct file *file)
6151{
6152 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006153 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6154 if (mi == NULL)
6155 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156
6157 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006158 if (error)
6159 kfree(mi);
6160 else {
6161 struct seq_file *p = file->private_data;
6162 p->private = mi;
6163 mi->event = atomic_read(&md_event_count);
6164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 return error;
6166}
6167
NeilBrownd7603b72006-01-06 00:20:30 -08006168static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6169{
6170 struct seq_file *m = filp->private_data;
6171 struct mdstat_info *mi = m->private;
6172 int mask;
6173
6174 poll_wait(filp, &md_event_waiters, wait);
6175
6176 /* always allow read */
6177 mask = POLLIN | POLLRDNORM;
6178
6179 if (mi->event != atomic_read(&md_event_count))
6180 mask |= POLLERR | POLLPRI;
6181 return mask;
6182}
6183
Arjan van de Venfa027c22007-02-12 00:55:33 -08006184static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006185 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 .open = md_seq_open,
6187 .read = seq_read,
6188 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006189 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006190 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191};
6192
NeilBrown2604b702006-01-06 00:20:36 -08006193int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006196 list_add_tail(&p->list, &pers_list);
6197 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198 spin_unlock(&pers_lock);
6199 return 0;
6200}
6201
NeilBrown2604b702006-01-06 00:20:36 -08006202int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203{
NeilBrown2604b702006-01-06 00:20:36 -08006204 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006206 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207 spin_unlock(&pers_lock);
6208 return 0;
6209}
6210
NeilBrowneea1bf32009-03-31 14:27:02 +11006211static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212{
6213 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006215 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216
6217 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006218 rcu_read_lock();
6219 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006221 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6222 (int)part_stat_read(&disk->part0, sectors[1]) -
6223 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006224 /* sync IO will cause sync_io to increase before the disk_stats
6225 * as sync_io is counted when a request starts, and
6226 * disk_stats is counted when it completes.
6227 * So resync activity will cause curr_events to be smaller than
6228 * when there was no such activity.
6229 * non-sync IO will cause disk_stat to increase without
6230 * increasing sync_io so curr_events will (eventually)
6231 * be larger than it was before. Once it becomes
6232 * substantially larger, the test below will cause
6233 * the array to appear non-idle, and resync will slow
6234 * down.
6235 * If there is a lot of outstanding resync activity when
6236 * we set last_event to curr_events, then all that activity
6237 * completing might cause the array to appear non-idle
6238 * and resync will be slowed down even though there might
6239 * not have been non-resync activity. This will only
6240 * happen once though. 'last_events' will soon reflect
6241 * the state where there is little or no outstanding
6242 * resync requests, and further resync activity will
6243 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006244 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006246 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247 rdev->last_events = curr_events;
6248 idle = 0;
6249 }
6250 }
NeilBrown4b809912008-07-21 17:05:25 +10006251 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 return idle;
6253}
6254
6255void md_done_sync(mddev_t *mddev, int blocks, int ok)
6256{
6257 /* another "blocks" (512byte) blocks have been synced */
6258 atomic_sub(blocks, &mddev->recovery_active);
6259 wake_up(&mddev->recovery_wait);
6260 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006261 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 md_wakeup_thread(mddev->thread);
6263 // stop recovery, signal do_sync ....
6264 }
6265}
6266
6267
NeilBrown06d91a52005-06-21 17:17:12 -07006268/* md_write_start(mddev, bi)
6269 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006270 * in superblock) before writing, schedule a superblock update
6271 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006272 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006273void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274{
Neil Brown0fd62b82008-06-28 08:31:36 +10006275 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006276 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006277 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006278
NeilBrownf91de922005-11-08 21:39:36 -08006279 BUG_ON(mddev->ro == 1);
6280 if (mddev->ro == 2) {
6281 /* need to switch to read/write */
6282 mddev->ro = 0;
6283 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6284 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006285 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006286 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006287 }
NeilBrown06d91a52005-06-21 17:17:12 -07006288 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006289 if (mddev->safemode == 1)
6290 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006291 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006292 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006293 if (mddev->in_sync) {
6294 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006295 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006296 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006297 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006298 }
NeilBrowna9701a32005-11-08 21:39:34 -08006299 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006300 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006301 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006302 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006303 wait_event(mddev->sb_wait,
6304 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6305 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306}
6307
6308void md_write_end(mddev_t *mddev)
6309{
6310 if (atomic_dec_and_test(&mddev->writes_pending)) {
6311 if (mddev->safemode == 2)
6312 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006313 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6315 }
6316}
6317
NeilBrown2a2275d2007-01-26 00:57:11 -08006318/* md_allow_write(mddev)
6319 * Calling this ensures that the array is marked 'active' so that writes
6320 * may proceed without blocking. It is important to call this before
6321 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6322 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006323 *
6324 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6325 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006326 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006327int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006328{
6329 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006330 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006331 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006332 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006333 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006334 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006335
6336 spin_lock_irq(&mddev->write_lock);
6337 if (mddev->in_sync) {
6338 mddev->in_sync = 0;
6339 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6340 if (mddev->safemode_delay &&
6341 mddev->safemode == 0)
6342 mddev->safemode = 1;
6343 spin_unlock_irq(&mddev->write_lock);
6344 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006345 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006346 } else
6347 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006348
6349 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6350 return -EAGAIN;
6351 else
6352 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006353}
6354EXPORT_SYMBOL_GPL(md_allow_write);
6355
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356#define SYNC_MARKS 10
6357#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006358void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359{
6360 mddev_t *mddev2;
6361 unsigned int currspeed = 0,
6362 window;
NeilBrown57afd892005-06-21 17:17:13 -07006363 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364 unsigned long mark[SYNC_MARKS];
6365 sector_t mark_cnt[SYNC_MARKS];
6366 int last_mark,m;
6367 struct list_head *tmp;
6368 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006369 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006370 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006371 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372
6373 /* just incase thread restarts... */
6374 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6375 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006376 if (mddev->ro) /* never try to sync a read-only array */
6377 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378
NeilBrown61df9d92006-10-03 01:15:57 -07006379 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6380 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6381 desc = "data-check";
6382 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6383 desc = "requested-resync";
6384 else
6385 desc = "resync";
6386 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6387 desc = "reshape";
6388 else
6389 desc = "recovery";
6390
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391 /* we overload curr_resync somewhat here.
6392 * 0 == not engaged in resync at all
6393 * 2 == checking that there is no conflict with another sync
6394 * 1 == like 2, but have yielded to allow conflicting resync to
6395 * commense
6396 * other == active in resync - this many blocks
6397 *
6398 * Before starting a resync we must have set curr_resync to
6399 * 2, and then checked that every "conflicting" array has curr_resync
6400 * less than ours. When we find one that is the same or higher
6401 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6402 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6403 * This will mean we have to start checking from the beginning again.
6404 *
6405 */
6406
6407 do {
6408 mddev->curr_resync = 2;
6409
6410 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08006411 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07006412 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413 goto skip;
6414 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08006415 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416 if (mddev2 == mddev)
6417 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006418 if (!mddev->parallel_resync
6419 && mddev2->curr_resync
6420 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 DEFINE_WAIT(wq);
6422 if (mddev < mddev2 && mddev->curr_resync == 2) {
6423 /* arbitrarily yield */
6424 mddev->curr_resync = 1;
6425 wake_up(&resync_wait);
6426 }
6427 if (mddev > mddev2 && mddev->curr_resync == 1)
6428 /* no need to wait here, we can wait the next
6429 * time 'round when curr_resync == 2
6430 */
6431 continue;
NeilBrown97441972008-09-19 11:49:54 +10006432 /* We need to wait 'interruptible' so as not to
6433 * contribute to the load average, and not to
6434 * be caught by 'softlockup'
6435 */
6436 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006437 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006438 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006439 printk(KERN_INFO "md: delaying %s of %s"
6440 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006442 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006444 if (signal_pending(current))
6445 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446 schedule();
6447 finish_wait(&resync_wait, &wq);
6448 goto try_again;
6449 }
6450 finish_wait(&resync_wait, &wq);
6451 }
6452 }
6453 } while (mddev->curr_resync < 2);
6454
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006455 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006456 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006458 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459 */
6460 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006461 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006462 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006463 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6464 j = mddev->resync_min;
6465 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006466 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006467
NeilBrownccfcc3c2006-03-27 01:18:09 -08006468 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006469 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006470 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006472 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006473 j = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006474 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006475 if (rdev->raid_disk >= 0 &&
6476 !test_bit(Faulty, &rdev->flags) &&
6477 !test_bit(In_sync, &rdev->flags) &&
6478 rdev->recovery_offset < j)
6479 j = rdev->recovery_offset;
6480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481
NeilBrown61df9d92006-10-03 01:15:57 -07006482 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6483 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6484 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006485 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006486 "(but not more than %d KB/sec) for %s.\n",
6487 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488
NeilBrowneea1bf32009-03-31 14:27:02 +11006489 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006490
NeilBrown57afd892005-06-21 17:17:13 -07006491 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492 for (m = 0; m < SYNC_MARKS; m++) {
6493 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006494 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495 }
6496 last_mark = 0;
6497 mddev->resync_mark = mark[last_mark];
6498 mddev->resync_mark_cnt = mark_cnt[last_mark];
6499
6500 /*
6501 * Tune reconstruction:
6502 */
6503 window = 32*(PAGE_SIZE/512);
6504 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6505 window/2,(unsigned long long) max_sectors/2);
6506
6507 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508 last_check = 0;
6509
6510 if (j>2) {
6511 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006512 "md: resuming %s of %s from checkpoint.\n",
6513 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514 mddev->curr_resync = j;
6515 }
NeilBrownefa59332009-12-14 12:49:49 +11006516 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517
6518 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006519 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520
NeilBrown57afd892005-06-21 17:17:13 -07006521 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006522
NeilBrown7a91ee12009-05-26 12:57:21 +10006523 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6524 ((mddev->curr_resync > mddev->curr_resync_completed &&
6525 (mddev->curr_resync - mddev->curr_resync_completed)
6526 > (max_sectors >> 4)) ||
6527 (j - mddev->curr_resync_completed)*2
6528 >= mddev->resync_max - mddev->curr_resync_completed
6529 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006530 /* time to update curr_resync_completed */
6531 blk_unplug(mddev->queue);
6532 wait_event(mddev->recovery_wait,
6533 atomic_read(&mddev->recovery_active) == 0);
6534 mddev->curr_resync_completed =
6535 mddev->curr_resync;
6536 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006537 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006538 }
NeilBrownacb180b2009-04-14 16:28:34 +10006539
NeilBrowne62e58a2009-07-01 13:15:35 +10006540 while (j >= mddev->resync_max && !kthread_should_stop()) {
6541 /* As this condition is controlled by user-space,
6542 * we can block indefinitely, so use '_interruptible'
6543 * to avoid triggering warnings.
6544 */
6545 flush_signals(current); /* just in case */
6546 wait_event_interruptible(mddev->recovery_wait,
6547 mddev->resync_max > j
6548 || kthread_should_stop());
6549 }
NeilBrownacb180b2009-04-14 16:28:34 +10006550
6551 if (kthread_should_stop())
6552 goto interrupted;
6553
NeilBrown57afd892005-06-21 17:17:13 -07006554 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006555 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006556 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006557 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558 goto out;
6559 }
NeilBrown57afd892005-06-21 17:17:13 -07006560
6561 if (!skipped) { /* actual IO requested */
6562 io_sectors += sectors;
6563 atomic_add(sectors, &mddev->recovery_active);
6564 }
6565
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566 j += sectors;
6567 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006568 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006569 if (last_check == 0)
6570 /* this is the earliers that rebuilt will be
6571 * visible in /proc/mdstat
6572 */
6573 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006574
6575 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576 continue;
6577
NeilBrown57afd892005-06-21 17:17:13 -07006578 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579
NeilBrowndfc70642008-05-23 13:04:39 -07006580 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581 break;
6582
6583 repeat:
6584 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6585 /* step marks */
6586 int next = (last_mark+1) % SYNC_MARKS;
6587
6588 mddev->resync_mark = mark[next];
6589 mddev->resync_mark_cnt = mark_cnt[next];
6590 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006591 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592 last_mark = next;
6593 }
6594
6595
NeilBrownc6207272008-02-06 01:39:52 -08006596 if (kthread_should_stop())
6597 goto interrupted;
6598
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599
6600 /*
6601 * this loop exits only if either when we are slower than
6602 * the 'hard' speed limit, or the system was IO-idle for
6603 * a jiffy.
6604 * the system might be non-idle CPU-wise, but we only care
6605 * about not overloading the IO subsystem. (things like an
6606 * e2fsck being done on the RAID array should execute fast)
6607 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006608 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609 cond_resched();
6610
NeilBrown57afd892005-06-21 17:17:13 -07006611 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6612 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613
NeilBrown88202a02006-01-06 00:21:36 -08006614 if (currspeed > speed_min(mddev)) {
6615 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006616 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006617 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618 goto repeat;
6619 }
6620 }
6621 }
NeilBrown61df9d92006-10-03 01:15:57 -07006622 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623 /*
6624 * this also signals 'finished resyncing' to md_stop
6625 */
6626 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006627 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628
6629 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6630
6631 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006632 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633
NeilBrowndfc70642008-05-23 13:04:39 -07006634 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006635 mddev->curr_resync > 2) {
6636 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6637 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6638 if (mddev->curr_resync >= mddev->recovery_cp) {
6639 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006640 "md: checkpointing %s of %s.\n",
6641 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006642 mddev->recovery_cp = mddev->curr_resync;
6643 }
6644 } else
6645 mddev->recovery_cp = MaxSector;
6646 } else {
6647 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6648 mddev->curr_resync = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006649 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006650 if (rdev->raid_disk >= 0 &&
6651 !test_bit(Faulty, &rdev->flags) &&
6652 !test_bit(In_sync, &rdev->flags) &&
6653 rdev->recovery_offset < mddev->curr_resync)
6654 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656 }
NeilBrown17571282006-12-10 02:20:52 -08006657 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006660 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6661 /* We completed so min/max setting can be forgotten if used. */
6662 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6663 mddev->resync_min = 0;
6664 mddev->resync_max = MaxSector;
6665 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6666 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006668 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6669 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006670 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671 wake_up(&resync_wait);
6672 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6673 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006674 return;
6675
6676 interrupted:
6677 /*
6678 * got a signal, exit.
6679 */
6680 printk(KERN_INFO
6681 "md: md_do_sync() got signal ... exiting\n");
6682 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6683 goto out;
6684
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685}
NeilBrown29269552006-03-27 01:18:10 -08006686EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687
6688
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006689static int remove_and_add_spares(mddev_t *mddev)
6690{
6691 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006692 int spares = 0;
6693
NeilBrown97e4f422009-03-31 14:33:13 +11006694 mddev->curr_resync_completed = 0;
6695
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006696 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006697 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006698 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006699 (test_bit(Faulty, &rdev->flags) ||
6700 ! test_bit(In_sync, &rdev->flags)) &&
6701 atomic_read(&rdev->nr_pending)==0) {
6702 if (mddev->pers->hot_remove_disk(
6703 mddev, rdev->raid_disk)==0) {
6704 char nm[20];
6705 sprintf(nm,"rd%d", rdev->raid_disk);
6706 sysfs_remove_link(&mddev->kobj, nm);
6707 rdev->raid_disk = -1;
6708 }
6709 }
6710
NeilBrown4044ba52009-01-09 08:31:11 +11006711 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006712 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006713 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006714 !test_bit(In_sync, &rdev->flags) &&
6715 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006716 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006717 if (rdev->raid_disk < 0
6718 && !test_bit(Faulty, &rdev->flags)) {
6719 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006720 if (mddev->pers->
6721 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006722 char nm[20];
6723 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006724 if (sysfs_create_link(&mddev->kobj,
6725 &rdev->kobj, nm))
6726 printk(KERN_WARNING
6727 "md: cannot register "
6728 "%s for %s\n",
6729 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006730 spares++;
6731 md_new_event(mddev);
6732 } else
6733 break;
6734 }
NeilBrowndfc70642008-05-23 13:04:39 -07006735 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006736 }
6737 return spares;
6738}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739/*
6740 * This routine is regularly called by all per-raid-array threads to
6741 * deal with generic issues like resync and super-block update.
6742 * Raid personalities that don't have a thread (linear/raid0) do not
6743 * need this as they never do any recovery or update the superblock.
6744 *
6745 * It does not do any resync itself, but rather "forks" off other threads
6746 * to do that as needed.
6747 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6748 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006749 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750 * and wakeups up this thread which will reap the thread and finish up.
6751 * This thread also removes any faulty devices (with nr_pending == 0).
6752 *
6753 * The overall approach is:
6754 * 1/ if the superblock needs updating, update it.
6755 * 2/ If a recovery thread is running, don't do anything else.
6756 * 3/ If recovery has finished, clean up, possibly marking spares active.
6757 * 4/ If there are any faulty devices, remove them.
6758 * 5/ If array is degraded, try to add spares devices
6759 * 6/ If array has spares or is not in-sync, start a resync thread.
6760 */
6761void md_check_recovery(mddev_t *mddev)
6762{
6763 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764
6765
NeilBrown5f404022005-06-21 17:17:16 -07006766 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11006767 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768
6769 if (mddev->ro)
6770 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006771
6772 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006773 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006774 printk(KERN_INFO "md: %s in immediate safe mode\n",
6775 mdname(mddev));
6776 mddev->safemode = 2;
6777 }
6778 flush_signals(current);
6779 }
6780
NeilBrownc89a8ee2008-08-05 15:54:13 +10006781 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6782 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006784 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006786 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006787 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006788 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6789 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790 ))
6791 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006792
NeilBrowndf5b89b2006-03-27 01:18:20 -08006793 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006794 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006795
NeilBrownc89a8ee2008-08-05 15:54:13 +10006796 if (mddev->ro) {
6797 /* Only thing we do on a ro array is remove
6798 * failed devices.
6799 */
6800 remove_and_add_spares(mddev);
6801 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6802 goto unlock;
6803 }
6804
NeilBrown31a59e32008-04-30 00:52:30 -07006805 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006806 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006807 spin_lock_irq(&mddev->write_lock);
6808 if (mddev->safemode &&
6809 !atomic_read(&mddev->writes_pending) &&
6810 !mddev->in_sync &&
6811 mddev->recovery_cp == MaxSector) {
6812 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006813 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006814 if (mddev->persistent)
6815 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6816 }
6817 if (mddev->safemode == 1)
6818 mddev->safemode = 0;
6819 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006820 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006821 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006822 }
NeilBrownfca4d842005-06-21 17:17:11 -07006823
NeilBrown850b2b42006-10-03 01:15:46 -07006824 if (mddev->flags)
6825 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006826
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006827 list_for_each_entry(rdev, &mddev->disks, same_set)
Neil Brown52664732008-06-28 08:31:44 +10006828 if (test_and_clear_bit(StateChanged, &rdev->flags))
NeilBrown3c0ee632008-10-21 13:25:28 +11006829 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown52664732008-06-28 08:31:44 +10006830
NeilBrown06d91a52005-06-21 17:17:12 -07006831
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6833 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6834 /* resync/recovery still happening */
6835 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6836 goto unlock;
6837 }
6838 if (mddev->sync_thread) {
6839 /* resync has finished, collect result */
6840 md_unregister_thread(mddev->sync_thread);
6841 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006842 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6843 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844 /* success...*/
6845 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006846 if (mddev->pers->spare_active(mddev))
6847 sysfs_notify(&mddev->kobj, NULL,
6848 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849 }
NeilBrowncea9c222009-03-31 15:15:05 +11006850 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6851 mddev->pers->finish_reshape)
6852 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07006853 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006854
6855 /* if array is no-longer degraded, then any saved_raid_disk
6856 * information must be scrapped
6857 */
6858 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006859 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07006860 rdev->saved_raid_disk = -1;
6861
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862 mddev->recovery = 0;
6863 /* flag recovery needed just to double check */
6864 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11006865 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006866 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867 goto unlock;
6868 }
Neil Brown72a23c22008-06-28 08:31:41 +10006869 /* Set RUNNING before clearing NEEDED to avoid
6870 * any transients in the value of "sync_action".
6871 */
6872 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6873 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006874 /* Clear some bits that don't mean anything, but
6875 * might be left set
6876 */
NeilBrown24dd4692005-11-08 21:39:26 -08006877 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6878 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006880 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6881 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006882 /* no recovery is running.
6883 * remove any failed drives, then
6884 * add spares if possible.
6885 * Spare are also removed and re-added, to allow
6886 * the personality to fail the re-add.
6887 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006889 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10006890 if (mddev->pers->check_reshape == NULL ||
6891 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006892 /* Cannot proceed */
6893 goto unlock;
6894 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006895 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006896 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006897 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6898 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07006899 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006900 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006901 } else if (mddev->recovery_cp < MaxSector) {
6902 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006903 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006904 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6905 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006907
Linus Torvalds1da177e2005-04-16 15:20:36 -07006908 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006909 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6910 /* We are adding a device or devices to an array
6911 * which has the bitmap stored on all devices.
6912 * So make sure all bitmap pages get written
6913 */
6914 bitmap_write_all(mddev->bitmap);
6915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916 mddev->sync_thread = md_register_thread(md_do_sync,
6917 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006918 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919 if (!mddev->sync_thread) {
6920 printk(KERN_ERR "%s: could not start resync"
6921 " thread...\n",
6922 mdname(mddev));
6923 /* leave the spares where they are, it shouldn't hurt */
6924 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006925 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11006927 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006928 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929 }
6930 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006931 if (!mddev->sync_thread) {
6932 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6933 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6934 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11006935 if (mddev->sysfs_action)
6936 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10006937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938 mddev_unlock(mddev);
6939 }
6940}
6941
Dan Williams6bfe0b42008-04-30 00:52:32 -07006942void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6943{
NeilBrown3c0ee632008-10-21 13:25:28 +11006944 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07006945 wait_event_timeout(rdev->blocked_wait,
6946 !test_bit(Blocked, &rdev->flags),
6947 msecs_to_jiffies(5000));
6948 rdev_dec_pending(rdev, mddev);
6949}
6950EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6951
Adrian Bunk75c96f82005-05-05 16:16:09 -07006952static int md_notify_reboot(struct notifier_block *this,
6953 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006954{
6955 struct list_head *tmp;
6956 mddev_t *mddev;
6957
6958 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6959
6960 printk(KERN_INFO "md: stopping all md devices.\n");
6961
NeilBrown29ac4aa2008-02-06 01:39:58 -08006962 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006963 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10006964 /* Force a switch to readonly even array
6965 * appears to still be in use. Hence
6966 * the '100'.
6967 */
NeilBrownd710e132008-10-13 11:55:12 +11006968 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10006969 mddev_unlock(mddev);
6970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971 /*
6972 * certain more exotic SCSI devices are known to be
6973 * volatile wrt too early system reboots. While the
6974 * right place to handle this issue is the given
6975 * driver, we do want to have a safe RAID driver ...
6976 */
6977 mdelay(1000*1);
6978 }
6979 return NOTIFY_DONE;
6980}
6981
Adrian Bunk75c96f82005-05-05 16:16:09 -07006982static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983 .notifier_call = md_notify_reboot,
6984 .next = NULL,
6985 .priority = INT_MAX, /* before any real devices */
6986};
6987
6988static void md_geninit(void)
6989{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6991
Denis V. Lunevc7705f32008-04-29 01:02:35 -07006992 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993}
6994
Adrian Bunk75c96f82005-05-05 16:16:09 -07006995static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006997 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998 return -1;
6999 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007000 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001 return -1;
7002 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007003 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007004 md_probe, NULL, NULL);
7005 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006 md_probe, NULL, NULL);
7007
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007009 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010
7011 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007012 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013}
7014
7015
7016#ifndef MODULE
7017
7018/*
7019 * Searches all registered partitions for autorun RAID arrays
7020 * at boot time.
7021 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007022
7023static LIST_HEAD(all_detected_devices);
7024struct detected_devices_node {
7025 struct list_head list;
7026 dev_t dev;
7027};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007028
7029void md_autodetect_dev(dev_t dev)
7030{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007031 struct detected_devices_node *node_detected_dev;
7032
7033 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7034 if (node_detected_dev) {
7035 node_detected_dev->dev = dev;
7036 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7037 } else {
7038 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7039 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041}
7042
7043
7044static void autostart_arrays(int part)
7045{
7046 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007047 struct detected_devices_node *node_detected_dev;
7048 dev_t dev;
7049 int i_scanned, i_passed;
7050
7051 i_scanned = 0;
7052 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007053
7054 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7055
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007056 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7057 i_scanned++;
7058 node_detected_dev = list_entry(all_detected_devices.next,
7059 struct detected_devices_node, list);
7060 list_del(&node_detected_dev->list);
7061 dev = node_detected_dev->dev;
7062 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007063 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064 if (IS_ERR(rdev))
7065 continue;
7066
NeilBrownb2d444d2005-11-08 21:39:31 -08007067 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068 MD_BUG();
7069 continue;
7070 }
NeilBrownd0fae182008-03-04 14:29:31 -08007071 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007072 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007073 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007075
7076 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7077 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007078
7079 autorun_devices(part);
7080}
7081
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007082#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083
7084static __exit void md_exit(void)
7085{
7086 mddev_t *mddev;
7087 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007088
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007089 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007090 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007091
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007092 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093 unregister_blkdev(mdp_major, "mdp");
7094 unregister_reboot_notifier(&md_notifier);
7095 unregister_sysctl_table(raid_table_header);
7096 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007097 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007099 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007100 }
7101}
7102
Dan Williams685784a2007-07-09 11:56:42 -07007103subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104module_exit(md_exit)
7105
NeilBrownf91de922005-11-08 21:39:36 -08007106static int get_ro(char *buffer, struct kernel_param *kp)
7107{
7108 return sprintf(buffer, "%d", start_readonly);
7109}
7110static int set_ro(const char *val, struct kernel_param *kp)
7111{
7112 char *e;
7113 int num = simple_strtoul(val, &e, 10);
7114 if (*val && (*e == '\0' || *e == '\n')) {
7115 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007116 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007117 }
7118 return -EINVAL;
7119}
7120
NeilBrown80ca3a42006-07-10 04:44:18 -07007121module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7122module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007123
NeilBrownefeb53c2009-01-09 08:31:10 +11007124module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007125
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126EXPORT_SYMBOL(register_md_personality);
7127EXPORT_SYMBOL(unregister_md_personality);
7128EXPORT_SYMBOL(md_error);
7129EXPORT_SYMBOL(md_done_sync);
7130EXPORT_SYMBOL(md_write_start);
7131EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132EXPORT_SYMBOL(md_register_thread);
7133EXPORT_SYMBOL(md_unregister_thread);
7134EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135EXPORT_SYMBOL(md_check_recovery);
7136MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07007137MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007138MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);