blob: 569f25183db64d4163a461998355622facbdab6b [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);
NeilBrownaa5cbd12009-12-14 12:49:46 +1100466 mutex_init(&new->bitmap_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{
853 if (!mddev->bitmap_file && !mddev->bitmap_offset)
854 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;
NeilBrown92232142005-08-18 11:24:16 -0700981 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700982 mddev->default_bitmap_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) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001016 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -07001017 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001018
NeilBrown41158c72005-06-21 17:17:25 -07001019 } else if (mddev->pers == NULL) {
1020 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 ++ev1;
1022 if (ev1 < mddev->events)
1023 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001024 } else if (mddev->bitmap) {
1025 /* if adding to array with a bitmap, then we can accept an
1026 * older device ... but not too old.
1027 */
NeilBrown41158c72005-06-21 17:17:25 -07001028 if (ev1 < mddev->bitmap->events_cleared)
1029 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001030 } else {
1031 if (ev1 < mddev->events)
1032 /* just a hot-add of a new device, leave raid_disk at -1 */
1033 return 0;
1034 }
NeilBrown41158c72005-06-21 17:17:25 -07001035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 desc = sb->disks + rdev->desc_nr;
1038
1039 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001040 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001041 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1042 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001043 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001045 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1046 /* active but not in sync implies recovery up to
1047 * reshape position. We don't know exactly where
1048 * that is, so set to zero for now */
1049 if (mddev->minor_version >= 91) {
1050 rdev->recovery_offset = 0;
1051 rdev->raid_disk = desc->raid_disk;
1052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001054 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1055 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001056 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001057 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return 0;
1059}
1060
1061/*
1062 * sync_super for 0.90.0
1063 */
1064static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1065{
1066 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 mdk_rdev_t *rdev2;
1068 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 /* make rdev->sb match mddev data..
1072 *
1073 * 1/ zero out disks
1074 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1075 * 3/ any empty disks < next_spare become removed
1076 *
1077 * disks[0] gets initialised to REMOVED because
1078 * we cannot be sure from other fields if it has
1079 * been initialised or not.
1080 */
1081 int i;
1082 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1083
NeilBrown61181562005-09-09 16:24:02 -07001084 rdev->sb_size = MD_SB_BYTES;
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 sb = (mdp_super_t*)page_address(rdev->sb_page);
1087
1088 memset(sb, 0, sizeof(*sb));
1089
1090 sb->md_magic = MD_SB_MAGIC;
1091 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 sb->patch_version = mddev->patch_version;
1093 sb->gvalid_words = 0; /* ignored */
1094 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1095 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1096 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1097 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1098
1099 sb->ctime = mddev->ctime;
1100 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001101 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 sb->raid_disks = mddev->raid_disks;
1103 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001104 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 sb->utime = mddev->utime;
1106 sb->state = 0;
1107 sb->events_hi = (mddev->events>>32);
1108 sb->events_lo = (u32)mddev->events;
1109
NeilBrownf6705572006-03-27 01:18:11 -08001110 if (mddev->reshape_position == MaxSector)
1111 sb->minor_version = 90;
1112 else {
1113 sb->minor_version = 91;
1114 sb->reshape_position = mddev->reshape_position;
1115 sb->new_level = mddev->new_level;
1116 sb->delta_disks = mddev->delta_disks;
1117 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001118 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001119 }
1120 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (mddev->in_sync)
1122 {
1123 sb->recovery_cp = mddev->recovery_cp;
1124 sb->cp_events_hi = (mddev->events>>32);
1125 sb->cp_events_lo = (u32)mddev->events;
1126 if (mddev->recovery_cp == MaxSector)
1127 sb->state = (1<< MD_SB_CLEAN);
1128 } else
1129 sb->recovery_cp = 0;
1130
1131 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001132 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
NeilBrowna654b9d82005-06-21 17:17:27 -07001134 if (mddev->bitmap && mddev->bitmap_file == NULL)
1135 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001138 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001140 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001141 int is_active = test_bit(In_sync, &rdev2->flags);
1142
1143 if (rdev2->raid_disk >= 0 &&
1144 sb->minor_version >= 91)
1145 /* we have nowhere to store the recovery_offset,
1146 * but if it is not below the reshape_position,
1147 * we can piggy-back on that.
1148 */
1149 is_active = 1;
1150 if (rdev2->raid_disk < 0 ||
1151 test_bit(Faulty, &rdev2->flags))
1152 is_active = 0;
1153 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001154 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001156 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001157 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 d = &sb->disks[rdev2->desc_nr];
1159 nr_disks++;
1160 d->number = rdev2->desc_nr;
1161 d->major = MAJOR(rdev2->bdev->bd_dev);
1162 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001163 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 d->raid_disk = rdev2->raid_disk;
1165 else
1166 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001167 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001169 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001171 if (test_bit(In_sync, &rdev2->flags))
1172 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 active++;
1174 working++;
1175 } else {
1176 d->state = 0;
1177 spare++;
1178 working++;
1179 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001180 if (test_bit(WriteMostly, &rdev2->flags))
1181 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 /* now set the "removed" and "faulty" bits on any missing devices */
1184 for (i=0 ; i < mddev->raid_disks ; i++) {
1185 mdp_disk_t *d = &sb->disks[i];
1186 if (d->state == 0 && d->number == 0) {
1187 d->number = i;
1188 d->raid_disk = i;
1189 d->state = (1<<MD_DISK_REMOVED);
1190 d->state |= (1<<MD_DISK_FAULTY);
1191 failed++;
1192 }
1193 }
1194 sb->nr_disks = nr_disks;
1195 sb->active_disks = active;
1196 sb->working_disks = working;
1197 sb->failed_disks = failed;
1198 sb->spare_disks = spare;
1199
1200 sb->this_disk = sb->disks[rdev->desc_nr];
1201 sb->sb_csum = calc_sb_csum(sb);
1202}
1203
1204/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001205 * rdev_size_change for 0.90.0
1206 */
1207static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001208super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001209{
Andre Noll58c0fed2009-03-31 14:33:13 +11001210 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001211 return 0; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001212 if (rdev->mddev->bitmap_offset)
1213 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001214 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001215 if (!num_sectors || num_sectors > rdev->sb_start)
1216 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001217 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001218 rdev->sb_page);
1219 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001220 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001221}
1222
1223
1224/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 * version 1 superblock
1226 */
1227
NeilBrown1c05b4b2006-10-21 10:24:08 -07001228static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001230 __le32 disk_csum;
1231 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 unsigned long long newcsum;
1233 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001234 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 int i;
1236
1237 disk_csum = sb->sb_csum;
1238 sb->sb_csum = 0;
1239 newcsum = 0;
1240 for (i=0; size>=4; size -= 4 )
1241 newcsum += le32_to_cpu(*isuper++);
1242
1243 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001244 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1247 sb->sb_csum = disk_csum;
1248 return cpu_to_le32(csum);
1249}
1250
1251static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1252{
1253 struct mdp_superblock_1 *sb;
1254 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001255 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001257 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 /*
Andre Noll0f420352008-07-11 22:02:23 +10001260 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 * It is always aligned to a 4K boundary and
1262 * depeding on minor_version, it can be:
1263 * 0: At least 8K, but less than 12K, from end of device
1264 * 1: At start of device
1265 * 2: 4K from start of device.
1266 */
1267 switch(minor_version) {
1268 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001269 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1270 sb_start -= 8*2;
1271 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 break;
1273 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001274 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 break;
1276 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001277 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 break;
1279 default:
1280 return -EINVAL;
1281 }
Andre Noll0f420352008-07-11 22:02:23 +10001282 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
NeilBrown0002b272005-09-09 16:23:53 -07001284 /* superblock is rarely larger than 1K, but it can be larger,
1285 * and it is safe to read 4k, so we do that
1286 */
1287 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 if (ret) return ret;
1289
1290
1291 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1292
1293 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1294 sb->major_version != cpu_to_le32(1) ||
1295 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001296 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001297 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return -EINVAL;
1299
1300 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1301 printk("md: invalid superblock checksum on %s\n",
1302 bdevname(rdev->bdev,b));
1303 return -EINVAL;
1304 }
1305 if (le64_to_cpu(sb->data_size) < 10) {
1306 printk("md: data_size too small on %s\n",
1307 bdevname(rdev->bdev,b));
1308 return -EINVAL;
1309 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 rdev->preferred_minor = 0xffff;
1312 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001313 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
NeilBrown0002b272005-09-09 16:23:53 -07001315 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001316 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001317 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001318 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1319
1320 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001321 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001322 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001323
NeilBrown31b65a02006-07-10 04:44:14 -07001324 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1325 rdev->desc_nr = -1;
1326 else
1327 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1328
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001329 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001330 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001331 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 __u64 ev1, ev2;
1333 struct mdp_superblock_1 *refsb =
1334 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1335
1336 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1337 sb->level != refsb->level ||
1338 sb->layout != refsb->layout ||
1339 sb->chunksize != refsb->chunksize) {
1340 printk(KERN_WARNING "md: %s has strangely different"
1341 " superblock to %s\n",
1342 bdevname(rdev->bdev,b),
1343 bdevname(refdev->bdev,b2));
1344 return -EINVAL;
1345 }
1346 ev1 = le64_to_cpu(sb->events);
1347 ev2 = le64_to_cpu(refsb->events);
1348
1349 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001350 ret = 1;
1351 else
1352 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
NeilBrowna1801f82008-03-04 14:29:31 -08001354 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001355 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1356 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001358 rdev->sectors = rdev->sb_start;
1359 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001361 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001362 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001363 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001364 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365}
1366
1367static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1368{
1369 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001370 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
NeilBrown41158c72005-06-21 17:17:25 -07001372 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001373 clear_bit(Faulty, &rdev->flags);
1374 clear_bit(In_sync, &rdev->flags);
1375 clear_bit(WriteMostly, &rdev->flags);
1376 clear_bit(BarriersNotsupp, &rdev->flags);
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (mddev->raid_disks == 0) {
1379 mddev->major_version = 1;
1380 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001381 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001382 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1384 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1385 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001386 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 mddev->layout = le32_to_cpu(sb->layout);
1388 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001389 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001390 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001391 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001392 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1395 memcpy(mddev->uuid, sb->set_uuid, 16);
1396
1397 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001398
NeilBrown71c08052005-09-09 16:23:51 -07001399 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001400 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001401 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001402
NeilBrownf6705572006-03-27 01:18:11 -08001403 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1404 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1405 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1406 mddev->new_level = le32_to_cpu(sb->new_level);
1407 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001408 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001409 } else {
1410 mddev->reshape_position = MaxSector;
1411 mddev->delta_disks = 0;
1412 mddev->new_level = mddev->level;
1413 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001414 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001415 }
1416
NeilBrown41158c72005-06-21 17:17:25 -07001417 } else if (mddev->pers == NULL) {
1418 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 ++ev1;
1420 if (ev1 < mddev->events)
1421 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001422 } else if (mddev->bitmap) {
1423 /* If adding to array with a bitmap, then we can accept an
1424 * older device, but not too old.
1425 */
NeilBrown41158c72005-06-21 17:17:25 -07001426 if (ev1 < mddev->bitmap->events_cleared)
1427 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001428 } else {
1429 if (ev1 < mddev->events)
1430 /* just a hot-add of a new device, leave raid_disk at -1 */
1431 return 0;
1432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 if (mddev->level != LEVEL_MULTIPATH) {
1434 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001435 if (rdev->desc_nr < 0 ||
1436 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1437 role = 0xffff;
1438 rdev->desc_nr = -1;
1439 } else
1440 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 switch(role) {
1442 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 break;
1444 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001445 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 break;
1447 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001448 if ((le32_to_cpu(sb->feature_map) &
1449 MD_FEATURE_RECOVERY_OFFSET))
1450 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1451 else
1452 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 rdev->raid_disk = role;
1454 break;
1455 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001456 if (sb->devflags & WriteMostly1)
1457 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001458 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001459 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return 0;
1462}
1463
1464static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1465{
1466 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 mdk_rdev_t *rdev2;
1468 int max_dev, i;
1469 /* make rdev->sb match mddev and rdev data. */
1470
1471 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1472
1473 sb->feature_map = 0;
1474 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001475 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 memset(sb->pad1, 0, sizeof(sb->pad1));
1477 memset(sb->pad2, 0, sizeof(sb->pad2));
1478 memset(sb->pad3, 0, sizeof(sb->pad3));
1479
1480 sb->utime = cpu_to_le64((__u64)mddev->utime);
1481 sb->events = cpu_to_le64(mddev->events);
1482 if (mddev->in_sync)
1483 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1484 else
1485 sb->resync_offset = cpu_to_le64(0);
1486
NeilBrown1c05b4b2006-10-21 10:24:08 -07001487 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001488
NeilBrownf0ca3402006-02-02 14:28:04 -08001489 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001490 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001491 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001492 sb->level = cpu_to_le32(mddev->level);
1493 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001494
NeilBrowna654b9d82005-06-21 17:17:27 -07001495 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1496 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001497 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001498 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001499
1500 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001501 !test_bit(In_sync, &rdev->flags)) {
NeilBrown97e4f422009-03-31 14:33:13 +11001502 if (rdev->recovery_offset > 0) {
1503 sb->feature_map |=
1504 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1505 sb->recovery_offset =
1506 cpu_to_le64(rdev->recovery_offset);
1507 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001508 }
1509
NeilBrownf6705572006-03-27 01:18:11 -08001510 if (mddev->reshape_position != MaxSector) {
1511 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1512 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1513 sb->new_layout = cpu_to_le32(mddev->new_layout);
1514 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1515 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001516 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001517 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001520 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 if (rdev2->desc_nr+1 > max_dev)
1522 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001523
NeilBrown70471da2009-08-03 10:59:57 +10001524 if (max_dev > le32_to_cpu(sb->max_dev)) {
1525 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001526 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001527 rdev->sb_size = max_dev * 2 + 256;
1528 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1529 if (rdev->sb_size & bmask)
1530 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 for (i=0; i<max_dev;i++)
1533 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1534
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001535 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001537 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001539 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001541 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1542 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 else
1544 sb->dev_roles[i] = cpu_to_le16(0xffff);
1545 }
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 sb->sb_csum = calc_sb_1_csum(sb);
1548}
1549
Chris Webb0cd17fe2008-06-28 08:31:46 +10001550static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001551super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001552{
1553 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001554 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001555 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001556 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001557 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001558 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001559 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1560 max_sectors -= rdev->data_offset;
1561 if (!num_sectors || num_sectors > max_sectors)
1562 num_sectors = max_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001563 } else if (rdev->mddev->bitmap_offset) {
1564 /* minor version 0 with bitmap we can't move */
1565 return 0;
1566 } else {
1567 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001568 sector_t sb_start;
1569 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1570 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001571 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001572 if (!num_sectors || num_sectors > max_sectors)
1573 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001574 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001575 }
1576 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001577 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001578 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001579 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001580 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001581 rdev->sb_page);
1582 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001583 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001584}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Adrian Bunk75c96f82005-05-05 16:16:09 -07001586static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 [0] = {
1588 .name = "0.90.0",
1589 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001590 .load_super = super_90_load,
1591 .validate_super = super_90_validate,
1592 .sync_super = super_90_sync,
1593 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 },
1595 [1] = {
1596 .name = "md-1",
1597 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001598 .load_super = super_1_load,
1599 .validate_super = super_1_validate,
1600 .sync_super = super_1_sync,
1601 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 },
1603};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1606{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001607 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
NeilBrown4b809912008-07-21 17:05:25 +10001609 rcu_read_lock();
1610 rdev_for_each_rcu(rdev, mddev1)
1611 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001612 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001613 rdev2->bdev->bd_contains) {
1614 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001615 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001616 }
1617 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 return 0;
1619}
1620
1621static LIST_HEAD(pending_raid_disks);
1622
Andre Nollac5e7112009-08-03 10:59:47 +10001623/*
1624 * Try to register data integrity profile for an mddev
1625 *
1626 * This is called when an array is started and after a disk has been kicked
1627 * from the array. It only succeeds if all working and active component devices
1628 * are integrity capable with matching profiles.
1629 */
1630int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001631{
Andre Nollac5e7112009-08-03 10:59:47 +10001632 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001633
Andre Nollac5e7112009-08-03 10:59:47 +10001634 if (list_empty(&mddev->disks))
1635 return 0; /* nothing to do */
1636 if (blk_get_integrity(mddev->gendisk))
1637 return 0; /* already registered */
1638 list_for_each_entry(rdev, &mddev->disks, same_set) {
1639 /* skip spares and non-functional disks */
1640 if (test_bit(Faulty, &rdev->flags))
1641 continue;
1642 if (rdev->raid_disk < 0)
1643 continue;
1644 /*
1645 * If at least one rdev is not integrity capable, we can not
1646 * enable data integrity for the md device.
1647 */
1648 if (!bdev_get_integrity(rdev->bdev))
1649 return -EINVAL;
1650 if (!reference) {
1651 /* Use the first rdev as the reference */
1652 reference = rdev;
1653 continue;
1654 }
1655 /* does this rdev's profile match the reference profile? */
1656 if (blk_integrity_compare(reference->bdev->bd_disk,
1657 rdev->bdev->bd_disk) < 0)
1658 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001659 }
Andre Nollac5e7112009-08-03 10:59:47 +10001660 /*
1661 * All component devices are integrity capable and have matching
1662 * profiles, register the common profile for the md device.
1663 */
1664 if (blk_integrity_register(mddev->gendisk,
1665 bdev_get_integrity(reference->bdev)) != 0) {
1666 printk(KERN_ERR "md: failed to register integrity for %s\n",
1667 mdname(mddev));
1668 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001669 }
Andre Nollac5e7112009-08-03 10:59:47 +10001670 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1671 mdname(mddev));
1672 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001673}
Andre Nollac5e7112009-08-03 10:59:47 +10001674EXPORT_SYMBOL(md_integrity_register);
1675
1676/* Disable data integrity if non-capable/non-matching disk is being added */
1677void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1678{
1679 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1680 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1681
1682 if (!bi_mddev) /* nothing to do */
1683 return;
1684 if (rdev->raid_disk < 0) /* skip spares */
1685 return;
1686 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1687 rdev->bdev->bd_disk) >= 0)
1688 return;
1689 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1690 blk_integrity_unregister(mddev->gendisk);
1691}
1692EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1695{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001696 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001697 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001698 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001699 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 if (rdev->mddev) {
1702 MD_BUG();
1703 return -EINVAL;
1704 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001705
1706 /* prevent duplicates */
1707 if (find_rdev(mddev, rdev->bdev->bd_dev))
1708 return -EEXIST;
1709
Andre Nolldd8ac332009-03-31 14:33:13 +11001710 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1711 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1712 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001713 if (mddev->pers) {
1714 /* Cannot change size, so fail
1715 * If mddev->level <= 0, then we don't care
1716 * about aligning sizes (e.g. linear)
1717 */
1718 if (mddev->level > 0)
1719 return -ENOSPC;
1720 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001721 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 /* Verify rdev->desc_nr is unique.
1725 * If it is -1, assign a free number, else
1726 * check number is not in use
1727 */
1728 if (rdev->desc_nr < 0) {
1729 int choice = 0;
1730 if (mddev->pers) choice = mddev->raid_disks;
1731 while (find_rdev_nr(mddev, choice))
1732 choice++;
1733 rdev->desc_nr = choice;
1734 } else {
1735 if (find_rdev_nr(mddev, rdev->desc_nr))
1736 return -EBUSY;
1737 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001738 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1739 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1740 mdname(mddev), mddev->max_disks);
1741 return -EBUSY;
1742 }
NeilBrown19133a42005-11-08 21:39:35 -08001743 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001744 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001745 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001748 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001749
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001750 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001751 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001752
Tejun Heo0762b8b2008-08-25 19:56:12 +09001753 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001754 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1755 kobject_del(&rdev->kobj);
1756 goto fail;
1757 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001758 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1759
NeilBrown4b809912008-07-21 17:05:25 +10001760 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001761 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001762
1763 /* May as well allow recovery to be retried once */
1764 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001767
1768 fail:
1769 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1770 b, mdname(mddev));
1771 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772}
1773
NeilBrown177a99b2008-02-06 01:39:56 -08001774static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001775{
1776 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1777 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001778 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001779}
1780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1782{
1783 char b[BDEVNAME_SIZE];
1784 if (!rdev->mddev) {
1785 MD_BUG();
1786 return;
1787 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001788 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001789 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1791 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001792 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001793 sysfs_put(rdev->sysfs_state);
1794 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001795 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001796 * writing to 'remove' to "dev/state". We also need
1797 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001798 */
NeilBrown4b809912008-07-21 17:05:25 +10001799 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001800 INIT_WORK(&rdev->del_work, md_delayed_delete);
1801 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001802 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
1804
1805/*
1806 * prevent the device from being mounted, repartitioned or
1807 * otherwise reused by a RAID array (or any other kernel
1808 * subsystem), by bd_claiming the device.
1809 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001810static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
1812 int err = 0;
1813 struct block_device *bdev;
1814 char b[BDEVNAME_SIZE];
1815
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001816 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 if (IS_ERR(bdev)) {
1818 printk(KERN_ERR "md: could not open %s.\n",
1819 __bdevname(dev, b));
1820 return PTR_ERR(bdev);
1821 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001822 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 if (err) {
1824 printk(KERN_ERR "md: could not bd_claim %s.\n",
1825 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001826 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 return err;
1828 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001829 if (!shared)
1830 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 rdev->bdev = bdev;
1832 return err;
1833}
1834
1835static void unlock_rdev(mdk_rdev_t *rdev)
1836{
1837 struct block_device *bdev = rdev->bdev;
1838 rdev->bdev = NULL;
1839 if (!bdev)
1840 MD_BUG();
1841 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001842 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843}
1844
1845void md_autodetect_dev(dev_t dev);
1846
1847static void export_rdev(mdk_rdev_t * rdev)
1848{
1849 char b[BDEVNAME_SIZE];
1850 printk(KERN_INFO "md: export_rdev(%s)\n",
1851 bdevname(rdev->bdev,b));
1852 if (rdev->mddev)
1853 MD_BUG();
1854 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001856 if (test_bit(AutoDetected, &rdev->flags))
1857 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858#endif
1859 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001860 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861}
1862
1863static void kick_rdev_from_array(mdk_rdev_t * rdev)
1864{
1865 unbind_rdev_from_array(rdev);
1866 export_rdev(rdev);
1867}
1868
1869static void export_array(mddev_t *mddev)
1870{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001871 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
NeilBrownd089c6a2008-02-06 01:39:59 -08001873 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (!rdev->mddev) {
1875 MD_BUG();
1876 continue;
1877 }
1878 kick_rdev_from_array(rdev);
1879 }
1880 if (!list_empty(&mddev->disks))
1881 MD_BUG();
1882 mddev->raid_disks = 0;
1883 mddev->major_version = 0;
1884}
1885
1886static void print_desc(mdp_disk_t *desc)
1887{
1888 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1889 desc->major,desc->minor,desc->raid_disk,desc->state);
1890}
1891
Cheng Renquancd2ac932009-01-09 08:31:08 +11001892static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
1894 int i;
1895
1896 printk(KERN_INFO
1897 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1898 sb->major_version, sb->minor_version, sb->patch_version,
1899 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1900 sb->ctime);
1901 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1902 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1903 sb->md_minor, sb->layout, sb->chunk_size);
1904 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1905 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1906 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1907 sb->failed_disks, sb->spare_disks,
1908 sb->sb_csum, (unsigned long)sb->events_lo);
1909
1910 printk(KERN_INFO);
1911 for (i = 0; i < MD_SB_DISKS; i++) {
1912 mdp_disk_t *desc;
1913
1914 desc = sb->disks + i;
1915 if (desc->number || desc->major || desc->minor ||
1916 desc->raid_disk || (desc->state && (desc->state != 4))) {
1917 printk(" D %2d: ", i);
1918 print_desc(desc);
1919 }
1920 }
1921 printk(KERN_INFO "md: THIS: ");
1922 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
1924
Cheng Renquancd2ac932009-01-09 08:31:08 +11001925static void print_sb_1(struct mdp_superblock_1 *sb)
1926{
1927 __u8 *uuid;
1928
1929 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001930 printk(KERN_INFO
1931 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1932 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1933 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001934 le32_to_cpu(sb->major_version),
1935 le32_to_cpu(sb->feature_map),
1936 uuid[0], uuid[1], uuid[2], uuid[3],
1937 uuid[4], uuid[5], uuid[6], uuid[7],
1938 uuid[8], uuid[9], uuid[10], uuid[11],
1939 uuid[12], uuid[13], uuid[14], uuid[15],
1940 sb->set_name,
1941 (unsigned long long)le64_to_cpu(sb->ctime)
1942 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1943
1944 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001945 printk(KERN_INFO
1946 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11001947 " RO:%llu\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001948 "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1949 ":%02x%02x%02x%02x%02x%02x\n"
1950 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1951 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001952 le32_to_cpu(sb->level),
1953 (unsigned long long)le64_to_cpu(sb->size),
1954 le32_to_cpu(sb->raid_disks),
1955 le32_to_cpu(sb->layout),
1956 le32_to_cpu(sb->chunksize),
1957 (unsigned long long)le64_to_cpu(sb->data_offset),
1958 (unsigned long long)le64_to_cpu(sb->data_size),
1959 (unsigned long long)le64_to_cpu(sb->super_offset),
1960 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1961 le32_to_cpu(sb->dev_number),
1962 uuid[0], uuid[1], uuid[2], uuid[3],
1963 uuid[4], uuid[5], uuid[6], uuid[7],
1964 uuid[8], uuid[9], uuid[10], uuid[11],
1965 uuid[12], uuid[13], uuid[14], uuid[15],
1966 sb->devflags,
1967 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1968 (unsigned long long)le64_to_cpu(sb->events),
1969 (unsigned long long)le64_to_cpu(sb->resync_offset),
1970 le32_to_cpu(sb->sb_csum),
1971 le32_to_cpu(sb->max_dev)
1972 );
1973}
1974
1975static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11001978 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1979 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08001980 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1981 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11001983 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1984 switch (major_version) {
1985 case 0:
1986 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1987 break;
1988 case 1:
1989 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1990 break;
1991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 } else
1993 printk(KERN_INFO "md: no rdev superblock!\n");
1994}
1995
Adrian Bunk5e563412006-06-26 00:27:42 -07001996static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001998 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 mdk_rdev_t *rdev;
2000 mddev_t *mddev;
2001 char b[BDEVNAME_SIZE];
2002
2003 printk("\n");
2004 printk("md: **********************************\n");
2005 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2006 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002007 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
NeilBrown32a76272005-06-21 17:17:14 -07002009 if (mddev->bitmap)
2010 bitmap_print_sb(mddev->bitmap);
2011 else
2012 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002013 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 printk("<%s>", bdevname(rdev->bdev,b));
2015 printk("\n");
2016
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002017 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002018 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
2020 printk("md: **********************************\n");
2021 printk("\n");
2022}
2023
2024
NeilBrown42543762006-06-26 00:27:57 -07002025static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
NeilBrown42543762006-06-26 00:27:57 -07002027 /* Update each superblock (in-memory image), but
2028 * if we are allowed to, skip spares which already
2029 * have the right event counter, or have one earlier
2030 * (which would mean they aren't being marked as dirty
2031 * with the rest of the array)
2032 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
NeilBrown5e865102009-11-12 12:08:04 +11002035 /* First make sure individual recovery_offsets are correct */
2036 list_for_each_entry(rdev, &mddev->disks, same_set) {
2037 if (rdev->raid_disk >= 0 &&
2038 !test_bit(In_sync, &rdev->flags) &&
2039 mddev->curr_resync_completed > rdev->recovery_offset)
2040 rdev->recovery_offset = mddev->curr_resync_completed;
2041
2042 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002043 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002044 if (rdev->sb_events == mddev->events ||
2045 (nospares &&
2046 rdev->raid_disk < 0 &&
2047 (rdev->sb_events&1)==0 &&
2048 rdev->sb_events+1 == mddev->events)) {
2049 /* Don't update this superblock */
2050 rdev->sb_loaded = 2;
2051 } else {
2052 super_types[mddev->major_version].
2053 sync_super(mddev, rdev);
2054 rdev->sb_loaded = 1;
2055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 }
2057}
2058
NeilBrown850b2b42006-10-03 01:15:46 -07002059static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002062 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002063 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
NeilBrown1b57f132009-06-18 08:48:19 +10002065 mddev->utime = get_seconds();
NeilBrown8377bc82008-04-30 00:52:28 -07002066 if (mddev->external)
2067 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08002069 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002070
NeilBrown850b2b42006-10-03 01:15:46 -07002071 set_bit(MD_CHANGE_PENDING, &mddev->flags);
2072 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2073 force_change = 1;
2074 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2075 /* just a clean<-> dirty transition, possibly leave spares alone,
2076 * though if events isn't the right even/odd, we will have to do
2077 * spares after all
2078 */
2079 nospares = 1;
2080 if (force_change)
2081 nospares = 0;
2082 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002083 /* If the array is degraded, then skipping spares is both
2084 * dangerous and fairly pointless.
2085 * Dangerous because a device that was removed from the array
2086 * might have a event_count that still looks up-to-date,
2087 * so it can be re-added without a resync.
2088 * Pointless because if there are any spares to skip,
2089 * then a recovery will happen and soon that array won't
2090 * be degraded any more and the spare can go back to sleep then.
2091 */
NeilBrown850b2b42006-10-03 01:15:46 -07002092 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002093
NeilBrown06d91a52005-06-21 17:17:12 -07002094 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002095
2096 /* If this is just a dirty<->clean transition, and the array is clean
2097 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002098 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002099 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08002100 && (mddev->events & 1)
2101 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07002102 mddev->events--;
2103 else {
2104 /* otherwise we have to go forward and ... */
2105 mddev->events ++;
2106 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
NeilBrown51d56682009-08-13 09:54:02 +10002107 /* .. if the array isn't clean, an 'even' event must also go
2108 * to spares. */
2109 if ((mddev->events&1)==0)
NeilBrown42543762006-06-26 00:27:57 -07002110 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002111 } else {
NeilBrown51d56682009-08-13 09:54:02 +10002112 /* otherwise an 'odd' event must go to spares */
2113 if ((mddev->events&1))
NeilBrown42543762006-06-26 00:27:57 -07002114 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002115 }
2116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
2118 if (!mddev->events) {
2119 /*
2120 * oops, this 64-bit counter should never wrap.
2121 * Either we are in around ~1 trillion A.C., assuming
2122 * 1 reboot per second, or we have a bug:
2123 */
2124 MD_BUG();
2125 mddev->events --;
2126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
2128 /*
2129 * do not write anything to disk if using
2130 * nonpersistent superblocks
2131 */
NeilBrown06d91a52005-06-21 17:17:12 -07002132 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08002133 if (!mddev->external)
2134 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2135
NeilBrowna9701a32005-11-08 21:39:34 -08002136 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002137 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 return;
NeilBrown06d91a52005-06-21 17:17:12 -07002139 }
NeilBrowne6910632008-02-06 01:39:51 -08002140 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002141 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143 dprintk(KERN_INFO
2144 "md: updating %s RAID superblock on device (in sync %d)\n",
2145 mdname(mddev),mddev->in_sync);
2146
NeilBrown4ad13662007-07-17 04:06:13 -07002147 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002148 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 char b[BDEVNAME_SIZE];
2150 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002151 if (rdev->sb_loaded != 1)
2152 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002153 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 dprintk("(skipping faulty ");
2155
2156 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002157 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002158 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002159 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002160 rdev->sb_page);
2161 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2162 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002163 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002164 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 } else
2167 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002168 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 /* only need to write one superblock... */
2170 break;
2171 }
NeilBrowna9701a32005-11-08 21:39:34 -08002172 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002173 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002174
NeilBrowna9701a32005-11-08 21:39:34 -08002175 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002176 if (mddev->in_sync != sync_req ||
2177 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002178 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002179 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002180 goto repeat;
2181 }
NeilBrown850b2b42006-10-03 01:15:46 -07002182 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002183 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002184 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002185 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2186 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188}
2189
Andre Noll7f6ce762008-03-23 18:34:54 +01002190/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002191 * We want to accept with case. For this we use cmd_match.
2192 */
2193static int cmd_match(const char *cmd, const char *str)
2194{
2195 /* See if cmd, written into a sysfs file, matches
2196 * str. They must either be the same, or cmd can
2197 * have a trailing newline
2198 */
2199 while (*cmd && *str && *cmd == *str) {
2200 cmd++;
2201 str++;
2202 }
2203 if (*cmd == '\n')
2204 cmd++;
2205 if (*str || *cmd)
2206 return 0;
2207 return 1;
2208}
2209
NeilBrown86e6ffd2005-11-08 21:39:24 -08002210struct rdev_sysfs_entry {
2211 struct attribute attr;
2212 ssize_t (*show)(mdk_rdev_t *, char *);
2213 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2214};
2215
2216static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002217state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002218{
2219 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002220 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002221
NeilBrownb2d444d2005-11-08 21:39:31 -08002222 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002223 len+= sprintf(page+len, "%sfaulty",sep);
2224 sep = ",";
2225 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002226 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002227 len += sprintf(page+len, "%sin_sync",sep);
2228 sep = ",";
2229 }
NeilBrownf6556752006-06-26 00:28:01 -07002230 if (test_bit(WriteMostly, &rdev->flags)) {
2231 len += sprintf(page+len, "%swrite_mostly",sep);
2232 sep = ",";
2233 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002234 if (test_bit(Blocked, &rdev->flags)) {
2235 len += sprintf(page+len, "%sblocked", sep);
2236 sep = ",";
2237 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002238 if (!test_bit(Faulty, &rdev->flags) &&
2239 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002240 len += sprintf(page+len, "%sspare", sep);
2241 sep = ",";
2242 }
2243 return len+sprintf(page+len, "\n");
2244}
2245
NeilBrown45dc2de2006-06-26 00:27:58 -07002246static ssize_t
2247state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2248{
2249 /* can write
2250 * faulty - simulates and error
2251 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002252 * writemostly - sets write_mostly
2253 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002254 * blocked - sets the Blocked flag
2255 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002256 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002257 */
2258 int err = -EINVAL;
2259 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2260 md_error(rdev->mddev, rdev);
2261 err = 0;
2262 } else if (cmd_match(buf, "remove")) {
2263 if (rdev->raid_disk >= 0)
2264 err = -EBUSY;
2265 else {
2266 mddev_t *mddev = rdev->mddev;
2267 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002268 if (mddev->pers)
2269 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002270 md_new_event(mddev);
2271 err = 0;
2272 }
NeilBrownf6556752006-06-26 00:28:01 -07002273 } else if (cmd_match(buf, "writemostly")) {
2274 set_bit(WriteMostly, &rdev->flags);
2275 err = 0;
2276 } else if (cmd_match(buf, "-writemostly")) {
2277 clear_bit(WriteMostly, &rdev->flags);
2278 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002279 } else if (cmd_match(buf, "blocked")) {
2280 set_bit(Blocked, &rdev->flags);
2281 err = 0;
2282 } else if (cmd_match(buf, "-blocked")) {
2283 clear_bit(Blocked, &rdev->flags);
2284 wake_up(&rdev->blocked_wait);
2285 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2286 md_wakeup_thread(rdev->mddev->thread);
2287
2288 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002289 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2290 set_bit(In_sync, &rdev->flags);
2291 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002292 }
NeilBrown3c0ee632008-10-21 13:25:28 +11002293 if (!err && rdev->sysfs_state)
2294 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002295 return err ? err : len;
2296}
NeilBrown80ca3a42006-07-10 04:44:18 -07002297static struct rdev_sysfs_entry rdev_state =
2298__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002299
2300static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002301errors_show(mdk_rdev_t *rdev, char *page)
2302{
2303 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2304}
2305
2306static ssize_t
2307errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2308{
2309 char *e;
2310 unsigned long n = simple_strtoul(buf, &e, 10);
2311 if (*buf && (*e == 0 || *e == '\n')) {
2312 atomic_set(&rdev->corrected_errors, n);
2313 return len;
2314 }
2315 return -EINVAL;
2316}
2317static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002318__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002319
NeilBrown014236d2006-01-06 00:20:55 -08002320static ssize_t
2321slot_show(mdk_rdev_t *rdev, char *page)
2322{
2323 if (rdev->raid_disk < 0)
2324 return sprintf(page, "none\n");
2325 else
2326 return sprintf(page, "%d\n", rdev->raid_disk);
2327}
2328
2329static ssize_t
2330slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2331{
2332 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002333 int err;
2334 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002335 int slot = simple_strtoul(buf, &e, 10);
2336 if (strncmp(buf, "none", 4)==0)
2337 slot = -1;
2338 else if (e==buf || (*e && *e!= '\n'))
2339 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002340 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002341 /* Setting 'slot' on an active array requires also
2342 * updating the 'rd%d' link, and communicating
2343 * with the personality with ->hot_*_disk.
2344 * For now we only support removing
2345 * failed/spare devices. This normally happens automatically,
2346 * but not when the metadata is externally managed.
2347 */
NeilBrownc303da62008-02-06 01:39:51 -08002348 if (rdev->raid_disk == -1)
2349 return -EEXIST;
2350 /* personality does all needed checks */
2351 if (rdev->mddev->pers->hot_add_disk == NULL)
2352 return -EINVAL;
2353 err = rdev->mddev->pers->
2354 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2355 if (err)
2356 return err;
2357 sprintf(nm, "rd%d", rdev->raid_disk);
2358 sysfs_remove_link(&rdev->mddev->kobj, nm);
2359 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2360 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002361 } else if (rdev->mddev->pers) {
2362 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002363 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002364 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002365 */
2366
2367 if (rdev->raid_disk != -1)
2368 return -EBUSY;
2369
2370 if (rdev->mddev->pers->hot_add_disk == NULL)
2371 return -EINVAL;
2372
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002373 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002374 if (rdev2->raid_disk == slot)
2375 return -EEXIST;
2376
2377 rdev->raid_disk = slot;
2378 if (test_bit(In_sync, &rdev->flags))
2379 rdev->saved_raid_disk = slot;
2380 else
2381 rdev->saved_raid_disk = -1;
2382 err = rdev->mddev->pers->
2383 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002384 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002385 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002386 return err;
Neil Brown52664732008-06-28 08:31:44 +10002387 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002388 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002389 sprintf(nm, "rd%d", rdev->raid_disk);
2390 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2391 printk(KERN_WARNING
2392 "md: cannot register "
2393 "%s for %s\n",
2394 nm, mdname(rdev->mddev));
2395
2396 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002397 } else {
2398 if (slot >= rdev->mddev->raid_disks)
2399 return -ENOSPC;
2400 rdev->raid_disk = slot;
2401 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002402 clear_bit(Faulty, &rdev->flags);
2403 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002404 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002405 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002406 }
NeilBrown014236d2006-01-06 00:20:55 -08002407 return len;
2408}
2409
2410
2411static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002412__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002413
NeilBrown93c8cad2006-01-06 00:20:56 -08002414static ssize_t
2415offset_show(mdk_rdev_t *rdev, char *page)
2416{
Andrew Morton6961ece2006-01-06 00:20:59 -08002417 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002418}
2419
2420static ssize_t
2421offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2422{
2423 char *e;
2424 unsigned long long offset = simple_strtoull(buf, &e, 10);
2425 if (e==buf || (*e && *e != '\n'))
2426 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002427 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002428 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002429 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002430 /* Must set offset before size, so overlap checks
2431 * can be sane */
2432 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002433 rdev->data_offset = offset;
2434 return len;
2435}
2436
2437static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002438__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002439
NeilBrown83303b62006-01-06 00:21:06 -08002440static ssize_t
2441rdev_size_show(mdk_rdev_t *rdev, char *page)
2442{
Andre Nolldd8ac332009-03-31 14:33:13 +11002443 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002444}
2445
NeilBrownc5d79ad2008-02-06 01:39:54 -08002446static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2447{
2448 /* check if two start/length pairs overlap */
2449 if (s1+l1 <= s2)
2450 return 0;
2451 if (s2+l2 <= s1)
2452 return 0;
2453 return 1;
2454}
2455
Dan Williamsb522adc2009-03-31 15:00:31 +11002456static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2457{
2458 unsigned long long blocks;
2459 sector_t new;
2460
2461 if (strict_strtoull(buf, 10, &blocks) < 0)
2462 return -EINVAL;
2463
2464 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2465 return -EINVAL; /* sector conversion overflow */
2466
2467 new = blocks * 2;
2468 if (new != blocks * 2)
2469 return -EINVAL; /* unsigned long long to sector_t overflow */
2470
2471 *sectors = new;
2472 return 0;
2473}
2474
NeilBrown83303b62006-01-06 00:21:06 -08002475static ssize_t
2476rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2477{
NeilBrown27c529b2008-03-04 14:29:33 -08002478 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002479 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002480 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002481
Dan Williamsb522adc2009-03-31 15:00:31 +11002482 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002483 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002484 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002485 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002486 sectors = super_types[my_mddev->major_version].
2487 rdev_size_change(rdev, sectors);
2488 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002489 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002490 } else if (!sectors)
2491 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2492 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002493 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002494 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002495 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002496
Andre Nolldd8ac332009-03-31 14:33:13 +11002497 rdev->sectors = sectors;
2498 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002499 /* need to check that all other rdevs with the same ->bdev
2500 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002501 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002502 * we have to change it back, we will have the lock again.
2503 */
2504 mddev_t *mddev;
2505 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002506 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002507
NeilBrown27c529b2008-03-04 14:29:33 -08002508 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002509 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002510 mdk_rdev_t *rdev2;
2511
2512 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002513 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002514 if (test_bit(AllReserved, &rdev2->flags) ||
2515 (rdev->bdev == rdev2->bdev &&
2516 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002517 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002518 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002519 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002520 overlap = 1;
2521 break;
2522 }
2523 mddev_unlock(mddev);
2524 if (overlap) {
2525 mddev_put(mddev);
2526 break;
2527 }
2528 }
NeilBrown27c529b2008-03-04 14:29:33 -08002529 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002530 if (overlap) {
2531 /* Someone else could have slipped in a size
2532 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002533 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002534 * safe, and trust userspace not to race with
2535 * itself
2536 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002537 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002538 return -EBUSY;
2539 }
2540 }
NeilBrown83303b62006-01-06 00:21:06 -08002541 return len;
2542}
2543
2544static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002545__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002546
NeilBrown86e6ffd2005-11-08 21:39:24 -08002547static struct attribute *rdev_default_attrs[] = {
2548 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002549 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002550 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002551 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002552 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002553 NULL,
2554};
2555static ssize_t
2556rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2557{
2558 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2559 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002560 mddev_t *mddev = rdev->mddev;
2561 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002562
2563 if (!entry->show)
2564 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002565
2566 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2567 if (!rv) {
2568 if (rdev->mddev == NULL)
2569 rv = -EBUSY;
2570 else
2571 rv = entry->show(rdev, page);
2572 mddev_unlock(mddev);
2573 }
2574 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002575}
2576
2577static ssize_t
2578rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2579 const char *page, size_t length)
2580{
2581 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2582 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002583 ssize_t rv;
2584 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002585
2586 if (!entry->store)
2587 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002588 if (!capable(CAP_SYS_ADMIN))
2589 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002590 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002591 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002592 if (rdev->mddev == NULL)
2593 rv = -EBUSY;
2594 else
2595 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002596 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002597 }
2598 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002599}
2600
2601static void rdev_free(struct kobject *ko)
2602{
2603 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2604 kfree(rdev);
2605}
2606static struct sysfs_ops rdev_sysfs_ops = {
2607 .show = rdev_attr_show,
2608 .store = rdev_attr_store,
2609};
2610static struct kobj_type rdev_ktype = {
2611 .release = rdev_free,
2612 .sysfs_ops = &rdev_sysfs_ops,
2613 .default_attrs = rdev_default_attrs,
2614};
2615
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616/*
2617 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2618 *
2619 * mark the device faulty if:
2620 *
2621 * - the device is nonexistent (zero size)
2622 * - the device has no valid superblock
2623 *
2624 * a faulty rdev _never_ has rdev->sb set.
2625 */
2626static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2627{
2628 char b[BDEVNAME_SIZE];
2629 int err;
2630 mdk_rdev_t *rdev;
2631 sector_t size;
2632
NeilBrown9ffae0c2006-01-06 00:20:32 -08002633 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 if (!rdev) {
2635 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2636 return ERR_PTR(-ENOMEM);
2637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
2639 if ((err = alloc_disk_sb(rdev)))
2640 goto abort_free;
2641
NeilBrownc5d79ad2008-02-06 01:39:54 -08002642 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 if (err)
2644 goto abort_free;
2645
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002646 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002647
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002649 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002650 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002651 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002653 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002655 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002656 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
2658 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2659 if (!size) {
2660 printk(KERN_WARNING
2661 "md: %s has zero or unknown size, marking faulty!\n",
2662 bdevname(rdev->bdev,b));
2663 err = -EINVAL;
2664 goto abort_free;
2665 }
2666
2667 if (super_format >= 0) {
2668 err = super_types[super_format].
2669 load_super(rdev, NULL, super_minor);
2670 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002671 printk(KERN_WARNING
2672 "md: %s does not have a valid v%d.%d "
2673 "superblock, not importing!\n",
2674 bdevname(rdev->bdev,b),
2675 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 goto abort_free;
2677 }
2678 if (err < 0) {
2679 printk(KERN_WARNING
2680 "md: could not read %s's sb, not importing!\n",
2681 bdevname(rdev->bdev,b));
2682 goto abort_free;
2683 }
2684 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002685
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002687 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
2689 return rdev;
2690
2691abort_free:
2692 if (rdev->sb_page) {
2693 if (rdev->bdev)
2694 unlock_rdev(rdev);
2695 free_disk_sb(rdev);
2696 }
2697 kfree(rdev);
2698 return ERR_PTR(err);
2699}
2700
2701/*
2702 * Check a full RAID array for plausibility
2703 */
2704
2705
NeilBrowna757e642005-04-16 15:26:42 -07002706static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707{
2708 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002709 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 char b[BDEVNAME_SIZE];
2711
2712 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002713 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 switch (super_types[mddev->major_version].
2715 load_super(rdev, freshest, mddev->minor_version)) {
2716 case 1:
2717 freshest = rdev;
2718 break;
2719 case 0:
2720 break;
2721 default:
2722 printk( KERN_ERR \
2723 "md: fatal superblock inconsistency in %s"
2724 " -- removing from array\n",
2725 bdevname(rdev->bdev,b));
2726 kick_rdev_from_array(rdev);
2727 }
2728
2729
2730 super_types[mddev->major_version].
2731 validate_super(mddev, freshest);
2732
2733 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002734 rdev_for_each(rdev, tmp, mddev) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002735 if (rdev->desc_nr >= mddev->max_disks ||
2736 i > mddev->max_disks) {
2737 printk(KERN_WARNING
2738 "md: %s: %s: only %d devices permitted\n",
2739 mdname(mddev), bdevname(rdev->bdev, b),
2740 mddev->max_disks);
2741 kick_rdev_from_array(rdev);
2742 continue;
2743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 if (rdev != freshest)
2745 if (super_types[mddev->major_version].
2746 validate_super(mddev, rdev)) {
2747 printk(KERN_WARNING "md: kicking non-fresh %s"
2748 " from array!\n",
2749 bdevname(rdev->bdev,b));
2750 kick_rdev_from_array(rdev);
2751 continue;
2752 }
2753 if (mddev->level == LEVEL_MULTIPATH) {
2754 rdev->desc_nr = i++;
2755 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002756 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002757 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002758 rdev->raid_disk = -1;
2759 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 }
2761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762}
2763
NeilBrown19052c02008-08-05 15:54:13 +10002764static void md_safemode_timeout(unsigned long data);
2765
NeilBrowneae17012005-11-08 21:39:23 -08002766static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002767safe_delay_show(mddev_t *mddev, char *page)
2768{
2769 int msec = (mddev->safemode_delay*1000)/HZ;
2770 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2771}
2772static ssize_t
2773safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2774{
2775 int scale=1;
2776 int dot=0;
2777 int i;
2778 unsigned long msec;
2779 char buf[30];
Dan Williams97ce0a72008-09-24 22:48:19 -07002780
NeilBrown16f17b32006-06-26 00:27:37 -07002781 /* remove a period, and count digits after it */
2782 if (len >= sizeof(buf))
2783 return -EINVAL;
Dan Williams97ce0a72008-09-24 22:48:19 -07002784 strlcpy(buf, cbuf, sizeof(buf));
NeilBrown16f17b32006-06-26 00:27:37 -07002785 for (i=0; i<len; i++) {
2786 if (dot) {
2787 if (isdigit(buf[i])) {
2788 buf[i-1] = buf[i];
2789 scale *= 10;
2790 }
2791 buf[i] = 0;
2792 } else if (buf[i] == '.') {
2793 dot=1;
2794 buf[i] = 0;
2795 }
2796 }
Dan Williams97ce0a72008-09-24 22:48:19 -07002797 if (strict_strtoul(buf, 10, &msec) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002798 return -EINVAL;
2799 msec = (msec * 1000) / scale;
2800 if (msec == 0)
2801 mddev->safemode_delay = 0;
2802 else {
NeilBrown19052c02008-08-05 15:54:13 +10002803 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002804 mddev->safemode_delay = (msec*HZ)/1000;
2805 if (mddev->safemode_delay == 0)
2806 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002807 if (mddev->safemode_delay < old_delay)
2808 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002809 }
2810 return len;
2811}
2812static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002813__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002814
2815static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002816level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002817{
NeilBrown2604b702006-01-06 00:20:36 -08002818 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002819 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002820 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002821 else if (mddev->clevel[0])
2822 return sprintf(page, "%s\n", mddev->clevel);
2823 else if (mddev->level != LEVEL_NONE)
2824 return sprintf(page, "%d\n", mddev->level);
2825 else
2826 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002827}
2828
NeilBrownd9d166c2006-01-06 00:20:51 -08002829static ssize_t
2830level_store(mddev_t *mddev, const char *buf, size_t len)
2831{
NeilBrown245f46c2009-03-31 14:39:39 +11002832 char level[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002833 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002834 struct mdk_personality *pers;
2835 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002836 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002837
2838 if (mddev->pers == NULL) {
2839 if (len == 0)
2840 return 0;
2841 if (len >= sizeof(mddev->clevel))
2842 return -ENOSPC;
2843 strncpy(mddev->clevel, buf, len);
2844 if (mddev->clevel[len-1] == '\n')
2845 len--;
2846 mddev->clevel[len] = 0;
2847 mddev->level = LEVEL_NONE;
2848 return rv;
2849 }
2850
2851 /* request to change the personality. Need to ensure:
2852 * - array is not engaged in resync/recovery/reshape
2853 * - old personality can be suspended
2854 * - new personality will access other array.
2855 */
2856
2857 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownd9d166c2006-01-06 00:20:51 -08002858 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002859
2860 if (!mddev->pers->quiesce) {
2861 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2862 mdname(mddev), mddev->pers->name);
2863 return -EINVAL;
2864 }
2865
2866 /* Now find the new personality */
2867 if (len == 0 || len >= sizeof(level))
2868 return -EINVAL;
2869 strncpy(level, buf, len);
2870 if (level[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002871 len--;
NeilBrown245f46c2009-03-31 14:39:39 +11002872 level[len] = 0;
2873
2874 request_module("md-%s", level);
2875 spin_lock(&pers_lock);
2876 pers = find_pers(LEVEL_NONE, level);
2877 if (!pers || !try_module_get(pers->owner)) {
2878 spin_unlock(&pers_lock);
2879 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2880 return -EINVAL;
2881 }
2882 spin_unlock(&pers_lock);
2883
2884 if (pers == mddev->pers) {
2885 /* Nothing to do! */
2886 module_put(pers->owner);
2887 return rv;
2888 }
2889 if (!pers->takeover) {
2890 module_put(pers->owner);
2891 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2892 mdname(mddev), level);
2893 return -EINVAL;
2894 }
2895
2896 /* ->takeover must set new_* and/or delta_disks
2897 * if it succeeds, and may set them when it fails.
2898 */
2899 priv = pers->takeover(mddev);
2900 if (IS_ERR(priv)) {
2901 mddev->new_level = mddev->level;
2902 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002903 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002904 mddev->raid_disks -= mddev->delta_disks;
2905 mddev->delta_disks = 0;
2906 module_put(pers->owner);
2907 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2908 mdname(mddev), level);
2909 return PTR_ERR(priv);
2910 }
2911
2912 /* Looks like we have a winner */
2913 mddev_suspend(mddev);
2914 mddev->pers->stop(mddev);
2915 module_put(mddev->pers->owner);
NeilBrown3a981b02009-08-03 10:59:55 +10002916 /* Invalidate devices that are now superfluous */
2917 list_for_each_entry(rdev, &mddev->disks, same_set)
2918 if (rdev->raid_disk >= mddev->raid_disks) {
2919 rdev->raid_disk = -1;
2920 clear_bit(In_sync, &rdev->flags);
2921 }
NeilBrown245f46c2009-03-31 14:39:39 +11002922 mddev->pers = pers;
2923 mddev->private = priv;
2924 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2925 mddev->level = mddev->new_level;
2926 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002927 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002928 mddev->delta_disks = 0;
2929 pers->run(mddev);
2930 mddev_resume(mddev);
2931 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2932 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2933 md_wakeup_thread(mddev->thread);
NeilBrownd9d166c2006-01-06 00:20:51 -08002934 return rv;
2935}
2936
2937static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002938__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002939
NeilBrownd4dbd022006-06-26 00:27:59 -07002940
2941static ssize_t
2942layout_show(mddev_t *mddev, char *page)
2943{
2944 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002945 if (mddev->reshape_position != MaxSector &&
2946 mddev->layout != mddev->new_layout)
2947 return sprintf(page, "%d (%d)\n",
2948 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002949 return sprintf(page, "%d\n", mddev->layout);
2950}
2951
2952static ssize_t
2953layout_store(mddev_t *mddev, const char *buf, size_t len)
2954{
2955 char *e;
2956 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002957
2958 if (!*buf || (*e && *e != '\n'))
2959 return -EINVAL;
2960
NeilBrownb3546032009-03-31 14:56:41 +11002961 if (mddev->pers) {
2962 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10002963 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11002964 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10002965 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10002966 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10002967 if (err) {
2968 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11002969 return err;
NeilBrown597a7112009-06-18 08:47:42 +10002970 }
NeilBrownb3546032009-03-31 14:56:41 +11002971 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07002972 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11002973 if (mddev->reshape_position == MaxSector)
2974 mddev->layout = n;
2975 }
NeilBrownd4dbd022006-06-26 00:27:59 -07002976 return len;
2977}
2978static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002979__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002980
2981
NeilBrowneae17012005-11-08 21:39:23 -08002982static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002983raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002984{
NeilBrownbb636542005-11-08 21:39:45 -08002985 if (mddev->raid_disks == 0)
2986 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002987 if (mddev->reshape_position != MaxSector &&
2988 mddev->delta_disks != 0)
2989 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2990 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002991 return sprintf(page, "%d\n", mddev->raid_disks);
2992}
2993
NeilBrownda943b992006-01-06 00:20:54 -08002994static int update_raid_disks(mddev_t *mddev, int raid_disks);
2995
2996static ssize_t
2997raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2998{
NeilBrownda943b992006-01-06 00:20:54 -08002999 char *e;
3000 int rv = 0;
3001 unsigned long n = simple_strtoul(buf, &e, 10);
3002
3003 if (!*buf || (*e && *e != '\n'))
3004 return -EINVAL;
3005
3006 if (mddev->pers)
3007 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003008 else if (mddev->reshape_position != MaxSector) {
3009 int olddisks = mddev->raid_disks - mddev->delta_disks;
3010 mddev->delta_disks = n - olddisks;
3011 mddev->raid_disks = n;
3012 } else
NeilBrownda943b992006-01-06 00:20:54 -08003013 mddev->raid_disks = n;
3014 return rv ? rv : len;
3015}
3016static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003017__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003018
NeilBrown24dd4692005-11-08 21:39:26 -08003019static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003020chunk_size_show(mddev_t *mddev, char *page)
3021{
NeilBrown08a02ec2007-05-09 02:35:38 -07003022 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003023 mddev->chunk_sectors != mddev->new_chunk_sectors)
3024 return sprintf(page, "%d (%d)\n",
3025 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003026 mddev->chunk_sectors << 9);
3027 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003028}
3029
3030static ssize_t
3031chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3032{
NeilBrown3b343802006-01-06 00:20:47 -08003033 char *e;
3034 unsigned long n = simple_strtoul(buf, &e, 10);
3035
NeilBrown3b343802006-01-06 00:20:47 -08003036 if (!*buf || (*e && *e != '\n'))
3037 return -EINVAL;
3038
NeilBrownb3546032009-03-31 14:56:41 +11003039 if (mddev->pers) {
3040 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003041 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003042 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003043 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003044 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003045 if (err) {
3046 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003047 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003048 }
NeilBrownb3546032009-03-31 14:56:41 +11003049 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003050 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003051 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003052 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003053 }
NeilBrown3b343802006-01-06 00:20:47 -08003054 return len;
3055}
3056static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003057__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003058
NeilBrowna94213b2006-06-26 00:28:00 -07003059static ssize_t
3060resync_start_show(mddev_t *mddev, char *page)
3061{
NeilBrownd1a7c502009-03-31 15:24:32 +11003062 if (mddev->recovery_cp == MaxSector)
3063 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003064 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3065}
3066
3067static ssize_t
3068resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3069{
NeilBrowna94213b2006-06-26 00:28:00 -07003070 char *e;
3071 unsigned long long n = simple_strtoull(buf, &e, 10);
3072
3073 if (mddev->pers)
3074 return -EBUSY;
3075 if (!*buf || (*e && *e != '\n'))
3076 return -EINVAL;
3077
3078 mddev->recovery_cp = n;
3079 return len;
3080}
3081static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003082__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003083
NeilBrown9e653b62006-06-26 00:27:58 -07003084/*
3085 * The array state can be:
3086 *
3087 * clear
3088 * No devices, no size, no level
3089 * Equivalent to STOP_ARRAY ioctl
3090 * inactive
3091 * May have some settings, but array is not active
3092 * all IO results in error
3093 * When written, doesn't tear down array, but just stops it
3094 * suspended (not supported yet)
3095 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003096 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003097 * readonly
3098 * no resync can happen. no superblocks get written.
3099 * write requests fail
3100 * read-auto
3101 * like readonly, but behaves like 'clean' on a write request.
3102 *
3103 * clean - no pending writes, but otherwise active.
3104 * When written to inactive array, starts without resync
3105 * If a write request arrives then
3106 * if metadata is known, mark 'dirty' and switch to 'active'.
3107 * if not known, block and switch to write-pending
3108 * If written to an active array that has pending writes, then fails.
3109 * active
3110 * fully active: IO and resync can be happening.
3111 * When written to inactive array, starts with resync
3112 *
3113 * write-pending
3114 * clean, but writes are blocked waiting for 'active' to be written.
3115 *
3116 * active-idle
3117 * like active, but no writes have been seen for a while (100msec).
3118 *
3119 */
3120enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3121 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003122static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003123 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3124 "write-pending", "active-idle", NULL };
3125
3126static int match_word(const char *word, char **list)
3127{
3128 int n;
3129 for (n=0; list[n]; n++)
3130 if (cmd_match(word, list[n]))
3131 break;
3132 return n;
3133}
3134
3135static ssize_t
3136array_state_show(mddev_t *mddev, char *page)
3137{
3138 enum array_state st = inactive;
3139
3140 if (mddev->pers)
3141 switch(mddev->ro) {
3142 case 1:
3143 st = readonly;
3144 break;
3145 case 2:
3146 st = read_auto;
3147 break;
3148 case 0:
3149 if (mddev->in_sync)
3150 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08003151 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3152 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003153 else if (mddev->safemode)
3154 st = active_idle;
3155 else
3156 st = active;
3157 }
3158 else {
3159 if (list_empty(&mddev->disks) &&
3160 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003161 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003162 st = clear;
3163 else
3164 st = inactive;
3165 }
3166 return sprintf(page, "%s\n", array_states[st]);
3167}
3168
Neil Browndf5b20c2008-07-11 22:02:22 +10003169static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003170static int do_md_run(mddev_t * mddev);
3171static int restart_array(mddev_t *mddev);
3172
3173static ssize_t
3174array_state_store(mddev_t *mddev, const char *buf, size_t len)
3175{
3176 int err = -EINVAL;
3177 enum array_state st = match_word(buf, array_states);
3178 switch(st) {
3179 case bad_word:
3180 break;
3181 case clear:
3182 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003183 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003184 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003185 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003186 break;
3187 case inactive:
3188 /* stopping an active array */
3189 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003190 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003191 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003192 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003193 } else
3194 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003195 break;
3196 case suspended:
3197 break; /* not supported yet */
3198 case readonly:
3199 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10003200 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003201 else {
3202 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003203 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003204 err = do_md_run(mddev);
3205 }
3206 break;
3207 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003208 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003209 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10003210 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003211 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003212 err = restart_array(mddev);
3213 if (err == 0) {
3214 mddev->ro = 2;
3215 set_disk_ro(mddev->gendisk, 0);
3216 }
NeilBrown9e653b62006-06-26 00:27:58 -07003217 } else {
3218 mddev->ro = 2;
3219 err = do_md_run(mddev);
3220 }
3221 break;
3222 case clean:
3223 if (mddev->pers) {
3224 restart_array(mddev);
3225 spin_lock_irq(&mddev->write_lock);
3226 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003227 if (mddev->in_sync == 0) {
3228 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003229 if (mddev->safemode == 1)
3230 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003231 if (mddev->persistent)
3232 set_bit(MD_CHANGE_CLEAN,
3233 &mddev->flags);
3234 }
3235 err = 0;
3236 } else
3237 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003238 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003239 } else
3240 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003241 break;
3242 case active:
3243 if (mddev->pers) {
3244 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003245 if (mddev->external)
3246 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003247 wake_up(&mddev->sb_wait);
3248 err = 0;
3249 } else {
3250 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003251 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003252 err = do_md_run(mddev);
3253 }
3254 break;
3255 case write_pending:
3256 case active_idle:
3257 /* these cannot be set */
3258 break;
3259 }
3260 if (err)
3261 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003262 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003263 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003264 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003265 }
NeilBrown9e653b62006-06-26 00:27:58 -07003266}
NeilBrown80ca3a42006-07-10 04:44:18 -07003267static struct md_sysfs_entry md_array_state =
3268__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003269
NeilBrown6d7ff732006-01-06 00:21:16 -08003270static ssize_t
3271null_show(mddev_t *mddev, char *page)
3272{
3273 return -EINVAL;
3274}
3275
3276static ssize_t
3277new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3278{
3279 /* buf must be %d:%d\n? giving major and minor numbers */
3280 /* The new device is added to the array.
3281 * If the array has a persistent superblock, we read the
3282 * superblock to initialise info and check validity.
3283 * Otherwise, only checking done is that in bind_rdev_to_array,
3284 * which mainly checks size.
3285 */
3286 char *e;
3287 int major = simple_strtoul(buf, &e, 10);
3288 int minor;
3289 dev_t dev;
3290 mdk_rdev_t *rdev;
3291 int err;
3292
3293 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3294 return -EINVAL;
3295 minor = simple_strtoul(e+1, &e, 10);
3296 if (*e && *e != '\n')
3297 return -EINVAL;
3298 dev = MKDEV(major, minor);
3299 if (major != MAJOR(dev) ||
3300 minor != MINOR(dev))
3301 return -EOVERFLOW;
3302
3303
3304 if (mddev->persistent) {
3305 rdev = md_import_device(dev, mddev->major_version,
3306 mddev->minor_version);
3307 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3308 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3309 mdk_rdev_t, same_set);
3310 err = super_types[mddev->major_version]
3311 .load_super(rdev, rdev0, mddev->minor_version);
3312 if (err < 0)
3313 goto out;
3314 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003315 } else if (mddev->external)
3316 rdev = md_import_device(dev, -2, -1);
3317 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003318 rdev = md_import_device(dev, -1, -1);
3319
3320 if (IS_ERR(rdev))
3321 return PTR_ERR(rdev);
3322 err = bind_rdev_to_array(rdev, mddev);
3323 out:
3324 if (err)
3325 export_rdev(rdev);
3326 return err ? err : len;
3327}
3328
3329static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003330__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003331
3332static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003333bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3334{
3335 char *end;
3336 unsigned long chunk, end_chunk;
3337
3338 if (!mddev->bitmap)
3339 goto out;
3340 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3341 while (*buf) {
3342 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3343 if (buf == end) break;
3344 if (*end == '-') { /* range */
3345 buf = end + 1;
3346 end_chunk = simple_strtoul(buf, &end, 0);
3347 if (buf == end) break;
3348 }
3349 if (*end && !isspace(*end)) break;
3350 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3351 buf = end;
3352 while (isspace(*buf)) buf++;
3353 }
3354 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3355out:
3356 return len;
3357}
3358
3359static struct md_sysfs_entry md_bitmap =
3360__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3361
3362static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003363size_show(mddev_t *mddev, char *page)
3364{
Andre Noll58c0fed2009-03-31 14:33:13 +11003365 return sprintf(page, "%llu\n",
3366 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003367}
3368
Andre Nolld71f9f82008-07-11 22:02:22 +10003369static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003370
3371static ssize_t
3372size_store(mddev_t *mddev, const char *buf, size_t len)
3373{
3374 /* If array is inactive, we can reduce the component size, but
3375 * not increase it (except from 0).
3376 * If array is active, we can try an on-line resize
3377 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003378 sector_t sectors;
3379 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003380
Andre Noll58c0fed2009-03-31 14:33:13 +11003381 if (err < 0)
3382 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003383 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003384 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003385 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003386 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003387 if (mddev->dev_sectors == 0 ||
3388 mddev->dev_sectors > sectors)
3389 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003390 else
3391 err = -ENOSPC;
3392 }
3393 return err ? err : len;
3394}
3395
3396static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003397__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003398
NeilBrown8bb93aa2006-01-06 00:20:50 -08003399
3400/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003401 * This is one of
3402 * 'none' for arrays with no metadata (good luck...)
3403 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003404 * or N.M for internally known formats
3405 */
3406static ssize_t
3407metadata_show(mddev_t *mddev, char *page)
3408{
3409 if (mddev->persistent)
3410 return sprintf(page, "%d.%d\n",
3411 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003412 else if (mddev->external)
3413 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003414 else
3415 return sprintf(page, "none\n");
3416}
3417
3418static ssize_t
3419metadata_store(mddev_t *mddev, const char *buf, size_t len)
3420{
3421 int major, minor;
3422 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003423 /* Changing the details of 'external' metadata is
3424 * always permitted. Otherwise there must be
3425 * no devices attached to the array.
3426 */
3427 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3428 ;
3429 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003430 return -EBUSY;
3431
3432 if (cmd_match(buf, "none")) {
3433 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003434 mddev->external = 0;
3435 mddev->major_version = 0;
3436 mddev->minor_version = 90;
3437 return len;
3438 }
3439 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003440 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003441 if (namelen >= sizeof(mddev->metadata_type))
3442 namelen = sizeof(mddev->metadata_type)-1;
3443 strncpy(mddev->metadata_type, buf+9, namelen);
3444 mddev->metadata_type[namelen] = 0;
3445 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3446 mddev->metadata_type[--namelen] = 0;
3447 mddev->persistent = 0;
3448 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003449 mddev->major_version = 0;
3450 mddev->minor_version = 90;
3451 return len;
3452 }
3453 major = simple_strtoul(buf, &e, 10);
3454 if (e==buf || *e != '.')
3455 return -EINVAL;
3456 buf = e+1;
3457 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003458 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003459 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003460 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003461 return -ENOENT;
3462 mddev->major_version = major;
3463 mddev->minor_version = minor;
3464 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003465 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003466 return len;
3467}
3468
3469static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003470__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003471
NeilBrowna35b0d62006-01-06 00:20:49 -08003472static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003473action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003474{
NeilBrown7eec3142005-11-08 21:39:44 -08003475 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003476 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3477 type = "frozen";
3478 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003479 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003480 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3481 type = "reshape";
3482 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003483 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3484 type = "resync";
3485 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3486 type = "check";
3487 else
3488 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003489 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003490 type = "recover";
3491 }
3492 return sprintf(page, "%s\n", type);
3493}
3494
3495static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003496action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003497{
NeilBrown7eec3142005-11-08 21:39:44 -08003498 if (!mddev->pers || !mddev->pers->sync_request)
3499 return -EINVAL;
3500
NeilBrownb6a9ce62009-05-26 09:41:17 +10003501 if (cmd_match(page, "frozen"))
3502 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3503 else
3504 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3505
3506 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003507 if (mddev->sync_thread) {
3508 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3509 md_unregister_thread(mddev->sync_thread);
3510 mddev->sync_thread = NULL;
3511 mddev->recovery = 0;
3512 }
NeilBrown03c902e2006-01-06 00:20:46 -08003513 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3514 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003515 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003516 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003517 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003518 else if (cmd_match(page, "recover")) {
3519 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3520 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3521 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003522 int err;
3523 if (mddev->pers->start_reshape == NULL)
3524 return -EINVAL;
3525 err = mddev->pers->start_reshape(mddev);
3526 if (err)
3527 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003528 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003529 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003530 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003531 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003532 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003533 return -EINVAL;
3534 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3535 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003536 }
NeilBrown03c902e2006-01-06 00:20:46 -08003537 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003538 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003539 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003540 return len;
3541}
3542
NeilBrown9d888832005-11-08 21:39:26 -08003543static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003544mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003545{
3546 return sprintf(page, "%llu\n",
3547 (unsigned long long) mddev->resync_mismatches);
3548}
3549
NeilBrown80ca3a42006-07-10 04:44:18 -07003550static struct md_sysfs_entry md_scan_mode =
3551__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003552
NeilBrown96de1e62005-11-08 21:39:39 -08003553
NeilBrown80ca3a42006-07-10 04:44:18 -07003554static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003555
NeilBrown88202a02006-01-06 00:21:36 -08003556static ssize_t
3557sync_min_show(mddev_t *mddev, char *page)
3558{
3559 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3560 mddev->sync_speed_min ? "local": "system");
3561}
3562
3563static ssize_t
3564sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3565{
3566 int min;
3567 char *e;
3568 if (strncmp(buf, "system", 6)==0) {
3569 mddev->sync_speed_min = 0;
3570 return len;
3571 }
3572 min = simple_strtoul(buf, &e, 10);
3573 if (buf == e || (*e && *e != '\n') || min <= 0)
3574 return -EINVAL;
3575 mddev->sync_speed_min = min;
3576 return len;
3577}
3578
3579static struct md_sysfs_entry md_sync_min =
3580__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3581
3582static ssize_t
3583sync_max_show(mddev_t *mddev, char *page)
3584{
3585 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3586 mddev->sync_speed_max ? "local": "system");
3587}
3588
3589static ssize_t
3590sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3591{
3592 int max;
3593 char *e;
3594 if (strncmp(buf, "system", 6)==0) {
3595 mddev->sync_speed_max = 0;
3596 return len;
3597 }
3598 max = simple_strtoul(buf, &e, 10);
3599 if (buf == e || (*e && *e != '\n') || max <= 0)
3600 return -EINVAL;
3601 mddev->sync_speed_max = max;
3602 return len;
3603}
3604
3605static struct md_sysfs_entry md_sync_max =
3606__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3607
Iustin Popd7f3d292007-10-16 23:30:54 -07003608static ssize_t
3609degraded_show(mddev_t *mddev, char *page)
3610{
3611 return sprintf(page, "%d\n", mddev->degraded);
3612}
3613static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003614
3615static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003616sync_force_parallel_show(mddev_t *mddev, char *page)
3617{
3618 return sprintf(page, "%d\n", mddev->parallel_resync);
3619}
3620
3621static ssize_t
3622sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3623{
3624 long n;
3625
3626 if (strict_strtol(buf, 10, &n))
3627 return -EINVAL;
3628
3629 if (n != 0 && n != 1)
3630 return -EINVAL;
3631
3632 mddev->parallel_resync = n;
3633
3634 if (mddev->sync_thread)
3635 wake_up(&resync_wait);
3636
3637 return len;
3638}
3639
3640/* force parallel resync, even with shared block devices */
3641static struct md_sysfs_entry md_sync_force_parallel =
3642__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3643 sync_force_parallel_show, sync_force_parallel_store);
3644
3645static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003646sync_speed_show(mddev_t *mddev, char *page)
3647{
3648 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003649 if (mddev->curr_resync == 0)
3650 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003651 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3652 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003653 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003654 db = resync - mddev->resync_mark_cnt;
3655 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003656}
3657
NeilBrown80ca3a42006-07-10 04:44:18 -07003658static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003659
3660static ssize_t
3661sync_completed_show(mddev_t *mddev, char *page)
3662{
Andre Noll58c0fed2009-03-31 14:33:13 +11003663 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003664
NeilBrownacb180b2009-04-14 16:28:34 +10003665 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3666 return sprintf(page, "none\n");
3667
NeilBrown88202a02006-01-06 00:21:36 -08003668 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003669 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003670 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003671 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003672
NeilBrownacb180b2009-04-14 16:28:34 +10003673 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003674 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003675}
3676
NeilBrown80ca3a42006-07-10 04:44:18 -07003677static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003678
NeilBrowne464eaf2006-03-27 01:18:14 -08003679static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003680min_sync_show(mddev_t *mddev, char *page)
3681{
3682 return sprintf(page, "%llu\n",
3683 (unsigned long long)mddev->resync_min);
3684}
3685static ssize_t
3686min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3687{
3688 unsigned long long min;
3689 if (strict_strtoull(buf, 10, &min))
3690 return -EINVAL;
3691 if (min > mddev->resync_max)
3692 return -EINVAL;
3693 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3694 return -EBUSY;
3695
3696 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003697 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003698 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003699 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003700 return -EINVAL;
3701 }
3702 mddev->resync_min = min;
3703
3704 return len;
3705}
3706
3707static struct md_sysfs_entry md_min_sync =
3708__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3709
3710static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003711max_sync_show(mddev_t *mddev, char *page)
3712{
3713 if (mddev->resync_max == MaxSector)
3714 return sprintf(page, "max\n");
3715 else
3716 return sprintf(page, "%llu\n",
3717 (unsigned long long)mddev->resync_max);
3718}
3719static ssize_t
3720max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3721{
3722 if (strncmp(buf, "max", 3) == 0)
3723 mddev->resync_max = MaxSector;
3724 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003725 unsigned long long max;
3726 if (strict_strtoull(buf, 10, &max))
3727 return -EINVAL;
3728 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003729 return -EINVAL;
3730 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003731 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003732 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3733 return -EBUSY;
3734
3735 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003736 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003737 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003738 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003739 return -EINVAL;
3740 }
3741 mddev->resync_max = max;
3742 }
3743 wake_up(&mddev->recovery_wait);
3744 return len;
3745}
3746
3747static struct md_sysfs_entry md_max_sync =
3748__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3749
3750static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003751suspend_lo_show(mddev_t *mddev, char *page)
3752{
3753 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3754}
3755
3756static ssize_t
3757suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3758{
3759 char *e;
3760 unsigned long long new = simple_strtoull(buf, &e, 10);
3761
NeilBrownb8d966e2009-07-01 11:14:04 +10003762 if (mddev->pers == NULL ||
3763 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003764 return -EINVAL;
3765 if (buf == e || (*e && *e != '\n'))
3766 return -EINVAL;
3767 if (new >= mddev->suspend_hi ||
3768 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3769 mddev->suspend_lo = new;
3770 mddev->pers->quiesce(mddev, 2);
3771 return len;
3772 } else
3773 return -EINVAL;
3774}
3775static struct md_sysfs_entry md_suspend_lo =
3776__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3777
3778
3779static ssize_t
3780suspend_hi_show(mddev_t *mddev, char *page)
3781{
3782 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3783}
3784
3785static ssize_t
3786suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3787{
3788 char *e;
3789 unsigned long long new = simple_strtoull(buf, &e, 10);
3790
NeilBrownb8d966e2009-07-01 11:14:04 +10003791 if (mddev->pers == NULL ||
3792 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003793 return -EINVAL;
3794 if (buf == e || (*e && *e != '\n'))
3795 return -EINVAL;
3796 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3797 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3798 mddev->suspend_hi = new;
3799 mddev->pers->quiesce(mddev, 1);
3800 mddev->pers->quiesce(mddev, 0);
3801 return len;
3802 } else
3803 return -EINVAL;
3804}
3805static struct md_sysfs_entry md_suspend_hi =
3806__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3807
NeilBrown08a02ec2007-05-09 02:35:38 -07003808static ssize_t
3809reshape_position_show(mddev_t *mddev, char *page)
3810{
3811 if (mddev->reshape_position != MaxSector)
3812 return sprintf(page, "%llu\n",
3813 (unsigned long long)mddev->reshape_position);
3814 strcpy(page, "none\n");
3815 return 5;
3816}
3817
3818static ssize_t
3819reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3820{
3821 char *e;
3822 unsigned long long new = simple_strtoull(buf, &e, 10);
3823 if (mddev->pers)
3824 return -EBUSY;
3825 if (buf == e || (*e && *e != '\n'))
3826 return -EINVAL;
3827 mddev->reshape_position = new;
3828 mddev->delta_disks = 0;
3829 mddev->new_level = mddev->level;
3830 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003831 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07003832 return len;
3833}
3834
3835static struct md_sysfs_entry md_reshape_position =
3836__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3837 reshape_position_store);
3838
Dan Williamsb522adc2009-03-31 15:00:31 +11003839static ssize_t
3840array_size_show(mddev_t *mddev, char *page)
3841{
3842 if (mddev->external_size)
3843 return sprintf(page, "%llu\n",
3844 (unsigned long long)mddev->array_sectors/2);
3845 else
3846 return sprintf(page, "default\n");
3847}
3848
3849static ssize_t
3850array_size_store(mddev_t *mddev, const char *buf, size_t len)
3851{
3852 sector_t sectors;
3853
3854 if (strncmp(buf, "default", 7) == 0) {
3855 if (mddev->pers)
3856 sectors = mddev->pers->size(mddev, 0, 0);
3857 else
3858 sectors = mddev->array_sectors;
3859
3860 mddev->external_size = 0;
3861 } else {
3862 if (strict_blocks_to_sectors(buf, &sectors) < 0)
3863 return -EINVAL;
3864 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10003865 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11003866
3867 mddev->external_size = 1;
3868 }
3869
3870 mddev->array_sectors = sectors;
3871 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10003872 if (mddev->pers)
3873 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11003874
3875 return len;
3876}
3877
3878static struct md_sysfs_entry md_array_size =
3879__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
3880 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08003881
NeilBrowneae17012005-11-08 21:39:23 -08003882static struct attribute *md_default_attrs[] = {
3883 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003884 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003885 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003886 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003887 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003888 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003889 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08003890 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003891 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003892 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003893 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11003894 &md_array_size.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003895 NULL,
3896};
3897
3898static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003899 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003900 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003901 &md_sync_min.attr,
3902 &md_sync_max.attr,
3903 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003904 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003905 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003906 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003907 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003908 &md_suspend_lo.attr,
3909 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003910 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003911 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003912 NULL,
3913};
NeilBrown411036f2005-11-08 21:39:40 -08003914static struct attribute_group md_redundancy_group = {
3915 .name = NULL,
3916 .attrs = md_redundancy_attrs,
3917};
3918
NeilBrowneae17012005-11-08 21:39:23 -08003919
3920static ssize_t
3921md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3922{
3923 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3924 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003925 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003926
3927 if (!entry->show)
3928 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003929 rv = mddev_lock(mddev);
3930 if (!rv) {
3931 rv = entry->show(mddev, page);
3932 mddev_unlock(mddev);
3933 }
NeilBrown96de1e62005-11-08 21:39:39 -08003934 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003935}
3936
3937static ssize_t
3938md_attr_store(struct kobject *kobj, struct attribute *attr,
3939 const char *page, size_t length)
3940{
3941 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3942 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003943 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003944
3945 if (!entry->store)
3946 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003947 if (!capable(CAP_SYS_ADMIN))
3948 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003949 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11003950 if (mddev->hold_active == UNTIL_IOCTL)
3951 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003952 if (!rv) {
3953 rv = entry->store(mddev, page, length);
3954 mddev_unlock(mddev);
3955 }
NeilBrown96de1e62005-11-08 21:39:39 -08003956 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003957}
3958
3959static void md_free(struct kobject *ko)
3960{
3961 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11003962
3963 if (mddev->sysfs_state)
3964 sysfs_put(mddev->sysfs_state);
3965
3966 if (mddev->gendisk) {
3967 del_gendisk(mddev->gendisk);
3968 put_disk(mddev->gendisk);
3969 }
3970 if (mddev->queue)
3971 blk_cleanup_queue(mddev->queue);
3972
NeilBrowneae17012005-11-08 21:39:23 -08003973 kfree(mddev);
3974}
3975
3976static struct sysfs_ops md_sysfs_ops = {
3977 .show = md_attr_show,
3978 .store = md_attr_store,
3979};
3980static struct kobj_type md_ktype = {
3981 .release = md_free,
3982 .sysfs_ops = &md_sysfs_ops,
3983 .default_attrs = md_default_attrs,
3984};
3985
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986int mdp_major = 0;
3987
Dan Williams5fd3a172009-03-04 00:57:25 -07003988static void mddev_delayed_delete(struct work_struct *ws)
3989{
3990 mddev_t *mddev = container_of(ws, mddev_t, del_work);
3991
3992 if (mddev->private == &md_redundancy_group) {
3993 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3994 if (mddev->sysfs_action)
3995 sysfs_put(mddev->sysfs_action);
3996 mddev->sysfs_action = NULL;
3997 mddev->private = NULL;
3998 }
3999 kobject_del(&mddev->kobj);
4000 kobject_put(&mddev->kobj);
4001}
4002
NeilBrownefeb53c2009-01-09 08:31:10 +11004003static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004005 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 mddev_t *mddev = mddev_find(dev);
4007 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004008 int partitioned;
4009 int shift;
4010 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004011 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
4013 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004014 return -ENODEV;
4015
4016 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4017 shift = partitioned ? MdpMinorShift : 0;
4018 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019
NeilBrownd3374822009-01-09 08:31:10 +11004020 /* wait for any previous instance if this device
4021 * to be completed removed (mddev_delayed_delete).
4022 */
4023 flush_scheduled_work();
4024
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004025 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004026 error = -EEXIST;
4027 if (mddev->gendisk)
4028 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004029
4030 if (name) {
4031 /* Need to ensure that 'name' is not a duplicate.
4032 */
4033 mddev_t *mddev2;
4034 spin_lock(&all_mddevs_lock);
4035
4036 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4037 if (mddev2->gendisk &&
4038 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4039 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004040 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004041 }
4042 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 }
NeilBrown8b765392009-01-09 08:31:08 +11004044
NeilBrown0909dc42009-07-01 12:27:21 +10004045 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004046 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004047 if (!mddev->queue)
4048 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004049 mddev->queue->queuedata = mddev;
4050
NeilBrown8b765392009-01-09 08:31:08 +11004051 /* Can be unlocked because the queue is new: no concurrency */
4052 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4053
NeilBrown409c57f2009-03-31 14:39:39 +11004054 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004055
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 disk = alloc_disk(1 << shift);
4057 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004058 blk_cleanup_queue(mddev->queue);
4059 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004060 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004062 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004064 if (name)
4065 strcpy(disk->disk_name, name);
4066 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004068 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 disk->fops = &md_fops;
4071 disk->private_data = mddev;
4072 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004073 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004074 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004075 * remove it now.
4076 */
4077 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 add_disk(disk);
4079 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004080 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4081 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004082 if (error) {
4083 /* This isn't possible, but as kobject_init_and_add is marked
4084 * __must_check, we must do something with the result
4085 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004086 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4087 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004088 error = 0;
4089 }
4090 abort:
4091 mutex_unlock(&disks_mutex);
4092 if (!error) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004093 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11004094 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
4095 }
NeilBrownd3374822009-01-09 08:31:10 +11004096 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004097 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004098}
4099
4100static struct kobject *md_probe(dev_t dev, int *part, void *data)
4101{
4102 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 return NULL;
4104}
4105
NeilBrownefeb53c2009-01-09 08:31:10 +11004106static int add_named_array(const char *val, struct kernel_param *kp)
4107{
4108 /* val must be "md_*" where * is not all digits.
4109 * We allocate an array with a large free minor number, and
4110 * set the name to val. val must not already be an active name.
4111 */
4112 int len = strlen(val);
4113 char buf[DISK_NAME_LEN];
4114
4115 while (len && val[len-1] == '\n')
4116 len--;
4117 if (len >= DISK_NAME_LEN)
4118 return -E2BIG;
4119 strlcpy(buf, val, len+1);
4120 if (strncmp(buf, "md_", 3) != 0)
4121 return -EINVAL;
4122 return md_alloc(0, buf);
4123}
4124
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125static void md_safemode_timeout(unsigned long data)
4126{
4127 mddev_t *mddev = (mddev_t *) data;
4128
Neil Brown0fd62b82008-06-28 08:31:36 +10004129 if (!atomic_read(&mddev->writes_pending)) {
4130 mddev->safemode = 1;
4131 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11004132 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 md_wakeup_thread(mddev->thread);
4135}
4136
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004137static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138
4139static int do_md_run(mddev_t * mddev)
4140{
NeilBrown2604b702006-01-06 00:20:36 -08004141 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 mdk_rdev_t *rdev;
4143 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08004144 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
NeilBrowna757e642005-04-16 15:26:42 -07004146 if (list_empty(&mddev->disks))
4147 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149
4150 if (mddev->pers)
4151 return -EBUSY;
4152
4153 /*
4154 * Analyze all RAID superblock(s)
4155 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004156 if (!mddev->raid_disks) {
4157 if (!mddev->persistent)
4158 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004159 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
NeilBrownd9d166c2006-01-06 00:20:51 -08004162 if (mddev->level != LEVEL_NONE)
4163 request_module("md-level-%d", mddev->level);
4164 else if (mddev->clevel[0])
4165 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166
4167 /*
4168 * Drop all container device buffers, from now on
4169 * the only valid external interface is through the md
4170 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004172 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004173 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 continue;
4175 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004176 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004177
4178 /* perform some consistency tests on the device.
4179 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004180 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004181 */
Andre Noll0f420352008-07-11 22:02:23 +10004182 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004183 if (mddev->dev_sectors &&
4184 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004185 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004186 printk("md: %s: data overlaps metadata\n",
4187 mdname(mddev));
4188 return -EINVAL;
4189 }
4190 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004191 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004192 > rdev->data_offset) {
4193 printk("md: %s: metadata overlaps data\n",
4194 mdname(mddev));
4195 return -EINVAL;
4196 }
4197 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004198 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 }
4200
4201 md_probe(mddev->unit, NULL, NULL);
4202 disk = mddev->gendisk;
4203 if (!disk)
4204 return -ENOMEM;
4205
4206 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004207 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004208 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004210 if (mddev->level != LEVEL_NONE)
4211 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4212 mddev->level);
4213 else
4214 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4215 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 return -EINVAL;
4217 }
NeilBrown2604b702006-01-06 00:20:36 -08004218 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004220 if (mddev->level != pers->level) {
4221 mddev->level = pers->level;
4222 mddev->new_level = pers->level;
4223 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004224 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225
NeilBrownf6705572006-03-27 01:18:11 -08004226 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004227 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004228 /* This personality cannot handle reshaping... */
4229 mddev->pers = NULL;
4230 module_put(pers->owner);
4231 return -EINVAL;
4232 }
4233
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004234 if (pers->sync_request) {
4235 /* Warn if this is a potentially silly
4236 * configuration.
4237 */
4238 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4239 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004240 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004241
4242 list_for_each_entry(rdev, &mddev->disks, same_set)
4243 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004244 if (rdev < rdev2 &&
4245 rdev->bdev->bd_contains ==
4246 rdev2->bdev->bd_contains) {
4247 printk(KERN_WARNING
4248 "%s: WARNING: %s appears to be"
4249 " on the same physical disk as"
4250 " %s.\n",
4251 mdname(mddev),
4252 bdevname(rdev->bdev,b),
4253 bdevname(rdev2->bdev,b2));
4254 warned = 1;
4255 }
4256 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004257
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004258 if (warned)
4259 printk(KERN_WARNING
4260 "True protection against single-disk"
4261 " failure might be compromised.\n");
4262 }
4263
NeilBrown657390d2005-08-26 18:34:16 -07004264 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004265 /* may be over-ridden by personality */
4266 mddev->resync_max_sectors = mddev->dev_sectors;
4267
NeilBrowna9701a32005-11-08 21:39:34 -08004268 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004269 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270
NeilBrownf91de922005-11-08 21:39:36 -08004271 if (start_readonly)
4272 mddev->ro = 2; /* read-only, but switch on first write */
4273
NeilBrownb15c2e52006-01-06 00:20:16 -08004274 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004275 if (err)
4276 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004277 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4278 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4279 " but 'external_size' not in effect?\n", __func__);
4280 printk(KERN_ERR
4281 "md: invalid array_size %llu > default size %llu\n",
4282 (unsigned long long)mddev->array_sectors / 2,
4283 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4284 err = -EINVAL;
4285 mddev->pers->stop(mddev);
4286 }
4287 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004288 err = bitmap_create(mddev);
4289 if (err) {
4290 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4291 mdname(mddev), err);
4292 mddev->pers->stop(mddev);
4293 }
4294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 module_put(mddev->pers->owner);
4297 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004298 bitmap_destroy(mddev);
4299 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004301 if (mddev->pers->sync_request) {
4302 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4303 printk(KERN_WARNING
4304 "md: cannot register extra attributes for %s\n",
4305 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004306 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004307 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004308 mddev->ro = 0;
4309
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 atomic_set(&mddev->writes_pending,0);
4311 mddev->safemode = 0;
4312 mddev->safemode_timer.function = md_safemode_timeout;
4313 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004314 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004316
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004317 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004318 if (rdev->raid_disk >= 0) {
4319 char nm[20];
4320 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004321 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4322 printk("md: cannot register %s for %s\n",
4323 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325
4326 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4327
NeilBrown850b2b42006-10-03 01:15:46 -07004328 if (mddev->flags)
4329 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
Andre Nollf233ea52008-07-21 17:05:22 +10004331 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004333 /* If there is a partially-recovered drive we need to
4334 * start recovery here. If we leave it to md_check_recovery,
4335 * it will remove the drives and not do the right thing
4336 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07004337 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004338 int spares = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004339 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004340 if (rdev->raid_disk >= 0 &&
4341 !test_bit(In_sync, &rdev->flags) &&
4342 !test_bit(Faulty, &rdev->flags))
4343 /* complete an interrupted recovery */
4344 spares++;
4345 if (spares && mddev->pers->sync_request) {
4346 mddev->recovery = 0;
4347 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4348 mddev->sync_thread = md_register_thread(md_do_sync,
4349 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10004350 "resync");
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004351 if (!mddev->sync_thread) {
4352 printk(KERN_ERR "%s: could not start resync"
4353 " thread...\n",
4354 mdname(mddev));
4355 /* leave the spares where they are, it shouldn't hurt */
4356 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07004357 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004358 }
4359 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07004360 md_wakeup_thread(mddev->thread);
4361 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004362
NeilBrown449aad32009-08-03 10:59:58 +10004363 revalidate_disk(mddev->gendisk);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004364 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08004365 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004366 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004367 if (mddev->sysfs_action)
4368 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004369 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09004370 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 return 0;
4372}
4373
4374static int restart_array(mddev_t *mddev)
4375{
4376 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377
Andre Noll80fab1d2008-07-11 22:02:21 +10004378 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004380 return -ENXIO;
4381 if (!mddev->pers)
4382 return -EINVAL;
4383 if (!mddev->ro)
4384 return -EBUSY;
4385 mddev->safemode = 0;
4386 mddev->ro = 0;
4387 set_disk_ro(disk, 0);
4388 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4389 mdname(mddev));
4390 /* Kick recovery or resync if necessary */
4391 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4392 md_wakeup_thread(mddev->thread);
4393 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004394 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396}
4397
NeilBrownacc55e22006-06-26 00:27:47 -07004398/* similar to deny_write_access, but accounts for our holding a reference
4399 * to the file ourselves */
4400static int deny_bitmap_write_access(struct file * file)
4401{
4402 struct inode *inode = file->f_mapping->host;
4403
4404 spin_lock(&inode->i_lock);
4405 if (atomic_read(&inode->i_writecount) > 1) {
4406 spin_unlock(&inode->i_lock);
4407 return -ETXTBSY;
4408 }
4409 atomic_set(&inode->i_writecount, -1);
4410 spin_unlock(&inode->i_lock);
4411
4412 return 0;
4413}
4414
4415static void restore_bitmap_write_access(struct file *file)
4416{
4417 struct inode *inode = file->f_mapping->host;
4418
4419 spin_lock(&inode->i_lock);
4420 atomic_set(&inode->i_writecount, 1);
4421 spin_unlock(&inode->i_lock);
4422}
4423
NeilBrown9e653b62006-06-26 00:27:58 -07004424/* mode:
4425 * 0 - completely stop and dis-assemble array
4426 * 1 - switch to readonly
4427 * 2 - stop but do not disassemble array
4428 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004429static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430{
4431 int err = 0;
4432 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004433 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
NeilBrownc8c00a62009-08-10 12:50:52 +10004435 mutex_lock(&mddev->open_mutex);
NeilBrownf2ea68c2008-07-21 17:05:25 +10004436 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004437 printk("md: %s still in use.\n",mdname(mddev));
NeilBrownc8c00a62009-08-10 12:50:52 +10004438 err = -EBUSY;
4439 } else if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440
4441 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004442 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4444 md_unregister_thread(mddev->sync_thread);
4445 mddev->sync_thread = NULL;
4446 }
4447
4448 del_timer_sync(&mddev->safemode_timer);
4449
NeilBrown9e653b62006-06-26 00:27:58 -07004450 switch(mode) {
4451 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004453 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 goto out;
4455 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004456 break;
4457 case 0: /* disassemble */
4458 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004459 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004460 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 if (mddev->ro)
4462 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004463
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004465 mddev->queue->merge_bvec_fn = NULL;
4466 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004467 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 module_put(mddev->pers->owner);
Dan Williams5fd3a172009-03-04 00:57:25 -07004469 if (mddev->pers->sync_request)
4470 mddev->private = &md_redundancy_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004472 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004473 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004474
NeilBrownc4647292009-05-07 12:51:06 +10004475 list_for_each_entry(rdev, &mddev->disks, same_set)
4476 if (rdev->raid_disk >= 0) {
4477 char nm[20];
4478 sprintf(nm, "rd%d", rdev->raid_disk);
4479 sysfs_remove_link(&mddev->kobj, nm);
4480 }
4481
NeilBrown0d4ca602006-12-10 02:20:44 -08004482 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004483 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08004484
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 if (mddev->ro)
4486 mddev->ro = 0;
4487 }
NeilBrown850b2b42006-10-03 01:15:46 -07004488 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 /* mark array as shutdown cleanly */
4490 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07004491 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 }
NeilBrown9e653b62006-06-26 00:27:58 -07004493 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004495 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown80ffb3c2009-08-18 10:35:26 +10004496 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 }
NeilBrownc8c00a62009-08-10 12:50:52 +10004498out:
4499 mutex_unlock(&mddev->open_mutex);
4500 if (err)
4501 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 /*
4503 * Free resources if final stop
4504 */
NeilBrown9e653b62006-06-26 00:27:58 -07004505 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004506
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4508
NeilBrown978f9462006-02-02 14:28:05 -08004509 bitmap_destroy(mddev);
4510 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07004511 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08004512 fput(mddev->bitmap_file);
4513 mddev->bitmap_file = NULL;
4514 }
4515 mddev->bitmap_offset = 0;
4516
NeilBrown177a99b2008-02-06 01:39:56 -08004517 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07004518 flush_scheduled_work();
4519
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 export_array(mddev);
4521
Andre Nollf233ea52008-07-21 17:05:22 +10004522 mddev->array_sectors = 0;
Dan Williamsb522adc2009-03-31 15:00:31 +11004523 mddev->external_size = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004524 mddev->dev_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004525 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004526 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004527 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004528 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004529 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004530 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004531 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004532 mddev->level = LEVEL_NONE;
4533 mddev->clevel[0] = 0;
4534 mddev->flags = 0;
4535 mddev->ro = 0;
4536 mddev->metadata_type[0] = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10004537 mddev->chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004538 mddev->ctime = mddev->utime = 0;
4539 mddev->layout = 0;
4540 mddev->max_disks = 0;
4541 mddev->events = 0;
4542 mddev->delta_disks = 0;
4543 mddev->new_level = LEVEL_NONE;
4544 mddev->new_layout = 0;
Andre Noll664e7c42009-06-18 08:45:27 +10004545 mddev->new_chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004546 mddev->curr_resync = 0;
4547 mddev->resync_mismatches = 0;
4548 mddev->suspend_lo = mddev->suspend_hi = 0;
4549 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4550 mddev->recovery = 0;
4551 mddev->in_sync = 0;
4552 mddev->changed = 0;
4553 mddev->degraded = 0;
4554 mddev->barriers_work = 0;
4555 mddev->safemode = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004556 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004557 if (mddev->hold_active == UNTIL_STOP)
4558 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004559
NeilBrowna8a55c32006-06-26 00:27:39 -07004560 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4562 mdname(mddev));
4563 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004564 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004565 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004566 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 return err;
4568}
4569
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004570#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571static void autorun_array(mddev_t *mddev)
4572{
4573 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 int err;
4575
NeilBrowna757e642005-04-16 15:26:42 -07004576 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
4579 printk(KERN_INFO "md: running: ");
4580
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004581 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 char b[BDEVNAME_SIZE];
4583 printk("<%s>", bdevname(rdev->bdev,b));
4584 }
4585 printk("\n");
4586
NeilBrownd710e132008-10-13 11:55:12 +11004587 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 if (err) {
4589 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004590 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 }
4592}
4593
4594/*
4595 * lets try to run arrays based on all disks that have arrived
4596 * until now. (those are in pending_raid_disks)
4597 *
4598 * the method: pick the first pending disk, collect all disks with
4599 * the same UUID, remove all from the pending list and put them into
4600 * the 'same_array' list. Then order this list based on superblock
4601 * update time (freshest comes first), kick out 'old' disks and
4602 * compare superblocks. If everything's fine then run it.
4603 *
4604 * If "unit" is allocated, then bump its reference count
4605 */
4606static void autorun_devices(int part)
4607{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004608 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 mddev_t *mddev;
4610 char b[BDEVNAME_SIZE];
4611
4612 printk(KERN_INFO "md: autorun ...\n");
4613 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004614 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004616 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 rdev0 = list_entry(pending_raid_disks.next,
4618 mdk_rdev_t, same_set);
4619
4620 printk(KERN_INFO "md: considering %s ...\n",
4621 bdevname(rdev0->bdev,b));
4622 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004623 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 if (super_90_load(rdev, rdev0, 0) >= 0) {
4625 printk(KERN_INFO "md: adding %s ...\n",
4626 bdevname(rdev->bdev,b));
4627 list_move(&rdev->same_set, &candidates);
4628 }
4629 /*
4630 * now we have a set of devices, with all of them having
4631 * mostly sane superblocks. It's time to allocate the
4632 * mddev.
4633 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004634 if (part) {
4635 dev = MKDEV(mdp_major,
4636 rdev0->preferred_minor << MdpMinorShift);
4637 unit = MINOR(dev) >> MdpMinorShift;
4638 } else {
4639 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4640 unit = MINOR(dev);
4641 }
4642 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4644 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4645 break;
4646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647
4648 md_probe(dev, NULL, NULL);
4649 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004650 if (!mddev || !mddev->gendisk) {
4651 if (mddev)
4652 mddev_put(mddev);
4653 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 "md: cannot allocate memory for md drive.\n");
4655 break;
4656 }
4657 if (mddev_lock(mddev))
4658 printk(KERN_WARNING "md: %s locked, cannot run\n",
4659 mdname(mddev));
4660 else if (mddev->raid_disks || mddev->major_version
4661 || !list_empty(&mddev->disks)) {
4662 printk(KERN_WARNING
4663 "md: %s already running, cannot run %s\n",
4664 mdname(mddev), bdevname(rdev0->bdev,b));
4665 mddev_unlock(mddev);
4666 } else {
4667 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004668 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004669 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 list_del_init(&rdev->same_set);
4671 if (bind_rdev_to_array(rdev, mddev))
4672 export_rdev(rdev);
4673 }
4674 autorun_array(mddev);
4675 mddev_unlock(mddev);
4676 }
4677 /* on success, candidates will be empty, on error
4678 * it won't...
4679 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004680 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004681 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 mddev_put(mddev);
4685 }
4686 printk(KERN_INFO "md: ... autorun DONE.\n");
4687}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004688#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690static int get_version(void __user * arg)
4691{
4692 mdu_version_t ver;
4693
4694 ver.major = MD_MAJOR_VERSION;
4695 ver.minor = MD_MINOR_VERSION;
4696 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4697
4698 if (copy_to_user(arg, &ver, sizeof(ver)))
4699 return -EFAULT;
4700
4701 return 0;
4702}
4703
4704static int get_array_info(mddev_t * mddev, void __user * arg)
4705{
4706 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004707 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709
NeilBrowna9f326e2009-09-23 18:06:41 +10004710 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004711 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004713 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 failed++;
4715 else {
4716 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004717 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004718 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 else
4720 spare++;
4721 }
4722 }
4723
4724 info.major_version = mddev->major_version;
4725 info.minor_version = mddev->minor_version;
4726 info.patch_version = MD_PATCHLEVEL_VERSION;
4727 info.ctime = mddev->ctime;
4728 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004729 info.size = mddev->dev_sectors / 2;
4730 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004731 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 info.nr_disks = nr;
4733 info.raid_disks = mddev->raid_disks;
4734 info.md_minor = mddev->md_minor;
4735 info.not_persistent= !mddev->persistent;
4736
4737 info.utime = mddev->utime;
4738 info.state = 0;
4739 if (mddev->in_sync)
4740 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07004741 if (mddev->bitmap && mddev->bitmap_offset)
4742 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004743 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 info.working_disks = working;
4745 info.failed_disks = failed;
4746 info.spare_disks = spare;
4747
4748 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004749 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750
4751 if (copy_to_user(arg, &info, sizeof(info)))
4752 return -EFAULT;
4753
4754 return 0;
4755}
4756
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004757static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004758{
4759 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4760 char *ptr, *buf = NULL;
4761 int err = -ENOMEM;
4762
Dan Williamsb5470dc2008-06-27 21:44:04 -07004763 if (md_allow_write(mddev))
4764 file = kmalloc(sizeof(*file), GFP_NOIO);
4765 else
4766 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004767
NeilBrown32a76272005-06-21 17:17:14 -07004768 if (!file)
4769 goto out;
4770
4771 /* bitmap disabled, zero the first byte and copy out */
4772 if (!mddev->bitmap || !mddev->bitmap->file) {
4773 file->pathname[0] = '\0';
4774 goto copy_out;
4775 }
4776
4777 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4778 if (!buf)
4779 goto out;
4780
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004781 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4782 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004783 goto out;
4784
4785 strcpy(file->pathname, ptr);
4786
4787copy_out:
4788 err = 0;
4789 if (copy_to_user(arg, file, sizeof(*file)))
4790 err = -EFAULT;
4791out:
4792 kfree(buf);
4793 kfree(file);
4794 return err;
4795}
4796
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797static int get_disk_info(mddev_t * mddev, void __user * arg)
4798{
4799 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800 mdk_rdev_t *rdev;
4801
4802 if (copy_from_user(&info, arg, sizeof(info)))
4803 return -EFAULT;
4804
Andre Noll26ef3792008-07-11 22:02:21 +10004805 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 if (rdev) {
4807 info.major = MAJOR(rdev->bdev->bd_dev);
4808 info.minor = MINOR(rdev->bdev->bd_dev);
4809 info.raid_disk = rdev->raid_disk;
4810 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004811 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004813 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 info.state |= (1<<MD_DISK_ACTIVE);
4815 info.state |= (1<<MD_DISK_SYNC);
4816 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004817 if (test_bit(WriteMostly, &rdev->flags))
4818 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 } else {
4820 info.major = info.minor = 0;
4821 info.raid_disk = -1;
4822 info.state = (1<<MD_DISK_REMOVED);
4823 }
4824
4825 if (copy_to_user(arg, &info, sizeof(info)))
4826 return -EFAULT;
4827
4828 return 0;
4829}
4830
4831static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4832{
4833 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4834 mdk_rdev_t *rdev;
4835 dev_t dev = MKDEV(info->major,info->minor);
4836
4837 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4838 return -EOVERFLOW;
4839
4840 if (!mddev->raid_disks) {
4841 int err;
4842 /* expecting a device which has a superblock */
4843 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4844 if (IS_ERR(rdev)) {
4845 printk(KERN_WARNING
4846 "md: md_import_device returned %ld\n",
4847 PTR_ERR(rdev));
4848 return PTR_ERR(rdev);
4849 }
4850 if (!list_empty(&mddev->disks)) {
4851 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4852 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10004853 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 .load_super(rdev, rdev0, mddev->minor_version);
4855 if (err < 0) {
4856 printk(KERN_WARNING
4857 "md: %s has different UUID to %s\n",
4858 bdevname(rdev->bdev,b),
4859 bdevname(rdev0->bdev,b2));
4860 export_rdev(rdev);
4861 return -EINVAL;
4862 }
4863 }
4864 err = bind_rdev_to_array(rdev, mddev);
4865 if (err)
4866 export_rdev(rdev);
4867 return err;
4868 }
4869
4870 /*
4871 * add_new_disk can be used once the array is assembled
4872 * to add "hot spares". They must already have a superblock
4873 * written
4874 */
4875 if (mddev->pers) {
4876 int err;
4877 if (!mddev->pers->hot_add_disk) {
4878 printk(KERN_WARNING
4879 "%s: personality does not support diskops!\n",
4880 mdname(mddev));
4881 return -EINVAL;
4882 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004883 if (mddev->persistent)
4884 rdev = md_import_device(dev, mddev->major_version,
4885 mddev->minor_version);
4886 else
4887 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 if (IS_ERR(rdev)) {
4889 printk(KERN_WARNING
4890 "md: md_import_device returned %ld\n",
4891 PTR_ERR(rdev));
4892 return PTR_ERR(rdev);
4893 }
NeilBrown41158c72005-06-21 17:17:25 -07004894 /* set save_raid_disk if appropriate */
4895 if (!mddev->persistent) {
4896 if (info->state & (1<<MD_DISK_SYNC) &&
4897 info->raid_disk < mddev->raid_disks)
4898 rdev->raid_disk = info->raid_disk;
4899 else
4900 rdev->raid_disk = -1;
4901 } else
4902 super_types[mddev->major_version].
4903 validate_super(mddev, rdev);
4904 rdev->saved_raid_disk = rdev->raid_disk;
4905
NeilBrownb2d444d2005-11-08 21:39:31 -08004906 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004907 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4908 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11004909 else
4910 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004911
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 rdev->raid_disk = -1;
4913 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004914 if (!err && !mddev->pers->hot_remove_disk) {
4915 /* If there is hot_add_disk but no hot_remove_disk
4916 * then added disks for geometry changes,
4917 * and should be added immediately.
4918 */
4919 super_types[mddev->major_version].
4920 validate_super(mddev, rdev);
4921 err = mddev->pers->hot_add_disk(mddev, rdev);
4922 if (err)
4923 unbind_rdev_from_array(rdev);
4924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 if (err)
4926 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004927 else
NeilBrown3c0ee632008-10-21 13:25:28 +11004928 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07004929
NeilBrown17571282006-12-10 02:20:52 -08004930 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004931 if (mddev->degraded)
4932 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004933 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004934 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 return err;
4936 }
4937
4938 /* otherwise, add_new_disk is only allowed
4939 * for major_version==0 superblocks
4940 */
4941 if (mddev->major_version != 0) {
4942 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4943 mdname(mddev));
4944 return -EINVAL;
4945 }
4946
4947 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4948 int err;
NeilBrownd710e132008-10-13 11:55:12 +11004949 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 if (IS_ERR(rdev)) {
4951 printk(KERN_WARNING
4952 "md: error, md_import_device() returned %ld\n",
4953 PTR_ERR(rdev));
4954 return PTR_ERR(rdev);
4955 }
4956 rdev->desc_nr = info->number;
4957 if (info->raid_disk < mddev->raid_disks)
4958 rdev->raid_disk = info->raid_disk;
4959 else
4960 rdev->raid_disk = -1;
4961
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004963 if (info->state & (1<<MD_DISK_SYNC))
4964 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004966 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4967 set_bit(WriteMostly, &rdev->flags);
4968
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 if (!mddev->persistent) {
4970 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10004971 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 } else
Andre Noll0f420352008-07-11 22:02:23 +10004973 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10004974 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975
NeilBrown2bf071b2006-01-06 00:20:55 -08004976 err = bind_rdev_to_array(rdev, mddev);
4977 if (err) {
4978 export_rdev(rdev);
4979 return err;
4980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 }
4982
4983 return 0;
4984}
4985
4986static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4987{
4988 char b[BDEVNAME_SIZE];
4989 mdk_rdev_t *rdev;
4990
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 rdev = find_rdev(mddev, dev);
4992 if (!rdev)
4993 return -ENXIO;
4994
4995 if (rdev->raid_disk >= 0)
4996 goto busy;
4997
4998 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07004999 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005000 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001
5002 return 0;
5003busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005004 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 bdevname(rdev->bdev,b), mdname(mddev));
5006 return -EBUSY;
5007}
5008
5009static int hot_add_disk(mddev_t * mddev, dev_t dev)
5010{
5011 char b[BDEVNAME_SIZE];
5012 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 mdk_rdev_t *rdev;
5014
5015 if (!mddev->pers)
5016 return -ENODEV;
5017
5018 if (mddev->major_version != 0) {
5019 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5020 " version-0 superblocks.\n",
5021 mdname(mddev));
5022 return -EINVAL;
5023 }
5024 if (!mddev->pers->hot_add_disk) {
5025 printk(KERN_WARNING
5026 "%s: personality does not support diskops!\n",
5027 mdname(mddev));
5028 return -EINVAL;
5029 }
5030
NeilBrownd710e132008-10-13 11:55:12 +11005031 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 if (IS_ERR(rdev)) {
5033 printk(KERN_WARNING
5034 "md: error, md_import_device() returned %ld\n",
5035 PTR_ERR(rdev));
5036 return -EINVAL;
5037 }
5038
5039 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10005040 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 else
Andre Noll0f420352008-07-11 22:02:23 +10005042 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043
NeilBrown8190e752009-06-18 08:48:58 +10005044 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045
NeilBrownb2d444d2005-11-08 21:39:31 -08005046 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 printk(KERN_WARNING
5048 "md: can not hot-add faulty %s disk to %s!\n",
5049 bdevname(rdev->bdev,b), mdname(mddev));
5050 err = -EINVAL;
5051 goto abort_export;
5052 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005053 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005055 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005056 err = bind_rdev_to_array(rdev, mddev);
5057 if (err)
5058 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059
5060 /*
5061 * The rest should better be atomic, we can have disk failures
5062 * noticed in interrupt contexts ...
5063 */
5064
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 rdev->raid_disk = -1;
5066
NeilBrown850b2b42006-10-03 01:15:46 -07005067 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068
5069 /*
5070 * Kick recovery, maybe this spare has to be added to the
5071 * array immediately.
5072 */
5073 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5074 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005075 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 return 0;
5077
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078abort_export:
5079 export_rdev(rdev);
5080 return err;
5081}
5082
NeilBrown32a76272005-06-21 17:17:14 -07005083static int set_bitmap_file(mddev_t *mddev, int fd)
5084{
5085 int err;
5086
NeilBrown36fa3062005-09-09 16:23:45 -07005087 if (mddev->pers) {
5088 if (!mddev->pers->quiesce)
5089 return -EBUSY;
5090 if (mddev->recovery || mddev->sync_thread)
5091 return -EBUSY;
5092 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005093 }
5094
NeilBrown36fa3062005-09-09 16:23:45 -07005095
5096 if (fd >= 0) {
5097 if (mddev->bitmap)
5098 return -EEXIST; /* cannot add when bitmap is present */
5099 mddev->bitmap_file = fget(fd);
5100
5101 if (mddev->bitmap_file == NULL) {
5102 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5103 mdname(mddev));
5104 return -EBADF;
5105 }
5106
5107 err = deny_bitmap_write_access(mddev->bitmap_file);
5108 if (err) {
5109 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5110 mdname(mddev));
5111 fput(mddev->bitmap_file);
5112 mddev->bitmap_file = NULL;
5113 return err;
5114 }
NeilBrowna654b9d82005-06-21 17:17:27 -07005115 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005116 } else if (mddev->bitmap == NULL)
5117 return -ENOENT; /* cannot remove what isn't there */
5118 err = 0;
5119 if (mddev->pers) {
5120 mddev->pers->quiesce(mddev, 1);
5121 if (fd >= 0)
5122 err = bitmap_create(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07005123 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005124 bitmap_destroy(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07005125 fd = -1; /* make sure to put the file */
5126 }
NeilBrown36fa3062005-09-09 16:23:45 -07005127 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab322006-06-26 00:27:43 -07005128 }
5129 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07005130 if (mddev->bitmap_file) {
5131 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07005132 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07005133 }
NeilBrown36fa3062005-09-09 16:23:45 -07005134 mddev->bitmap_file = NULL;
5135 }
5136
NeilBrown32a76272005-06-21 17:17:14 -07005137 return err;
5138}
5139
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140/*
5141 * set_array_info is used two different ways
5142 * The original usage is when creating a new array.
5143 * In this usage, raid_disks is > 0 and it together with
5144 * level, size, not_persistent,layout,chunksize determine the
5145 * shape of the array.
5146 * This will always create an array with a type-0.90.0 superblock.
5147 * The newer usage is when assembling an array.
5148 * In this case raid_disks will be 0, and the major_version field is
5149 * use to determine which style super-blocks are to be found on the devices.
5150 * The minor and patch _version numbers are also kept incase the
5151 * super_block handler wishes to interpret them.
5152 */
5153static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5154{
5155
5156 if (info->raid_disks == 0) {
5157 /* just setting version number for superblock loading */
5158 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005159 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 super_types[info->major_version].name == NULL) {
5161 /* maybe try to auto-load a module? */
5162 printk(KERN_INFO
5163 "md: superblock version %d not known\n",
5164 info->major_version);
5165 return -EINVAL;
5166 }
5167 mddev->major_version = info->major_version;
5168 mddev->minor_version = info->minor_version;
5169 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005170 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 return 0;
5172 }
5173 mddev->major_version = MD_MAJOR_VERSION;
5174 mddev->minor_version = MD_MINOR_VERSION;
5175 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5176 mddev->ctime = get_seconds();
5177
5178 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005179 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005180 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 mddev->raid_disks = info->raid_disks;
5182 /* don't set md_minor, it is determined by which /dev/md* was
5183 * openned
5184 */
5185 if (info->state & (1<<MD_SB_CLEAN))
5186 mddev->recovery_cp = MaxSector;
5187 else
5188 mddev->recovery_cp = 0;
5189 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005190 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191
5192 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005193 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194
5195 mddev->max_disks = MD_SB_DISKS;
5196
NeilBrowne6910632008-02-06 01:39:51 -08005197 if (mddev->persistent)
5198 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005199 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200
NeilBrownb2a27032005-11-28 13:44:12 -08005201 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
5202 mddev->bitmap_offset = 0;
5203
NeilBrownf6705572006-03-27 01:18:11 -08005204 mddev->reshape_position = MaxSector;
5205
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 /*
5207 * Generate a 128 bit UUID
5208 */
5209 get_random_bytes(mddev->uuid, 16);
5210
NeilBrownf6705572006-03-27 01:18:11 -08005211 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005212 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005213 mddev->new_layout = mddev->layout;
5214 mddev->delta_disks = 0;
5215
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 return 0;
5217}
5218
Dan Williams1f403622009-03-31 14:59:03 +11005219void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5220{
Dan Williamsb522adc2009-03-31 15:00:31 +11005221 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5222
5223 if (mddev->external_size)
5224 return;
5225
Dan Williams1f403622009-03-31 14:59:03 +11005226 mddev->array_sectors = array_sectors;
5227}
5228EXPORT_SYMBOL(md_set_array_sectors);
5229
Andre Nolld71f9f82008-07-11 22:02:22 +10005230static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005231{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005232 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005233 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005234 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005235
5236 if (mddev->pers->resize == NULL)
5237 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005238 /* The "num_sectors" is the number of sectors of each device that
5239 * is used. This can only make sense for arrays with redundancy.
5240 * linear and raid0 always use whatever space is available. We can only
5241 * consider changing this number if no resync or reconstruction is
5242 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005243 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005244 * of each device. If num_sectors is zero, we find the largest size
5245 * that fits.
5246
NeilBrowna35b0d62006-01-06 00:20:49 -08005247 */
5248 if (mddev->sync_thread)
5249 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005250 if (mddev->bitmap)
5251 /* Sorry, cannot grow a bitmap yet, just remove it,
5252 * grow, and re-add.
5253 */
5254 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005255 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005256 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005257
Andre Nolld71f9f82008-07-11 22:02:22 +10005258 if (fit && (num_sectors == 0 || num_sectors > avail))
5259 num_sectors = avail;
5260 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005261 return -ENOSPC;
5262 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005263 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005264 if (!rv)
5265 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005266 return rv;
5267}
5268
NeilBrownda943b992006-01-06 00:20:54 -08005269static int update_raid_disks(mddev_t *mddev, int raid_disks)
5270{
5271 int rv;
5272 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005273 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005274 return -EINVAL;
5275 if (raid_disks <= 0 ||
5276 raid_disks >= mddev->max_disks)
5277 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005278 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005279 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005280 mddev->delta_disks = raid_disks - mddev->raid_disks;
5281
5282 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005283 return rv;
5284}
5285
5286
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287/*
5288 * update_array_info is used to change the configuration of an
5289 * on-line array.
5290 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5291 * fields in the info are checked against the array.
5292 * Any differences that cannot be handled will cause an error.
5293 * Normally, only one change can be managed at a time.
5294 */
5295static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5296{
5297 int rv = 0;
5298 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005299 int state = 0;
5300
5301 /* calculate expected state,ignoring low bits */
5302 if (mddev->bitmap && mddev->bitmap_offset)
5303 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304
5305 if (mddev->major_version != info->major_version ||
5306 mddev->minor_version != info->minor_version ||
5307/* mddev->patch_version != info->patch_version || */
5308 mddev->ctime != info->ctime ||
5309 mddev->level != info->level ||
5310/* mddev->layout != info->layout || */
5311 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005312 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005313 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5314 ((state^info->state) & 0xfffffe00)
5315 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 return -EINVAL;
5317 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005318 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5319 cnt++;
5320 if (mddev->raid_disks != info->raid_disks)
5321 cnt++;
5322 if (mddev->layout != info->layout)
5323 cnt++;
5324 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5325 cnt++;
5326 if (cnt == 0)
5327 return 0;
5328 if (cnt > 1)
5329 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330
5331 if (mddev->layout != info->layout) {
5332 /* Change layout
5333 * we don't need to do anything at the md level, the
5334 * personality will take care of it all.
5335 */
NeilBrown50ac1682009-06-18 08:47:55 +10005336 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005338 else {
5339 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005340 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005341 if (rv)
5342 mddev->new_layout = mddev->layout;
5343 return rv;
5344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005346 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005347 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348
NeilBrownda943b992006-01-06 00:20:54 -08005349 if (mddev->raid_disks != info->raid_disks)
5350 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351
NeilBrown36fa3062005-09-09 16:23:45 -07005352 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5353 if (mddev->pers->quiesce == NULL)
5354 return -EINVAL;
5355 if (mddev->recovery || mddev->sync_thread)
5356 return -EBUSY;
5357 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5358 /* add the bitmap */
5359 if (mddev->bitmap)
5360 return -EEXIST;
5361 if (mddev->default_bitmap_offset == 0)
5362 return -EINVAL;
5363 mddev->bitmap_offset = mddev->default_bitmap_offset;
5364 mddev->pers->quiesce(mddev, 1);
5365 rv = bitmap_create(mddev);
5366 if (rv)
5367 bitmap_destroy(mddev);
5368 mddev->pers->quiesce(mddev, 0);
5369 } else {
5370 /* remove the bitmap */
5371 if (!mddev->bitmap)
5372 return -ENOENT;
5373 if (mddev->bitmap->file)
5374 return -EINVAL;
5375 mddev->pers->quiesce(mddev, 1);
5376 bitmap_destroy(mddev);
5377 mddev->pers->quiesce(mddev, 0);
5378 mddev->bitmap_offset = 0;
5379 }
5380 }
NeilBrown850b2b42006-10-03 01:15:46 -07005381 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 return rv;
5383}
5384
5385static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5386{
5387 mdk_rdev_t *rdev;
5388
5389 if (mddev->pers == NULL)
5390 return -ENODEV;
5391
5392 rdev = find_rdev(mddev, dev);
5393 if (!rdev)
5394 return -ENODEV;
5395
5396 md_error(mddev, rdev);
5397 return 0;
5398}
5399
Andre Noll2f9618c2008-04-25 18:57:58 +02005400/*
5401 * We have a problem here : there is no easy way to give a CHS
5402 * virtual geometry. We currently pretend that we have a 2 heads
5403 * 4 sectors (with a BIG number of cylinders...). This drives
5404 * dosfs just mad... ;-)
5405 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005406static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5407{
5408 mddev_t *mddev = bdev->bd_disk->private_data;
5409
5410 geo->heads = 2;
5411 geo->sectors = 4;
5412 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5413 return 0;
5414}
5415
Al Viroa39907f2008-03-02 10:31:15 -05005416static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 unsigned int cmd, unsigned long arg)
5418{
5419 int err = 0;
5420 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 mddev_t *mddev = NULL;
5422
5423 if (!capable(CAP_SYS_ADMIN))
5424 return -EACCES;
5425
5426 /*
5427 * Commands dealing with the RAID driver but not any
5428 * particular array:
5429 */
5430 switch (cmd)
5431 {
5432 case RAID_VERSION:
5433 err = get_version(argp);
5434 goto done;
5435
5436 case PRINT_RAID_DEBUG:
5437 err = 0;
5438 md_print_devices();
5439 goto done;
5440
5441#ifndef MODULE
5442 case RAID_AUTORUN:
5443 err = 0;
5444 autostart_arrays(arg);
5445 goto done;
5446#endif
5447 default:;
5448 }
5449
5450 /*
5451 * Commands creating/starting a new array:
5452 */
5453
Al Viroa39907f2008-03-02 10:31:15 -05005454 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455
5456 if (!mddev) {
5457 BUG();
5458 goto abort;
5459 }
5460
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 err = mddev_lock(mddev);
5462 if (err) {
5463 printk(KERN_INFO
5464 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5465 err, cmd);
5466 goto abort;
5467 }
5468
5469 switch (cmd)
5470 {
5471 case SET_ARRAY_INFO:
5472 {
5473 mdu_array_info_t info;
5474 if (!arg)
5475 memset(&info, 0, sizeof(info));
5476 else if (copy_from_user(&info, argp, sizeof(info))) {
5477 err = -EFAULT;
5478 goto abort_unlock;
5479 }
5480 if (mddev->pers) {
5481 err = update_array_info(mddev, &info);
5482 if (err) {
5483 printk(KERN_WARNING "md: couldn't update"
5484 " array info. %d\n", err);
5485 goto abort_unlock;
5486 }
5487 goto done_unlock;
5488 }
5489 if (!list_empty(&mddev->disks)) {
5490 printk(KERN_WARNING
5491 "md: array %s already has disks!\n",
5492 mdname(mddev));
5493 err = -EBUSY;
5494 goto abort_unlock;
5495 }
5496 if (mddev->raid_disks) {
5497 printk(KERN_WARNING
5498 "md: array %s already initialised!\n",
5499 mdname(mddev));
5500 err = -EBUSY;
5501 goto abort_unlock;
5502 }
5503 err = set_array_info(mddev, &info);
5504 if (err) {
5505 printk(KERN_WARNING "md: couldn't set"
5506 " array info. %d\n", err);
5507 goto abort_unlock;
5508 }
5509 }
5510 goto done_unlock;
5511
5512 default:;
5513 }
5514
5515 /*
5516 * Commands querying/configuring an existing array:
5517 */
NeilBrown32a76272005-06-21 17:17:14 -07005518 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005519 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005520 if ((!mddev->raid_disks && !mddev->external)
5521 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5522 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5523 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 err = -ENODEV;
5525 goto abort_unlock;
5526 }
5527
5528 /*
5529 * Commands even a read-only array can execute:
5530 */
5531 switch (cmd)
5532 {
5533 case GET_ARRAY_INFO:
5534 err = get_array_info(mddev, argp);
5535 goto done_unlock;
5536
NeilBrown32a76272005-06-21 17:17:14 -07005537 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005538 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005539 goto done_unlock;
5540
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 case GET_DISK_INFO:
5542 err = get_disk_info(mddev, argp);
5543 goto done_unlock;
5544
5545 case RESTART_ARRAY_RW:
5546 err = restart_array(mddev);
5547 goto done_unlock;
5548
5549 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005550 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 goto done_unlock;
5552
5553 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005554 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 goto done_unlock;
5556
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 }
5558
5559 /*
5560 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005561 * superblock, so we do not allow them on read-only arrays.
5562 * However non-MD ioctls (e.g. get-size) will still come through
5563 * here and hit the 'default' below, so only disallow
5564 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005566 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005567 if (mddev->ro == 2) {
5568 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005569 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005570 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5571 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005572 } else {
5573 err = -EROFS;
5574 goto abort_unlock;
5575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 }
5577
5578 switch (cmd)
5579 {
5580 case ADD_NEW_DISK:
5581 {
5582 mdu_disk_info_t info;
5583 if (copy_from_user(&info, argp, sizeof(info)))
5584 err = -EFAULT;
5585 else
5586 err = add_new_disk(mddev, &info);
5587 goto done_unlock;
5588 }
5589
5590 case HOT_REMOVE_DISK:
5591 err = hot_remove_disk(mddev, new_decode_dev(arg));
5592 goto done_unlock;
5593
5594 case HOT_ADD_DISK:
5595 err = hot_add_disk(mddev, new_decode_dev(arg));
5596 goto done_unlock;
5597
5598 case SET_DISK_FAULTY:
5599 err = set_disk_faulty(mddev, new_decode_dev(arg));
5600 goto done_unlock;
5601
5602 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005603 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 goto done_unlock;
5605
NeilBrown32a76272005-06-21 17:17:14 -07005606 case SET_BITMAP_FILE:
5607 err = set_bitmap_file(mddev, (int)arg);
5608 goto done_unlock;
5609
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 err = -EINVAL;
5612 goto abort_unlock;
5613 }
5614
5615done_unlock:
5616abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005617 if (mddev->hold_active == UNTIL_IOCTL &&
5618 err != -EINVAL)
5619 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 mddev_unlock(mddev);
5621
5622 return err;
5623done:
5624 if (err)
5625 MD_BUG();
5626abort:
5627 return err;
5628}
5629
Al Viroa39907f2008-03-02 10:31:15 -05005630static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631{
5632 /*
5633 * Succeed if we can lock the mddev, which confirms that
5634 * it isn't being stopped right now.
5635 */
NeilBrownd3374822009-01-09 08:31:10 +11005636 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 int err;
5638
NeilBrownd3374822009-01-09 08:31:10 +11005639 if (mddev->gendisk != bdev->bd_disk) {
5640 /* we are racing with mddev_put which is discarding this
5641 * bd_disk.
5642 */
5643 mddev_put(mddev);
5644 /* Wait until bdev->bd_disk is definitely gone */
5645 flush_scheduled_work();
5646 /* Then retry the open from the top */
5647 return -ERESTARTSYS;
5648 }
5649 BUG_ON(mddev != bdev->bd_disk->private_data);
5650
NeilBrownc8c00a62009-08-10 12:50:52 +10005651 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 goto out;
5653
5654 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005655 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005656 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657
Al Viroa39907f2008-03-02 10:31:15 -05005658 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 out:
5660 return err;
5661}
5662
Al Viroa39907f2008-03-02 10:31:15 -05005663static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664{
Al Viroa39907f2008-03-02 10:31:15 -05005665 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005667 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005668 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 mddev_put(mddev);
5670
5671 return 0;
5672}
5673
Linus Torvalds44ce62942007-05-09 18:51:36 -07005674static int md_media_changed(struct gendisk *disk)
5675{
5676 mddev_t *mddev = disk->private_data;
5677
5678 return mddev->changed;
5679}
5680
5681static int md_revalidate(struct gendisk *disk)
5682{
5683 mddev_t *mddev = disk->private_data;
5684
5685 mddev->changed = 0;
5686 return 0;
5687}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005688static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689{
5690 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005691 .open = md_open,
5692 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005693 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005694 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07005695 .media_changed = md_media_changed,
5696 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697};
5698
Adrian Bunk75c96f82005-05-05 16:16:09 -07005699static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700{
5701 mdk_thread_t *thread = arg;
5702
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 /*
5704 * md_thread is a 'system-thread', it's priority should be very
5705 * high. We avoid resource deadlocks individually in each
5706 * raid personality. (RAID5 does preallocation) We also use RR and
5707 * the very same RT priority as kswapd, thus we will never get
5708 * into a priority inversion deadlock.
5709 *
5710 * we definitely have to have equal or higher priority than
5711 * bdflush, otherwise bdflush will deadlock if there are too
5712 * many dirty RAID5 blocks.
5713 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714
NeilBrown6985c432005-10-19 21:23:47 -07005715 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005716 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717
NeilBrown93588e22005-11-15 00:09:12 -08005718 /* We need to wait INTERRUPTIBLE so that
5719 * we don't add to the load-average.
5720 * That means we need to be sure no signals are
5721 * pending
5722 */
5723 if (signal_pending(current))
5724 flush_signals(current);
5725
5726 wait_event_interruptible_timeout
5727 (thread->wqueue,
5728 test_bit(THREAD_WAKEUP, &thread->flags)
5729 || kthread_should_stop(),
5730 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731
5732 clear_bit(THREAD_WAKEUP, &thread->flags);
5733
NeilBrown787453c2005-11-08 21:39:43 -08005734 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005736
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 return 0;
5738}
5739
5740void md_wakeup_thread(mdk_thread_t *thread)
5741{
5742 if (thread) {
5743 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5744 set_bit(THREAD_WAKEUP, &thread->flags);
5745 wake_up(&thread->wqueue);
5746 }
5747}
5748
5749mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5750 const char *name)
5751{
5752 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753
NeilBrown9ffae0c2006-01-06 00:20:32 -08005754 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 if (!thread)
5756 return NULL;
5757
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 init_waitqueue_head(&thread->wqueue);
5759
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 thread->run = run;
5761 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005762 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10005763 thread->tsk = kthread_run(md_thread, thread,
5764 "%s_%s",
5765 mdname(thread->mddev),
5766 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07005767 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 kfree(thread);
5769 return NULL;
5770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 return thread;
5772}
5773
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774void md_unregister_thread(mdk_thread_t *thread)
5775{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005776 if (!thread)
5777 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005778 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005779
5780 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 kfree(thread);
5782}
5783
5784void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5785{
5786 if (!mddev) {
5787 MD_BUG();
5788 return;
5789 }
5790
NeilBrownb2d444d2005-11-08 21:39:31 -08005791 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005793
5794 if (mddev->external)
5795 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005796/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5798 mdname(mddev),
5799 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5800 __builtin_return_address(0),__builtin_return_address(1),
5801 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005802*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005803 if (!mddev->pers)
5804 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 if (!mddev->pers->error_handler)
5806 return;
5807 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005808 if (mddev->degraded)
5809 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005810 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5812 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5813 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005814 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815}
5816
5817/* seq_file implementation /proc/mdstat */
5818
5819static void status_unused(struct seq_file *seq)
5820{
5821 int i = 0;
5822 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823
5824 seq_printf(seq, "unused devices: ");
5825
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005826 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 char b[BDEVNAME_SIZE];
5828 i++;
5829 seq_printf(seq, "%s ",
5830 bdevname(rdev->bdev,b));
5831 }
5832 if (!i)
5833 seq_printf(seq, "<none>");
5834
5835 seq_printf(seq, "\n");
5836}
5837
5838
5839static void status_resync(struct seq_file *seq, mddev_t * mddev)
5840{
NeilBrowndd71cf62009-05-07 12:49:35 +10005841 sector_t max_sectors, resync, res;
5842 unsigned long dt, db;
5843 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08005844 int scale;
5845 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846
NeilBrowndd71cf62009-05-07 12:49:35 +10005847 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848
5849 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10005850 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 else
NeilBrowndd71cf62009-05-07 12:49:35 +10005852 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853
5854 /*
5855 * Should not happen.
5856 */
NeilBrowndd71cf62009-05-07 12:49:35 +10005857 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 MD_BUG();
5859 return;
5860 }
NeilBrown4588b422006-03-27 01:18:04 -08005861 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10005862 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08005863 * u32, as those are the requirements for sector_div.
5864 * Thus 'scale' must be at least 10
5865 */
5866 scale = 10;
5867 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10005868 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08005869 scale++;
5870 }
5871 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10005872 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08005873
5874 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 {
NeilBrown4588b422006-03-27 01:18:04 -08005876 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 seq_printf(seq, "[");
5878 for (i = 0; i < x; i++)
5879 seq_printf(seq, "=");
5880 seq_printf(seq, ">");
5881 for (i = 0; i < y; i++)
5882 seq_printf(seq, ".");
5883 seq_printf(seq, "] ");
5884 }
NeilBrown4588b422006-03-27 01:18:04 -08005885 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005886 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5887 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005888 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5889 "check" :
5890 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5891 "resync" : "recovery"))),
5892 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10005893 (unsigned long long) resync/2,
5894 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895
5896 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 * dt: time from mark until now
5898 * db: blocks written from mark until now
5899 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10005900 *
5901 * rt is a sector_t, so could be 32bit or 64bit.
5902 * So we divide before multiply in case it is 32bit and close
5903 * to the limit.
5904 * We scale the divisor (db) by 32 to avoid loosing precision
5905 * near the end of resync when the number of remaining sectors
5906 * is close to 'db'.
5907 * We then divide rt by 32 after multiplying by db to compensate.
5908 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 */
5910 dt = ((jiffies - mddev->resync_mark) / HZ);
5911 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005912 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5913 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914
NeilBrowndd71cf62009-05-07 12:49:35 +10005915 rt = max_sectors - resync; /* number of remaining sectors */
5916 sector_div(rt, db/32+1);
5917 rt *= dt;
5918 rt >>= 5;
5919
5920 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
5921 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922
NeilBrownff4e8d92006-07-10 04:44:16 -07005923 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924}
5925
5926static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5927{
5928 struct list_head *tmp;
5929 loff_t l = *pos;
5930 mddev_t *mddev;
5931
5932 if (l >= 0x10000)
5933 return NULL;
5934 if (!l--)
5935 /* header */
5936 return (void*)1;
5937
5938 spin_lock(&all_mddevs_lock);
5939 list_for_each(tmp,&all_mddevs)
5940 if (!l--) {
5941 mddev = list_entry(tmp, mddev_t, all_mddevs);
5942 mddev_get(mddev);
5943 spin_unlock(&all_mddevs_lock);
5944 return mddev;
5945 }
5946 spin_unlock(&all_mddevs_lock);
5947 if (!l--)
5948 return (void*)2;/* tail */
5949 return NULL;
5950}
5951
5952static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5953{
5954 struct list_head *tmp;
5955 mddev_t *next_mddev, *mddev = v;
5956
5957 ++*pos;
5958 if (v == (void*)2)
5959 return NULL;
5960
5961 spin_lock(&all_mddevs_lock);
5962 if (v == (void*)1)
5963 tmp = all_mddevs.next;
5964 else
5965 tmp = mddev->all_mddevs.next;
5966 if (tmp != &all_mddevs)
5967 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5968 else {
5969 next_mddev = (void*)2;
5970 *pos = 0x10000;
5971 }
5972 spin_unlock(&all_mddevs_lock);
5973
5974 if (v != (void*)1)
5975 mddev_put(mddev);
5976 return next_mddev;
5977
5978}
5979
5980static void md_seq_stop(struct seq_file *seq, void *v)
5981{
5982 mddev_t *mddev = v;
5983
5984 if (mddev && v != (void*)1 && v != (void*)2)
5985 mddev_put(mddev);
5986}
5987
NeilBrownd7603b72006-01-06 00:20:30 -08005988struct mdstat_info {
5989 int event;
5990};
5991
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992static int md_seq_show(struct seq_file *seq, void *v)
5993{
5994 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11005995 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005997 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005998 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999
6000 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006001 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 seq_printf(seq, "Personalities : ");
6003 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006004 list_for_each_entry(pers, &pers_list, list)
6005 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006
6007 spin_unlock(&pers_lock);
6008 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006009 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 return 0;
6011 }
6012 if (v == (void*)2) {
6013 status_unused(seq);
6014 return 0;
6015 }
6016
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006017 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006019
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6021 seq_printf(seq, "%s : %sactive", mdname(mddev),
6022 mddev->pers ? "" : "in");
6023 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006024 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006026 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006027 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 seq_printf(seq, " %s", mddev->pers->name);
6029 }
6030
Andre Nolldd8ac332009-03-31 14:33:13 +11006031 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006032 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 char b[BDEVNAME_SIZE];
6034 seq_printf(seq, " %s[%d]",
6035 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006036 if (test_bit(WriteMostly, &rdev->flags))
6037 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006038 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 seq_printf(seq, "(F)");
6040 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006041 } else if (rdev->raid_disk < 0)
6042 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006043 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 }
6045
6046 if (!list_empty(&mddev->disks)) {
6047 if (mddev->pers)
6048 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006049 (unsigned long long)
6050 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 else
6052 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006053 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006055 if (mddev->persistent) {
6056 if (mddev->major_version != 0 ||
6057 mddev->minor_version != 90) {
6058 seq_printf(seq," super %d.%d",
6059 mddev->major_version,
6060 mddev->minor_version);
6061 }
NeilBrowne6910632008-02-06 01:39:51 -08006062 } else if (mddev->external)
6063 seq_printf(seq, " super external:%s",
6064 mddev->metadata_type);
6065 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006066 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067
6068 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006069 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006071 if (mddev->pers->sync_request) {
6072 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006073 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006074 seq_printf(seq, "\n ");
6075 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6076 seq_printf(seq, "\tresync=DELAYED\n ");
6077 else if (mddev->recovery_cp < MaxSector)
6078 seq_printf(seq, "\tresync=PENDING\n ");
6079 }
NeilBrown32a76272005-06-21 17:17:14 -07006080 } else
6081 seq_printf(seq, "\n ");
6082
6083 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006084 unsigned long chunk_kb;
6085 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006086 spin_lock_irqsave(&bitmap->lock, flags);
6087 chunk_kb = bitmap->chunksize >> 10;
6088 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6089 "%lu%s chunk",
6090 bitmap->pages - bitmap->missing_pages,
6091 bitmap->pages,
6092 (bitmap->pages - bitmap->missing_pages)
6093 << (PAGE_SHIFT - 10),
6094 chunk_kb ? chunk_kb : bitmap->chunksize,
6095 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006096 if (bitmap->file) {
6097 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006098 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006099 }
NeilBrown78d742d2005-06-21 17:17:15 -07006100
NeilBrown32a76272005-06-21 17:17:14 -07006101 seq_printf(seq, "\n");
6102 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006103 }
6104
6105 seq_printf(seq, "\n");
6106 }
6107 mddev_unlock(mddev);
6108
6109 return 0;
6110}
6111
Jan Engelhardt110518b2009-05-07 12:49:37 +10006112static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 .start = md_seq_start,
6114 .next = md_seq_next,
6115 .stop = md_seq_stop,
6116 .show = md_seq_show,
6117};
6118
6119static int md_seq_open(struct inode *inode, struct file *file)
6120{
6121 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006122 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6123 if (mi == NULL)
6124 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125
6126 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006127 if (error)
6128 kfree(mi);
6129 else {
6130 struct seq_file *p = file->private_data;
6131 p->private = mi;
6132 mi->event = atomic_read(&md_event_count);
6133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134 return error;
6135}
6136
NeilBrownd7603b72006-01-06 00:20:30 -08006137static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6138{
6139 struct seq_file *m = filp->private_data;
6140 struct mdstat_info *mi = m->private;
6141 int mask;
6142
6143 poll_wait(filp, &md_event_waiters, wait);
6144
6145 /* always allow read */
6146 mask = POLLIN | POLLRDNORM;
6147
6148 if (mi->event != atomic_read(&md_event_count))
6149 mask |= POLLERR | POLLPRI;
6150 return mask;
6151}
6152
Arjan van de Venfa027c22007-02-12 00:55:33 -08006153static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006154 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155 .open = md_seq_open,
6156 .read = seq_read,
6157 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006158 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006159 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160};
6161
NeilBrown2604b702006-01-06 00:20:36 -08006162int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006165 list_add_tail(&p->list, &pers_list);
6166 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167 spin_unlock(&pers_lock);
6168 return 0;
6169}
6170
NeilBrown2604b702006-01-06 00:20:36 -08006171int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172{
NeilBrown2604b702006-01-06 00:20:36 -08006173 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006175 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 spin_unlock(&pers_lock);
6177 return 0;
6178}
6179
NeilBrowneea1bf32009-03-31 14:27:02 +11006180static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181{
6182 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006184 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185
6186 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006187 rcu_read_lock();
6188 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006190 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6191 (int)part_stat_read(&disk->part0, sectors[1]) -
6192 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006193 /* sync IO will cause sync_io to increase before the disk_stats
6194 * as sync_io is counted when a request starts, and
6195 * disk_stats is counted when it completes.
6196 * So resync activity will cause curr_events to be smaller than
6197 * when there was no such activity.
6198 * non-sync IO will cause disk_stat to increase without
6199 * increasing sync_io so curr_events will (eventually)
6200 * be larger than it was before. Once it becomes
6201 * substantially larger, the test below will cause
6202 * the array to appear non-idle, and resync will slow
6203 * down.
6204 * If there is a lot of outstanding resync activity when
6205 * we set last_event to curr_events, then all that activity
6206 * completing might cause the array to appear non-idle
6207 * and resync will be slowed down even though there might
6208 * not have been non-resync activity. This will only
6209 * happen once though. 'last_events' will soon reflect
6210 * the state where there is little or no outstanding
6211 * resync requests, and further resync activity will
6212 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006213 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006215 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216 rdev->last_events = curr_events;
6217 idle = 0;
6218 }
6219 }
NeilBrown4b809912008-07-21 17:05:25 +10006220 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221 return idle;
6222}
6223
6224void md_done_sync(mddev_t *mddev, int blocks, int ok)
6225{
6226 /* another "blocks" (512byte) blocks have been synced */
6227 atomic_sub(blocks, &mddev->recovery_active);
6228 wake_up(&mddev->recovery_wait);
6229 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006230 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 md_wakeup_thread(mddev->thread);
6232 // stop recovery, signal do_sync ....
6233 }
6234}
6235
6236
NeilBrown06d91a52005-06-21 17:17:12 -07006237/* md_write_start(mddev, bi)
6238 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006239 * in superblock) before writing, schedule a superblock update
6240 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006241 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006242void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243{
Neil Brown0fd62b82008-06-28 08:31:36 +10006244 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006245 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006246 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006247
NeilBrownf91de922005-11-08 21:39:36 -08006248 BUG_ON(mddev->ro == 1);
6249 if (mddev->ro == 2) {
6250 /* need to switch to read/write */
6251 mddev->ro = 0;
6252 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6253 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006254 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006255 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006256 }
NeilBrown06d91a52005-06-21 17:17:12 -07006257 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006258 if (mddev->safemode == 1)
6259 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006260 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006261 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006262 if (mddev->in_sync) {
6263 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006264 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006265 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006266 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006267 }
NeilBrowna9701a32005-11-08 21:39:34 -08006268 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006269 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006270 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006271 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006272 wait_event(mddev->sb_wait,
6273 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6274 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275}
6276
6277void md_write_end(mddev_t *mddev)
6278{
6279 if (atomic_dec_and_test(&mddev->writes_pending)) {
6280 if (mddev->safemode == 2)
6281 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006282 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6284 }
6285}
6286
NeilBrown2a2275d2007-01-26 00:57:11 -08006287/* md_allow_write(mddev)
6288 * Calling this ensures that the array is marked 'active' so that writes
6289 * may proceed without blocking. It is important to call this before
6290 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6291 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006292 *
6293 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6294 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006295 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006296int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006297{
6298 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006299 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006300 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006301 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006302 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006303 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006304
6305 spin_lock_irq(&mddev->write_lock);
6306 if (mddev->in_sync) {
6307 mddev->in_sync = 0;
6308 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6309 if (mddev->safemode_delay &&
6310 mddev->safemode == 0)
6311 mddev->safemode = 1;
6312 spin_unlock_irq(&mddev->write_lock);
6313 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006314 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006315 } else
6316 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006317
6318 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6319 return -EAGAIN;
6320 else
6321 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006322}
6323EXPORT_SYMBOL_GPL(md_allow_write);
6324
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325#define SYNC_MARKS 10
6326#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006327void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328{
6329 mddev_t *mddev2;
6330 unsigned int currspeed = 0,
6331 window;
NeilBrown57afd892005-06-21 17:17:13 -07006332 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333 unsigned long mark[SYNC_MARKS];
6334 sector_t mark_cnt[SYNC_MARKS];
6335 int last_mark,m;
6336 struct list_head *tmp;
6337 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006338 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006339 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006340 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341
6342 /* just incase thread restarts... */
6343 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6344 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006345 if (mddev->ro) /* never try to sync a read-only array */
6346 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347
NeilBrown61df9d92006-10-03 01:15:57 -07006348 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6349 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6350 desc = "data-check";
6351 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6352 desc = "requested-resync";
6353 else
6354 desc = "resync";
6355 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6356 desc = "reshape";
6357 else
6358 desc = "recovery";
6359
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 /* we overload curr_resync somewhat here.
6361 * 0 == not engaged in resync at all
6362 * 2 == checking that there is no conflict with another sync
6363 * 1 == like 2, but have yielded to allow conflicting resync to
6364 * commense
6365 * other == active in resync - this many blocks
6366 *
6367 * Before starting a resync we must have set curr_resync to
6368 * 2, and then checked that every "conflicting" array has curr_resync
6369 * less than ours. When we find one that is the same or higher
6370 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6371 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6372 * This will mean we have to start checking from the beginning again.
6373 *
6374 */
6375
6376 do {
6377 mddev->curr_resync = 2;
6378
6379 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08006380 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07006381 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382 goto skip;
6383 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08006384 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 if (mddev2 == mddev)
6386 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006387 if (!mddev->parallel_resync
6388 && mddev2->curr_resync
6389 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390 DEFINE_WAIT(wq);
6391 if (mddev < mddev2 && mddev->curr_resync == 2) {
6392 /* arbitrarily yield */
6393 mddev->curr_resync = 1;
6394 wake_up(&resync_wait);
6395 }
6396 if (mddev > mddev2 && mddev->curr_resync == 1)
6397 /* no need to wait here, we can wait the next
6398 * time 'round when curr_resync == 2
6399 */
6400 continue;
NeilBrown97441972008-09-19 11:49:54 +10006401 /* We need to wait 'interruptible' so as not to
6402 * contribute to the load average, and not to
6403 * be caught by 'softlockup'
6404 */
6405 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006406 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006407 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006408 printk(KERN_INFO "md: delaying %s of %s"
6409 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006411 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006413 if (signal_pending(current))
6414 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415 schedule();
6416 finish_wait(&resync_wait, &wq);
6417 goto try_again;
6418 }
6419 finish_wait(&resync_wait, &wq);
6420 }
6421 }
6422 } while (mddev->curr_resync < 2);
6423
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006424 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006425 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006427 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 */
6429 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006430 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006431 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006432 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6433 j = mddev->resync_min;
6434 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006435 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006436
NeilBrownccfcc3c2006-03-27 01:18:09 -08006437 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006438 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006439 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006441 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006442 j = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006443 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006444 if (rdev->raid_disk >= 0 &&
6445 !test_bit(Faulty, &rdev->flags) &&
6446 !test_bit(In_sync, &rdev->flags) &&
6447 rdev->recovery_offset < j)
6448 j = rdev->recovery_offset;
6449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450
NeilBrown61df9d92006-10-03 01:15:57 -07006451 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6452 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6453 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006454 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006455 "(but not more than %d KB/sec) for %s.\n",
6456 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457
NeilBrowneea1bf32009-03-31 14:27:02 +11006458 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006459
NeilBrown57afd892005-06-21 17:17:13 -07006460 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461 for (m = 0; m < SYNC_MARKS; m++) {
6462 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006463 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464 }
6465 last_mark = 0;
6466 mddev->resync_mark = mark[last_mark];
6467 mddev->resync_mark_cnt = mark_cnt[last_mark];
6468
6469 /*
6470 * Tune reconstruction:
6471 */
6472 window = 32*(PAGE_SIZE/512);
6473 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6474 window/2,(unsigned long long) max_sectors/2);
6475
6476 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477 last_check = 0;
6478
6479 if (j>2) {
6480 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006481 "md: resuming %s of %s from checkpoint.\n",
6482 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483 mddev->curr_resync = j;
6484 }
NeilBrownefa59332009-12-14 12:49:49 +11006485 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486
6487 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006488 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489
NeilBrown57afd892005-06-21 17:17:13 -07006490 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006491
NeilBrown7a91ee12009-05-26 12:57:21 +10006492 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6493 ((mddev->curr_resync > mddev->curr_resync_completed &&
6494 (mddev->curr_resync - mddev->curr_resync_completed)
6495 > (max_sectors >> 4)) ||
6496 (j - mddev->curr_resync_completed)*2
6497 >= mddev->resync_max - mddev->curr_resync_completed
6498 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006499 /* time to update curr_resync_completed */
6500 blk_unplug(mddev->queue);
6501 wait_event(mddev->recovery_wait,
6502 atomic_read(&mddev->recovery_active) == 0);
6503 mddev->curr_resync_completed =
6504 mddev->curr_resync;
6505 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006506 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006507 }
NeilBrownacb180b2009-04-14 16:28:34 +10006508
NeilBrowne62e58a2009-07-01 13:15:35 +10006509 while (j >= mddev->resync_max && !kthread_should_stop()) {
6510 /* As this condition is controlled by user-space,
6511 * we can block indefinitely, so use '_interruptible'
6512 * to avoid triggering warnings.
6513 */
6514 flush_signals(current); /* just in case */
6515 wait_event_interruptible(mddev->recovery_wait,
6516 mddev->resync_max > j
6517 || kthread_should_stop());
6518 }
NeilBrownacb180b2009-04-14 16:28:34 +10006519
6520 if (kthread_should_stop())
6521 goto interrupted;
6522
NeilBrown57afd892005-06-21 17:17:13 -07006523 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006524 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006525 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006526 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527 goto out;
6528 }
NeilBrown57afd892005-06-21 17:17:13 -07006529
6530 if (!skipped) { /* actual IO requested */
6531 io_sectors += sectors;
6532 atomic_add(sectors, &mddev->recovery_active);
6533 }
6534
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535 j += sectors;
6536 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006537 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006538 if (last_check == 0)
6539 /* this is the earliers that rebuilt will be
6540 * visible in /proc/mdstat
6541 */
6542 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006543
6544 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545 continue;
6546
NeilBrown57afd892005-06-21 17:17:13 -07006547 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548
NeilBrowndfc70642008-05-23 13:04:39 -07006549 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550 break;
6551
6552 repeat:
6553 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6554 /* step marks */
6555 int next = (last_mark+1) % SYNC_MARKS;
6556
6557 mddev->resync_mark = mark[next];
6558 mddev->resync_mark_cnt = mark_cnt[next];
6559 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006560 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561 last_mark = next;
6562 }
6563
6564
NeilBrownc6207272008-02-06 01:39:52 -08006565 if (kthread_should_stop())
6566 goto interrupted;
6567
Linus Torvalds1da177e2005-04-16 15:20:36 -07006568
6569 /*
6570 * this loop exits only if either when we are slower than
6571 * the 'hard' speed limit, or the system was IO-idle for
6572 * a jiffy.
6573 * the system might be non-idle CPU-wise, but we only care
6574 * about not overloading the IO subsystem. (things like an
6575 * e2fsck being done on the RAID array should execute fast)
6576 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006577 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 cond_resched();
6579
NeilBrown57afd892005-06-21 17:17:13 -07006580 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6581 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582
NeilBrown88202a02006-01-06 00:21:36 -08006583 if (currspeed > speed_min(mddev)) {
6584 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006585 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006586 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587 goto repeat;
6588 }
6589 }
6590 }
NeilBrown61df9d92006-10-03 01:15:57 -07006591 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592 /*
6593 * this also signals 'finished resyncing' to md_stop
6594 */
6595 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006596 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597
6598 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6599
6600 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006601 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602
NeilBrowndfc70642008-05-23 13:04:39 -07006603 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006604 mddev->curr_resync > 2) {
6605 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6606 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6607 if (mddev->curr_resync >= mddev->recovery_cp) {
6608 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006609 "md: checkpointing %s of %s.\n",
6610 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006611 mddev->recovery_cp = mddev->curr_resync;
6612 }
6613 } else
6614 mddev->recovery_cp = MaxSector;
6615 } else {
6616 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6617 mddev->curr_resync = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006618 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006619 if (rdev->raid_disk >= 0 &&
6620 !test_bit(Faulty, &rdev->flags) &&
6621 !test_bit(In_sync, &rdev->flags) &&
6622 rdev->recovery_offset < mddev->curr_resync)
6623 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625 }
NeilBrown17571282006-12-10 02:20:52 -08006626 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006629 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6630 /* We completed so min/max setting can be forgotten if used. */
6631 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6632 mddev->resync_min = 0;
6633 mddev->resync_max = MaxSector;
6634 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6635 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006637 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6638 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006639 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640 wake_up(&resync_wait);
6641 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6642 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006643 return;
6644
6645 interrupted:
6646 /*
6647 * got a signal, exit.
6648 */
6649 printk(KERN_INFO
6650 "md: md_do_sync() got signal ... exiting\n");
6651 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6652 goto out;
6653
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654}
NeilBrown29269552006-03-27 01:18:10 -08006655EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656
6657
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006658static int remove_and_add_spares(mddev_t *mddev)
6659{
6660 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006661 int spares = 0;
6662
NeilBrown97e4f422009-03-31 14:33:13 +11006663 mddev->curr_resync_completed = 0;
6664
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006665 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006666 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006667 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006668 (test_bit(Faulty, &rdev->flags) ||
6669 ! test_bit(In_sync, &rdev->flags)) &&
6670 atomic_read(&rdev->nr_pending)==0) {
6671 if (mddev->pers->hot_remove_disk(
6672 mddev, rdev->raid_disk)==0) {
6673 char nm[20];
6674 sprintf(nm,"rd%d", rdev->raid_disk);
6675 sysfs_remove_link(&mddev->kobj, nm);
6676 rdev->raid_disk = -1;
6677 }
6678 }
6679
NeilBrown4044ba52009-01-09 08:31:11 +11006680 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006681 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006682 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006683 !test_bit(In_sync, &rdev->flags) &&
6684 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006685 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006686 if (rdev->raid_disk < 0
6687 && !test_bit(Faulty, &rdev->flags)) {
6688 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006689 if (mddev->pers->
6690 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006691 char nm[20];
6692 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006693 if (sysfs_create_link(&mddev->kobj,
6694 &rdev->kobj, nm))
6695 printk(KERN_WARNING
6696 "md: cannot register "
6697 "%s for %s\n",
6698 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006699 spares++;
6700 md_new_event(mddev);
6701 } else
6702 break;
6703 }
NeilBrowndfc70642008-05-23 13:04:39 -07006704 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006705 }
6706 return spares;
6707}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708/*
6709 * This routine is regularly called by all per-raid-array threads to
6710 * deal with generic issues like resync and super-block update.
6711 * Raid personalities that don't have a thread (linear/raid0) do not
6712 * need this as they never do any recovery or update the superblock.
6713 *
6714 * It does not do any resync itself, but rather "forks" off other threads
6715 * to do that as needed.
6716 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6717 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006718 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006719 * and wakeups up this thread which will reap the thread and finish up.
6720 * This thread also removes any faulty devices (with nr_pending == 0).
6721 *
6722 * The overall approach is:
6723 * 1/ if the superblock needs updating, update it.
6724 * 2/ If a recovery thread is running, don't do anything else.
6725 * 3/ If recovery has finished, clean up, possibly marking spares active.
6726 * 4/ If there are any faulty devices, remove them.
6727 * 5/ If array is degraded, try to add spares devices
6728 * 6/ If array has spares or is not in-sync, start a resync thread.
6729 */
6730void md_check_recovery(mddev_t *mddev)
6731{
6732 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733
6734
NeilBrown5f404022005-06-21 17:17:16 -07006735 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11006736 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737
6738 if (mddev->ro)
6739 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006740
6741 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006742 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006743 printk(KERN_INFO "md: %s in immediate safe mode\n",
6744 mdname(mddev));
6745 mddev->safemode = 2;
6746 }
6747 flush_signals(current);
6748 }
6749
NeilBrownc89a8ee2008-08-05 15:54:13 +10006750 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6751 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006753 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006755 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006756 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006757 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6758 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759 ))
6760 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006761
NeilBrowndf5b89b2006-03-27 01:18:20 -08006762 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006763 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006764
NeilBrownc89a8ee2008-08-05 15:54:13 +10006765 if (mddev->ro) {
6766 /* Only thing we do on a ro array is remove
6767 * failed devices.
6768 */
6769 remove_and_add_spares(mddev);
6770 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6771 goto unlock;
6772 }
6773
NeilBrown31a59e32008-04-30 00:52:30 -07006774 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006775 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006776 spin_lock_irq(&mddev->write_lock);
6777 if (mddev->safemode &&
6778 !atomic_read(&mddev->writes_pending) &&
6779 !mddev->in_sync &&
6780 mddev->recovery_cp == MaxSector) {
6781 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006782 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006783 if (mddev->persistent)
6784 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6785 }
6786 if (mddev->safemode == 1)
6787 mddev->safemode = 0;
6788 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006789 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006790 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006791 }
NeilBrownfca4d842005-06-21 17:17:11 -07006792
NeilBrown850b2b42006-10-03 01:15:46 -07006793 if (mddev->flags)
6794 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006795
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006796 list_for_each_entry(rdev, &mddev->disks, same_set)
Neil Brown52664732008-06-28 08:31:44 +10006797 if (test_and_clear_bit(StateChanged, &rdev->flags))
NeilBrown3c0ee632008-10-21 13:25:28 +11006798 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown52664732008-06-28 08:31:44 +10006799
NeilBrown06d91a52005-06-21 17:17:12 -07006800
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6802 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6803 /* resync/recovery still happening */
6804 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6805 goto unlock;
6806 }
6807 if (mddev->sync_thread) {
6808 /* resync has finished, collect result */
6809 md_unregister_thread(mddev->sync_thread);
6810 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006811 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6812 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813 /* success...*/
6814 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006815 if (mddev->pers->spare_active(mddev))
6816 sysfs_notify(&mddev->kobj, NULL,
6817 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818 }
NeilBrowncea9c222009-03-31 15:15:05 +11006819 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6820 mddev->pers->finish_reshape)
6821 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07006822 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006823
6824 /* if array is no-longer degraded, then any saved_raid_disk
6825 * information must be scrapped
6826 */
6827 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006828 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07006829 rdev->saved_raid_disk = -1;
6830
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831 mddev->recovery = 0;
6832 /* flag recovery needed just to double check */
6833 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11006834 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006835 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836 goto unlock;
6837 }
Neil Brown72a23c22008-06-28 08:31:41 +10006838 /* Set RUNNING before clearing NEEDED to avoid
6839 * any transients in the value of "sync_action".
6840 */
6841 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6842 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006843 /* Clear some bits that don't mean anything, but
6844 * might be left set
6845 */
NeilBrown24dd4692005-11-08 21:39:26 -08006846 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6847 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006849 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6850 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006851 /* no recovery is running.
6852 * remove any failed drives, then
6853 * add spares if possible.
6854 * Spare are also removed and re-added, to allow
6855 * the personality to fail the re-add.
6856 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006858 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10006859 if (mddev->pers->check_reshape == NULL ||
6860 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006861 /* Cannot proceed */
6862 goto unlock;
6863 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006864 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006865 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006866 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6867 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07006868 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006869 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006870 } else if (mddev->recovery_cp < MaxSector) {
6871 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006872 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006873 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6874 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006876
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006878 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6879 /* We are adding a device or devices to an array
6880 * which has the bitmap stored on all devices.
6881 * So make sure all bitmap pages get written
6882 */
6883 bitmap_write_all(mddev->bitmap);
6884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885 mddev->sync_thread = md_register_thread(md_do_sync,
6886 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006887 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888 if (!mddev->sync_thread) {
6889 printk(KERN_ERR "%s: could not start resync"
6890 " thread...\n",
6891 mdname(mddev));
6892 /* leave the spares where they are, it shouldn't hurt */
6893 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006894 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11006896 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006897 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898 }
6899 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006900 if (!mddev->sync_thread) {
6901 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6902 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6903 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11006904 if (mddev->sysfs_action)
6905 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10006906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006907 mddev_unlock(mddev);
6908 }
6909}
6910
Dan Williams6bfe0b42008-04-30 00:52:32 -07006911void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6912{
NeilBrown3c0ee632008-10-21 13:25:28 +11006913 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07006914 wait_event_timeout(rdev->blocked_wait,
6915 !test_bit(Blocked, &rdev->flags),
6916 msecs_to_jiffies(5000));
6917 rdev_dec_pending(rdev, mddev);
6918}
6919EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6920
Adrian Bunk75c96f82005-05-05 16:16:09 -07006921static int md_notify_reboot(struct notifier_block *this,
6922 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923{
6924 struct list_head *tmp;
6925 mddev_t *mddev;
6926
6927 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6928
6929 printk(KERN_INFO "md: stopping all md devices.\n");
6930
NeilBrown29ac4aa2008-02-06 01:39:58 -08006931 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006932 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10006933 /* Force a switch to readonly even array
6934 * appears to still be in use. Hence
6935 * the '100'.
6936 */
NeilBrownd710e132008-10-13 11:55:12 +11006937 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10006938 mddev_unlock(mddev);
6939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940 /*
6941 * certain more exotic SCSI devices are known to be
6942 * volatile wrt too early system reboots. While the
6943 * right place to handle this issue is the given
6944 * driver, we do want to have a safe RAID driver ...
6945 */
6946 mdelay(1000*1);
6947 }
6948 return NOTIFY_DONE;
6949}
6950
Adrian Bunk75c96f82005-05-05 16:16:09 -07006951static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952 .notifier_call = md_notify_reboot,
6953 .next = NULL,
6954 .priority = INT_MAX, /* before any real devices */
6955};
6956
6957static void md_geninit(void)
6958{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6960
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07006961 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962}
6963
Adrian Bunk75c96f82005-05-05 16:16:09 -07006964static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006966 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967 return -1;
6968 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006969 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970 return -1;
6971 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006972 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07006973 md_probe, NULL, NULL);
6974 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975 md_probe, NULL, NULL);
6976
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08006978 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979
6980 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11006981 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982}
6983
6984
6985#ifndef MODULE
6986
6987/*
6988 * Searches all registered partitions for autorun RAID arrays
6989 * at boot time.
6990 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006991
6992static LIST_HEAD(all_detected_devices);
6993struct detected_devices_node {
6994 struct list_head list;
6995 dev_t dev;
6996};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997
6998void md_autodetect_dev(dev_t dev)
6999{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007000 struct detected_devices_node *node_detected_dev;
7001
7002 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7003 if (node_detected_dev) {
7004 node_detected_dev->dev = dev;
7005 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7006 } else {
7007 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7008 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010}
7011
7012
7013static void autostart_arrays(int part)
7014{
7015 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007016 struct detected_devices_node *node_detected_dev;
7017 dev_t dev;
7018 int i_scanned, i_passed;
7019
7020 i_scanned = 0;
7021 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007022
7023 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7024
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007025 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7026 i_scanned++;
7027 node_detected_dev = list_entry(all_detected_devices.next,
7028 struct detected_devices_node, list);
7029 list_del(&node_detected_dev->list);
7030 dev = node_detected_dev->dev;
7031 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007032 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007033 if (IS_ERR(rdev))
7034 continue;
7035
NeilBrownb2d444d2005-11-08 21:39:31 -08007036 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037 MD_BUG();
7038 continue;
7039 }
NeilBrownd0fae182008-03-04 14:29:31 -08007040 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007042 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007044
7045 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7046 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047
7048 autorun_devices(part);
7049}
7050
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007051#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052
7053static __exit void md_exit(void)
7054{
7055 mddev_t *mddev;
7056 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007057
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007058 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007059 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007061 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007062 unregister_blkdev(mdp_major, "mdp");
7063 unregister_reboot_notifier(&md_notifier);
7064 unregister_sysctl_table(raid_table_header);
7065 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007066 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007068 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069 }
7070}
7071
Dan Williams685784a2007-07-09 11:56:42 -07007072subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073module_exit(md_exit)
7074
NeilBrownf91de922005-11-08 21:39:36 -08007075static int get_ro(char *buffer, struct kernel_param *kp)
7076{
7077 return sprintf(buffer, "%d", start_readonly);
7078}
7079static int set_ro(const char *val, struct kernel_param *kp)
7080{
7081 char *e;
7082 int num = simple_strtoul(val, &e, 10);
7083 if (*val && (*e == '\0' || *e == '\n')) {
7084 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007085 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007086 }
7087 return -EINVAL;
7088}
7089
NeilBrown80ca3a42006-07-10 04:44:18 -07007090module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7091module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007092
NeilBrownefeb53c2009-01-09 08:31:10 +11007093module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007094
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095EXPORT_SYMBOL(register_md_personality);
7096EXPORT_SYMBOL(unregister_md_personality);
7097EXPORT_SYMBOL(md_error);
7098EXPORT_SYMBOL(md_done_sync);
7099EXPORT_SYMBOL(md_write_start);
7100EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101EXPORT_SYMBOL(md_register_thread);
7102EXPORT_SYMBOL(md_unregister_thread);
7103EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104EXPORT_SYMBOL(md_check_recovery);
7105MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07007106MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007107MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);