blob: c56c64d1307596b198b8ef03dac4f7294959a227 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/buffer_head.h> /* for invalidate_bdev */
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110042#include <linux/hdreg.h>
43#include <linux/proc_fs.h>
44#include <linux/random.h>
45#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070046#include <linux/file.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110047#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110048#include <linux/raid/md_p.h>
49#include <linux/raid/md_u.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110050#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110051#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#define DEBUG 0
54#define dprintk(x...) ((void)(DEBUG && printk(x)))
55
56
57#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110058static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
NeilBrown2604b702006-01-06 00:20:36 -080061static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static DEFINE_SPINLOCK(pers_lock);
63
Adrian Bunk5e563412006-06-26 00:27:42 -070064static void md_print_devices(void);
65
Bernd Schubert90b08712008-05-23 13:04:38 -070066static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
67
Adrian Bunk5e563412006-06-26 00:27:42 -070068#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*
71 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
72 * is 1000 KB/sec, so the extra system load does not show up that much.
73 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070074 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * subsystem is idle. There is also an 'absolute maximum' reconstruction
76 * speed limit - in case reconstruction slows down your system despite
77 * idle IO detection.
78 *
79 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080080 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
82
83static int sysctl_speed_limit_min = 1000;
84static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080085static inline int speed_min(mddev_t *mddev)
86{
87 return mddev->sync_speed_min ?
88 mddev->sync_speed_min : sysctl_speed_limit_min;
89}
90
91static inline int speed_max(mddev_t *mddev)
92{
93 return mddev->sync_speed_max ?
94 mddev->sync_speed_max : sysctl_speed_limit_max;
95}
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97static struct ctl_table_header *raid_table_header;
98
99static ctl_table raid_table[] = {
100 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 .procname = "speed_limit_min",
102 .data = &sysctl_speed_limit_min,
103 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700104 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800105 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 },
107 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 .procname = "speed_limit_max",
109 .data = &sysctl_speed_limit_max,
110 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700111 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800112 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800114 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};
116
117static ctl_table raid_dir_table[] = {
118 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .procname = "raid",
120 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700121 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 .child = raid_table,
123 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800124 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
127static ctl_table raid_root_table[] = {
128 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .procname = "dev",
130 .maxlen = 0,
131 .mode = 0555,
132 .child = raid_dir_table,
133 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800134 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700137static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
NeilBrownf91de922005-11-08 21:39:36 -0800139static int start_readonly;
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/*
NeilBrownd7603b72006-01-06 00:20:30 -0800142 * We have a system wide 'event count' that is incremented
143 * on any 'interesting' event, and readers of /proc/mdstat
144 * can use 'poll' or 'select' to find out when the event
145 * count increases.
146 *
147 * Events are:
148 * start array, stop array, error, add device, remove device,
149 * start build, activate spare
150 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800151static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800152static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800153void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800154{
155 atomic_inc(&md_event_count);
156 wake_up(&md_event_waiters);
157}
NeilBrown29269552006-03-27 01:18:10 -0800158EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800159
NeilBrownc331eb02006-05-30 21:27:13 -0700160/* Alternate version that can be called from interrupts
161 * when calling sysfs_notify isn't needed.
162 */
Adrian Bunk05381952006-06-26 00:28:01 -0700163static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700164{
165 atomic_inc(&md_event_count);
166 wake_up(&md_event_waiters);
167}
168
NeilBrownd7603b72006-01-06 00:20:30 -0800169/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * Enables to iterate over all existing md arrays
171 * all_mddevs_lock protects this list.
172 */
173static LIST_HEAD(all_mddevs);
174static DEFINE_SPINLOCK(all_mddevs_lock);
175
176
177/*
178 * iterates through all used mddevs in the system.
179 * We take care to grab the all_mddevs_lock whenever navigating
180 * the list, and to always hold a refcount when unlocked.
181 * Any code which breaks out of this loop while own
182 * a reference to the current mddev and must mddev_put it.
183 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800184#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 \
186 for (({ spin_lock(&all_mddevs_lock); \
187 tmp = all_mddevs.next; \
188 mddev = NULL;}); \
189 ({ if (tmp != &all_mddevs) \
190 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
191 spin_unlock(&all_mddevs_lock); \
192 if (mddev) mddev_put(mddev); \
193 mddev = list_entry(tmp, mddev_t, all_mddevs); \
194 tmp != &all_mddevs;}); \
195 ({ spin_lock(&all_mddevs_lock); \
196 tmp = tmp->next;}) \
197 )
198
199
NeilBrown409c57f2009-03-31 14:39:39 +1100200/* Rather than calling directly into the personality make_request function,
201 * IO requests come here first so that we can check if the device is
202 * being suspended pending a reconfiguration.
203 * We hold a refcount over the call to ->make_request. By the time that
204 * call has finished, the bio has been linked into some internal structure
205 * and so is visible to ->quiesce(), so we don't need the refcount any more.
206 */
207static int md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
NeilBrown409c57f2009-03-31 14:39:39 +1100209 mddev_t *mddev = q->queuedata;
210 int rv;
211 if (mddev == NULL || mddev->pers == NULL) {
212 bio_io_error(bio);
213 return 0;
214 }
215 rcu_read_lock();
NeilBrowna2826aa2009-12-14 12:49:49 +1100216 if (mddev->suspended || mddev->barrier) {
NeilBrown409c57f2009-03-31 14:39:39 +1100217 DEFINE_WAIT(__wait);
218 for (;;) {
219 prepare_to_wait(&mddev->sb_wait, &__wait,
220 TASK_UNINTERRUPTIBLE);
NeilBrowna2826aa2009-12-14 12:49:49 +1100221 if (!mddev->suspended && !mddev->barrier)
NeilBrown409c57f2009-03-31 14:39:39 +1100222 break;
223 rcu_read_unlock();
224 schedule();
225 rcu_read_lock();
226 }
227 finish_wait(&mddev->sb_wait, &__wait);
228 }
229 atomic_inc(&mddev->active_io);
230 rcu_read_unlock();
231 rv = mddev->pers->make_request(q, bio);
232 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
233 wake_up(&mddev->sb_wait);
234
235 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
NeilBrown409c57f2009-03-31 14:39:39 +1100238static void mddev_suspend(mddev_t *mddev)
239{
240 BUG_ON(mddev->suspended);
241 mddev->suspended = 1;
242 synchronize_rcu();
243 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
244 mddev->pers->quiesce(mddev, 1);
245 md_unregister_thread(mddev->thread);
246 mddev->thread = NULL;
247 /* we now know that no code is executing in the personality module,
248 * except possibly the tail end of a ->bi_end_io function, but that
249 * is certain to complete before the module has a chance to get
250 * unloaded
251 */
252}
253
254static void mddev_resume(mddev_t *mddev)
255{
256 mddev->suspended = 0;
257 wake_up(&mddev->sb_wait);
258 mddev->pers->quiesce(mddev, 0);
259}
260
NeilBrown3fa841d2009-09-23 18:10:29 +1000261int mddev_congested(mddev_t *mddev, int bits)
262{
NeilBrowna2826aa2009-12-14 12:49:49 +1100263 if (mddev->barrier)
264 return 1;
NeilBrown3fa841d2009-09-23 18:10:29 +1000265 return mddev->suspended;
266}
267EXPORT_SYMBOL(mddev_congested);
268
NeilBrowna2826aa2009-12-14 12:49:49 +1100269/*
270 * Generic barrier handling for md
271 */
272
273#define POST_REQUEST_BARRIER ((void*)1)
274
275static void md_end_barrier(struct bio *bio, int err)
276{
277 mdk_rdev_t *rdev = bio->bi_private;
278 mddev_t *mddev = rdev->mddev;
279 if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER)
280 set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags);
281
282 rdev_dec_pending(rdev, mddev);
283
284 if (atomic_dec_and_test(&mddev->flush_pending)) {
285 if (mddev->barrier == POST_REQUEST_BARRIER) {
286 /* This was a post-request barrier */
287 mddev->barrier = NULL;
288 wake_up(&mddev->sb_wait);
289 } else
290 /* The pre-request barrier has finished */
291 schedule_work(&mddev->barrier_work);
292 }
293 bio_put(bio);
294}
295
296static void submit_barriers(mddev_t *mddev)
297{
298 mdk_rdev_t *rdev;
299
300 rcu_read_lock();
301 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
302 if (rdev->raid_disk >= 0 &&
303 !test_bit(Faulty, &rdev->flags)) {
304 /* Take two references, one is dropped
305 * when request finishes, one after
306 * we reclaim rcu_read_lock
307 */
308 struct bio *bi;
309 atomic_inc(&rdev->nr_pending);
310 atomic_inc(&rdev->nr_pending);
311 rcu_read_unlock();
312 bi = bio_alloc(GFP_KERNEL, 0);
313 bi->bi_end_io = md_end_barrier;
314 bi->bi_private = rdev;
315 bi->bi_bdev = rdev->bdev;
316 atomic_inc(&mddev->flush_pending);
317 submit_bio(WRITE_BARRIER, bi);
318 rcu_read_lock();
319 rdev_dec_pending(rdev, mddev);
320 }
321 rcu_read_unlock();
322}
323
324static void md_submit_barrier(struct work_struct *ws)
325{
326 mddev_t *mddev = container_of(ws, mddev_t, barrier_work);
327 struct bio *bio = mddev->barrier;
328
329 atomic_set(&mddev->flush_pending, 1);
330
331 if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags))
332 bio_endio(bio, -EOPNOTSUPP);
333 else if (bio->bi_size == 0)
334 /* an empty barrier - all done */
335 bio_endio(bio, 0);
336 else {
337 bio->bi_rw &= ~(1<<BIO_RW_BARRIER);
338 if (mddev->pers->make_request(mddev->queue, bio))
339 generic_make_request(bio);
340 mddev->barrier = POST_REQUEST_BARRIER;
341 submit_barriers(mddev);
342 }
343 if (atomic_dec_and_test(&mddev->flush_pending)) {
344 mddev->barrier = NULL;
345 wake_up(&mddev->sb_wait);
346 }
347}
348
349void md_barrier_request(mddev_t *mddev, struct bio *bio)
350{
351 spin_lock_irq(&mddev->write_lock);
352 wait_event_lock_irq(mddev->sb_wait,
353 !mddev->barrier,
354 mddev->write_lock, /*nothing*/);
355 mddev->barrier = bio;
356 spin_unlock_irq(&mddev->write_lock);
357
358 atomic_set(&mddev->flush_pending, 1);
359 INIT_WORK(&mddev->barrier_work, md_submit_barrier);
360
361 submit_barriers(mddev);
362
363 if (atomic_dec_and_test(&mddev->flush_pending))
364 schedule_work(&mddev->barrier_work);
365}
366EXPORT_SYMBOL(md_barrier_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368static inline mddev_t *mddev_get(mddev_t *mddev)
369{
370 atomic_inc(&mddev->active);
371 return mddev;
372}
373
Dan Williams5fd3a172009-03-04 00:57:25 -0700374static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376static void mddev_put(mddev_t *mddev)
377{
378 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
379 return;
NeilBrownd3374822009-01-09 08:31:10 +1100380 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
381 !mddev->hold_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 list_del(&mddev->all_mddevs);
NeilBrownd3374822009-01-09 08:31:10 +1100383 if (mddev->gendisk) {
384 /* we did a probe so need to clean up.
385 * Call schedule_work inside the spinlock
386 * so that flush_scheduled_work() after
387 * mddev_find will succeed in waiting for the
388 * work to be done.
389 */
390 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
391 schedule_work(&mddev->del_work);
392 } else
393 kfree(mddev);
394 }
395 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
398static mddev_t * mddev_find(dev_t unit)
399{
400 mddev_t *mddev, *new = NULL;
401
402 retry:
403 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
NeilBrownefeb53c2009-01-09 08:31:10 +1100405 if (unit) {
406 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
407 if (mddev->unit == unit) {
408 mddev_get(mddev);
409 spin_unlock(&all_mddevs_lock);
410 kfree(new);
411 return mddev;
412 }
413
414 if (new) {
415 list_add(&new->all_mddevs, &all_mddevs);
416 spin_unlock(&all_mddevs_lock);
417 new->hold_active = UNTIL_IOCTL;
418 return new;
419 }
420 } else if (new) {
421 /* find an unused unit number */
422 static int next_minor = 512;
423 int start = next_minor;
424 int is_free = 0;
425 int dev = 0;
426 while (!is_free) {
427 dev = MKDEV(MD_MAJOR, next_minor);
428 next_minor++;
429 if (next_minor > MINORMASK)
430 next_minor = 0;
431 if (next_minor == start) {
432 /* Oh dear, all in use. */
433 spin_unlock(&all_mddevs_lock);
434 kfree(new);
435 return NULL;
436 }
437
438 is_free = 1;
439 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
440 if (mddev->unit == dev) {
441 is_free = 0;
442 break;
443 }
444 }
445 new->unit = dev;
446 new->md_minor = MINOR(dev);
447 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 list_add(&new->all_mddevs, &all_mddevs);
449 spin_unlock(&all_mddevs_lock);
450 return new;
451 }
452 spin_unlock(&all_mddevs_lock);
453
NeilBrown9ffae0c2006-01-06 00:20:32 -0800454 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (!new)
456 return NULL;
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 new->unit = unit;
459 if (MAJOR(unit) == MD_MAJOR)
460 new->md_minor = MINOR(unit);
461 else
462 new->md_minor = MINOR(unit) >> MdpMinorShift;
463
NeilBrownc8c00a62009-08-10 12:50:52 +1000464 mutex_init(&new->open_mutex);
NeilBrowndf5b89b2006-03-27 01:18:20 -0800465 mutex_init(&new->reconfig_mutex);
NeilBrownc3d97142009-12-14 12:49:52 +1100466 mutex_init(&new->bitmap_info.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 INIT_LIST_HEAD(&new->disks);
468 INIT_LIST_HEAD(&new->all_mddevs);
469 init_timer(&new->safemode_timer);
470 atomic_set(&new->active, 1);
NeilBrownf2ea68c2008-07-21 17:05:25 +1000471 atomic_set(&new->openers, 0);
NeilBrown409c57f2009-03-31 14:39:39 +1100472 atomic_set(&new->active_io, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700473 spin_lock_init(&new->write_lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100474 atomic_set(&new->flush_pending, 0);
NeilBrown3d310eb2005-06-21 17:17:26 -0700475 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700476 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700477 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000478 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800479 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700480 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 goto retry;
483}
484
485static inline int mddev_lock(mddev_t * mddev)
486{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800487 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
Dan Williamsb522adc2009-03-31 15:00:31 +1100490static inline int mddev_is_locked(mddev_t *mddev)
491{
492 return mutex_is_locked(&mddev->reconfig_mutex);
493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495static inline int mddev_trylock(mddev_t * mddev)
496{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800497 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
500static inline void mddev_unlock(mddev_t * mddev)
501{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800502 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
NeilBrown005eca52005-08-22 13:11:08 -0700504 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
NeilBrown2989ddb2006-01-06 00:20:43 -0800507static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100509 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100511 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (rdev->desc_nr == nr)
513 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return NULL;
516}
517
518static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
519{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 mdk_rdev_t *rdev;
521
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100522 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (rdev->bdev->bd_dev == dev)
524 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return NULL;
527}
528
NeilBrownd9d166c2006-01-06 00:20:51 -0800529static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800530{
531 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800532 list_for_each_entry(pers, &pers_list, list) {
533 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800534 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800535 if (strcmp(pers->name, clevel)==0)
536 return pers;
537 }
NeilBrown2604b702006-01-06 00:20:36 -0800538 return NULL;
539}
540
Andre Nollb73df2d2008-07-11 22:02:23 +1000541/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700542static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Andre Nollb73df2d2008-07-11 22:02:23 +1000544 sector_t num_sectors = bdev->bd_inode->i_size / 512;
545 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548static int alloc_disk_sb(mdk_rdev_t * rdev)
549{
550 if (rdev->sb_page)
551 MD_BUG();
552
553 rdev->sb_page = alloc_page(GFP_KERNEL);
554 if (!rdev->sb_page) {
555 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000556 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558
559 return 0;
560}
561
562static void free_disk_sb(mdk_rdev_t * rdev)
563{
564 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800565 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 rdev->sb_loaded = 0;
567 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000568 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100569 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571}
572
573
NeilBrown6712ecf2007-09-27 12:47:43 +0200574static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700575{
576 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800577 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700578
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700579 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
580 printk("md: super_written gets error=%d, uptodate=%d\n",
581 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
582 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800583 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700584 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700585
NeilBrowna9701a32005-11-08 21:39:34 -0800586 if (atomic_dec_and_test(&mddev->pending_writes))
587 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700588 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700589}
590
NeilBrown6712ecf2007-09-27 12:47:43 +0200591static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800592{
593 struct bio *bio2 = bio->bi_private;
594 mdk_rdev_t *rdev = bio2->bi_private;
595 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800596
597 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
598 error == -EOPNOTSUPP) {
599 unsigned long flags;
600 /* barriers don't appear to be supported :-( */
601 set_bit(BarriersNotsupp, &rdev->flags);
602 mddev->barriers_work = 0;
603 spin_lock_irqsave(&mddev->write_lock, flags);
604 bio2->bi_next = mddev->biolist;
605 mddev->biolist = bio2;
606 spin_unlock_irqrestore(&mddev->write_lock, flags);
607 wake_up(&mddev->sb_wait);
608 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200609 } else {
610 bio_put(bio2);
611 bio->bi_private = rdev;
612 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800613 }
NeilBrowna9701a32005-11-08 21:39:34 -0800614}
615
NeilBrown7bfa19f2005-06-21 17:17:28 -0700616void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
617 sector_t sector, int size, struct page *page)
618{
619 /* write first size bytes of page to sector of rdev
620 * Increment mddev->pending_writes before returning
621 * and decrement it on completion, waking up sb_wait
622 * if zero is reached.
623 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800624 *
625 * As we might need to resubmit the request if BIO_RW_BARRIER
626 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700627 */
628 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Jens Axboe93dbb392009-02-16 10:25:40 +0100629 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700630
631 bio->bi_bdev = rdev->bdev;
632 bio->bi_sector = sector;
633 bio_add_page(bio, page, size, 0);
634 bio->bi_private = rdev;
635 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800636 bio->bi_rw = rw;
637
NeilBrown7bfa19f2005-06-21 17:17:28 -0700638 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800639 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
640 struct bio *rbio;
641 rw |= (1<<BIO_RW_BARRIER);
642 rbio = bio_clone(bio, GFP_NOIO);
643 rbio->bi_private = bio;
644 rbio->bi_end_io = super_written_barrier;
645 submit_bio(rw, rbio);
646 } else
647 submit_bio(rw, bio);
648}
649
650void md_super_wait(mddev_t *mddev)
651{
652 /* wait for all superblock writes that were scheduled to complete.
653 * if any had to be retried (due to BARRIER problems), retry them
654 */
655 DEFINE_WAIT(wq);
656 for(;;) {
657 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
658 if (atomic_read(&mddev->pending_writes)==0)
659 break;
660 while (mddev->biolist) {
661 struct bio *bio;
662 spin_lock_irq(&mddev->write_lock);
663 bio = mddev->biolist;
664 mddev->biolist = bio->bi_next ;
665 bio->bi_next = NULL;
666 spin_unlock_irq(&mddev->write_lock);
667 submit_bio(bio->bi_rw, bio);
668 }
669 schedule();
670 }
671 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700672}
673
NeilBrown6712ecf2007-09-27 12:47:43 +0200674static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
NeilBrowna654b9d82005-06-21 17:17:27 -0700679int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 struct page *page, int rw)
681{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700682 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 struct completion event;
684 int ret;
685
Jens Axboe93dbb392009-02-16 10:25:40 +0100686 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 bio->bi_bdev = bdev;
689 bio->bi_sector = sector;
690 bio_add_page(bio, page, size, 0);
691 init_completion(&event);
692 bio->bi_private = &event;
693 bio->bi_end_io = bi_complete;
694 submit_bio(rw, bio);
695 wait_for_completion(&event);
696
697 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
698 bio_put(bio);
699 return ret;
700}
NeilBrowna8745db2006-01-06 00:20:34 -0800701EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
NeilBrown0002b272005-09-09 16:23:53 -0700703static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 char b[BDEVNAME_SIZE];
706 if (!rdev->sb_page) {
707 MD_BUG();
708 return -EINVAL;
709 }
710 if (rdev->sb_loaded)
711 return 0;
712
713
Andre Noll0f420352008-07-11 22:02:23 +1000714 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 goto fail;
716 rdev->sb_loaded = 1;
717 return 0;
718
719fail:
720 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
721 bdevname(rdev->bdev,b));
722 return -EINVAL;
723}
724
725static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
726{
Andre Noll05710462008-07-11 22:02:20 +1000727 return sb1->set_uuid0 == sb2->set_uuid0 &&
728 sb1->set_uuid1 == sb2->set_uuid1 &&
729 sb1->set_uuid2 == sb2->set_uuid2 &&
730 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
734{
735 int ret;
736 mdp_super_t *tmp1, *tmp2;
737
738 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
739 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
740
741 if (!tmp1 || !tmp2) {
742 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100743 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 goto abort;
745 }
746
747 *tmp1 = *sb1;
748 *tmp2 = *sb2;
749
750 /*
751 * nr_disks is not constant
752 */
753 tmp1->nr_disks = 0;
754 tmp2->nr_disks = 0;
755
Andre Nollce0c8e02008-07-11 22:02:20 +1000756 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700758 kfree(tmp1);
759 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return ret;
761}
762
NeilBrown4d167f02007-05-09 02:35:37 -0700763
764static u32 md_csum_fold(u32 csum)
765{
766 csum = (csum & 0xffff) + (csum >> 16);
767 return (csum & 0xffff) + (csum >> 16);
768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770static unsigned int calc_sb_csum(mdp_super_t * sb)
771{
NeilBrown4d167f02007-05-09 02:35:37 -0700772 u64 newcsum = 0;
773 u32 *sb32 = (u32*)sb;
774 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 unsigned int disk_csum, csum;
776
777 disk_csum = sb->sb_csum;
778 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700779
780 for (i = 0; i < MD_SB_BYTES/4 ; i++)
781 newcsum += sb32[i];
782 csum = (newcsum & 0xffffffff) + (newcsum>>32);
783
784
785#ifdef CONFIG_ALPHA
786 /* This used to use csum_partial, which was wrong for several
787 * reasons including that different results are returned on
788 * different architectures. It isn't critical that we get exactly
789 * the same return value as before (we always csum_fold before
790 * testing, and that removes any differences). However as we
791 * know that csum_partial always returned a 16bit value on
792 * alphas, do a fold to maximise conformity to previous behaviour.
793 */
794 sb->sb_csum = md_csum_fold(disk_csum);
795#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700797#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return csum;
799}
800
801
802/*
803 * Handle superblock details.
804 * We want to be able to handle multiple superblock formats
805 * so we have a common interface to them all, and an array of
806 * different handlers.
807 * We rely on user-space to write the initial superblock, and support
808 * reading and updating of superblocks.
809 * Interface methods are:
810 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
811 * loads and validates a superblock on dev.
812 * if refdev != NULL, compare superblocks on both devices
813 * Return:
814 * 0 - dev has a superblock that is compatible with refdev
815 * 1 - dev has a superblock that is compatible and newer than refdev
816 * so dev should be used as the refdev in future
817 * -EINVAL superblock incompatible or invalid
818 * -othererror e.g. -EIO
819 *
820 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
821 * Verify that dev is acceptable into mddev.
822 * The first time, mddev->raid_disks will be 0, and data from
823 * dev should be merged in. Subsequent calls check that dev
824 * is new enough. Return 0 or -EINVAL
825 *
826 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
827 * Update the superblock for rdev with data in mddev
828 * This does not write to disc.
829 *
830 */
831
832struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000833 char *name;
834 struct module *owner;
835 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
836 int minor_version);
837 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
838 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
839 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000840 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841};
842
843/*
Andre Noll0894cc32009-06-18 08:49:23 +1000844 * Check that the given mddev has no bitmap.
845 *
846 * This function is called from the run method of all personalities that do not
847 * support bitmaps. It prints an error message and returns non-zero if mddev
848 * has a bitmap. Otherwise, it returns 0.
849 *
850 */
851int md_check_no_bitmap(mddev_t *mddev)
852{
NeilBrownc3d97142009-12-14 12:49:52 +1100853 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000854 return 0;
855 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
856 mdname(mddev), mddev->pers->name);
857 return 1;
858}
859EXPORT_SYMBOL(md_check_no_bitmap);
860
861/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 * load_super for 0.90.0
863 */
864static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
865{
866 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
867 mdp_super_t *sb;
868 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 /*
Andre Noll0f420352008-07-11 22:02:23 +1000871 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 * it's at the end of the disk.
873 *
874 * It also happens to be a multiple of 4Kb.
875 */
Andre Noll0f420352008-07-11 22:02:23 +1000876 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
NeilBrown0002b272005-09-09 16:23:53 -0700878 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (ret) return ret;
880
881 ret = -EINVAL;
882
883 bdevname(rdev->bdev, b);
884 sb = (mdp_super_t*)page_address(rdev->sb_page);
885
886 if (sb->md_magic != MD_SB_MAGIC) {
887 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
888 b);
889 goto abort;
890 }
891
892 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800893 sb->minor_version < 90 ||
894 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
896 sb->major_version, sb->minor_version,
897 b);
898 goto abort;
899 }
900
901 if (sb->raid_disks <= 0)
902 goto abort;
903
NeilBrown4d167f02007-05-09 02:35:37 -0700904 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
906 b);
907 goto abort;
908 }
909
910 rdev->preferred_minor = sb->md_minor;
911 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700912 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 if (sb->level == LEVEL_MULTIPATH)
915 rdev->desc_nr = -1;
916 else
917 rdev->desc_nr = sb->this_disk.number;
918
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700919 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700921 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 __u64 ev1, ev2;
923 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
924 if (!uuid_equal(refsb, sb)) {
925 printk(KERN_WARNING "md: %s has different UUID to %s\n",
926 b, bdevname(refdev->bdev,b2));
927 goto abort;
928 }
929 if (!sb_equal(refsb, sb)) {
930 printk(KERN_WARNING "md: %s has same UUID"
931 " but different superblock to %s\n",
932 b, bdevname(refdev->bdev, b2));
933 goto abort;
934 }
935 ev1 = md_event(sb);
936 ev2 = md_event(refsb);
937 if (ev1 > ev2)
938 ret = 1;
939 else
940 ret = 0;
941 }
NeilBrown8190e752009-06-18 08:48:58 +1000942 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Andre Nolldd8ac332009-03-31 14:33:13 +1100944 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -0800945 /* "this cannot possibly happen" ... */
946 ret = -EINVAL;
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 abort:
949 return ret;
950}
951
952/*
953 * validate_super for 0.90.0
954 */
955static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
956{
957 mdp_disk_t *desc;
958 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700959 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
NeilBrown41158c72005-06-21 17:17:25 -0700961 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800962 clear_bit(Faulty, &rdev->flags);
963 clear_bit(In_sync, &rdev->flags);
964 clear_bit(WriteMostly, &rdev->flags);
965 clear_bit(BarriersNotsupp, &rdev->flags);
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (mddev->raid_disks == 0) {
968 mddev->major_version = 0;
969 mddev->minor_version = sb->minor_version;
970 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800971 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +1000972 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 mddev->ctime = sb->ctime;
974 mddev->utime = sb->utime;
975 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800976 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 mddev->layout = sb->layout;
978 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +1100979 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -0700980 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +1100981 mddev->bitmap_info.offset = 0;
982 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
NeilBrownf6705572006-03-27 01:18:11 -0800984 if (mddev->minor_version >= 91) {
985 mddev->reshape_position = sb->reshape_position;
986 mddev->delta_disks = sb->delta_disks;
987 mddev->new_level = sb->new_level;
988 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +1000989 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -0800990 } else {
991 mddev->reshape_position = MaxSector;
992 mddev->delta_disks = 0;
993 mddev->new_level = mddev->level;
994 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +1000995 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -0800996 }
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (sb->state & (1<<MD_SB_CLEAN))
999 mddev->recovery_cp = MaxSector;
1000 else {
1001 if (sb->events_hi == sb->cp_events_hi &&
1002 sb->events_lo == sb->cp_events_lo) {
1003 mddev->recovery_cp = sb->recovery_cp;
1004 } else
1005 mddev->recovery_cp = 0;
1006 }
1007
1008 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1009 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1010 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1011 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1012
1013 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001014
1015 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001016 mddev->bitmap_info.file == NULL)
1017 mddev->bitmap_info.offset =
1018 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001019
NeilBrown41158c72005-06-21 17:17:25 -07001020 } else if (mddev->pers == NULL) {
1021 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 ++ev1;
1023 if (ev1 < mddev->events)
1024 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001025 } else if (mddev->bitmap) {
1026 /* if adding to array with a bitmap, then we can accept an
1027 * older device ... but not too old.
1028 */
NeilBrown41158c72005-06-21 17:17:25 -07001029 if (ev1 < mddev->bitmap->events_cleared)
1030 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001031 } else {
1032 if (ev1 < mddev->events)
1033 /* just a hot-add of a new device, leave raid_disk at -1 */
1034 return 0;
1035 }
NeilBrown41158c72005-06-21 17:17:25 -07001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 desc = sb->disks + rdev->desc_nr;
1039
1040 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001041 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001042 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1043 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001044 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd92009-11-13 17:40:48 +11001046 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1047 /* active but not in sync implies recovery up to
1048 * reshape position. We don't know exactly where
1049 * that is, so set to zero for now */
1050 if (mddev->minor_version >= 91) {
1051 rdev->recovery_offset = 0;
1052 rdev->raid_disk = desc->raid_disk;
1053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001055 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1056 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001057 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001058 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return 0;
1060}
1061
1062/*
1063 * sync_super for 0.90.0
1064 */
1065static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1066{
1067 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 mdk_rdev_t *rdev2;
1069 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 /* make rdev->sb match mddev data..
1073 *
1074 * 1/ zero out disks
1075 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1076 * 3/ any empty disks < next_spare become removed
1077 *
1078 * disks[0] gets initialised to REMOVED because
1079 * we cannot be sure from other fields if it has
1080 * been initialised or not.
1081 */
1082 int i;
1083 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1084
NeilBrown61181562005-09-09 16:24:02 -07001085 rdev->sb_size = MD_SB_BYTES;
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 sb = (mdp_super_t*)page_address(rdev->sb_page);
1088
1089 memset(sb, 0, sizeof(*sb));
1090
1091 sb->md_magic = MD_SB_MAGIC;
1092 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 sb->patch_version = mddev->patch_version;
1094 sb->gvalid_words = 0; /* ignored */
1095 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1096 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1097 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1098 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1099
1100 sb->ctime = mddev->ctime;
1101 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001102 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 sb->raid_disks = mddev->raid_disks;
1104 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001105 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 sb->utime = mddev->utime;
1107 sb->state = 0;
1108 sb->events_hi = (mddev->events>>32);
1109 sb->events_lo = (u32)mddev->events;
1110
NeilBrownf6705572006-03-27 01:18:11 -08001111 if (mddev->reshape_position == MaxSector)
1112 sb->minor_version = 90;
1113 else {
1114 sb->minor_version = 91;
1115 sb->reshape_position = mddev->reshape_position;
1116 sb->new_level = mddev->new_level;
1117 sb->delta_disks = mddev->delta_disks;
1118 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001119 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001120 }
1121 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (mddev->in_sync)
1123 {
1124 sb->recovery_cp = mddev->recovery_cp;
1125 sb->cp_events_hi = (mddev->events>>32);
1126 sb->cp_events_lo = (u32)mddev->events;
1127 if (mddev->recovery_cp == MaxSector)
1128 sb->state = (1<< MD_SB_CLEAN);
1129 } else
1130 sb->recovery_cp = 0;
1131
1132 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001133 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
NeilBrownc3d97142009-12-14 12:49:52 +11001135 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001136 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001139 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001141 int desc_nr;
NeilBrown0261cd92009-11-13 17:40:48 +11001142 int is_active = test_bit(In_sync, &rdev2->flags);
1143
1144 if (rdev2->raid_disk >= 0 &&
1145 sb->minor_version >= 91)
1146 /* we have nowhere to store the recovery_offset,
1147 * but if it is not below the reshape_position,
1148 * we can piggy-back on that.
1149 */
1150 is_active = 1;
1151 if (rdev2->raid_disk < 0 ||
1152 test_bit(Faulty, &rdev2->flags))
1153 is_active = 0;
1154 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001155 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001157 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001158 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 d = &sb->disks[rdev2->desc_nr];
1160 nr_disks++;
1161 d->number = rdev2->desc_nr;
1162 d->major = MAJOR(rdev2->bdev->bd_dev);
1163 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd92009-11-13 17:40:48 +11001164 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 d->raid_disk = rdev2->raid_disk;
1166 else
1167 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001168 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd92009-11-13 17:40:48 +11001170 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd92009-11-13 17:40:48 +11001172 if (test_bit(In_sync, &rdev2->flags))
1173 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 active++;
1175 working++;
1176 } else {
1177 d->state = 0;
1178 spare++;
1179 working++;
1180 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001181 if (test_bit(WriteMostly, &rdev2->flags))
1182 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 /* now set the "removed" and "faulty" bits on any missing devices */
1185 for (i=0 ; i < mddev->raid_disks ; i++) {
1186 mdp_disk_t *d = &sb->disks[i];
1187 if (d->state == 0 && d->number == 0) {
1188 d->number = i;
1189 d->raid_disk = i;
1190 d->state = (1<<MD_DISK_REMOVED);
1191 d->state |= (1<<MD_DISK_FAULTY);
1192 failed++;
1193 }
1194 }
1195 sb->nr_disks = nr_disks;
1196 sb->active_disks = active;
1197 sb->working_disks = working;
1198 sb->failed_disks = failed;
1199 sb->spare_disks = spare;
1200
1201 sb->this_disk = sb->disks[rdev->desc_nr];
1202 sb->sb_csum = calc_sb_csum(sb);
1203}
1204
1205/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001206 * rdev_size_change for 0.90.0
1207 */
1208static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001209super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001210{
Andre Noll58c0fed2009-03-31 14:33:13 +11001211 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001212 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001213 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001214 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001215 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001216 if (!num_sectors || num_sectors > rdev->sb_start)
1217 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001218 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001219 rdev->sb_page);
1220 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001221 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001222}
1223
1224
1225/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 * version 1 superblock
1227 */
1228
NeilBrown1c05b4b2006-10-21 10:24:08 -07001229static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001231 __le32 disk_csum;
1232 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 unsigned long long newcsum;
1234 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001235 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 int i;
1237
1238 disk_csum = sb->sb_csum;
1239 sb->sb_csum = 0;
1240 newcsum = 0;
1241 for (i=0; size>=4; size -= 4 )
1242 newcsum += le32_to_cpu(*isuper++);
1243
1244 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001245 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1248 sb->sb_csum = disk_csum;
1249 return cpu_to_le32(csum);
1250}
1251
1252static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1253{
1254 struct mdp_superblock_1 *sb;
1255 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001256 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001258 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 /*
Andre Noll0f420352008-07-11 22:02:23 +10001261 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 * It is always aligned to a 4K boundary and
1263 * depeding on minor_version, it can be:
1264 * 0: At least 8K, but less than 12K, from end of device
1265 * 1: At start of device
1266 * 2: 4K from start of device.
1267 */
1268 switch(minor_version) {
1269 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001270 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1271 sb_start -= 8*2;
1272 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 break;
1274 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001275 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 break;
1277 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001278 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 break;
1280 default:
1281 return -EINVAL;
1282 }
Andre Noll0f420352008-07-11 22:02:23 +10001283 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
NeilBrown0002b272005-09-09 16:23:53 -07001285 /* superblock is rarely larger than 1K, but it can be larger,
1286 * and it is safe to read 4k, so we do that
1287 */
1288 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (ret) return ret;
1290
1291
1292 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1293
1294 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1295 sb->major_version != cpu_to_le32(1) ||
1296 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001297 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001298 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return -EINVAL;
1300
1301 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1302 printk("md: invalid superblock checksum on %s\n",
1303 bdevname(rdev->bdev,b));
1304 return -EINVAL;
1305 }
1306 if (le64_to_cpu(sb->data_size) < 10) {
1307 printk("md: data_size too small on %s\n",
1308 bdevname(rdev->bdev,b));
1309 return -EINVAL;
1310 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 rdev->preferred_minor = 0xffff;
1313 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001314 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
NeilBrown0002b272005-09-09 16:23:53 -07001316 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001317 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001318 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001319 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1320
1321 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001322 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001323 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001324
NeilBrown31b65a02006-07-10 04:44:14 -07001325 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1326 rdev->desc_nr = -1;
1327 else
1328 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1329
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001330 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001331 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001332 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 __u64 ev1, ev2;
1334 struct mdp_superblock_1 *refsb =
1335 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1336
1337 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1338 sb->level != refsb->level ||
1339 sb->layout != refsb->layout ||
1340 sb->chunksize != refsb->chunksize) {
1341 printk(KERN_WARNING "md: %s has strangely different"
1342 " superblock to %s\n",
1343 bdevname(rdev->bdev,b),
1344 bdevname(refdev->bdev,b2));
1345 return -EINVAL;
1346 }
1347 ev1 = le64_to_cpu(sb->events);
1348 ev2 = le64_to_cpu(refsb->events);
1349
1350 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001351 ret = 1;
1352 else
1353 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
NeilBrowna1801f82008-03-04 14:29:31 -08001355 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001356 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1357 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001359 rdev->sectors = rdev->sb_start;
1360 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001362 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001363 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001364 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001365 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366}
1367
1368static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1369{
1370 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001371 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
NeilBrown41158c72005-06-21 17:17:25 -07001373 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001374 clear_bit(Faulty, &rdev->flags);
1375 clear_bit(In_sync, &rdev->flags);
1376 clear_bit(WriteMostly, &rdev->flags);
1377 clear_bit(BarriersNotsupp, &rdev->flags);
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (mddev->raid_disks == 0) {
1380 mddev->major_version = 1;
1381 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001382 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001383 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1385 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1386 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001387 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 mddev->layout = le32_to_cpu(sb->layout);
1389 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001390 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001391 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001392 mddev->bitmap_info.offset = 0;
1393 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1396 memcpy(mddev->uuid, sb->set_uuid, 16);
1397
1398 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001399
NeilBrown71c08052005-09-09 16:23:51 -07001400 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001401 mddev->bitmap_info.file == NULL )
1402 mddev->bitmap_info.offset =
1403 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001404
NeilBrownf6705572006-03-27 01:18:11 -08001405 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1406 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1407 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1408 mddev->new_level = le32_to_cpu(sb->new_level);
1409 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001410 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001411 } else {
1412 mddev->reshape_position = MaxSector;
1413 mddev->delta_disks = 0;
1414 mddev->new_level = mddev->level;
1415 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001416 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001417 }
1418
NeilBrown41158c72005-06-21 17:17:25 -07001419 } else if (mddev->pers == NULL) {
1420 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 ++ev1;
1422 if (ev1 < mddev->events)
1423 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001424 } else if (mddev->bitmap) {
1425 /* If adding to array with a bitmap, then we can accept an
1426 * older device, but not too old.
1427 */
NeilBrown41158c72005-06-21 17:17:25 -07001428 if (ev1 < mddev->bitmap->events_cleared)
1429 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001430 } else {
1431 if (ev1 < mddev->events)
1432 /* just a hot-add of a new device, leave raid_disk at -1 */
1433 return 0;
1434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (mddev->level != LEVEL_MULTIPATH) {
1436 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001437 if (rdev->desc_nr < 0 ||
1438 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1439 role = 0xffff;
1440 rdev->desc_nr = -1;
1441 } else
1442 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 switch(role) {
1444 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 break;
1446 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001447 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 break;
1449 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001450 if ((le32_to_cpu(sb->feature_map) &
1451 MD_FEATURE_RECOVERY_OFFSET))
1452 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1453 else
1454 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 rdev->raid_disk = role;
1456 break;
1457 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001458 if (sb->devflags & WriteMostly1)
1459 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001460 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001461 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return 0;
1464}
1465
1466static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1467{
1468 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 mdk_rdev_t *rdev2;
1470 int max_dev, i;
1471 /* make rdev->sb match mddev and rdev data. */
1472
1473 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1474
1475 sb->feature_map = 0;
1476 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001477 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 memset(sb->pad1, 0, sizeof(sb->pad1));
1479 memset(sb->pad2, 0, sizeof(sb->pad2));
1480 memset(sb->pad3, 0, sizeof(sb->pad3));
1481
1482 sb->utime = cpu_to_le64((__u64)mddev->utime);
1483 sb->events = cpu_to_le64(mddev->events);
1484 if (mddev->in_sync)
1485 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1486 else
1487 sb->resync_offset = cpu_to_le64(0);
1488
NeilBrown1c05b4b2006-10-21 10:24:08 -07001489 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001490
NeilBrownf0ca3402006-02-02 14:28:04 -08001491 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001492 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001493 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001494 sb->level = cpu_to_le32(mddev->level);
1495 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001496
NeilBrownc3d97142009-12-14 12:49:52 +11001497 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1498 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001499 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001500 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001501
1502 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001503 !test_bit(In_sync, &rdev->flags)) {
NeilBrown97e4f422009-03-31 14:33:13 +11001504 if (rdev->recovery_offset > 0) {
1505 sb->feature_map |=
1506 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1507 sb->recovery_offset =
1508 cpu_to_le64(rdev->recovery_offset);
1509 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001510 }
1511
NeilBrownf6705572006-03-27 01:18:11 -08001512 if (mddev->reshape_position != MaxSector) {
1513 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1514 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1515 sb->new_layout = cpu_to_le32(mddev->new_layout);
1516 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1517 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001518 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001519 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001522 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (rdev2->desc_nr+1 > max_dev)
1524 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001525
NeilBrown70471da2009-08-03 10:59:57 +10001526 if (max_dev > le32_to_cpu(sb->max_dev)) {
1527 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001528 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001529 rdev->sb_size = max_dev * 2 + 256;
1530 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1531 if (rdev->sb_size & bmask)
1532 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 for (i=0; i<max_dev;i++)
1535 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1536
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001537 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001539 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001541 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001543 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1544 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 else
1546 sb->dev_roles[i] = cpu_to_le16(0xffff);
1547 }
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 sb->sb_csum = calc_sb_1_csum(sb);
1550}
1551
Chris Webb0cd17fe2008-06-28 08:31:46 +10001552static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001553super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001554{
1555 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001556 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001557 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001558 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001559 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001560 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001561 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1562 max_sectors -= rdev->data_offset;
1563 if (!num_sectors || num_sectors > max_sectors)
1564 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001565 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001566 /* minor version 0 with bitmap we can't move */
1567 return 0;
1568 } else {
1569 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001570 sector_t sb_start;
1571 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1572 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001573 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001574 if (!num_sectors || num_sectors > max_sectors)
1575 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001576 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001577 }
1578 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001579 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001580 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001581 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001582 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001583 rdev->sb_page);
1584 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001585 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001586}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Adrian Bunk75c96f82005-05-05 16:16:09 -07001588static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 [0] = {
1590 .name = "0.90.0",
1591 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001592 .load_super = super_90_load,
1593 .validate_super = super_90_validate,
1594 .sync_super = super_90_sync,
1595 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 },
1597 [1] = {
1598 .name = "md-1",
1599 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001600 .load_super = super_1_load,
1601 .validate_super = super_1_validate,
1602 .sync_super = super_1_sync,
1603 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 },
1605};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1608{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001609 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
NeilBrown4b809912008-07-21 17:05:25 +10001611 rcu_read_lock();
1612 rdev_for_each_rcu(rdev, mddev1)
1613 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001614 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001615 rdev2->bdev->bd_contains) {
1616 rcu_read_unlock();
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001617 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001618 }
1619 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 return 0;
1621}
1622
1623static LIST_HEAD(pending_raid_disks);
1624
Andre Nollac5e7112009-08-03 10:59:47 +10001625/*
1626 * Try to register data integrity profile for an mddev
1627 *
1628 * This is called when an array is started and after a disk has been kicked
1629 * from the array. It only succeeds if all working and active component devices
1630 * are integrity capable with matching profiles.
1631 */
1632int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001633{
Andre Nollac5e7112009-08-03 10:59:47 +10001634 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001635
Andre Nollac5e7112009-08-03 10:59:47 +10001636 if (list_empty(&mddev->disks))
1637 return 0; /* nothing to do */
1638 if (blk_get_integrity(mddev->gendisk))
1639 return 0; /* already registered */
1640 list_for_each_entry(rdev, &mddev->disks, same_set) {
1641 /* skip spares and non-functional disks */
1642 if (test_bit(Faulty, &rdev->flags))
1643 continue;
1644 if (rdev->raid_disk < 0)
1645 continue;
1646 /*
1647 * If at least one rdev is not integrity capable, we can not
1648 * enable data integrity for the md device.
1649 */
1650 if (!bdev_get_integrity(rdev->bdev))
1651 return -EINVAL;
1652 if (!reference) {
1653 /* Use the first rdev as the reference */
1654 reference = rdev;
1655 continue;
1656 }
1657 /* does this rdev's profile match the reference profile? */
1658 if (blk_integrity_compare(reference->bdev->bd_disk,
1659 rdev->bdev->bd_disk) < 0)
1660 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001661 }
Andre Nollac5e7112009-08-03 10:59:47 +10001662 /*
1663 * All component devices are integrity capable and have matching
1664 * profiles, register the common profile for the md device.
1665 */
1666 if (blk_integrity_register(mddev->gendisk,
1667 bdev_get_integrity(reference->bdev)) != 0) {
1668 printk(KERN_ERR "md: failed to register integrity for %s\n",
1669 mdname(mddev));
1670 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001671 }
Andre Nollac5e7112009-08-03 10:59:47 +10001672 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1673 mdname(mddev));
1674 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001675}
Andre Nollac5e7112009-08-03 10:59:47 +10001676EXPORT_SYMBOL(md_integrity_register);
1677
1678/* Disable data integrity if non-capable/non-matching disk is being added */
1679void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1680{
1681 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1682 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1683
1684 if (!bi_mddev) /* nothing to do */
1685 return;
1686 if (rdev->raid_disk < 0) /* skip spares */
1687 return;
1688 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1689 rdev->bdev->bd_disk) >= 0)
1690 return;
1691 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1692 blk_integrity_unregister(mddev->gendisk);
1693}
1694EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1697{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001698 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001699 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001700 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001701 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703 if (rdev->mddev) {
1704 MD_BUG();
1705 return -EINVAL;
1706 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001707
1708 /* prevent duplicates */
1709 if (find_rdev(mddev, rdev->bdev->bd_dev))
1710 return -EEXIST;
1711
Andre Nolldd8ac332009-03-31 14:33:13 +11001712 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1713 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1714 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001715 if (mddev->pers) {
1716 /* Cannot change size, so fail
1717 * If mddev->level <= 0, then we don't care
1718 * about aligning sizes (e.g. linear)
1719 */
1720 if (mddev->level > 0)
1721 return -ENOSPC;
1722 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001723 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 /* Verify rdev->desc_nr is unique.
1727 * If it is -1, assign a free number, else
1728 * check number is not in use
1729 */
1730 if (rdev->desc_nr < 0) {
1731 int choice = 0;
1732 if (mddev->pers) choice = mddev->raid_disks;
1733 while (find_rdev_nr(mddev, choice))
1734 choice++;
1735 rdev->desc_nr = choice;
1736 } else {
1737 if (find_rdev_nr(mddev, rdev->desc_nr))
1738 return -EBUSY;
1739 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001740 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1741 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1742 mdname(mddev), mddev->max_disks);
1743 return -EBUSY;
1744 }
NeilBrown19133a42005-11-08 21:39:35 -08001745 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001746 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001747 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001750 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001751
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001752 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001753 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001754
Tejun Heo0762b8b2008-08-25 19:56:12 +09001755 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001756 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1757 kobject_del(&rdev->kobj);
1758 goto fail;
1759 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001760 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1761
NeilBrown4b809912008-07-21 17:05:25 +10001762 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001763 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001764
1765 /* May as well allow recovery to be retried once */
1766 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001769
1770 fail:
1771 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1772 b, mdname(mddev));
1773 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
NeilBrown177a99b2008-02-06 01:39:56 -08001776static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001777{
1778 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1779 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001780 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001781}
1782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1784{
1785 char b[BDEVNAME_SIZE];
1786 if (!rdev->mddev) {
1787 MD_BUG();
1788 return;
1789 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001790 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001791 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1793 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001794 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001795 sysfs_put(rdev->sysfs_state);
1796 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001797 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001798 * writing to 'remove' to "dev/state". We also need
1799 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001800 */
NeilBrown4b809912008-07-21 17:05:25 +10001801 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001802 INIT_WORK(&rdev->del_work, md_delayed_delete);
1803 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001804 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
1807/*
1808 * prevent the device from being mounted, repartitioned or
1809 * otherwise reused by a RAID array (or any other kernel
1810 * subsystem), by bd_claiming the device.
1811 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001812static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
1814 int err = 0;
1815 struct block_device *bdev;
1816 char b[BDEVNAME_SIZE];
1817
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001818 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (IS_ERR(bdev)) {
1820 printk(KERN_ERR "md: could not open %s.\n",
1821 __bdevname(dev, b));
1822 return PTR_ERR(bdev);
1823 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001824 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (err) {
1826 printk(KERN_ERR "md: could not bd_claim %s.\n",
1827 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001828 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 return err;
1830 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001831 if (!shared)
1832 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 rdev->bdev = bdev;
1834 return err;
1835}
1836
1837static void unlock_rdev(mdk_rdev_t *rdev)
1838{
1839 struct block_device *bdev = rdev->bdev;
1840 rdev->bdev = NULL;
1841 if (!bdev)
1842 MD_BUG();
1843 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001844 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845}
1846
1847void md_autodetect_dev(dev_t dev);
1848
1849static void export_rdev(mdk_rdev_t * rdev)
1850{
1851 char b[BDEVNAME_SIZE];
1852 printk(KERN_INFO "md: export_rdev(%s)\n",
1853 bdevname(rdev->bdev,b));
1854 if (rdev->mddev)
1855 MD_BUG();
1856 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001858 if (test_bit(AutoDetected, &rdev->flags))
1859 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860#endif
1861 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001862 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863}
1864
1865static void kick_rdev_from_array(mdk_rdev_t * rdev)
1866{
1867 unbind_rdev_from_array(rdev);
1868 export_rdev(rdev);
1869}
1870
1871static void export_array(mddev_t *mddev)
1872{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001873 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
NeilBrownd089c6a2008-02-06 01:39:59 -08001875 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 if (!rdev->mddev) {
1877 MD_BUG();
1878 continue;
1879 }
1880 kick_rdev_from_array(rdev);
1881 }
1882 if (!list_empty(&mddev->disks))
1883 MD_BUG();
1884 mddev->raid_disks = 0;
1885 mddev->major_version = 0;
1886}
1887
1888static void print_desc(mdp_disk_t *desc)
1889{
1890 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1891 desc->major,desc->minor,desc->raid_disk,desc->state);
1892}
1893
Cheng Renquancd2ac932009-01-09 08:31:08 +11001894static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
1896 int i;
1897
1898 printk(KERN_INFO
1899 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1900 sb->major_version, sb->minor_version, sb->patch_version,
1901 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1902 sb->ctime);
1903 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1904 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1905 sb->md_minor, sb->layout, sb->chunk_size);
1906 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1907 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1908 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1909 sb->failed_disks, sb->spare_disks,
1910 sb->sb_csum, (unsigned long)sb->events_lo);
1911
1912 printk(KERN_INFO);
1913 for (i = 0; i < MD_SB_DISKS; i++) {
1914 mdp_disk_t *desc;
1915
1916 desc = sb->disks + i;
1917 if (desc->number || desc->major || desc->minor ||
1918 desc->raid_disk || (desc->state && (desc->state != 4))) {
1919 printk(" D %2d: ", i);
1920 print_desc(desc);
1921 }
1922 }
1923 printk(KERN_INFO "md: THIS: ");
1924 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
1926
Cheng Renquancd2ac932009-01-09 08:31:08 +11001927static void print_sb_1(struct mdp_superblock_1 *sb)
1928{
1929 __u8 *uuid;
1930
1931 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001932 printk(KERN_INFO
1933 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1934 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1935 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001936 le32_to_cpu(sb->major_version),
1937 le32_to_cpu(sb->feature_map),
1938 uuid[0], uuid[1], uuid[2], uuid[3],
1939 uuid[4], uuid[5], uuid[6], uuid[7],
1940 uuid[8], uuid[9], uuid[10], uuid[11],
1941 uuid[12], uuid[13], uuid[14], uuid[15],
1942 sb->set_name,
1943 (unsigned long long)le64_to_cpu(sb->ctime)
1944 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1945
1946 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001947 printk(KERN_INFO
1948 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11001949 " RO:%llu\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001950 "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1951 ":%02x%02x%02x%02x%02x%02x\n"
1952 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1953 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001954 le32_to_cpu(sb->level),
1955 (unsigned long long)le64_to_cpu(sb->size),
1956 le32_to_cpu(sb->raid_disks),
1957 le32_to_cpu(sb->layout),
1958 le32_to_cpu(sb->chunksize),
1959 (unsigned long long)le64_to_cpu(sb->data_offset),
1960 (unsigned long long)le64_to_cpu(sb->data_size),
1961 (unsigned long long)le64_to_cpu(sb->super_offset),
1962 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1963 le32_to_cpu(sb->dev_number),
1964 uuid[0], uuid[1], uuid[2], uuid[3],
1965 uuid[4], uuid[5], uuid[6], uuid[7],
1966 uuid[8], uuid[9], uuid[10], uuid[11],
1967 uuid[12], uuid[13], uuid[14], uuid[15],
1968 sb->devflags,
1969 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1970 (unsigned long long)le64_to_cpu(sb->events),
1971 (unsigned long long)le64_to_cpu(sb->resync_offset),
1972 le32_to_cpu(sb->sb_csum),
1973 le32_to_cpu(sb->max_dev)
1974 );
1975}
1976
1977static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
1979 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11001980 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1981 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08001982 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1983 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11001985 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1986 switch (major_version) {
1987 case 0:
1988 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1989 break;
1990 case 1:
1991 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1992 break;
1993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 } else
1995 printk(KERN_INFO "md: no rdev superblock!\n");
1996}
1997
Adrian Bunk5e563412006-06-26 00:27:42 -07001998static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002000 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 mdk_rdev_t *rdev;
2002 mddev_t *mddev;
2003 char b[BDEVNAME_SIZE];
2004
2005 printk("\n");
2006 printk("md: **********************************\n");
2007 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2008 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002009 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
NeilBrown32a76272005-06-21 17:17:14 -07002011 if (mddev->bitmap)
2012 bitmap_print_sb(mddev->bitmap);
2013 else
2014 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002015 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 printk("<%s>", bdevname(rdev->bdev,b));
2017 printk("\n");
2018
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002019 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002020 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
2022 printk("md: **********************************\n");
2023 printk("\n");
2024}
2025
2026
NeilBrown42543762006-06-26 00:27:57 -07002027static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
NeilBrown42543762006-06-26 00:27:57 -07002029 /* Update each superblock (in-memory image), but
2030 * if we are allowed to, skip spares which already
2031 * have the right event counter, or have one earlier
2032 * (which would mean they aren't being marked as dirty
2033 * with the rest of the array)
2034 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
NeilBrown5e865102009-11-12 12:08:04 +11002037 /* First make sure individual recovery_offsets are correct */
2038 list_for_each_entry(rdev, &mddev->disks, same_set) {
2039 if (rdev->raid_disk >= 0 &&
2040 !test_bit(In_sync, &rdev->flags) &&
2041 mddev->curr_resync_completed > rdev->recovery_offset)
2042 rdev->recovery_offset = mddev->curr_resync_completed;
2043
2044 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002045 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002046 if (rdev->sb_events == mddev->events ||
2047 (nospares &&
2048 rdev->raid_disk < 0 &&
2049 (rdev->sb_events&1)==0 &&
2050 rdev->sb_events+1 == mddev->events)) {
2051 /* Don't update this superblock */
2052 rdev->sb_loaded = 2;
2053 } else {
2054 super_types[mddev->major_version].
2055 sync_super(mddev, rdev);
2056 rdev->sb_loaded = 1;
2057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 }
2059}
2060
NeilBrown850b2b42006-10-03 01:15:46 -07002061static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002064 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002065 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
NeilBrown1b57f132009-06-18 08:48:19 +10002067 mddev->utime = get_seconds();
NeilBrown8377bc82008-04-30 00:52:28 -07002068 if (mddev->external)
2069 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08002071 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002072
NeilBrown850b2b42006-10-03 01:15:46 -07002073 set_bit(MD_CHANGE_PENDING, &mddev->flags);
2074 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2075 force_change = 1;
2076 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2077 /* just a clean<-> dirty transition, possibly leave spares alone,
2078 * though if events isn't the right even/odd, we will have to do
2079 * spares after all
2080 */
2081 nospares = 1;
2082 if (force_change)
2083 nospares = 0;
2084 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002085 /* If the array is degraded, then skipping spares is both
2086 * dangerous and fairly pointless.
2087 * Dangerous because a device that was removed from the array
2088 * might have a event_count that still looks up-to-date,
2089 * so it can be re-added without a resync.
2090 * Pointless because if there are any spares to skip,
2091 * then a recovery will happen and soon that array won't
2092 * be degraded any more and the spare can go back to sleep then.
2093 */
NeilBrown850b2b42006-10-03 01:15:46 -07002094 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002095
NeilBrown06d91a52005-06-21 17:17:12 -07002096 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002097
2098 /* If this is just a dirty<->clean transition, and the array is clean
2099 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002100 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002101 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08002102 && (mddev->events & 1)
2103 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07002104 mddev->events--;
2105 else {
2106 /* otherwise we have to go forward and ... */
2107 mddev->events ++;
2108 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
NeilBrown51d56682009-08-13 09:54:02 +10002109 /* .. if the array isn't clean, an 'even' event must also go
2110 * to spares. */
2111 if ((mddev->events&1)==0)
NeilBrown42543762006-06-26 00:27:57 -07002112 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002113 } else {
NeilBrown51d56682009-08-13 09:54:02 +10002114 /* otherwise an 'odd' event must go to spares */
2115 if ((mddev->events&1))
NeilBrown42543762006-06-26 00:27:57 -07002116 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002117 }
2118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
2120 if (!mddev->events) {
2121 /*
2122 * oops, this 64-bit counter should never wrap.
2123 * Either we are in around ~1 trillion A.C., assuming
2124 * 1 reboot per second, or we have a bug:
2125 */
2126 MD_BUG();
2127 mddev->events --;
2128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 /*
2131 * do not write anything to disk if using
2132 * nonpersistent superblocks
2133 */
NeilBrown06d91a52005-06-21 17:17:12 -07002134 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08002135 if (!mddev->external)
2136 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2137
NeilBrowna9701a32005-11-08 21:39:34 -08002138 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002139 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return;
NeilBrown06d91a52005-06-21 17:17:12 -07002141 }
NeilBrowne6910632008-02-06 01:39:51 -08002142 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002143 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 dprintk(KERN_INFO
2146 "md: updating %s RAID superblock on device (in sync %d)\n",
2147 mdname(mddev),mddev->in_sync);
2148
NeilBrown4ad13662007-07-17 04:06:13 -07002149 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002150 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 char b[BDEVNAME_SIZE];
2152 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002153 if (rdev->sb_loaded != 1)
2154 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002155 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 dprintk("(skipping faulty ");
2157
2158 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002159 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002160 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002161 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002162 rdev->sb_page);
2163 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2164 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002165 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002166 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 } else
2169 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002170 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 /* only need to write one superblock... */
2172 break;
2173 }
NeilBrowna9701a32005-11-08 21:39:34 -08002174 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002175 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002176
NeilBrowna9701a32005-11-08 21:39:34 -08002177 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002178 if (mddev->in_sync != sync_req ||
2179 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002180 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002181 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002182 goto repeat;
2183 }
NeilBrown850b2b42006-10-03 01:15:46 -07002184 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002185 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002186 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002187 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2188 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190}
2191
Andre Noll7f6ce762008-03-23 18:34:54 +01002192/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002193 * We want to accept with case. For this we use cmd_match.
2194 */
2195static int cmd_match(const char *cmd, const char *str)
2196{
2197 /* See if cmd, written into a sysfs file, matches
2198 * str. They must either be the same, or cmd can
2199 * have a trailing newline
2200 */
2201 while (*cmd && *str && *cmd == *str) {
2202 cmd++;
2203 str++;
2204 }
2205 if (*cmd == '\n')
2206 cmd++;
2207 if (*str || *cmd)
2208 return 0;
2209 return 1;
2210}
2211
NeilBrown86e6ffd2005-11-08 21:39:24 -08002212struct rdev_sysfs_entry {
2213 struct attribute attr;
2214 ssize_t (*show)(mdk_rdev_t *, char *);
2215 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2216};
2217
2218static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002219state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002220{
2221 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002222 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002223
NeilBrownb2d444d2005-11-08 21:39:31 -08002224 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002225 len+= sprintf(page+len, "%sfaulty",sep);
2226 sep = ",";
2227 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002228 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002229 len += sprintf(page+len, "%sin_sync",sep);
2230 sep = ",";
2231 }
NeilBrownf6556752006-06-26 00:28:01 -07002232 if (test_bit(WriteMostly, &rdev->flags)) {
2233 len += sprintf(page+len, "%swrite_mostly",sep);
2234 sep = ",";
2235 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002236 if (test_bit(Blocked, &rdev->flags)) {
2237 len += sprintf(page+len, "%sblocked", sep);
2238 sep = ",";
2239 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002240 if (!test_bit(Faulty, &rdev->flags) &&
2241 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002242 len += sprintf(page+len, "%sspare", sep);
2243 sep = ",";
2244 }
2245 return len+sprintf(page+len, "\n");
2246}
2247
NeilBrown45dc2de2006-06-26 00:27:58 -07002248static ssize_t
2249state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2250{
2251 /* can write
2252 * faulty - simulates and error
2253 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002254 * writemostly - sets write_mostly
2255 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002256 * blocked - sets the Blocked flag
2257 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002258 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002259 */
2260 int err = -EINVAL;
2261 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2262 md_error(rdev->mddev, rdev);
2263 err = 0;
2264 } else if (cmd_match(buf, "remove")) {
2265 if (rdev->raid_disk >= 0)
2266 err = -EBUSY;
2267 else {
2268 mddev_t *mddev = rdev->mddev;
2269 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002270 if (mddev->pers)
2271 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002272 md_new_event(mddev);
2273 err = 0;
2274 }
NeilBrownf6556752006-06-26 00:28:01 -07002275 } else if (cmd_match(buf, "writemostly")) {
2276 set_bit(WriteMostly, &rdev->flags);
2277 err = 0;
2278 } else if (cmd_match(buf, "-writemostly")) {
2279 clear_bit(WriteMostly, &rdev->flags);
2280 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002281 } else if (cmd_match(buf, "blocked")) {
2282 set_bit(Blocked, &rdev->flags);
2283 err = 0;
2284 } else if (cmd_match(buf, "-blocked")) {
2285 clear_bit(Blocked, &rdev->flags);
2286 wake_up(&rdev->blocked_wait);
2287 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2288 md_wakeup_thread(rdev->mddev->thread);
2289
2290 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002291 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2292 set_bit(In_sync, &rdev->flags);
2293 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002294 }
NeilBrown3c0ee632008-10-21 13:25:28 +11002295 if (!err && rdev->sysfs_state)
2296 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002297 return err ? err : len;
2298}
NeilBrown80ca3a42006-07-10 04:44:18 -07002299static struct rdev_sysfs_entry rdev_state =
2300__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002301
2302static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002303errors_show(mdk_rdev_t *rdev, char *page)
2304{
2305 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2306}
2307
2308static ssize_t
2309errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2310{
2311 char *e;
2312 unsigned long n = simple_strtoul(buf, &e, 10);
2313 if (*buf && (*e == 0 || *e == '\n')) {
2314 atomic_set(&rdev->corrected_errors, n);
2315 return len;
2316 }
2317 return -EINVAL;
2318}
2319static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002320__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002321
NeilBrown014236d2006-01-06 00:20:55 -08002322static ssize_t
2323slot_show(mdk_rdev_t *rdev, char *page)
2324{
2325 if (rdev->raid_disk < 0)
2326 return sprintf(page, "none\n");
2327 else
2328 return sprintf(page, "%d\n", rdev->raid_disk);
2329}
2330
2331static ssize_t
2332slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2333{
2334 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002335 int err;
2336 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002337 int slot = simple_strtoul(buf, &e, 10);
2338 if (strncmp(buf, "none", 4)==0)
2339 slot = -1;
2340 else if (e==buf || (*e && *e!= '\n'))
2341 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002342 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002343 /* Setting 'slot' on an active array requires also
2344 * updating the 'rd%d' link, and communicating
2345 * with the personality with ->hot_*_disk.
2346 * For now we only support removing
2347 * failed/spare devices. This normally happens automatically,
2348 * but not when the metadata is externally managed.
2349 */
NeilBrownc303da62008-02-06 01:39:51 -08002350 if (rdev->raid_disk == -1)
2351 return -EEXIST;
2352 /* personality does all needed checks */
2353 if (rdev->mddev->pers->hot_add_disk == NULL)
2354 return -EINVAL;
2355 err = rdev->mddev->pers->
2356 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2357 if (err)
2358 return err;
2359 sprintf(nm, "rd%d", rdev->raid_disk);
2360 sysfs_remove_link(&rdev->mddev->kobj, nm);
2361 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2362 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002363 } else if (rdev->mddev->pers) {
2364 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002365 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002366 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002367 */
2368
2369 if (rdev->raid_disk != -1)
2370 return -EBUSY;
2371
2372 if (rdev->mddev->pers->hot_add_disk == NULL)
2373 return -EINVAL;
2374
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002375 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002376 if (rdev2->raid_disk == slot)
2377 return -EEXIST;
2378
2379 rdev->raid_disk = slot;
2380 if (test_bit(In_sync, &rdev->flags))
2381 rdev->saved_raid_disk = slot;
2382 else
2383 rdev->saved_raid_disk = -1;
2384 err = rdev->mddev->pers->
2385 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002386 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002387 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002388 return err;
Neil Brown52664732008-06-28 08:31:44 +10002389 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002390 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002391 sprintf(nm, "rd%d", rdev->raid_disk);
2392 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2393 printk(KERN_WARNING
2394 "md: cannot register "
2395 "%s for %s\n",
2396 nm, mdname(rdev->mddev));
2397
2398 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002399 } else {
2400 if (slot >= rdev->mddev->raid_disks)
2401 return -ENOSPC;
2402 rdev->raid_disk = slot;
2403 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002404 clear_bit(Faulty, &rdev->flags);
2405 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002406 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002407 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002408 }
NeilBrown014236d2006-01-06 00:20:55 -08002409 return len;
2410}
2411
2412
2413static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002414__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002415
NeilBrown93c8cad2006-01-06 00:20:56 -08002416static ssize_t
2417offset_show(mdk_rdev_t *rdev, char *page)
2418{
Andrew Morton6961ece2006-01-06 00:20:59 -08002419 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002420}
2421
2422static ssize_t
2423offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2424{
2425 char *e;
2426 unsigned long long offset = simple_strtoull(buf, &e, 10);
2427 if (e==buf || (*e && *e != '\n'))
2428 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002429 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002430 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002431 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002432 /* Must set offset before size, so overlap checks
2433 * can be sane */
2434 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002435 rdev->data_offset = offset;
2436 return len;
2437}
2438
2439static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002440__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002441
NeilBrown83303b62006-01-06 00:21:06 -08002442static ssize_t
2443rdev_size_show(mdk_rdev_t *rdev, char *page)
2444{
Andre Nolldd8ac332009-03-31 14:33:13 +11002445 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002446}
2447
NeilBrownc5d79ad2008-02-06 01:39:54 -08002448static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2449{
2450 /* check if two start/length pairs overlap */
2451 if (s1+l1 <= s2)
2452 return 0;
2453 if (s2+l2 <= s1)
2454 return 0;
2455 return 1;
2456}
2457
Dan Williamsb522adc2009-03-31 15:00:31 +11002458static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2459{
2460 unsigned long long blocks;
2461 sector_t new;
2462
2463 if (strict_strtoull(buf, 10, &blocks) < 0)
2464 return -EINVAL;
2465
2466 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2467 return -EINVAL; /* sector conversion overflow */
2468
2469 new = blocks * 2;
2470 if (new != blocks * 2)
2471 return -EINVAL; /* unsigned long long to sector_t overflow */
2472
2473 *sectors = new;
2474 return 0;
2475}
2476
NeilBrown83303b62006-01-06 00:21:06 -08002477static ssize_t
2478rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2479{
NeilBrown27c529b2008-03-04 14:29:33 -08002480 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002481 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002482 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002483
Dan Williamsb522adc2009-03-31 15:00:31 +11002484 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002485 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002486 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002487 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002488 sectors = super_types[my_mddev->major_version].
2489 rdev_size_change(rdev, sectors);
2490 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002491 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002492 } else if (!sectors)
2493 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2494 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002495 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002496 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002497 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002498
Andre Nolldd8ac332009-03-31 14:33:13 +11002499 rdev->sectors = sectors;
2500 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002501 /* need to check that all other rdevs with the same ->bdev
2502 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002503 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002504 * we have to change it back, we will have the lock again.
2505 */
2506 mddev_t *mddev;
2507 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002508 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002509
NeilBrown27c529b2008-03-04 14:29:33 -08002510 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002511 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002512 mdk_rdev_t *rdev2;
2513
2514 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002515 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002516 if (test_bit(AllReserved, &rdev2->flags) ||
2517 (rdev->bdev == rdev2->bdev &&
2518 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002519 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002520 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002521 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002522 overlap = 1;
2523 break;
2524 }
2525 mddev_unlock(mddev);
2526 if (overlap) {
2527 mddev_put(mddev);
2528 break;
2529 }
2530 }
NeilBrown27c529b2008-03-04 14:29:33 -08002531 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002532 if (overlap) {
2533 /* Someone else could have slipped in a size
2534 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002535 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002536 * safe, and trust userspace not to race with
2537 * itself
2538 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002539 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002540 return -EBUSY;
2541 }
2542 }
NeilBrown83303b62006-01-06 00:21:06 -08002543 return len;
2544}
2545
2546static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002547__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002548
NeilBrown86e6ffd2005-11-08 21:39:24 -08002549static struct attribute *rdev_default_attrs[] = {
2550 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002551 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002552 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002553 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002554 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002555 NULL,
2556};
2557static ssize_t
2558rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2559{
2560 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2561 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002562 mddev_t *mddev = rdev->mddev;
2563 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002564
2565 if (!entry->show)
2566 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002567
2568 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2569 if (!rv) {
2570 if (rdev->mddev == NULL)
2571 rv = -EBUSY;
2572 else
2573 rv = entry->show(rdev, page);
2574 mddev_unlock(mddev);
2575 }
2576 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002577}
2578
2579static ssize_t
2580rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2581 const char *page, size_t length)
2582{
2583 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2584 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002585 ssize_t rv;
2586 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002587
2588 if (!entry->store)
2589 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002590 if (!capable(CAP_SYS_ADMIN))
2591 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002592 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002593 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002594 if (rdev->mddev == NULL)
2595 rv = -EBUSY;
2596 else
2597 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002598 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002599 }
2600 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002601}
2602
2603static void rdev_free(struct kobject *ko)
2604{
2605 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2606 kfree(rdev);
2607}
2608static struct sysfs_ops rdev_sysfs_ops = {
2609 .show = rdev_attr_show,
2610 .store = rdev_attr_store,
2611};
2612static struct kobj_type rdev_ktype = {
2613 .release = rdev_free,
2614 .sysfs_ops = &rdev_sysfs_ops,
2615 .default_attrs = rdev_default_attrs,
2616};
2617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618/*
2619 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2620 *
2621 * mark the device faulty if:
2622 *
2623 * - the device is nonexistent (zero size)
2624 * - the device has no valid superblock
2625 *
2626 * a faulty rdev _never_ has rdev->sb set.
2627 */
2628static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2629{
2630 char b[BDEVNAME_SIZE];
2631 int err;
2632 mdk_rdev_t *rdev;
2633 sector_t size;
2634
NeilBrown9ffae0c2006-01-06 00:20:32 -08002635 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 if (!rdev) {
2637 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2638 return ERR_PTR(-ENOMEM);
2639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
2641 if ((err = alloc_disk_sb(rdev)))
2642 goto abort_free;
2643
NeilBrownc5d79ad2008-02-06 01:39:54 -08002644 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 if (err)
2646 goto abort_free;
2647
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002648 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002651 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002652 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002653 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002655 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002657 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002658 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
2660 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2661 if (!size) {
2662 printk(KERN_WARNING
2663 "md: %s has zero or unknown size, marking faulty!\n",
2664 bdevname(rdev->bdev,b));
2665 err = -EINVAL;
2666 goto abort_free;
2667 }
2668
2669 if (super_format >= 0) {
2670 err = super_types[super_format].
2671 load_super(rdev, NULL, super_minor);
2672 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002673 printk(KERN_WARNING
2674 "md: %s does not have a valid v%d.%d "
2675 "superblock, not importing!\n",
2676 bdevname(rdev->bdev,b),
2677 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 goto abort_free;
2679 }
2680 if (err < 0) {
2681 printk(KERN_WARNING
2682 "md: could not read %s's sb, not importing!\n",
2683 bdevname(rdev->bdev,b));
2684 goto abort_free;
2685 }
2686 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002689 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
2691 return rdev;
2692
2693abort_free:
2694 if (rdev->sb_page) {
2695 if (rdev->bdev)
2696 unlock_rdev(rdev);
2697 free_disk_sb(rdev);
2698 }
2699 kfree(rdev);
2700 return ERR_PTR(err);
2701}
2702
2703/*
2704 * Check a full RAID array for plausibility
2705 */
2706
2707
NeilBrowna757e642005-04-16 15:26:42 -07002708static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709{
2710 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002711 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 char b[BDEVNAME_SIZE];
2713
2714 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002715 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 switch (super_types[mddev->major_version].
2717 load_super(rdev, freshest, mddev->minor_version)) {
2718 case 1:
2719 freshest = rdev;
2720 break;
2721 case 0:
2722 break;
2723 default:
2724 printk( KERN_ERR \
2725 "md: fatal superblock inconsistency in %s"
2726 " -- removing from array\n",
2727 bdevname(rdev->bdev,b));
2728 kick_rdev_from_array(rdev);
2729 }
2730
2731
2732 super_types[mddev->major_version].
2733 validate_super(mddev, freshest);
2734
2735 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002736 rdev_for_each(rdev, tmp, mddev) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002737 if (rdev->desc_nr >= mddev->max_disks ||
2738 i > mddev->max_disks) {
2739 printk(KERN_WARNING
2740 "md: %s: %s: only %d devices permitted\n",
2741 mdname(mddev), bdevname(rdev->bdev, b),
2742 mddev->max_disks);
2743 kick_rdev_from_array(rdev);
2744 continue;
2745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 if (rdev != freshest)
2747 if (super_types[mddev->major_version].
2748 validate_super(mddev, rdev)) {
2749 printk(KERN_WARNING "md: kicking non-fresh %s"
2750 " from array!\n",
2751 bdevname(rdev->bdev,b));
2752 kick_rdev_from_array(rdev);
2753 continue;
2754 }
2755 if (mddev->level == LEVEL_MULTIPATH) {
2756 rdev->desc_nr = i++;
2757 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002758 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002759 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002760 rdev->raid_disk = -1;
2761 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 }
2763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764}
2765
NeilBrown19052c02008-08-05 15:54:13 +10002766static void md_safemode_timeout(unsigned long data);
2767
NeilBrowneae17012005-11-08 21:39:23 -08002768static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002769safe_delay_show(mddev_t *mddev, char *page)
2770{
2771 int msec = (mddev->safemode_delay*1000)/HZ;
2772 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2773}
2774static ssize_t
2775safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2776{
2777 int scale=1;
2778 int dot=0;
2779 int i;
2780 unsigned long msec;
2781 char buf[30];
Dan Williams97ce0a72008-09-24 22:48:19 -07002782
NeilBrown16f17b32006-06-26 00:27:37 -07002783 /* remove a period, and count digits after it */
2784 if (len >= sizeof(buf))
2785 return -EINVAL;
Dan Williams97ce0a72008-09-24 22:48:19 -07002786 strlcpy(buf, cbuf, sizeof(buf));
NeilBrown16f17b32006-06-26 00:27:37 -07002787 for (i=0; i<len; i++) {
2788 if (dot) {
2789 if (isdigit(buf[i])) {
2790 buf[i-1] = buf[i];
2791 scale *= 10;
2792 }
2793 buf[i] = 0;
2794 } else if (buf[i] == '.') {
2795 dot=1;
2796 buf[i] = 0;
2797 }
2798 }
Dan Williams97ce0a72008-09-24 22:48:19 -07002799 if (strict_strtoul(buf, 10, &msec) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002800 return -EINVAL;
2801 msec = (msec * 1000) / scale;
2802 if (msec == 0)
2803 mddev->safemode_delay = 0;
2804 else {
NeilBrown19052c02008-08-05 15:54:13 +10002805 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002806 mddev->safemode_delay = (msec*HZ)/1000;
2807 if (mddev->safemode_delay == 0)
2808 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002809 if (mddev->safemode_delay < old_delay)
2810 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002811 }
2812 return len;
2813}
2814static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002815__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002816
2817static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002818level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002819{
NeilBrown2604b702006-01-06 00:20:36 -08002820 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002821 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002822 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002823 else if (mddev->clevel[0])
2824 return sprintf(page, "%s\n", mddev->clevel);
2825 else if (mddev->level != LEVEL_NONE)
2826 return sprintf(page, "%d\n", mddev->level);
2827 else
2828 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002829}
2830
NeilBrownd9d166c2006-01-06 00:20:51 -08002831static ssize_t
2832level_store(mddev_t *mddev, const char *buf, size_t len)
2833{
NeilBrown245f46c2009-03-31 14:39:39 +11002834 char level[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002835 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002836 struct mdk_personality *pers;
2837 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002838 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002839
2840 if (mddev->pers == NULL) {
2841 if (len == 0)
2842 return 0;
2843 if (len >= sizeof(mddev->clevel))
2844 return -ENOSPC;
2845 strncpy(mddev->clevel, buf, len);
2846 if (mddev->clevel[len-1] == '\n')
2847 len--;
2848 mddev->clevel[len] = 0;
2849 mddev->level = LEVEL_NONE;
2850 return rv;
2851 }
2852
2853 /* request to change the personality. Need to ensure:
2854 * - array is not engaged in resync/recovery/reshape
2855 * - old personality can be suspended
2856 * - new personality will access other array.
2857 */
2858
2859 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownd9d166c2006-01-06 00:20:51 -08002860 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002861
2862 if (!mddev->pers->quiesce) {
2863 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2864 mdname(mddev), mddev->pers->name);
2865 return -EINVAL;
2866 }
2867
2868 /* Now find the new personality */
2869 if (len == 0 || len >= sizeof(level))
2870 return -EINVAL;
2871 strncpy(level, buf, len);
2872 if (level[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002873 len--;
NeilBrown245f46c2009-03-31 14:39:39 +11002874 level[len] = 0;
2875
2876 request_module("md-%s", level);
2877 spin_lock(&pers_lock);
2878 pers = find_pers(LEVEL_NONE, level);
2879 if (!pers || !try_module_get(pers->owner)) {
2880 spin_unlock(&pers_lock);
2881 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2882 return -EINVAL;
2883 }
2884 spin_unlock(&pers_lock);
2885
2886 if (pers == mddev->pers) {
2887 /* Nothing to do! */
2888 module_put(pers->owner);
2889 return rv;
2890 }
2891 if (!pers->takeover) {
2892 module_put(pers->owner);
2893 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2894 mdname(mddev), level);
2895 return -EINVAL;
2896 }
2897
2898 /* ->takeover must set new_* and/or delta_disks
2899 * if it succeeds, and may set them when it fails.
2900 */
2901 priv = pers->takeover(mddev);
2902 if (IS_ERR(priv)) {
2903 mddev->new_level = mddev->level;
2904 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002905 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002906 mddev->raid_disks -= mddev->delta_disks;
2907 mddev->delta_disks = 0;
2908 module_put(pers->owner);
2909 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2910 mdname(mddev), level);
2911 return PTR_ERR(priv);
2912 }
2913
2914 /* Looks like we have a winner */
2915 mddev_suspend(mddev);
2916 mddev->pers->stop(mddev);
2917 module_put(mddev->pers->owner);
NeilBrown3a981b02009-08-03 10:59:55 +10002918 /* Invalidate devices that are now superfluous */
2919 list_for_each_entry(rdev, &mddev->disks, same_set)
2920 if (rdev->raid_disk >= mddev->raid_disks) {
2921 rdev->raid_disk = -1;
2922 clear_bit(In_sync, &rdev->flags);
2923 }
NeilBrown245f46c2009-03-31 14:39:39 +11002924 mddev->pers = pers;
2925 mddev->private = priv;
2926 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2927 mddev->level = mddev->new_level;
2928 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002929 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002930 mddev->delta_disks = 0;
2931 pers->run(mddev);
2932 mddev_resume(mddev);
2933 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2934 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2935 md_wakeup_thread(mddev->thread);
NeilBrownd9d166c2006-01-06 00:20:51 -08002936 return rv;
2937}
2938
2939static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002940__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002941
NeilBrownd4dbd022006-06-26 00:27:59 -07002942
2943static ssize_t
2944layout_show(mddev_t *mddev, char *page)
2945{
2946 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002947 if (mddev->reshape_position != MaxSector &&
2948 mddev->layout != mddev->new_layout)
2949 return sprintf(page, "%d (%d)\n",
2950 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002951 return sprintf(page, "%d\n", mddev->layout);
2952}
2953
2954static ssize_t
2955layout_store(mddev_t *mddev, const char *buf, size_t len)
2956{
2957 char *e;
2958 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002959
2960 if (!*buf || (*e && *e != '\n'))
2961 return -EINVAL;
2962
NeilBrownb3546032009-03-31 14:56:41 +11002963 if (mddev->pers) {
2964 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10002965 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11002966 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10002967 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10002968 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10002969 if (err) {
2970 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11002971 return err;
NeilBrown597a7112009-06-18 08:47:42 +10002972 }
NeilBrownb3546032009-03-31 14:56:41 +11002973 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07002974 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11002975 if (mddev->reshape_position == MaxSector)
2976 mddev->layout = n;
2977 }
NeilBrownd4dbd022006-06-26 00:27:59 -07002978 return len;
2979}
2980static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002981__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002982
2983
NeilBrowneae17012005-11-08 21:39:23 -08002984static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002985raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002986{
NeilBrownbb636542005-11-08 21:39:45 -08002987 if (mddev->raid_disks == 0)
2988 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002989 if (mddev->reshape_position != MaxSector &&
2990 mddev->delta_disks != 0)
2991 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2992 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002993 return sprintf(page, "%d\n", mddev->raid_disks);
2994}
2995
NeilBrownda943b92006-01-06 00:20:54 -08002996static int update_raid_disks(mddev_t *mddev, int raid_disks);
2997
2998static ssize_t
2999raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3000{
NeilBrownda943b92006-01-06 00:20:54 -08003001 char *e;
3002 int rv = 0;
3003 unsigned long n = simple_strtoul(buf, &e, 10);
3004
3005 if (!*buf || (*e && *e != '\n'))
3006 return -EINVAL;
3007
3008 if (mddev->pers)
3009 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003010 else if (mddev->reshape_position != MaxSector) {
3011 int olddisks = mddev->raid_disks - mddev->delta_disks;
3012 mddev->delta_disks = n - olddisks;
3013 mddev->raid_disks = n;
3014 } else
NeilBrownda943b92006-01-06 00:20:54 -08003015 mddev->raid_disks = n;
3016 return rv ? rv : len;
3017}
3018static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003019__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003020
NeilBrown24dd4692005-11-08 21:39:26 -08003021static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003022chunk_size_show(mddev_t *mddev, char *page)
3023{
NeilBrown08a02ec2007-05-09 02:35:38 -07003024 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003025 mddev->chunk_sectors != mddev->new_chunk_sectors)
3026 return sprintf(page, "%d (%d)\n",
3027 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003028 mddev->chunk_sectors << 9);
3029 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003030}
3031
3032static ssize_t
3033chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3034{
NeilBrown3b343802006-01-06 00:20:47 -08003035 char *e;
3036 unsigned long n = simple_strtoul(buf, &e, 10);
3037
NeilBrown3b343802006-01-06 00:20:47 -08003038 if (!*buf || (*e && *e != '\n'))
3039 return -EINVAL;
3040
NeilBrownb3546032009-03-31 14:56:41 +11003041 if (mddev->pers) {
3042 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003043 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003044 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003045 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003046 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003047 if (err) {
3048 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003049 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003050 }
NeilBrownb3546032009-03-31 14:56:41 +11003051 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003052 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003053 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003054 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003055 }
NeilBrown3b343802006-01-06 00:20:47 -08003056 return len;
3057}
3058static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003059__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003060
NeilBrowna94213b2006-06-26 00:28:00 -07003061static ssize_t
3062resync_start_show(mddev_t *mddev, char *page)
3063{
NeilBrownd1a7c502009-03-31 15:24:32 +11003064 if (mddev->recovery_cp == MaxSector)
3065 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003066 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3067}
3068
3069static ssize_t
3070resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3071{
NeilBrowna94213b2006-06-26 00:28:00 -07003072 char *e;
3073 unsigned long long n = simple_strtoull(buf, &e, 10);
3074
3075 if (mddev->pers)
3076 return -EBUSY;
3077 if (!*buf || (*e && *e != '\n'))
3078 return -EINVAL;
3079
3080 mddev->recovery_cp = n;
3081 return len;
3082}
3083static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003084__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003085
NeilBrown9e653b62006-06-26 00:27:58 -07003086/*
3087 * The array state can be:
3088 *
3089 * clear
3090 * No devices, no size, no level
3091 * Equivalent to STOP_ARRAY ioctl
3092 * inactive
3093 * May have some settings, but array is not active
3094 * all IO results in error
3095 * When written, doesn't tear down array, but just stops it
3096 * suspended (not supported yet)
3097 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003098 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003099 * readonly
3100 * no resync can happen. no superblocks get written.
3101 * write requests fail
3102 * read-auto
3103 * like readonly, but behaves like 'clean' on a write request.
3104 *
3105 * clean - no pending writes, but otherwise active.
3106 * When written to inactive array, starts without resync
3107 * If a write request arrives then
3108 * if metadata is known, mark 'dirty' and switch to 'active'.
3109 * if not known, block and switch to write-pending
3110 * If written to an active array that has pending writes, then fails.
3111 * active
3112 * fully active: IO and resync can be happening.
3113 * When written to inactive array, starts with resync
3114 *
3115 * write-pending
3116 * clean, but writes are blocked waiting for 'active' to be written.
3117 *
3118 * active-idle
3119 * like active, but no writes have been seen for a while (100msec).
3120 *
3121 */
3122enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3123 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003124static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003125 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3126 "write-pending", "active-idle", NULL };
3127
3128static int match_word(const char *word, char **list)
3129{
3130 int n;
3131 for (n=0; list[n]; n++)
3132 if (cmd_match(word, list[n]))
3133 break;
3134 return n;
3135}
3136
3137static ssize_t
3138array_state_show(mddev_t *mddev, char *page)
3139{
3140 enum array_state st = inactive;
3141
3142 if (mddev->pers)
3143 switch(mddev->ro) {
3144 case 1:
3145 st = readonly;
3146 break;
3147 case 2:
3148 st = read_auto;
3149 break;
3150 case 0:
3151 if (mddev->in_sync)
3152 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08003153 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3154 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003155 else if (mddev->safemode)
3156 st = active_idle;
3157 else
3158 st = active;
3159 }
3160 else {
3161 if (list_empty(&mddev->disks) &&
3162 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003163 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003164 st = clear;
3165 else
3166 st = inactive;
3167 }
3168 return sprintf(page, "%s\n", array_states[st]);
3169}
3170
Neil Browndf5b20c2008-07-11 22:02:22 +10003171static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003172static int do_md_run(mddev_t * mddev);
3173static int restart_array(mddev_t *mddev);
3174
3175static ssize_t
3176array_state_store(mddev_t *mddev, const char *buf, size_t len)
3177{
3178 int err = -EINVAL;
3179 enum array_state st = match_word(buf, array_states);
3180 switch(st) {
3181 case bad_word:
3182 break;
3183 case clear:
3184 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003185 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003186 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003187 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003188 break;
3189 case inactive:
3190 /* stopping an active array */
3191 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003192 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003193 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003194 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003195 } else
3196 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003197 break;
3198 case suspended:
3199 break; /* not supported yet */
3200 case readonly:
3201 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10003202 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003203 else {
3204 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003205 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003206 err = do_md_run(mddev);
3207 }
3208 break;
3209 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003210 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003211 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10003212 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003213 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003214 err = restart_array(mddev);
3215 if (err == 0) {
3216 mddev->ro = 2;
3217 set_disk_ro(mddev->gendisk, 0);
3218 }
NeilBrown9e653b62006-06-26 00:27:58 -07003219 } else {
3220 mddev->ro = 2;
3221 err = do_md_run(mddev);
3222 }
3223 break;
3224 case clean:
3225 if (mddev->pers) {
3226 restart_array(mddev);
3227 spin_lock_irq(&mddev->write_lock);
3228 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003229 if (mddev->in_sync == 0) {
3230 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003231 if (mddev->safemode == 1)
3232 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003233 if (mddev->persistent)
3234 set_bit(MD_CHANGE_CLEAN,
3235 &mddev->flags);
3236 }
3237 err = 0;
3238 } else
3239 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003240 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003241 } else
3242 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003243 break;
3244 case active:
3245 if (mddev->pers) {
3246 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003247 if (mddev->external)
3248 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003249 wake_up(&mddev->sb_wait);
3250 err = 0;
3251 } else {
3252 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003253 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003254 err = do_md_run(mddev);
3255 }
3256 break;
3257 case write_pending:
3258 case active_idle:
3259 /* these cannot be set */
3260 break;
3261 }
3262 if (err)
3263 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003264 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003265 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003266 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003267 }
NeilBrown9e653b62006-06-26 00:27:58 -07003268}
NeilBrown80ca3a42006-07-10 04:44:18 -07003269static struct md_sysfs_entry md_array_state =
3270__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003271
NeilBrown6d7ff732006-01-06 00:21:16 -08003272static ssize_t
3273null_show(mddev_t *mddev, char *page)
3274{
3275 return -EINVAL;
3276}
3277
3278static ssize_t
3279new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3280{
3281 /* buf must be %d:%d\n? giving major and minor numbers */
3282 /* The new device is added to the array.
3283 * If the array has a persistent superblock, we read the
3284 * superblock to initialise info and check validity.
3285 * Otherwise, only checking done is that in bind_rdev_to_array,
3286 * which mainly checks size.
3287 */
3288 char *e;
3289 int major = simple_strtoul(buf, &e, 10);
3290 int minor;
3291 dev_t dev;
3292 mdk_rdev_t *rdev;
3293 int err;
3294
3295 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3296 return -EINVAL;
3297 minor = simple_strtoul(e+1, &e, 10);
3298 if (*e && *e != '\n')
3299 return -EINVAL;
3300 dev = MKDEV(major, minor);
3301 if (major != MAJOR(dev) ||
3302 minor != MINOR(dev))
3303 return -EOVERFLOW;
3304
3305
3306 if (mddev->persistent) {
3307 rdev = md_import_device(dev, mddev->major_version,
3308 mddev->minor_version);
3309 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3310 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3311 mdk_rdev_t, same_set);
3312 err = super_types[mddev->major_version]
3313 .load_super(rdev, rdev0, mddev->minor_version);
3314 if (err < 0)
3315 goto out;
3316 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003317 } else if (mddev->external)
3318 rdev = md_import_device(dev, -2, -1);
3319 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003320 rdev = md_import_device(dev, -1, -1);
3321
3322 if (IS_ERR(rdev))
3323 return PTR_ERR(rdev);
3324 err = bind_rdev_to_array(rdev, mddev);
3325 out:
3326 if (err)
3327 export_rdev(rdev);
3328 return err ? err : len;
3329}
3330
3331static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003332__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003333
3334static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003335bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3336{
3337 char *end;
3338 unsigned long chunk, end_chunk;
3339
3340 if (!mddev->bitmap)
3341 goto out;
3342 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3343 while (*buf) {
3344 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3345 if (buf == end) break;
3346 if (*end == '-') { /* range */
3347 buf = end + 1;
3348 end_chunk = simple_strtoul(buf, &end, 0);
3349 if (buf == end) break;
3350 }
3351 if (*end && !isspace(*end)) break;
3352 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3353 buf = end;
3354 while (isspace(*buf)) buf++;
3355 }
3356 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3357out:
3358 return len;
3359}
3360
3361static struct md_sysfs_entry md_bitmap =
3362__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3363
3364static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003365size_show(mddev_t *mddev, char *page)
3366{
Andre Noll58c0fed2009-03-31 14:33:13 +11003367 return sprintf(page, "%llu\n",
3368 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003369}
3370
Andre Nolld71f9f82008-07-11 22:02:22 +10003371static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003372
3373static ssize_t
3374size_store(mddev_t *mddev, const char *buf, size_t len)
3375{
3376 /* If array is inactive, we can reduce the component size, but
3377 * not increase it (except from 0).
3378 * If array is active, we can try an on-line resize
3379 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003380 sector_t sectors;
3381 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003382
Andre Noll58c0fed2009-03-31 14:33:13 +11003383 if (err < 0)
3384 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003385 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003386 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003387 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003388 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003389 if (mddev->dev_sectors == 0 ||
3390 mddev->dev_sectors > sectors)
3391 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003392 else
3393 err = -ENOSPC;
3394 }
3395 return err ? err : len;
3396}
3397
3398static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003399__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003400
NeilBrown8bb93aa2006-01-06 00:20:50 -08003401
3402/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003403 * This is one of
3404 * 'none' for arrays with no metadata (good luck...)
3405 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003406 * or N.M for internally known formats
3407 */
3408static ssize_t
3409metadata_show(mddev_t *mddev, char *page)
3410{
3411 if (mddev->persistent)
3412 return sprintf(page, "%d.%d\n",
3413 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003414 else if (mddev->external)
3415 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003416 else
3417 return sprintf(page, "none\n");
3418}
3419
3420static ssize_t
3421metadata_store(mddev_t *mddev, const char *buf, size_t len)
3422{
3423 int major, minor;
3424 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003425 /* Changing the details of 'external' metadata is
3426 * always permitted. Otherwise there must be
3427 * no devices attached to the array.
3428 */
3429 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3430 ;
3431 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003432 return -EBUSY;
3433
3434 if (cmd_match(buf, "none")) {
3435 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003436 mddev->external = 0;
3437 mddev->major_version = 0;
3438 mddev->minor_version = 90;
3439 return len;
3440 }
3441 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003442 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003443 if (namelen >= sizeof(mddev->metadata_type))
3444 namelen = sizeof(mddev->metadata_type)-1;
3445 strncpy(mddev->metadata_type, buf+9, namelen);
3446 mddev->metadata_type[namelen] = 0;
3447 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3448 mddev->metadata_type[--namelen] = 0;
3449 mddev->persistent = 0;
3450 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003451 mddev->major_version = 0;
3452 mddev->minor_version = 90;
3453 return len;
3454 }
3455 major = simple_strtoul(buf, &e, 10);
3456 if (e==buf || *e != '.')
3457 return -EINVAL;
3458 buf = e+1;
3459 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003460 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003461 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003462 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003463 return -ENOENT;
3464 mddev->major_version = major;
3465 mddev->minor_version = minor;
3466 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003467 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003468 return len;
3469}
3470
3471static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003472__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003473
NeilBrowna35b0d62006-01-06 00:20:49 -08003474static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003475action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003476{
NeilBrown7eec3142005-11-08 21:39:44 -08003477 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003478 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3479 type = "frozen";
3480 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003481 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003482 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3483 type = "reshape";
3484 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003485 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3486 type = "resync";
3487 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3488 type = "check";
3489 else
3490 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003491 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003492 type = "recover";
3493 }
3494 return sprintf(page, "%s\n", type);
3495}
3496
3497static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003498action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003499{
NeilBrown7eec3142005-11-08 21:39:44 -08003500 if (!mddev->pers || !mddev->pers->sync_request)
3501 return -EINVAL;
3502
NeilBrownb6a9ce62009-05-26 09:41:17 +10003503 if (cmd_match(page, "frozen"))
3504 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3505 else
3506 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3507
3508 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003509 if (mddev->sync_thread) {
3510 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3511 md_unregister_thread(mddev->sync_thread);
3512 mddev->sync_thread = NULL;
3513 mddev->recovery = 0;
3514 }
NeilBrown03c902e2006-01-06 00:20:46 -08003515 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3516 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003517 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003518 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003519 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003520 else if (cmd_match(page, "recover")) {
3521 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3522 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3523 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003524 int err;
3525 if (mddev->pers->start_reshape == NULL)
3526 return -EINVAL;
3527 err = mddev->pers->start_reshape(mddev);
3528 if (err)
3529 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003530 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003531 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003532 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003533 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003534 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003535 return -EINVAL;
3536 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3537 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003538 }
NeilBrown03c902e2006-01-06 00:20:46 -08003539 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003540 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003541 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003542 return len;
3543}
3544
NeilBrown9d888832005-11-08 21:39:26 -08003545static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003546mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003547{
3548 return sprintf(page, "%llu\n",
3549 (unsigned long long) mddev->resync_mismatches);
3550}
3551
NeilBrown80ca3a42006-07-10 04:44:18 -07003552static struct md_sysfs_entry md_scan_mode =
3553__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003554
NeilBrown96de1e62005-11-08 21:39:39 -08003555
NeilBrown80ca3a42006-07-10 04:44:18 -07003556static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003557
NeilBrown88202a02006-01-06 00:21:36 -08003558static ssize_t
3559sync_min_show(mddev_t *mddev, char *page)
3560{
3561 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3562 mddev->sync_speed_min ? "local": "system");
3563}
3564
3565static ssize_t
3566sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3567{
3568 int min;
3569 char *e;
3570 if (strncmp(buf, "system", 6)==0) {
3571 mddev->sync_speed_min = 0;
3572 return len;
3573 }
3574 min = simple_strtoul(buf, &e, 10);
3575 if (buf == e || (*e && *e != '\n') || min <= 0)
3576 return -EINVAL;
3577 mddev->sync_speed_min = min;
3578 return len;
3579}
3580
3581static struct md_sysfs_entry md_sync_min =
3582__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3583
3584static ssize_t
3585sync_max_show(mddev_t *mddev, char *page)
3586{
3587 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3588 mddev->sync_speed_max ? "local": "system");
3589}
3590
3591static ssize_t
3592sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3593{
3594 int max;
3595 char *e;
3596 if (strncmp(buf, "system", 6)==0) {
3597 mddev->sync_speed_max = 0;
3598 return len;
3599 }
3600 max = simple_strtoul(buf, &e, 10);
3601 if (buf == e || (*e && *e != '\n') || max <= 0)
3602 return -EINVAL;
3603 mddev->sync_speed_max = max;
3604 return len;
3605}
3606
3607static struct md_sysfs_entry md_sync_max =
3608__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3609
Iustin Popd7f3d292007-10-16 23:30:54 -07003610static ssize_t
3611degraded_show(mddev_t *mddev, char *page)
3612{
3613 return sprintf(page, "%d\n", mddev->degraded);
3614}
3615static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003616
3617static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003618sync_force_parallel_show(mddev_t *mddev, char *page)
3619{
3620 return sprintf(page, "%d\n", mddev->parallel_resync);
3621}
3622
3623static ssize_t
3624sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3625{
3626 long n;
3627
3628 if (strict_strtol(buf, 10, &n))
3629 return -EINVAL;
3630
3631 if (n != 0 && n != 1)
3632 return -EINVAL;
3633
3634 mddev->parallel_resync = n;
3635
3636 if (mddev->sync_thread)
3637 wake_up(&resync_wait);
3638
3639 return len;
3640}
3641
3642/* force parallel resync, even with shared block devices */
3643static struct md_sysfs_entry md_sync_force_parallel =
3644__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3645 sync_force_parallel_show, sync_force_parallel_store);
3646
3647static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003648sync_speed_show(mddev_t *mddev, char *page)
3649{
3650 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003651 if (mddev->curr_resync == 0)
3652 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003653 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3654 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003655 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003656 db = resync - mddev->resync_mark_cnt;
3657 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003658}
3659
NeilBrown80ca3a42006-07-10 04:44:18 -07003660static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003661
3662static ssize_t
3663sync_completed_show(mddev_t *mddev, char *page)
3664{
Andre Noll58c0fed2009-03-31 14:33:13 +11003665 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003666
NeilBrownacb180b2009-04-14 16:28:34 +10003667 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3668 return sprintf(page, "none\n");
3669
NeilBrown88202a02006-01-06 00:21:36 -08003670 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003671 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003672 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003673 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003674
NeilBrownacb180b2009-04-14 16:28:34 +10003675 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003676 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003677}
3678
NeilBrown80ca3a42006-07-10 04:44:18 -07003679static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003680
NeilBrowne464eaf2006-03-27 01:18:14 -08003681static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003682min_sync_show(mddev_t *mddev, char *page)
3683{
3684 return sprintf(page, "%llu\n",
3685 (unsigned long long)mddev->resync_min);
3686}
3687static ssize_t
3688min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3689{
3690 unsigned long long min;
3691 if (strict_strtoull(buf, 10, &min))
3692 return -EINVAL;
3693 if (min > mddev->resync_max)
3694 return -EINVAL;
3695 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3696 return -EBUSY;
3697
3698 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003699 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003700 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003701 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003702 return -EINVAL;
3703 }
3704 mddev->resync_min = min;
3705
3706 return len;
3707}
3708
3709static struct md_sysfs_entry md_min_sync =
3710__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3711
3712static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003713max_sync_show(mddev_t *mddev, char *page)
3714{
3715 if (mddev->resync_max == MaxSector)
3716 return sprintf(page, "max\n");
3717 else
3718 return sprintf(page, "%llu\n",
3719 (unsigned long long)mddev->resync_max);
3720}
3721static ssize_t
3722max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3723{
3724 if (strncmp(buf, "max", 3) == 0)
3725 mddev->resync_max = MaxSector;
3726 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003727 unsigned long long max;
3728 if (strict_strtoull(buf, 10, &max))
3729 return -EINVAL;
3730 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003731 return -EINVAL;
3732 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003733 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003734 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3735 return -EBUSY;
3736
3737 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003738 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003739 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003740 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003741 return -EINVAL;
3742 }
3743 mddev->resync_max = max;
3744 }
3745 wake_up(&mddev->recovery_wait);
3746 return len;
3747}
3748
3749static struct md_sysfs_entry md_max_sync =
3750__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3751
3752static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003753suspend_lo_show(mddev_t *mddev, char *page)
3754{
3755 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3756}
3757
3758static ssize_t
3759suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3760{
3761 char *e;
3762 unsigned long long new = simple_strtoull(buf, &e, 10);
3763
NeilBrownb8d966e2009-07-01 11:14:04 +10003764 if (mddev->pers == NULL ||
3765 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003766 return -EINVAL;
3767 if (buf == e || (*e && *e != '\n'))
3768 return -EINVAL;
3769 if (new >= mddev->suspend_hi ||
3770 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3771 mddev->suspend_lo = new;
3772 mddev->pers->quiesce(mddev, 2);
3773 return len;
3774 } else
3775 return -EINVAL;
3776}
3777static struct md_sysfs_entry md_suspend_lo =
3778__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3779
3780
3781static ssize_t
3782suspend_hi_show(mddev_t *mddev, char *page)
3783{
3784 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3785}
3786
3787static ssize_t
3788suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3789{
3790 char *e;
3791 unsigned long long new = simple_strtoull(buf, &e, 10);
3792
NeilBrownb8d966e2009-07-01 11:14:04 +10003793 if (mddev->pers == NULL ||
3794 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003795 return -EINVAL;
3796 if (buf == e || (*e && *e != '\n'))
3797 return -EINVAL;
3798 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3799 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3800 mddev->suspend_hi = new;
3801 mddev->pers->quiesce(mddev, 1);
3802 mddev->pers->quiesce(mddev, 0);
3803 return len;
3804 } else
3805 return -EINVAL;
3806}
3807static struct md_sysfs_entry md_suspend_hi =
3808__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3809
NeilBrown08a02ec2007-05-09 02:35:38 -07003810static ssize_t
3811reshape_position_show(mddev_t *mddev, char *page)
3812{
3813 if (mddev->reshape_position != MaxSector)
3814 return sprintf(page, "%llu\n",
3815 (unsigned long long)mddev->reshape_position);
3816 strcpy(page, "none\n");
3817 return 5;
3818}
3819
3820static ssize_t
3821reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3822{
3823 char *e;
3824 unsigned long long new = simple_strtoull(buf, &e, 10);
3825 if (mddev->pers)
3826 return -EBUSY;
3827 if (buf == e || (*e && *e != '\n'))
3828 return -EINVAL;
3829 mddev->reshape_position = new;
3830 mddev->delta_disks = 0;
3831 mddev->new_level = mddev->level;
3832 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003833 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07003834 return len;
3835}
3836
3837static struct md_sysfs_entry md_reshape_position =
3838__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3839 reshape_position_store);
3840
Dan Williamsb522adc2009-03-31 15:00:31 +11003841static ssize_t
3842array_size_show(mddev_t *mddev, char *page)
3843{
3844 if (mddev->external_size)
3845 return sprintf(page, "%llu\n",
3846 (unsigned long long)mddev->array_sectors/2);
3847 else
3848 return sprintf(page, "default\n");
3849}
3850
3851static ssize_t
3852array_size_store(mddev_t *mddev, const char *buf, size_t len)
3853{
3854 sector_t sectors;
3855
3856 if (strncmp(buf, "default", 7) == 0) {
3857 if (mddev->pers)
3858 sectors = mddev->pers->size(mddev, 0, 0);
3859 else
3860 sectors = mddev->array_sectors;
3861
3862 mddev->external_size = 0;
3863 } else {
3864 if (strict_blocks_to_sectors(buf, &sectors) < 0)
3865 return -EINVAL;
3866 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10003867 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11003868
3869 mddev->external_size = 1;
3870 }
3871
3872 mddev->array_sectors = sectors;
3873 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10003874 if (mddev->pers)
3875 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11003876
3877 return len;
3878}
3879
3880static struct md_sysfs_entry md_array_size =
3881__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
3882 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08003883
NeilBrowneae17012005-11-08 21:39:23 -08003884static struct attribute *md_default_attrs[] = {
3885 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003886 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003887 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003888 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003889 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003890 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003891 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08003892 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003893 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003894 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003895 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11003896 &md_array_size.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003897 NULL,
3898};
3899
3900static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003901 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003902 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003903 &md_sync_min.attr,
3904 &md_sync_max.attr,
3905 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003906 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003907 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003908 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003909 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003910 &md_suspend_lo.attr,
3911 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003912 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003913 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003914 NULL,
3915};
NeilBrown411036f2005-11-08 21:39:40 -08003916static struct attribute_group md_redundancy_group = {
3917 .name = NULL,
3918 .attrs = md_redundancy_attrs,
3919};
3920
NeilBrowneae17012005-11-08 21:39:23 -08003921
3922static ssize_t
3923md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3924{
3925 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3926 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003927 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003928
3929 if (!entry->show)
3930 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003931 rv = mddev_lock(mddev);
3932 if (!rv) {
3933 rv = entry->show(mddev, page);
3934 mddev_unlock(mddev);
3935 }
NeilBrown96de1e62005-11-08 21:39:39 -08003936 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003937}
3938
3939static ssize_t
3940md_attr_store(struct kobject *kobj, struct attribute *attr,
3941 const char *page, size_t length)
3942{
3943 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3944 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003945 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003946
3947 if (!entry->store)
3948 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003949 if (!capable(CAP_SYS_ADMIN))
3950 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003951 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11003952 if (mddev->hold_active == UNTIL_IOCTL)
3953 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003954 if (!rv) {
3955 rv = entry->store(mddev, page, length);
3956 mddev_unlock(mddev);
3957 }
NeilBrown96de1e62005-11-08 21:39:39 -08003958 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003959}
3960
3961static void md_free(struct kobject *ko)
3962{
3963 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11003964
3965 if (mddev->sysfs_state)
3966 sysfs_put(mddev->sysfs_state);
3967
3968 if (mddev->gendisk) {
3969 del_gendisk(mddev->gendisk);
3970 put_disk(mddev->gendisk);
3971 }
3972 if (mddev->queue)
3973 blk_cleanup_queue(mddev->queue);
3974
NeilBrowneae17012005-11-08 21:39:23 -08003975 kfree(mddev);
3976}
3977
3978static struct sysfs_ops md_sysfs_ops = {
3979 .show = md_attr_show,
3980 .store = md_attr_store,
3981};
3982static struct kobj_type md_ktype = {
3983 .release = md_free,
3984 .sysfs_ops = &md_sysfs_ops,
3985 .default_attrs = md_default_attrs,
3986};
3987
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988int mdp_major = 0;
3989
Dan Williams5fd3a172009-03-04 00:57:25 -07003990static void mddev_delayed_delete(struct work_struct *ws)
3991{
3992 mddev_t *mddev = container_of(ws, mddev_t, del_work);
3993
3994 if (mddev->private == &md_redundancy_group) {
3995 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3996 if (mddev->sysfs_action)
3997 sysfs_put(mddev->sysfs_action);
3998 mddev->sysfs_action = NULL;
3999 mddev->private = NULL;
4000 }
4001 kobject_del(&mddev->kobj);
4002 kobject_put(&mddev->kobj);
4003}
4004
NeilBrownefeb53c2009-01-09 08:31:10 +11004005static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004007 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 mddev_t *mddev = mddev_find(dev);
4009 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004010 int partitioned;
4011 int shift;
4012 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004013 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014
4015 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004016 return -ENODEV;
4017
4018 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4019 shift = partitioned ? MdpMinorShift : 0;
4020 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021
NeilBrownd3374822009-01-09 08:31:10 +11004022 /* wait for any previous instance if this device
4023 * to be completed removed (mddev_delayed_delete).
4024 */
4025 flush_scheduled_work();
4026
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004027 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004028 error = -EEXIST;
4029 if (mddev->gendisk)
4030 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004031
4032 if (name) {
4033 /* Need to ensure that 'name' is not a duplicate.
4034 */
4035 mddev_t *mddev2;
4036 spin_lock(&all_mddevs_lock);
4037
4038 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4039 if (mddev2->gendisk &&
4040 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4041 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004042 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004043 }
4044 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 }
NeilBrown8b765392009-01-09 08:31:08 +11004046
NeilBrown0909dc42009-07-01 12:27:21 +10004047 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004048 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004049 if (!mddev->queue)
4050 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004051 mddev->queue->queuedata = mddev;
4052
NeilBrown8b765392009-01-09 08:31:08 +11004053 /* Can be unlocked because the queue is new: no concurrency */
4054 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4055
NeilBrown409c57f2009-03-31 14:39:39 +11004056 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004057
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 disk = alloc_disk(1 << shift);
4059 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004060 blk_cleanup_queue(mddev->queue);
4061 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004062 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004064 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004066 if (name)
4067 strcpy(disk->disk_name, name);
4068 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004070 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 disk->fops = &md_fops;
4073 disk->private_data = mddev;
4074 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004075 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004076 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004077 * remove it now.
4078 */
4079 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 add_disk(disk);
4081 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004082 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4083 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004084 if (error) {
4085 /* This isn't possible, but as kobject_init_and_add is marked
4086 * __must_check, we must do something with the result
4087 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004088 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4089 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004090 error = 0;
4091 }
4092 abort:
4093 mutex_unlock(&disks_mutex);
4094 if (!error) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004095 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11004096 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
4097 }
NeilBrownd3374822009-01-09 08:31:10 +11004098 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004099 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004100}
4101
4102static struct kobject *md_probe(dev_t dev, int *part, void *data)
4103{
4104 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 return NULL;
4106}
4107
NeilBrownefeb53c2009-01-09 08:31:10 +11004108static int add_named_array(const char *val, struct kernel_param *kp)
4109{
4110 /* val must be "md_*" where * is not all digits.
4111 * We allocate an array with a large free minor number, and
4112 * set the name to val. val must not already be an active name.
4113 */
4114 int len = strlen(val);
4115 char buf[DISK_NAME_LEN];
4116
4117 while (len && val[len-1] == '\n')
4118 len--;
4119 if (len >= DISK_NAME_LEN)
4120 return -E2BIG;
4121 strlcpy(buf, val, len+1);
4122 if (strncmp(buf, "md_", 3) != 0)
4123 return -EINVAL;
4124 return md_alloc(0, buf);
4125}
4126
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127static void md_safemode_timeout(unsigned long data)
4128{
4129 mddev_t *mddev = (mddev_t *) data;
4130
Neil Brown0fd62b82008-06-28 08:31:36 +10004131 if (!atomic_read(&mddev->writes_pending)) {
4132 mddev->safemode = 1;
4133 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11004134 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 md_wakeup_thread(mddev->thread);
4137}
4138
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004139static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140
4141static int do_md_run(mddev_t * mddev)
4142{
NeilBrown2604b702006-01-06 00:20:36 -08004143 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 mdk_rdev_t *rdev;
4145 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08004146 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147
NeilBrowna757e642005-04-16 15:26:42 -07004148 if (list_empty(&mddev->disks))
4149 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
4152 if (mddev->pers)
4153 return -EBUSY;
4154
4155 /*
4156 * Analyze all RAID superblock(s)
4157 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004158 if (!mddev->raid_disks) {
4159 if (!mddev->persistent)
4160 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004161 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
NeilBrownd9d166c2006-01-06 00:20:51 -08004164 if (mddev->level != LEVEL_NONE)
4165 request_module("md-level-%d", mddev->level);
4166 else if (mddev->clevel[0])
4167 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168
4169 /*
4170 * Drop all container device buffers, from now on
4171 * the only valid external interface is through the md
4172 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004174 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004175 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 continue;
4177 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004178 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004179
4180 /* perform some consistency tests on the device.
4181 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004182 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004183 */
Andre Noll0f420352008-07-11 22:02:23 +10004184 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004185 if (mddev->dev_sectors &&
4186 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004187 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004188 printk("md: %s: data overlaps metadata\n",
4189 mdname(mddev));
4190 return -EINVAL;
4191 }
4192 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004193 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004194 > rdev->data_offset) {
4195 printk("md: %s: metadata overlaps data\n",
4196 mdname(mddev));
4197 return -EINVAL;
4198 }
4199 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004200 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 }
4202
4203 md_probe(mddev->unit, NULL, NULL);
4204 disk = mddev->gendisk;
4205 if (!disk)
4206 return -ENOMEM;
4207
4208 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004209 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004210 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004212 if (mddev->level != LEVEL_NONE)
4213 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4214 mddev->level);
4215 else
4216 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4217 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 return -EINVAL;
4219 }
NeilBrown2604b702006-01-06 00:20:36 -08004220 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004222 if (mddev->level != pers->level) {
4223 mddev->level = pers->level;
4224 mddev->new_level = pers->level;
4225 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004226 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
NeilBrownf6705572006-03-27 01:18:11 -08004228 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004229 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004230 /* This personality cannot handle reshaping... */
4231 mddev->pers = NULL;
4232 module_put(pers->owner);
4233 return -EINVAL;
4234 }
4235
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004236 if (pers->sync_request) {
4237 /* Warn if this is a potentially silly
4238 * configuration.
4239 */
4240 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4241 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004242 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004243
4244 list_for_each_entry(rdev, &mddev->disks, same_set)
4245 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004246 if (rdev < rdev2 &&
4247 rdev->bdev->bd_contains ==
4248 rdev2->bdev->bd_contains) {
4249 printk(KERN_WARNING
4250 "%s: WARNING: %s appears to be"
4251 " on the same physical disk as"
4252 " %s.\n",
4253 mdname(mddev),
4254 bdevname(rdev->bdev,b),
4255 bdevname(rdev2->bdev,b2));
4256 warned = 1;
4257 }
4258 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004259
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004260 if (warned)
4261 printk(KERN_WARNING
4262 "True protection against single-disk"
4263 " failure might be compromised.\n");
4264 }
4265
NeilBrown657390d2005-08-26 18:34:16 -07004266 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004267 /* may be over-ridden by personality */
4268 mddev->resync_max_sectors = mddev->dev_sectors;
4269
NeilBrowna9701a32005-11-08 21:39:34 -08004270 mddev->barriers_work = 1;
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004271 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272
NeilBrownf91de922005-11-08 21:39:36 -08004273 if (start_readonly)
4274 mddev->ro = 2; /* read-only, but switch on first write */
4275
NeilBrownb15c2e52006-01-06 00:20:16 -08004276 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004277 if (err)
4278 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004279 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4280 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4281 " but 'external_size' not in effect?\n", __func__);
4282 printk(KERN_ERR
4283 "md: invalid array_size %llu > default size %llu\n",
4284 (unsigned long long)mddev->array_sectors / 2,
4285 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4286 err = -EINVAL;
4287 mddev->pers->stop(mddev);
4288 }
4289 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004290 err = bitmap_create(mddev);
4291 if (err) {
4292 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4293 mdname(mddev), err);
4294 mddev->pers->stop(mddev);
4295 }
4296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 module_put(mddev->pers->owner);
4299 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004300 bitmap_destroy(mddev);
4301 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004303 if (mddev->pers->sync_request) {
4304 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4305 printk(KERN_WARNING
4306 "md: cannot register extra attributes for %s\n",
4307 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004308 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004309 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004310 mddev->ro = 0;
4311
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 atomic_set(&mddev->writes_pending,0);
4313 mddev->safemode = 0;
4314 mddev->safemode_timer.function = md_safemode_timeout;
4315 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004316 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004318
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004319 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004320 if (rdev->raid_disk >= 0) {
4321 char nm[20];
4322 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004323 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4324 printk("md: cannot register %s for %s\n",
4325 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
4328 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4329
NeilBrown850b2b42006-10-03 01:15:46 -07004330 if (mddev->flags)
4331 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332
Andre Nollf233ea52008-07-21 17:05:22 +10004333 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004335 /* If there is a partially-recovered drive we need to
4336 * start recovery here. If we leave it to md_check_recovery,
4337 * it will remove the drives and not do the right thing
4338 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07004339 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004340 int spares = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004341 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004342 if (rdev->raid_disk >= 0 &&
4343 !test_bit(In_sync, &rdev->flags) &&
4344 !test_bit(Faulty, &rdev->flags))
4345 /* complete an interrupted recovery */
4346 spares++;
4347 if (spares && mddev->pers->sync_request) {
4348 mddev->recovery = 0;
4349 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4350 mddev->sync_thread = md_register_thread(md_do_sync,
4351 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10004352 "resync");
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004353 if (!mddev->sync_thread) {
4354 printk(KERN_ERR "%s: could not start resync"
4355 " thread...\n",
4356 mdname(mddev));
4357 /* leave the spares where they are, it shouldn't hurt */
4358 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07004359 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004360 }
4361 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07004362 md_wakeup_thread(mddev->thread);
4363 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004364
NeilBrown449aad32009-08-03 10:59:58 +10004365 revalidate_disk(mddev->gendisk);
Linus Torvalds44ce6292007-05-09 18:51:36 -07004366 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08004367 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004368 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004369 if (mddev->sysfs_action)
4370 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004371 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09004372 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 return 0;
4374}
4375
4376static int restart_array(mddev_t *mddev)
4377{
4378 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379
Andre Noll80fab1d2008-07-11 22:02:21 +10004380 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004382 return -ENXIO;
4383 if (!mddev->pers)
4384 return -EINVAL;
4385 if (!mddev->ro)
4386 return -EBUSY;
4387 mddev->safemode = 0;
4388 mddev->ro = 0;
4389 set_disk_ro(disk, 0);
4390 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4391 mdname(mddev));
4392 /* Kick recovery or resync if necessary */
4393 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4394 md_wakeup_thread(mddev->thread);
4395 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004396 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004397 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398}
4399
NeilBrownacc55e22006-06-26 00:27:47 -07004400/* similar to deny_write_access, but accounts for our holding a reference
4401 * to the file ourselves */
4402static int deny_bitmap_write_access(struct file * file)
4403{
4404 struct inode *inode = file->f_mapping->host;
4405
4406 spin_lock(&inode->i_lock);
4407 if (atomic_read(&inode->i_writecount) > 1) {
4408 spin_unlock(&inode->i_lock);
4409 return -ETXTBSY;
4410 }
4411 atomic_set(&inode->i_writecount, -1);
4412 spin_unlock(&inode->i_lock);
4413
4414 return 0;
4415}
4416
4417static void restore_bitmap_write_access(struct file *file)
4418{
4419 struct inode *inode = file->f_mapping->host;
4420
4421 spin_lock(&inode->i_lock);
4422 atomic_set(&inode->i_writecount, 1);
4423 spin_unlock(&inode->i_lock);
4424}
4425
NeilBrown9e653b62006-06-26 00:27:58 -07004426/* mode:
4427 * 0 - completely stop and dis-assemble array
4428 * 1 - switch to readonly
4429 * 2 - stop but do not disassemble array
4430 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004431static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432{
4433 int err = 0;
4434 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004435 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436
NeilBrownc8c00a62009-08-10 12:50:52 +10004437 mutex_lock(&mddev->open_mutex);
NeilBrownf2ea68c2008-07-21 17:05:25 +10004438 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004439 printk("md: %s still in use.\n",mdname(mddev));
NeilBrownc8c00a62009-08-10 12:50:52 +10004440 err = -EBUSY;
4441 } else if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
4443 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004444 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4446 md_unregister_thread(mddev->sync_thread);
4447 mddev->sync_thread = NULL;
4448 }
4449
4450 del_timer_sync(&mddev->safemode_timer);
4451
NeilBrown9e653b62006-06-26 00:27:58 -07004452 switch(mode) {
4453 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004455 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 goto out;
4457 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004458 break;
4459 case 0: /* disassemble */
4460 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004461 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004462 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 if (mddev->ro)
4464 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004465
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004467 mddev->queue->merge_bvec_fn = NULL;
4468 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004469 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 module_put(mddev->pers->owner);
Dan Williams5fd3a172009-03-04 00:57:25 -07004471 if (mddev->pers->sync_request)
4472 mddev->private = &md_redundancy_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004474 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004475 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004476
NeilBrownc4647292009-05-07 12:51:06 +10004477 list_for_each_entry(rdev, &mddev->disks, same_set)
4478 if (rdev->raid_disk >= 0) {
4479 char nm[20];
4480 sprintf(nm, "rd%d", rdev->raid_disk);
4481 sysfs_remove_link(&mddev->kobj, nm);
4482 }
4483
NeilBrown0d4ca602006-12-10 02:20:44 -08004484 set_capacity(disk, 0);
Linus Torvalds44ce6292007-05-09 18:51:36 -07004485 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08004486
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 if (mddev->ro)
4488 mddev->ro = 0;
4489 }
NeilBrown850b2b42006-10-03 01:15:46 -07004490 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 /* mark array as shutdown cleanly */
4492 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07004493 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 }
NeilBrown9e653b62006-06-26 00:27:58 -07004495 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004497 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown80ffb3c2009-08-18 10:35:26 +10004498 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 }
NeilBrownc8c00a62009-08-10 12:50:52 +10004500out:
4501 mutex_unlock(&mddev->open_mutex);
4502 if (err)
4503 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 /*
4505 * Free resources if final stop
4506 */
NeilBrown9e653b62006-06-26 00:27:58 -07004507 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004508
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4510
NeilBrown978f9462006-02-02 14:28:05 -08004511 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004512 if (mddev->bitmap_info.file) {
4513 restore_bitmap_write_access(mddev->bitmap_info.file);
4514 fput(mddev->bitmap_info.file);
4515 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004516 }
NeilBrownc3d97142009-12-14 12:49:52 +11004517 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004518
NeilBrown177a99b2008-02-06 01:39:56 -08004519 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07004520 flush_scheduled_work();
4521
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 export_array(mddev);
4523
Andre Nollf233ea52008-07-21 17:05:22 +10004524 mddev->array_sectors = 0;
Dan Williamsb522adc2009-03-31 15:00:31 +11004525 mddev->external_size = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004526 mddev->dev_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004527 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004528 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004529 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004530 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004531 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004532 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004533 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004534 mddev->level = LEVEL_NONE;
4535 mddev->clevel[0] = 0;
4536 mddev->flags = 0;
4537 mddev->ro = 0;
4538 mddev->metadata_type[0] = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10004539 mddev->chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004540 mddev->ctime = mddev->utime = 0;
4541 mddev->layout = 0;
4542 mddev->max_disks = 0;
4543 mddev->events = 0;
4544 mddev->delta_disks = 0;
4545 mddev->new_level = LEVEL_NONE;
4546 mddev->new_layout = 0;
Andre Noll664e7c42009-06-18 08:45:27 +10004547 mddev->new_chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004548 mddev->curr_resync = 0;
4549 mddev->resync_mismatches = 0;
4550 mddev->suspend_lo = mddev->suspend_hi = 0;
4551 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4552 mddev->recovery = 0;
4553 mddev->in_sync = 0;
4554 mddev->changed = 0;
4555 mddev->degraded = 0;
4556 mddev->barriers_work = 0;
4557 mddev->safemode = 0;
NeilBrownc3d97142009-12-14 12:49:52 +11004558 mddev->bitmap_info.offset = 0;
4559 mddev->bitmap_info.default_offset = 0;
NeilBrown42a04b52009-12-14 12:49:53 +11004560 mddev->bitmap_info.chunksize = 0;
4561 mddev->bitmap_info.daemon_sleep = 0;
4562 mddev->bitmap_info.max_write_behind = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004563 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004564 if (mddev->hold_active == UNTIL_STOP)
4565 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004566
NeilBrowna8a55c32006-06-26 00:27:39 -07004567 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4569 mdname(mddev));
4570 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004571 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004572 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004573 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 return err;
4575}
4576
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004577#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578static void autorun_array(mddev_t *mddev)
4579{
4580 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 int err;
4582
NeilBrowna757e642005-04-16 15:26:42 -07004583 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585
4586 printk(KERN_INFO "md: running: ");
4587
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004588 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 char b[BDEVNAME_SIZE];
4590 printk("<%s>", bdevname(rdev->bdev,b));
4591 }
4592 printk("\n");
4593
NeilBrownd710e132008-10-13 11:55:12 +11004594 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 if (err) {
4596 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004597 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 }
4599}
4600
4601/*
4602 * lets try to run arrays based on all disks that have arrived
4603 * until now. (those are in pending_raid_disks)
4604 *
4605 * the method: pick the first pending disk, collect all disks with
4606 * the same UUID, remove all from the pending list and put them into
4607 * the 'same_array' list. Then order this list based on superblock
4608 * update time (freshest comes first), kick out 'old' disks and
4609 * compare superblocks. If everything's fine then run it.
4610 *
4611 * If "unit" is allocated, then bump its reference count
4612 */
4613static void autorun_devices(int part)
4614{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004615 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 mddev_t *mddev;
4617 char b[BDEVNAME_SIZE];
4618
4619 printk(KERN_INFO "md: autorun ...\n");
4620 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004621 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004623 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 rdev0 = list_entry(pending_raid_disks.next,
4625 mdk_rdev_t, same_set);
4626
4627 printk(KERN_INFO "md: considering %s ...\n",
4628 bdevname(rdev0->bdev,b));
4629 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004630 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 if (super_90_load(rdev, rdev0, 0) >= 0) {
4632 printk(KERN_INFO "md: adding %s ...\n",
4633 bdevname(rdev->bdev,b));
4634 list_move(&rdev->same_set, &candidates);
4635 }
4636 /*
4637 * now we have a set of devices, with all of them having
4638 * mostly sane superblocks. It's time to allocate the
4639 * mddev.
4640 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004641 if (part) {
4642 dev = MKDEV(mdp_major,
4643 rdev0->preferred_minor << MdpMinorShift);
4644 unit = MINOR(dev) >> MdpMinorShift;
4645 } else {
4646 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4647 unit = MINOR(dev);
4648 }
4649 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4651 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4652 break;
4653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654
4655 md_probe(dev, NULL, NULL);
4656 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004657 if (!mddev || !mddev->gendisk) {
4658 if (mddev)
4659 mddev_put(mddev);
4660 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 "md: cannot allocate memory for md drive.\n");
4662 break;
4663 }
4664 if (mddev_lock(mddev))
4665 printk(KERN_WARNING "md: %s locked, cannot run\n",
4666 mdname(mddev));
4667 else if (mddev->raid_disks || mddev->major_version
4668 || !list_empty(&mddev->disks)) {
4669 printk(KERN_WARNING
4670 "md: %s already running, cannot run %s\n",
4671 mdname(mddev), bdevname(rdev0->bdev,b));
4672 mddev_unlock(mddev);
4673 } else {
4674 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004675 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004676 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 list_del_init(&rdev->same_set);
4678 if (bind_rdev_to_array(rdev, mddev))
4679 export_rdev(rdev);
4680 }
4681 autorun_array(mddev);
4682 mddev_unlock(mddev);
4683 }
4684 /* on success, candidates will be empty, on error
4685 * it won't...
4686 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004687 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004688 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 mddev_put(mddev);
4692 }
4693 printk(KERN_INFO "md: ... autorun DONE.\n");
4694}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004695#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697static int get_version(void __user * arg)
4698{
4699 mdu_version_t ver;
4700
4701 ver.major = MD_MAJOR_VERSION;
4702 ver.minor = MD_MINOR_VERSION;
4703 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4704
4705 if (copy_to_user(arg, &ver, sizeof(ver)))
4706 return -EFAULT;
4707
4708 return 0;
4709}
4710
4711static int get_array_info(mddev_t * mddev, void __user * arg)
4712{
4713 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004714 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716
NeilBrowna9f326e2009-09-23 18:06:41 +10004717 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004718 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004720 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 failed++;
4722 else {
4723 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004724 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004725 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 else
4727 spare++;
4728 }
4729 }
4730
4731 info.major_version = mddev->major_version;
4732 info.minor_version = mddev->minor_version;
4733 info.patch_version = MD_PATCHLEVEL_VERSION;
4734 info.ctime = mddev->ctime;
4735 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004736 info.size = mddev->dev_sectors / 2;
4737 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004738 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 info.nr_disks = nr;
4740 info.raid_disks = mddev->raid_disks;
4741 info.md_minor = mddev->md_minor;
4742 info.not_persistent= !mddev->persistent;
4743
4744 info.utime = mddev->utime;
4745 info.state = 0;
4746 if (mddev->in_sync)
4747 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11004748 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07004749 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004750 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 info.working_disks = working;
4752 info.failed_disks = failed;
4753 info.spare_disks = spare;
4754
4755 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004756 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757
4758 if (copy_to_user(arg, &info, sizeof(info)))
4759 return -EFAULT;
4760
4761 return 0;
4762}
4763
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004764static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004765{
4766 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4767 char *ptr, *buf = NULL;
4768 int err = -ENOMEM;
4769
Dan Williamsb5470dc2008-06-27 21:44:04 -07004770 if (md_allow_write(mddev))
4771 file = kmalloc(sizeof(*file), GFP_NOIO);
4772 else
4773 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004774
NeilBrown32a76272005-06-21 17:17:14 -07004775 if (!file)
4776 goto out;
4777
4778 /* bitmap disabled, zero the first byte and copy out */
4779 if (!mddev->bitmap || !mddev->bitmap->file) {
4780 file->pathname[0] = '\0';
4781 goto copy_out;
4782 }
4783
4784 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4785 if (!buf)
4786 goto out;
4787
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004788 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4789 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004790 goto out;
4791
4792 strcpy(file->pathname, ptr);
4793
4794copy_out:
4795 err = 0;
4796 if (copy_to_user(arg, file, sizeof(*file)))
4797 err = -EFAULT;
4798out:
4799 kfree(buf);
4800 kfree(file);
4801 return err;
4802}
4803
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804static int get_disk_info(mddev_t * mddev, void __user * arg)
4805{
4806 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 mdk_rdev_t *rdev;
4808
4809 if (copy_from_user(&info, arg, sizeof(info)))
4810 return -EFAULT;
4811
Andre Noll26ef3792008-07-11 22:02:21 +10004812 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 if (rdev) {
4814 info.major = MAJOR(rdev->bdev->bd_dev);
4815 info.minor = MINOR(rdev->bdev->bd_dev);
4816 info.raid_disk = rdev->raid_disk;
4817 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004818 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004820 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 info.state |= (1<<MD_DISK_ACTIVE);
4822 info.state |= (1<<MD_DISK_SYNC);
4823 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004824 if (test_bit(WriteMostly, &rdev->flags))
4825 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 } else {
4827 info.major = info.minor = 0;
4828 info.raid_disk = -1;
4829 info.state = (1<<MD_DISK_REMOVED);
4830 }
4831
4832 if (copy_to_user(arg, &info, sizeof(info)))
4833 return -EFAULT;
4834
4835 return 0;
4836}
4837
4838static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4839{
4840 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4841 mdk_rdev_t *rdev;
4842 dev_t dev = MKDEV(info->major,info->minor);
4843
4844 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4845 return -EOVERFLOW;
4846
4847 if (!mddev->raid_disks) {
4848 int err;
4849 /* expecting a device which has a superblock */
4850 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4851 if (IS_ERR(rdev)) {
4852 printk(KERN_WARNING
4853 "md: md_import_device returned %ld\n",
4854 PTR_ERR(rdev));
4855 return PTR_ERR(rdev);
4856 }
4857 if (!list_empty(&mddev->disks)) {
4858 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4859 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10004860 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 .load_super(rdev, rdev0, mddev->minor_version);
4862 if (err < 0) {
4863 printk(KERN_WARNING
4864 "md: %s has different UUID to %s\n",
4865 bdevname(rdev->bdev,b),
4866 bdevname(rdev0->bdev,b2));
4867 export_rdev(rdev);
4868 return -EINVAL;
4869 }
4870 }
4871 err = bind_rdev_to_array(rdev, mddev);
4872 if (err)
4873 export_rdev(rdev);
4874 return err;
4875 }
4876
4877 /*
4878 * add_new_disk can be used once the array is assembled
4879 * to add "hot spares". They must already have a superblock
4880 * written
4881 */
4882 if (mddev->pers) {
4883 int err;
4884 if (!mddev->pers->hot_add_disk) {
4885 printk(KERN_WARNING
4886 "%s: personality does not support diskops!\n",
4887 mdname(mddev));
4888 return -EINVAL;
4889 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004890 if (mddev->persistent)
4891 rdev = md_import_device(dev, mddev->major_version,
4892 mddev->minor_version);
4893 else
4894 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 if (IS_ERR(rdev)) {
4896 printk(KERN_WARNING
4897 "md: md_import_device returned %ld\n",
4898 PTR_ERR(rdev));
4899 return PTR_ERR(rdev);
4900 }
NeilBrown41158c72005-06-21 17:17:25 -07004901 /* set save_raid_disk if appropriate */
4902 if (!mddev->persistent) {
4903 if (info->state & (1<<MD_DISK_SYNC) &&
4904 info->raid_disk < mddev->raid_disks)
4905 rdev->raid_disk = info->raid_disk;
4906 else
4907 rdev->raid_disk = -1;
4908 } else
4909 super_types[mddev->major_version].
4910 validate_super(mddev, rdev);
4911 rdev->saved_raid_disk = rdev->raid_disk;
4912
NeilBrownb2d444d2005-11-08 21:39:31 -08004913 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004914 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4915 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11004916 else
4917 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004918
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 rdev->raid_disk = -1;
4920 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004921 if (!err && !mddev->pers->hot_remove_disk) {
4922 /* If there is hot_add_disk but no hot_remove_disk
4923 * then added disks for geometry changes,
4924 * and should be added immediately.
4925 */
4926 super_types[mddev->major_version].
4927 validate_super(mddev, rdev);
4928 err = mddev->pers->hot_add_disk(mddev, rdev);
4929 if (err)
4930 unbind_rdev_from_array(rdev);
4931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 if (err)
4933 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004934 else
NeilBrown3c0ee632008-10-21 13:25:28 +11004935 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07004936
NeilBrown17571282006-12-10 02:20:52 -08004937 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004938 if (mddev->degraded)
4939 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004940 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004941 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 return err;
4943 }
4944
4945 /* otherwise, add_new_disk is only allowed
4946 * for major_version==0 superblocks
4947 */
4948 if (mddev->major_version != 0) {
4949 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4950 mdname(mddev));
4951 return -EINVAL;
4952 }
4953
4954 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4955 int err;
NeilBrownd710e132008-10-13 11:55:12 +11004956 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 if (IS_ERR(rdev)) {
4958 printk(KERN_WARNING
4959 "md: error, md_import_device() returned %ld\n",
4960 PTR_ERR(rdev));
4961 return PTR_ERR(rdev);
4962 }
4963 rdev->desc_nr = info->number;
4964 if (info->raid_disk < mddev->raid_disks)
4965 rdev->raid_disk = info->raid_disk;
4966 else
4967 rdev->raid_disk = -1;
4968
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004970 if (info->state & (1<<MD_DISK_SYNC))
4971 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004973 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4974 set_bit(WriteMostly, &rdev->flags);
4975
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 if (!mddev->persistent) {
4977 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10004978 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 } else
Andre Noll0f420352008-07-11 22:02:23 +10004980 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10004981 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982
NeilBrown2bf071b2006-01-06 00:20:55 -08004983 err = bind_rdev_to_array(rdev, mddev);
4984 if (err) {
4985 export_rdev(rdev);
4986 return err;
4987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 }
4989
4990 return 0;
4991}
4992
4993static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4994{
4995 char b[BDEVNAME_SIZE];
4996 mdk_rdev_t *rdev;
4997
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 rdev = find_rdev(mddev, dev);
4999 if (!rdev)
5000 return -ENXIO;
5001
5002 if (rdev->raid_disk >= 0)
5003 goto busy;
5004
5005 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005006 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005007 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008
5009 return 0;
5010busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005011 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 bdevname(rdev->bdev,b), mdname(mddev));
5013 return -EBUSY;
5014}
5015
5016static int hot_add_disk(mddev_t * mddev, dev_t dev)
5017{
5018 char b[BDEVNAME_SIZE];
5019 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 mdk_rdev_t *rdev;
5021
5022 if (!mddev->pers)
5023 return -ENODEV;
5024
5025 if (mddev->major_version != 0) {
5026 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5027 " version-0 superblocks.\n",
5028 mdname(mddev));
5029 return -EINVAL;
5030 }
5031 if (!mddev->pers->hot_add_disk) {
5032 printk(KERN_WARNING
5033 "%s: personality does not support diskops!\n",
5034 mdname(mddev));
5035 return -EINVAL;
5036 }
5037
NeilBrownd710e132008-10-13 11:55:12 +11005038 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 if (IS_ERR(rdev)) {
5040 printk(KERN_WARNING
5041 "md: error, md_import_device() returned %ld\n",
5042 PTR_ERR(rdev));
5043 return -EINVAL;
5044 }
5045
5046 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10005047 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 else
Andre Noll0f420352008-07-11 22:02:23 +10005049 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050
NeilBrown8190e752009-06-18 08:48:58 +10005051 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052
NeilBrownb2d444d2005-11-08 21:39:31 -08005053 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 printk(KERN_WARNING
5055 "md: can not hot-add faulty %s disk to %s!\n",
5056 bdevname(rdev->bdev,b), mdname(mddev));
5057 err = -EINVAL;
5058 goto abort_export;
5059 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005060 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005062 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005063 err = bind_rdev_to_array(rdev, mddev);
5064 if (err)
5065 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066
5067 /*
5068 * The rest should better be atomic, we can have disk failures
5069 * noticed in interrupt contexts ...
5070 */
5071
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 rdev->raid_disk = -1;
5073
NeilBrown850b2b42006-10-03 01:15:46 -07005074 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075
5076 /*
5077 * Kick recovery, maybe this spare has to be added to the
5078 * array immediately.
5079 */
5080 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5081 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005082 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 return 0;
5084
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085abort_export:
5086 export_rdev(rdev);
5087 return err;
5088}
5089
NeilBrown32a76272005-06-21 17:17:14 -07005090static int set_bitmap_file(mddev_t *mddev, int fd)
5091{
5092 int err;
5093
NeilBrown36fa3062005-09-09 16:23:45 -07005094 if (mddev->pers) {
5095 if (!mddev->pers->quiesce)
5096 return -EBUSY;
5097 if (mddev->recovery || mddev->sync_thread)
5098 return -EBUSY;
5099 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005100 }
5101
NeilBrown36fa3062005-09-09 16:23:45 -07005102
5103 if (fd >= 0) {
5104 if (mddev->bitmap)
5105 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005106 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005107
NeilBrownc3d97142009-12-14 12:49:52 +11005108 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005109 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5110 mdname(mddev));
5111 return -EBADF;
5112 }
5113
NeilBrownc3d97142009-12-14 12:49:52 +11005114 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005115 if (err) {
5116 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5117 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005118 fput(mddev->bitmap_info.file);
5119 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005120 return err;
5121 }
NeilBrownc3d97142009-12-14 12:49:52 +11005122 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005123 } else if (mddev->bitmap == NULL)
5124 return -ENOENT; /* cannot remove what isn't there */
5125 err = 0;
5126 if (mddev->pers) {
5127 mddev->pers->quiesce(mddev, 1);
5128 if (fd >= 0)
5129 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005130 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005131 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005132 fd = -1; /* make sure to put the file */
5133 }
NeilBrown36fa3062005-09-09 16:23:45 -07005134 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005135 }
5136 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005137 if (mddev->bitmap_info.file) {
5138 restore_bitmap_write_access(mddev->bitmap_info.file);
5139 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005140 }
NeilBrownc3d97142009-12-14 12:49:52 +11005141 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005142 }
5143
NeilBrown32a76272005-06-21 17:17:14 -07005144 return err;
5145}
5146
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147/*
5148 * set_array_info is used two different ways
5149 * The original usage is when creating a new array.
5150 * In this usage, raid_disks is > 0 and it together with
5151 * level, size, not_persistent,layout,chunksize determine the
5152 * shape of the array.
5153 * This will always create an array with a type-0.90.0 superblock.
5154 * The newer usage is when assembling an array.
5155 * In this case raid_disks will be 0, and the major_version field is
5156 * use to determine which style super-blocks are to be found on the devices.
5157 * The minor and patch _version numbers are also kept incase the
5158 * super_block handler wishes to interpret them.
5159 */
5160static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5161{
5162
5163 if (info->raid_disks == 0) {
5164 /* just setting version number for superblock loading */
5165 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005166 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 super_types[info->major_version].name == NULL) {
5168 /* maybe try to auto-load a module? */
5169 printk(KERN_INFO
5170 "md: superblock version %d not known\n",
5171 info->major_version);
5172 return -EINVAL;
5173 }
5174 mddev->major_version = info->major_version;
5175 mddev->minor_version = info->minor_version;
5176 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005177 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 return 0;
5179 }
5180 mddev->major_version = MD_MAJOR_VERSION;
5181 mddev->minor_version = MD_MINOR_VERSION;
5182 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5183 mddev->ctime = get_seconds();
5184
5185 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005186 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005187 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 mddev->raid_disks = info->raid_disks;
5189 /* don't set md_minor, it is determined by which /dev/md* was
5190 * openned
5191 */
5192 if (info->state & (1<<MD_SB_CLEAN))
5193 mddev->recovery_cp = MaxSector;
5194 else
5195 mddev->recovery_cp = 0;
5196 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005197 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198
5199 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005200 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201
5202 mddev->max_disks = MD_SB_DISKS;
5203
NeilBrowne6910632008-02-06 01:39:51 -08005204 if (mddev->persistent)
5205 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005206 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207
NeilBrownc3d97142009-12-14 12:49:52 +11005208 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5209 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005210
NeilBrownf6705572006-03-27 01:18:11 -08005211 mddev->reshape_position = MaxSector;
5212
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 /*
5214 * Generate a 128 bit UUID
5215 */
5216 get_random_bytes(mddev->uuid, 16);
5217
NeilBrownf6705572006-03-27 01:18:11 -08005218 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005219 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005220 mddev->new_layout = mddev->layout;
5221 mddev->delta_disks = 0;
5222
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 return 0;
5224}
5225
Dan Williams1f403622009-03-31 14:59:03 +11005226void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5227{
Dan Williamsb522adc2009-03-31 15:00:31 +11005228 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5229
5230 if (mddev->external_size)
5231 return;
5232
Dan Williams1f403622009-03-31 14:59:03 +11005233 mddev->array_sectors = array_sectors;
5234}
5235EXPORT_SYMBOL(md_set_array_sectors);
5236
Andre Nolld71f9f82008-07-11 22:02:22 +10005237static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005238{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005239 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005240 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005241 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005242
5243 if (mddev->pers->resize == NULL)
5244 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005245 /* The "num_sectors" is the number of sectors of each device that
5246 * is used. This can only make sense for arrays with redundancy.
5247 * linear and raid0 always use whatever space is available. We can only
5248 * consider changing this number if no resync or reconstruction is
5249 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005250 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005251 * of each device. If num_sectors is zero, we find the largest size
5252 * that fits.
5253
NeilBrowna35b0d62006-01-06 00:20:49 -08005254 */
5255 if (mddev->sync_thread)
5256 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005257 if (mddev->bitmap)
5258 /* Sorry, cannot grow a bitmap yet, just remove it,
5259 * grow, and re-add.
5260 */
5261 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005262 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005263 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005264
Andre Nolld71f9f82008-07-11 22:02:22 +10005265 if (fit && (num_sectors == 0 || num_sectors > avail))
5266 num_sectors = avail;
5267 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005268 return -ENOSPC;
5269 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005270 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005271 if (!rv)
5272 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005273 return rv;
5274}
5275
NeilBrownda943b92006-01-06 00:20:54 -08005276static int update_raid_disks(mddev_t *mddev, int raid_disks)
5277{
5278 int rv;
5279 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005280 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b92006-01-06 00:20:54 -08005281 return -EINVAL;
5282 if (raid_disks <= 0 ||
5283 raid_disks >= mddev->max_disks)
5284 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005285 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b92006-01-06 00:20:54 -08005286 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005287 mddev->delta_disks = raid_disks - mddev->raid_disks;
5288
5289 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b92006-01-06 00:20:54 -08005290 return rv;
5291}
5292
5293
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294/*
5295 * update_array_info is used to change the configuration of an
5296 * on-line array.
5297 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5298 * fields in the info are checked against the array.
5299 * Any differences that cannot be handled will cause an error.
5300 * Normally, only one change can be managed at a time.
5301 */
5302static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5303{
5304 int rv = 0;
5305 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005306 int state = 0;
5307
5308 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005309 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005310 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311
5312 if (mddev->major_version != info->major_version ||
5313 mddev->minor_version != info->minor_version ||
5314/* mddev->patch_version != info->patch_version || */
5315 mddev->ctime != info->ctime ||
5316 mddev->level != info->level ||
5317/* mddev->layout != info->layout || */
5318 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005319 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005320 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5321 ((state^info->state) & 0xfffffe00)
5322 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 return -EINVAL;
5324 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005325 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5326 cnt++;
5327 if (mddev->raid_disks != info->raid_disks)
5328 cnt++;
5329 if (mddev->layout != info->layout)
5330 cnt++;
5331 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5332 cnt++;
5333 if (cnt == 0)
5334 return 0;
5335 if (cnt > 1)
5336 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337
5338 if (mddev->layout != info->layout) {
5339 /* Change layout
5340 * we don't need to do anything at the md level, the
5341 * personality will take care of it all.
5342 */
NeilBrown50ac1682009-06-18 08:47:55 +10005343 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005345 else {
5346 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005347 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005348 if (rv)
5349 mddev->new_layout = mddev->layout;
5350 return rv;
5351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005353 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005354 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355
NeilBrownda943b92006-01-06 00:20:54 -08005356 if (mddev->raid_disks != info->raid_disks)
5357 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358
NeilBrown36fa3062005-09-09 16:23:45 -07005359 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5360 if (mddev->pers->quiesce == NULL)
5361 return -EINVAL;
5362 if (mddev->recovery || mddev->sync_thread)
5363 return -EBUSY;
5364 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5365 /* add the bitmap */
5366 if (mddev->bitmap)
5367 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005368 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005369 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005370 mddev->bitmap_info.offset =
5371 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005372 mddev->pers->quiesce(mddev, 1);
5373 rv = bitmap_create(mddev);
5374 if (rv)
5375 bitmap_destroy(mddev);
5376 mddev->pers->quiesce(mddev, 0);
5377 } else {
5378 /* remove the bitmap */
5379 if (!mddev->bitmap)
5380 return -ENOENT;
5381 if (mddev->bitmap->file)
5382 return -EINVAL;
5383 mddev->pers->quiesce(mddev, 1);
5384 bitmap_destroy(mddev);
5385 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005386 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005387 }
5388 }
NeilBrown850b2b42006-10-03 01:15:46 -07005389 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 return rv;
5391}
5392
5393static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5394{
5395 mdk_rdev_t *rdev;
5396
5397 if (mddev->pers == NULL)
5398 return -ENODEV;
5399
5400 rdev = find_rdev(mddev, dev);
5401 if (!rdev)
5402 return -ENODEV;
5403
5404 md_error(mddev, rdev);
5405 return 0;
5406}
5407
Andre Noll2f9618c2008-04-25 18:57:58 +02005408/*
5409 * We have a problem here : there is no easy way to give a CHS
5410 * virtual geometry. We currently pretend that we have a 2 heads
5411 * 4 sectors (with a BIG number of cylinders...). This drives
5412 * dosfs just mad... ;-)
5413 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005414static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5415{
5416 mddev_t *mddev = bdev->bd_disk->private_data;
5417
5418 geo->heads = 2;
5419 geo->sectors = 4;
5420 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5421 return 0;
5422}
5423
Al Viroa39907f2008-03-02 10:31:15 -05005424static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 unsigned int cmd, unsigned long arg)
5426{
5427 int err = 0;
5428 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 mddev_t *mddev = NULL;
5430
5431 if (!capable(CAP_SYS_ADMIN))
5432 return -EACCES;
5433
5434 /*
5435 * Commands dealing with the RAID driver but not any
5436 * particular array:
5437 */
5438 switch (cmd)
5439 {
5440 case RAID_VERSION:
5441 err = get_version(argp);
5442 goto done;
5443
5444 case PRINT_RAID_DEBUG:
5445 err = 0;
5446 md_print_devices();
5447 goto done;
5448
5449#ifndef MODULE
5450 case RAID_AUTORUN:
5451 err = 0;
5452 autostart_arrays(arg);
5453 goto done;
5454#endif
5455 default:;
5456 }
5457
5458 /*
5459 * Commands creating/starting a new array:
5460 */
5461
Al Viroa39907f2008-03-02 10:31:15 -05005462 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463
5464 if (!mddev) {
5465 BUG();
5466 goto abort;
5467 }
5468
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 err = mddev_lock(mddev);
5470 if (err) {
5471 printk(KERN_INFO
5472 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5473 err, cmd);
5474 goto abort;
5475 }
5476
5477 switch (cmd)
5478 {
5479 case SET_ARRAY_INFO:
5480 {
5481 mdu_array_info_t info;
5482 if (!arg)
5483 memset(&info, 0, sizeof(info));
5484 else if (copy_from_user(&info, argp, sizeof(info))) {
5485 err = -EFAULT;
5486 goto abort_unlock;
5487 }
5488 if (mddev->pers) {
5489 err = update_array_info(mddev, &info);
5490 if (err) {
5491 printk(KERN_WARNING "md: couldn't update"
5492 " array info. %d\n", err);
5493 goto abort_unlock;
5494 }
5495 goto done_unlock;
5496 }
5497 if (!list_empty(&mddev->disks)) {
5498 printk(KERN_WARNING
5499 "md: array %s already has disks!\n",
5500 mdname(mddev));
5501 err = -EBUSY;
5502 goto abort_unlock;
5503 }
5504 if (mddev->raid_disks) {
5505 printk(KERN_WARNING
5506 "md: array %s already initialised!\n",
5507 mdname(mddev));
5508 err = -EBUSY;
5509 goto abort_unlock;
5510 }
5511 err = set_array_info(mddev, &info);
5512 if (err) {
5513 printk(KERN_WARNING "md: couldn't set"
5514 " array info. %d\n", err);
5515 goto abort_unlock;
5516 }
5517 }
5518 goto done_unlock;
5519
5520 default:;
5521 }
5522
5523 /*
5524 * Commands querying/configuring an existing array:
5525 */
NeilBrown32a76272005-06-21 17:17:14 -07005526 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005527 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005528 if ((!mddev->raid_disks && !mddev->external)
5529 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5530 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5531 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 err = -ENODEV;
5533 goto abort_unlock;
5534 }
5535
5536 /*
5537 * Commands even a read-only array can execute:
5538 */
5539 switch (cmd)
5540 {
5541 case GET_ARRAY_INFO:
5542 err = get_array_info(mddev, argp);
5543 goto done_unlock;
5544
NeilBrown32a76272005-06-21 17:17:14 -07005545 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005546 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005547 goto done_unlock;
5548
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 case GET_DISK_INFO:
5550 err = get_disk_info(mddev, argp);
5551 goto done_unlock;
5552
5553 case RESTART_ARRAY_RW:
5554 err = restart_array(mddev);
5555 goto done_unlock;
5556
5557 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005558 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 goto done_unlock;
5560
5561 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005562 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 goto done_unlock;
5564
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 }
5566
5567 /*
5568 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005569 * superblock, so we do not allow them on read-only arrays.
5570 * However non-MD ioctls (e.g. get-size) will still come through
5571 * here and hit the 'default' below, so only disallow
5572 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005574 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005575 if (mddev->ro == 2) {
5576 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005577 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005578 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5579 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005580 } else {
5581 err = -EROFS;
5582 goto abort_unlock;
5583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 }
5585
5586 switch (cmd)
5587 {
5588 case ADD_NEW_DISK:
5589 {
5590 mdu_disk_info_t info;
5591 if (copy_from_user(&info, argp, sizeof(info)))
5592 err = -EFAULT;
5593 else
5594 err = add_new_disk(mddev, &info);
5595 goto done_unlock;
5596 }
5597
5598 case HOT_REMOVE_DISK:
5599 err = hot_remove_disk(mddev, new_decode_dev(arg));
5600 goto done_unlock;
5601
5602 case HOT_ADD_DISK:
5603 err = hot_add_disk(mddev, new_decode_dev(arg));
5604 goto done_unlock;
5605
5606 case SET_DISK_FAULTY:
5607 err = set_disk_faulty(mddev, new_decode_dev(arg));
5608 goto done_unlock;
5609
5610 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005611 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 goto done_unlock;
5613
NeilBrown32a76272005-06-21 17:17:14 -07005614 case SET_BITMAP_FILE:
5615 err = set_bitmap_file(mddev, (int)arg);
5616 goto done_unlock;
5617
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 err = -EINVAL;
5620 goto abort_unlock;
5621 }
5622
5623done_unlock:
5624abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005625 if (mddev->hold_active == UNTIL_IOCTL &&
5626 err != -EINVAL)
5627 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 mddev_unlock(mddev);
5629
5630 return err;
5631done:
5632 if (err)
5633 MD_BUG();
5634abort:
5635 return err;
5636}
5637
Al Viroa39907f2008-03-02 10:31:15 -05005638static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639{
5640 /*
5641 * Succeed if we can lock the mddev, which confirms that
5642 * it isn't being stopped right now.
5643 */
NeilBrownd3374822009-01-09 08:31:10 +11005644 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 int err;
5646
NeilBrownd3374822009-01-09 08:31:10 +11005647 if (mddev->gendisk != bdev->bd_disk) {
5648 /* we are racing with mddev_put which is discarding this
5649 * bd_disk.
5650 */
5651 mddev_put(mddev);
5652 /* Wait until bdev->bd_disk is definitely gone */
5653 flush_scheduled_work();
5654 /* Then retry the open from the top */
5655 return -ERESTARTSYS;
5656 }
5657 BUG_ON(mddev != bdev->bd_disk->private_data);
5658
NeilBrownc8c00a62009-08-10 12:50:52 +10005659 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 goto out;
5661
5662 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005663 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005664 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665
Al Viroa39907f2008-03-02 10:31:15 -05005666 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 out:
5668 return err;
5669}
5670
Al Viroa39907f2008-03-02 10:31:15 -05005671static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672{
Al Viroa39907f2008-03-02 10:31:15 -05005673 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005675 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005676 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 mddev_put(mddev);
5678
5679 return 0;
5680}
5681
Linus Torvalds44ce6292007-05-09 18:51:36 -07005682static int md_media_changed(struct gendisk *disk)
5683{
5684 mddev_t *mddev = disk->private_data;
5685
5686 return mddev->changed;
5687}
5688
5689static int md_revalidate(struct gendisk *disk)
5690{
5691 mddev_t *mddev = disk->private_data;
5692
5693 mddev->changed = 0;
5694 return 0;
5695}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005696static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697{
5698 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005699 .open = md_open,
5700 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005701 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005702 .getgeo = md_getgeo,
Linus Torvalds44ce6292007-05-09 18:51:36 -07005703 .media_changed = md_media_changed,
5704 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705};
5706
Adrian Bunk75c96f82005-05-05 16:16:09 -07005707static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708{
5709 mdk_thread_t *thread = arg;
5710
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 /*
5712 * md_thread is a 'system-thread', it's priority should be very
5713 * high. We avoid resource deadlocks individually in each
5714 * raid personality. (RAID5 does preallocation) We also use RR and
5715 * the very same RT priority as kswapd, thus we will never get
5716 * into a priority inversion deadlock.
5717 *
5718 * we definitely have to have equal or higher priority than
5719 * bdflush, otherwise bdflush will deadlock if there are too
5720 * many dirty RAID5 blocks.
5721 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722
NeilBrown6985c432005-10-19 21:23:47 -07005723 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005724 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725
NeilBrown93588e22005-11-15 00:09:12 -08005726 /* We need to wait INTERRUPTIBLE so that
5727 * we don't add to the load-average.
5728 * That means we need to be sure no signals are
5729 * pending
5730 */
5731 if (signal_pending(current))
5732 flush_signals(current);
5733
5734 wait_event_interruptible_timeout
5735 (thread->wqueue,
5736 test_bit(THREAD_WAKEUP, &thread->flags)
5737 || kthread_should_stop(),
5738 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739
5740 clear_bit(THREAD_WAKEUP, &thread->flags);
5741
NeilBrown787453c2005-11-08 21:39:43 -08005742 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005744
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 return 0;
5746}
5747
5748void md_wakeup_thread(mdk_thread_t *thread)
5749{
5750 if (thread) {
5751 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5752 set_bit(THREAD_WAKEUP, &thread->flags);
5753 wake_up(&thread->wqueue);
5754 }
5755}
5756
5757mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5758 const char *name)
5759{
5760 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761
NeilBrown9ffae0c2006-01-06 00:20:32 -08005762 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 if (!thread)
5764 return NULL;
5765
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 init_waitqueue_head(&thread->wqueue);
5767
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 thread->run = run;
5769 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005770 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10005771 thread->tsk = kthread_run(md_thread, thread,
5772 "%s_%s",
5773 mdname(thread->mddev),
5774 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07005775 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 kfree(thread);
5777 return NULL;
5778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 return thread;
5780}
5781
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782void md_unregister_thread(mdk_thread_t *thread)
5783{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005784 if (!thread)
5785 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005786 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005787
5788 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 kfree(thread);
5790}
5791
5792void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5793{
5794 if (!mddev) {
5795 MD_BUG();
5796 return;
5797 }
5798
NeilBrownb2d444d2005-11-08 21:39:31 -08005799 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005801
5802 if (mddev->external)
5803 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005804/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5806 mdname(mddev),
5807 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5808 __builtin_return_address(0),__builtin_return_address(1),
5809 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005810*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005811 if (!mddev->pers)
5812 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 if (!mddev->pers->error_handler)
5814 return;
5815 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005816 if (mddev->degraded)
5817 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005818 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5820 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5821 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005822 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823}
5824
5825/* seq_file implementation /proc/mdstat */
5826
5827static void status_unused(struct seq_file *seq)
5828{
5829 int i = 0;
5830 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831
5832 seq_printf(seq, "unused devices: ");
5833
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005834 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 char b[BDEVNAME_SIZE];
5836 i++;
5837 seq_printf(seq, "%s ",
5838 bdevname(rdev->bdev,b));
5839 }
5840 if (!i)
5841 seq_printf(seq, "<none>");
5842
5843 seq_printf(seq, "\n");
5844}
5845
5846
5847static void status_resync(struct seq_file *seq, mddev_t * mddev)
5848{
NeilBrowndd71cf62009-05-07 12:49:35 +10005849 sector_t max_sectors, resync, res;
5850 unsigned long dt, db;
5851 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08005852 int scale;
5853 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854
NeilBrowndd71cf62009-05-07 12:49:35 +10005855 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856
5857 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10005858 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 else
NeilBrowndd71cf62009-05-07 12:49:35 +10005860 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861
5862 /*
5863 * Should not happen.
5864 */
NeilBrowndd71cf62009-05-07 12:49:35 +10005865 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 MD_BUG();
5867 return;
5868 }
NeilBrown4588b422006-03-27 01:18:04 -08005869 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10005870 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08005871 * u32, as those are the requirements for sector_div.
5872 * Thus 'scale' must be at least 10
5873 */
5874 scale = 10;
5875 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10005876 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08005877 scale++;
5878 }
5879 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10005880 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08005881
5882 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883 {
NeilBrown4588b422006-03-27 01:18:04 -08005884 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 seq_printf(seq, "[");
5886 for (i = 0; i < x; i++)
5887 seq_printf(seq, "=");
5888 seq_printf(seq, ">");
5889 for (i = 0; i < y; i++)
5890 seq_printf(seq, ".");
5891 seq_printf(seq, "] ");
5892 }
NeilBrown4588b422006-03-27 01:18:04 -08005893 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005894 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5895 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005896 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5897 "check" :
5898 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5899 "resync" : "recovery"))),
5900 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10005901 (unsigned long long) resync/2,
5902 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903
5904 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 * dt: time from mark until now
5906 * db: blocks written from mark until now
5907 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10005908 *
5909 * rt is a sector_t, so could be 32bit or 64bit.
5910 * So we divide before multiply in case it is 32bit and close
5911 * to the limit.
5912 * We scale the divisor (db) by 32 to avoid loosing precision
5913 * near the end of resync when the number of remaining sectors
5914 * is close to 'db'.
5915 * We then divide rt by 32 after multiplying by db to compensate.
5916 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 */
5918 dt = ((jiffies - mddev->resync_mark) / HZ);
5919 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005920 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5921 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922
NeilBrowndd71cf62009-05-07 12:49:35 +10005923 rt = max_sectors - resync; /* number of remaining sectors */
5924 sector_div(rt, db/32+1);
5925 rt *= dt;
5926 rt >>= 5;
5927
5928 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
5929 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930
NeilBrownff4e8d92006-07-10 04:44:16 -07005931 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932}
5933
5934static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5935{
5936 struct list_head *tmp;
5937 loff_t l = *pos;
5938 mddev_t *mddev;
5939
5940 if (l >= 0x10000)
5941 return NULL;
5942 if (!l--)
5943 /* header */
5944 return (void*)1;
5945
5946 spin_lock(&all_mddevs_lock);
5947 list_for_each(tmp,&all_mddevs)
5948 if (!l--) {
5949 mddev = list_entry(tmp, mddev_t, all_mddevs);
5950 mddev_get(mddev);
5951 spin_unlock(&all_mddevs_lock);
5952 return mddev;
5953 }
5954 spin_unlock(&all_mddevs_lock);
5955 if (!l--)
5956 return (void*)2;/* tail */
5957 return NULL;
5958}
5959
5960static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5961{
5962 struct list_head *tmp;
5963 mddev_t *next_mddev, *mddev = v;
5964
5965 ++*pos;
5966 if (v == (void*)2)
5967 return NULL;
5968
5969 spin_lock(&all_mddevs_lock);
5970 if (v == (void*)1)
5971 tmp = all_mddevs.next;
5972 else
5973 tmp = mddev->all_mddevs.next;
5974 if (tmp != &all_mddevs)
5975 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5976 else {
5977 next_mddev = (void*)2;
5978 *pos = 0x10000;
5979 }
5980 spin_unlock(&all_mddevs_lock);
5981
5982 if (v != (void*)1)
5983 mddev_put(mddev);
5984 return next_mddev;
5985
5986}
5987
5988static void md_seq_stop(struct seq_file *seq, void *v)
5989{
5990 mddev_t *mddev = v;
5991
5992 if (mddev && v != (void*)1 && v != (void*)2)
5993 mddev_put(mddev);
5994}
5995
NeilBrownd7603b72006-01-06 00:20:30 -08005996struct mdstat_info {
5997 int event;
5998};
5999
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000static int md_seq_show(struct seq_file *seq, void *v)
6001{
6002 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006003 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006005 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006006 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007
6008 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006009 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 seq_printf(seq, "Personalities : ");
6011 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006012 list_for_each_entry(pers, &pers_list, list)
6013 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014
6015 spin_unlock(&pers_lock);
6016 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006017 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 return 0;
6019 }
6020 if (v == (void*)2) {
6021 status_unused(seq);
6022 return 0;
6023 }
6024
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006025 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006027
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6029 seq_printf(seq, "%s : %sactive", mdname(mddev),
6030 mddev->pers ? "" : "in");
6031 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006032 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006034 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006035 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 seq_printf(seq, " %s", mddev->pers->name);
6037 }
6038
Andre Nolldd8ac332009-03-31 14:33:13 +11006039 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006040 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041 char b[BDEVNAME_SIZE];
6042 seq_printf(seq, " %s[%d]",
6043 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006044 if (test_bit(WriteMostly, &rdev->flags))
6045 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006046 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 seq_printf(seq, "(F)");
6048 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006049 } else if (rdev->raid_disk < 0)
6050 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006051 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 }
6053
6054 if (!list_empty(&mddev->disks)) {
6055 if (mddev->pers)
6056 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006057 (unsigned long long)
6058 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 else
6060 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006061 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006063 if (mddev->persistent) {
6064 if (mddev->major_version != 0 ||
6065 mddev->minor_version != 90) {
6066 seq_printf(seq," super %d.%d",
6067 mddev->major_version,
6068 mddev->minor_version);
6069 }
NeilBrowne6910632008-02-06 01:39:51 -08006070 } else if (mddev->external)
6071 seq_printf(seq, " super external:%s",
6072 mddev->metadata_type);
6073 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006074 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075
6076 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006077 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006079 if (mddev->pers->sync_request) {
6080 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006081 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006082 seq_printf(seq, "\n ");
6083 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6084 seq_printf(seq, "\tresync=DELAYED\n ");
6085 else if (mddev->recovery_cp < MaxSector)
6086 seq_printf(seq, "\tresync=PENDING\n ");
6087 }
NeilBrown32a76272005-06-21 17:17:14 -07006088 } else
6089 seq_printf(seq, "\n ");
6090
6091 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006092 unsigned long chunk_kb;
6093 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006094 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006095 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006096 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6097 "%lu%s chunk",
6098 bitmap->pages - bitmap->missing_pages,
6099 bitmap->pages,
6100 (bitmap->pages - bitmap->missing_pages)
6101 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006102 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006103 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006104 if (bitmap->file) {
6105 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006106 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006107 }
NeilBrown78d742d2005-06-21 17:17:15 -07006108
NeilBrown32a76272005-06-21 17:17:14 -07006109 seq_printf(seq, "\n");
6110 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 }
6112
6113 seq_printf(seq, "\n");
6114 }
6115 mddev_unlock(mddev);
6116
6117 return 0;
6118}
6119
Jan Engelhardt110518b2009-05-07 12:49:37 +10006120static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 .start = md_seq_start,
6122 .next = md_seq_next,
6123 .stop = md_seq_stop,
6124 .show = md_seq_show,
6125};
6126
6127static int md_seq_open(struct inode *inode, struct file *file)
6128{
6129 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006130 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6131 if (mi == NULL)
6132 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133
6134 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006135 if (error)
6136 kfree(mi);
6137 else {
6138 struct seq_file *p = file->private_data;
6139 p->private = mi;
6140 mi->event = atomic_read(&md_event_count);
6141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 return error;
6143}
6144
NeilBrownd7603b72006-01-06 00:20:30 -08006145static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6146{
6147 struct seq_file *m = filp->private_data;
6148 struct mdstat_info *mi = m->private;
6149 int mask;
6150
6151 poll_wait(filp, &md_event_waiters, wait);
6152
6153 /* always allow read */
6154 mask = POLLIN | POLLRDNORM;
6155
6156 if (mi->event != atomic_read(&md_event_count))
6157 mask |= POLLERR | POLLPRI;
6158 return mask;
6159}
6160
Arjan van de Venfa027c22007-02-12 00:55:33 -08006161static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006162 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 .open = md_seq_open,
6164 .read = seq_read,
6165 .llseek = seq_lseek,
Martin Peschkec3f94b42007-05-09 02:35:35 -07006166 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006167 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168};
6169
NeilBrown2604b702006-01-06 00:20:36 -08006170int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006173 list_add_tail(&p->list, &pers_list);
6174 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 spin_unlock(&pers_lock);
6176 return 0;
6177}
6178
NeilBrown2604b702006-01-06 00:20:36 -08006179int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180{
NeilBrown2604b702006-01-06 00:20:36 -08006181 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006183 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184 spin_unlock(&pers_lock);
6185 return 0;
6186}
6187
NeilBrowneea1bf32009-03-31 14:27:02 +11006188static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189{
6190 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006192 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193
6194 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006195 rcu_read_lock();
6196 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006198 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6199 (int)part_stat_read(&disk->part0, sectors[1]) -
6200 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006201 /* sync IO will cause sync_io to increase before the disk_stats
6202 * as sync_io is counted when a request starts, and
6203 * disk_stats is counted when it completes.
6204 * So resync activity will cause curr_events to be smaller than
6205 * when there was no such activity.
6206 * non-sync IO will cause disk_stat to increase without
6207 * increasing sync_io so curr_events will (eventually)
6208 * be larger than it was before. Once it becomes
6209 * substantially larger, the test below will cause
6210 * the array to appear non-idle, and resync will slow
6211 * down.
6212 * If there is a lot of outstanding resync activity when
6213 * we set last_event to curr_events, then all that activity
6214 * completing might cause the array to appear non-idle
6215 * and resync will be slowed down even though there might
6216 * not have been non-resync activity. This will only
6217 * happen once though. 'last_events' will soon reflect
6218 * the state where there is little or no outstanding
6219 * resync requests, and further resync activity will
6220 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006221 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006223 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224 rdev->last_events = curr_events;
6225 idle = 0;
6226 }
6227 }
NeilBrown4b809912008-07-21 17:05:25 +10006228 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 return idle;
6230}
6231
6232void md_done_sync(mddev_t *mddev, int blocks, int ok)
6233{
6234 /* another "blocks" (512byte) blocks have been synced */
6235 atomic_sub(blocks, &mddev->recovery_active);
6236 wake_up(&mddev->recovery_wait);
6237 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006238 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 md_wakeup_thread(mddev->thread);
6240 // stop recovery, signal do_sync ....
6241 }
6242}
6243
6244
NeilBrown06d91a52005-06-21 17:17:12 -07006245/* md_write_start(mddev, bi)
6246 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006247 * in superblock) before writing, schedule a superblock update
6248 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006249 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006250void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251{
Neil Brown0fd62b82008-06-28 08:31:36 +10006252 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006253 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006254 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006255
NeilBrownf91de922005-11-08 21:39:36 -08006256 BUG_ON(mddev->ro == 1);
6257 if (mddev->ro == 2) {
6258 /* need to switch to read/write */
6259 mddev->ro = 0;
6260 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6261 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006262 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006263 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006264 }
NeilBrown06d91a52005-06-21 17:17:12 -07006265 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006266 if (mddev->safemode == 1)
6267 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006268 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006269 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006270 if (mddev->in_sync) {
6271 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006272 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006273 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006274 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006275 }
NeilBrowna9701a32005-11-08 21:39:34 -08006276 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006277 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006278 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006279 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006280 wait_event(mddev->sb_wait,
6281 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6282 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283}
6284
6285void md_write_end(mddev_t *mddev)
6286{
6287 if (atomic_dec_and_test(&mddev->writes_pending)) {
6288 if (mddev->safemode == 2)
6289 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006290 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6292 }
6293}
6294
NeilBrown2a2275d2007-01-26 00:57:11 -08006295/* md_allow_write(mddev)
6296 * Calling this ensures that the array is marked 'active' so that writes
6297 * may proceed without blocking. It is important to call this before
6298 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6299 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006300 *
6301 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6302 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006303 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006304int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006305{
6306 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006307 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006308 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006309 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006310 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006311 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006312
6313 spin_lock_irq(&mddev->write_lock);
6314 if (mddev->in_sync) {
6315 mddev->in_sync = 0;
6316 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6317 if (mddev->safemode_delay &&
6318 mddev->safemode == 0)
6319 mddev->safemode = 1;
6320 spin_unlock_irq(&mddev->write_lock);
6321 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006322 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006323 } else
6324 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006325
6326 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6327 return -EAGAIN;
6328 else
6329 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006330}
6331EXPORT_SYMBOL_GPL(md_allow_write);
6332
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333#define SYNC_MARKS 10
6334#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006335void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336{
6337 mddev_t *mddev2;
6338 unsigned int currspeed = 0,
6339 window;
NeilBrown57afd892005-06-21 17:17:13 -07006340 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 unsigned long mark[SYNC_MARKS];
6342 sector_t mark_cnt[SYNC_MARKS];
6343 int last_mark,m;
6344 struct list_head *tmp;
6345 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006346 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006347 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006348 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349
6350 /* just incase thread restarts... */
6351 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6352 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006353 if (mddev->ro) /* never try to sync a read-only array */
6354 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355
NeilBrown61df9d92006-10-03 01:15:57 -07006356 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6357 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6358 desc = "data-check";
6359 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6360 desc = "requested-resync";
6361 else
6362 desc = "resync";
6363 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6364 desc = "reshape";
6365 else
6366 desc = "recovery";
6367
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368 /* we overload curr_resync somewhat here.
6369 * 0 == not engaged in resync at all
6370 * 2 == checking that there is no conflict with another sync
6371 * 1 == like 2, but have yielded to allow conflicting resync to
6372 * commense
6373 * other == active in resync - this many blocks
6374 *
6375 * Before starting a resync we must have set curr_resync to
6376 * 2, and then checked that every "conflicting" array has curr_resync
6377 * less than ours. When we find one that is the same or higher
6378 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6379 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6380 * This will mean we have to start checking from the beginning again.
6381 *
6382 */
6383
6384 do {
6385 mddev->curr_resync = 2;
6386
6387 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08006388 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07006389 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390 goto skip;
6391 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08006392 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393 if (mddev2 == mddev)
6394 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006395 if (!mddev->parallel_resync
6396 && mddev2->curr_resync
6397 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 DEFINE_WAIT(wq);
6399 if (mddev < mddev2 && mddev->curr_resync == 2) {
6400 /* arbitrarily yield */
6401 mddev->curr_resync = 1;
6402 wake_up(&resync_wait);
6403 }
6404 if (mddev > mddev2 && mddev->curr_resync == 1)
6405 /* no need to wait here, we can wait the next
6406 * time 'round when curr_resync == 2
6407 */
6408 continue;
NeilBrown97441972008-09-19 11:49:54 +10006409 /* We need to wait 'interruptible' so as not to
6410 * contribute to the load average, and not to
6411 * be caught by 'softlockup'
6412 */
6413 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006414 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006415 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006416 printk(KERN_INFO "md: delaying %s of %s"
6417 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006419 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006421 if (signal_pending(current))
6422 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423 schedule();
6424 finish_wait(&resync_wait, &wq);
6425 goto try_again;
6426 }
6427 finish_wait(&resync_wait, &wq);
6428 }
6429 }
6430 } while (mddev->curr_resync < 2);
6431
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006432 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006433 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006434 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006435 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436 */
6437 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006438 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006439 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006440 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6441 j = mddev->resync_min;
6442 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006443 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006444
NeilBrownccfcc3c2006-03-27 01:18:09 -08006445 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006446 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006447 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006449 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006450 j = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006451 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006452 if (rdev->raid_disk >= 0 &&
6453 !test_bit(Faulty, &rdev->flags) &&
6454 !test_bit(In_sync, &rdev->flags) &&
6455 rdev->recovery_offset < j)
6456 j = rdev->recovery_offset;
6457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458
NeilBrown61df9d92006-10-03 01:15:57 -07006459 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6460 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6461 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006462 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006463 "(but not more than %d KB/sec) for %s.\n",
6464 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465
NeilBrowneea1bf32009-03-31 14:27:02 +11006466 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006467
NeilBrown57afd892005-06-21 17:17:13 -07006468 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469 for (m = 0; m < SYNC_MARKS; m++) {
6470 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006471 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472 }
6473 last_mark = 0;
6474 mddev->resync_mark = mark[last_mark];
6475 mddev->resync_mark_cnt = mark_cnt[last_mark];
6476
6477 /*
6478 * Tune reconstruction:
6479 */
6480 window = 32*(PAGE_SIZE/512);
6481 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6482 window/2,(unsigned long long) max_sectors/2);
6483
6484 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485 last_check = 0;
6486
6487 if (j>2) {
6488 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006489 "md: resuming %s of %s from checkpoint.\n",
6490 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491 mddev->curr_resync = j;
6492 }
NeilBrownefa59332009-12-14 12:49:49 +11006493 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006494
6495 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006496 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497
NeilBrown57afd892005-06-21 17:17:13 -07006498 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006499
NeilBrown7a91ee12009-05-26 12:57:21 +10006500 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6501 ((mddev->curr_resync > mddev->curr_resync_completed &&
6502 (mddev->curr_resync - mddev->curr_resync_completed)
6503 > (max_sectors >> 4)) ||
6504 (j - mddev->curr_resync_completed)*2
6505 >= mddev->resync_max - mddev->curr_resync_completed
6506 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006507 /* time to update curr_resync_completed */
6508 blk_unplug(mddev->queue);
6509 wait_event(mddev->recovery_wait,
6510 atomic_read(&mddev->recovery_active) == 0);
6511 mddev->curr_resync_completed =
6512 mddev->curr_resync;
6513 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006514 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006515 }
NeilBrownacb180b2009-04-14 16:28:34 +10006516
NeilBrowne62e58a2009-07-01 13:15:35 +10006517 while (j >= mddev->resync_max && !kthread_should_stop()) {
6518 /* As this condition is controlled by user-space,
6519 * we can block indefinitely, so use '_interruptible'
6520 * to avoid triggering warnings.
6521 */
6522 flush_signals(current); /* just in case */
6523 wait_event_interruptible(mddev->recovery_wait,
6524 mddev->resync_max > j
6525 || kthread_should_stop());
6526 }
NeilBrownacb180b2009-04-14 16:28:34 +10006527
6528 if (kthread_should_stop())
6529 goto interrupted;
6530
NeilBrown57afd892005-06-21 17:17:13 -07006531 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006532 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006533 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006534 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535 goto out;
6536 }
NeilBrown57afd892005-06-21 17:17:13 -07006537
6538 if (!skipped) { /* actual IO requested */
6539 io_sectors += sectors;
6540 atomic_add(sectors, &mddev->recovery_active);
6541 }
6542
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543 j += sectors;
6544 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006545 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006546 if (last_check == 0)
6547 /* this is the earliers that rebuilt will be
6548 * visible in /proc/mdstat
6549 */
6550 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006551
6552 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553 continue;
6554
NeilBrown57afd892005-06-21 17:17:13 -07006555 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556
NeilBrowndfc70642008-05-23 13:04:39 -07006557 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558 break;
6559
6560 repeat:
6561 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6562 /* step marks */
6563 int next = (last_mark+1) % SYNC_MARKS;
6564
6565 mddev->resync_mark = mark[next];
6566 mddev->resync_mark_cnt = mark_cnt[next];
6567 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006568 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006569 last_mark = next;
6570 }
6571
6572
NeilBrownc6207272008-02-06 01:39:52 -08006573 if (kthread_should_stop())
6574 goto interrupted;
6575
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576
6577 /*
6578 * this loop exits only if either when we are slower than
6579 * the 'hard' speed limit, or the system was IO-idle for
6580 * a jiffy.
6581 * the system might be non-idle CPU-wise, but we only care
6582 * about not overloading the IO subsystem. (things like an
6583 * e2fsck being done on the RAID array should execute fast)
6584 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006585 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586 cond_resched();
6587
NeilBrown57afd892005-06-21 17:17:13 -07006588 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6589 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590
NeilBrown88202a02006-01-06 00:21:36 -08006591 if (currspeed > speed_min(mddev)) {
6592 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006593 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006594 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595 goto repeat;
6596 }
6597 }
6598 }
NeilBrown61df9d92006-10-03 01:15:57 -07006599 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600 /*
6601 * this also signals 'finished resyncing' to md_stop
6602 */
6603 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006604 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605
6606 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6607
6608 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006609 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610
NeilBrowndfc70642008-05-23 13:04:39 -07006611 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006612 mddev->curr_resync > 2) {
6613 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6614 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6615 if (mddev->curr_resync >= mddev->recovery_cp) {
6616 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006617 "md: checkpointing %s of %s.\n",
6618 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006619 mddev->recovery_cp = mddev->curr_resync;
6620 }
6621 } else
6622 mddev->recovery_cp = MaxSector;
6623 } else {
6624 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6625 mddev->curr_resync = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006626 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006627 if (rdev->raid_disk >= 0 &&
6628 !test_bit(Faulty, &rdev->flags) &&
6629 !test_bit(In_sync, &rdev->flags) &&
6630 rdev->recovery_offset < mddev->curr_resync)
6631 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633 }
NeilBrown17571282006-12-10 02:20:52 -08006634 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006637 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6638 /* We completed so min/max setting can be forgotten if used. */
6639 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6640 mddev->resync_min = 0;
6641 mddev->resync_max = MaxSector;
6642 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6643 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006645 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6646 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006647 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648 wake_up(&resync_wait);
6649 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6650 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006651 return;
6652
6653 interrupted:
6654 /*
6655 * got a signal, exit.
6656 */
6657 printk(KERN_INFO
6658 "md: md_do_sync() got signal ... exiting\n");
6659 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6660 goto out;
6661
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662}
NeilBrown29269552006-03-27 01:18:10 -08006663EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664
6665
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006666static int remove_and_add_spares(mddev_t *mddev)
6667{
6668 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006669 int spares = 0;
6670
NeilBrown97e4f422009-03-31 14:33:13 +11006671 mddev->curr_resync_completed = 0;
6672
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006673 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006674 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006675 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006676 (test_bit(Faulty, &rdev->flags) ||
6677 ! test_bit(In_sync, &rdev->flags)) &&
6678 atomic_read(&rdev->nr_pending)==0) {
6679 if (mddev->pers->hot_remove_disk(
6680 mddev, rdev->raid_disk)==0) {
6681 char nm[20];
6682 sprintf(nm,"rd%d", rdev->raid_disk);
6683 sysfs_remove_link(&mddev->kobj, nm);
6684 rdev->raid_disk = -1;
6685 }
6686 }
6687
NeilBrown4044ba52009-01-09 08:31:11 +11006688 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006689 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006690 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006691 !test_bit(In_sync, &rdev->flags) &&
6692 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006693 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006694 if (rdev->raid_disk < 0
6695 && !test_bit(Faulty, &rdev->flags)) {
6696 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006697 if (mddev->pers->
6698 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006699 char nm[20];
6700 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006701 if (sysfs_create_link(&mddev->kobj,
6702 &rdev->kobj, nm))
6703 printk(KERN_WARNING
6704 "md: cannot register "
6705 "%s for %s\n",
6706 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006707 spares++;
6708 md_new_event(mddev);
6709 } else
6710 break;
6711 }
NeilBrowndfc70642008-05-23 13:04:39 -07006712 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006713 }
6714 return spares;
6715}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716/*
6717 * This routine is regularly called by all per-raid-array threads to
6718 * deal with generic issues like resync and super-block update.
6719 * Raid personalities that don't have a thread (linear/raid0) do not
6720 * need this as they never do any recovery or update the superblock.
6721 *
6722 * It does not do any resync itself, but rather "forks" off other threads
6723 * to do that as needed.
6724 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6725 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006726 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727 * and wakeups up this thread which will reap the thread and finish up.
6728 * This thread also removes any faulty devices (with nr_pending == 0).
6729 *
6730 * The overall approach is:
6731 * 1/ if the superblock needs updating, update it.
6732 * 2/ If a recovery thread is running, don't do anything else.
6733 * 3/ If recovery has finished, clean up, possibly marking spares active.
6734 * 4/ If there are any faulty devices, remove them.
6735 * 5/ If array is degraded, try to add spares devices
6736 * 6/ If array has spares or is not in-sync, start a resync thread.
6737 */
6738void md_check_recovery(mddev_t *mddev)
6739{
6740 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741
6742
NeilBrown5f404022005-06-21 17:17:16 -07006743 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11006744 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745
6746 if (mddev->ro)
6747 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006748
6749 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006750 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006751 printk(KERN_INFO "md: %s in immediate safe mode\n",
6752 mdname(mddev));
6753 mddev->safemode = 2;
6754 }
6755 flush_signals(current);
6756 }
6757
NeilBrownc89a8ee2008-08-05 15:54:13 +10006758 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6759 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006761 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006763 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006764 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006765 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6766 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767 ))
6768 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006769
NeilBrowndf5b89b2006-03-27 01:18:20 -08006770 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006771 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006772
NeilBrownc89a8ee2008-08-05 15:54:13 +10006773 if (mddev->ro) {
6774 /* Only thing we do on a ro array is remove
6775 * failed devices.
6776 */
6777 remove_and_add_spares(mddev);
6778 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6779 goto unlock;
6780 }
6781
NeilBrown31a59e32008-04-30 00:52:30 -07006782 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006783 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006784 spin_lock_irq(&mddev->write_lock);
6785 if (mddev->safemode &&
6786 !atomic_read(&mddev->writes_pending) &&
6787 !mddev->in_sync &&
6788 mddev->recovery_cp == MaxSector) {
6789 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006790 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006791 if (mddev->persistent)
6792 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6793 }
6794 if (mddev->safemode == 1)
6795 mddev->safemode = 0;
6796 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006797 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006798 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006799 }
NeilBrownfca4d842005-06-21 17:17:11 -07006800
NeilBrown850b2b42006-10-03 01:15:46 -07006801 if (mddev->flags)
6802 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006803
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006804 list_for_each_entry(rdev, &mddev->disks, same_set)
Neil Brown52664732008-06-28 08:31:44 +10006805 if (test_and_clear_bit(StateChanged, &rdev->flags))
NeilBrown3c0ee632008-10-21 13:25:28 +11006806 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown52664732008-06-28 08:31:44 +10006807
NeilBrown06d91a52005-06-21 17:17:12 -07006808
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6810 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6811 /* resync/recovery still happening */
6812 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6813 goto unlock;
6814 }
6815 if (mddev->sync_thread) {
6816 /* resync has finished, collect result */
6817 md_unregister_thread(mddev->sync_thread);
6818 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006819 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6820 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821 /* success...*/
6822 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006823 if (mddev->pers->spare_active(mddev))
6824 sysfs_notify(&mddev->kobj, NULL,
6825 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 }
NeilBrowncea9c222009-03-31 15:15:05 +11006827 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6828 mddev->pers->finish_reshape)
6829 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07006830 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006831
6832 /* if array is no-longer degraded, then any saved_raid_disk
6833 * information must be scrapped
6834 */
6835 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006836 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07006837 rdev->saved_raid_disk = -1;
6838
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839 mddev->recovery = 0;
6840 /* flag recovery needed just to double check */
6841 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11006842 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006843 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844 goto unlock;
6845 }
Neil Brown72a23c22008-06-28 08:31:41 +10006846 /* Set RUNNING before clearing NEEDED to avoid
6847 * any transients in the value of "sync_action".
6848 */
6849 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6850 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006851 /* Clear some bits that don't mean anything, but
6852 * might be left set
6853 */
NeilBrown24dd4692005-11-08 21:39:26 -08006854 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6855 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006857 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6858 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859 /* no recovery is running.
6860 * remove any failed drives, then
6861 * add spares if possible.
6862 * Spare are also removed and re-added, to allow
6863 * the personality to fail the re-add.
6864 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006866 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10006867 if (mddev->pers->check_reshape == NULL ||
6868 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006869 /* Cannot proceed */
6870 goto unlock;
6871 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006872 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006873 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006874 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6875 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07006876 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006877 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006878 } else if (mddev->recovery_cp < MaxSector) {
6879 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006880 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006881 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6882 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006884
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006886 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6887 /* We are adding a device or devices to an array
6888 * which has the bitmap stored on all devices.
6889 * So make sure all bitmap pages get written
6890 */
6891 bitmap_write_all(mddev->bitmap);
6892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893 mddev->sync_thread = md_register_thread(md_do_sync,
6894 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006895 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896 if (!mddev->sync_thread) {
6897 printk(KERN_ERR "%s: could not start resync"
6898 " thread...\n",
6899 mdname(mddev));
6900 /* leave the spares where they are, it shouldn't hurt */
6901 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006902 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11006904 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006905 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906 }
6907 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006908 if (!mddev->sync_thread) {
6909 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6910 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6911 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11006912 if (mddev->sysfs_action)
6913 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10006914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915 mddev_unlock(mddev);
6916 }
6917}
6918
Dan Williams6bfe0b42008-04-30 00:52:32 -07006919void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6920{
NeilBrown3c0ee632008-10-21 13:25:28 +11006921 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07006922 wait_event_timeout(rdev->blocked_wait,
6923 !test_bit(Blocked, &rdev->flags),
6924 msecs_to_jiffies(5000));
6925 rdev_dec_pending(rdev, mddev);
6926}
6927EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6928
Adrian Bunk75c96f82005-05-05 16:16:09 -07006929static int md_notify_reboot(struct notifier_block *this,
6930 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931{
6932 struct list_head *tmp;
6933 mddev_t *mddev;
6934
6935 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6936
6937 printk(KERN_INFO "md: stopping all md devices.\n");
6938
NeilBrown29ac4aa2008-02-06 01:39:58 -08006939 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006940 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10006941 /* Force a switch to readonly even array
6942 * appears to still be in use. Hence
6943 * the '100'.
6944 */
NeilBrownd710e132008-10-13 11:55:12 +11006945 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10006946 mddev_unlock(mddev);
6947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948 /*
6949 * certain more exotic SCSI devices are known to be
6950 * volatile wrt too early system reboots. While the
6951 * right place to handle this issue is the given
6952 * driver, we do want to have a safe RAID driver ...
6953 */
6954 mdelay(1000*1);
6955 }
6956 return NOTIFY_DONE;
6957}
6958
Adrian Bunk75c96f82005-05-05 16:16:09 -07006959static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960 .notifier_call = md_notify_reboot,
6961 .next = NULL,
6962 .priority = INT_MAX, /* before any real devices */
6963};
6964
6965static void md_geninit(void)
6966{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006967 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6968
Denis V. Lunevc7705f32008-04-29 01:02:35 -07006969 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970}
6971
Adrian Bunk75c96f82005-05-05 16:16:09 -07006972static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006974 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975 return -1;
6976 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006977 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978 return -1;
6979 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006980 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07006981 md_probe, NULL, NULL);
6982 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983 md_probe, NULL, NULL);
6984
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08006986 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987
6988 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11006989 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990}
6991
6992
6993#ifndef MODULE
6994
6995/*
6996 * Searches all registered partitions for autorun RAID arrays
6997 * at boot time.
6998 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006999
7000static LIST_HEAD(all_detected_devices);
7001struct detected_devices_node {
7002 struct list_head list;
7003 dev_t dev;
7004};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005
7006void md_autodetect_dev(dev_t dev)
7007{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007008 struct detected_devices_node *node_detected_dev;
7009
7010 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7011 if (node_detected_dev) {
7012 node_detected_dev->dev = dev;
7013 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7014 } else {
7015 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7016 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018}
7019
7020
7021static void autostart_arrays(int part)
7022{
7023 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007024 struct detected_devices_node *node_detected_dev;
7025 dev_t dev;
7026 int i_scanned, i_passed;
7027
7028 i_scanned = 0;
7029 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030
7031 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7032
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007033 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7034 i_scanned++;
7035 node_detected_dev = list_entry(all_detected_devices.next,
7036 struct detected_devices_node, list);
7037 list_del(&node_detected_dev->list);
7038 dev = node_detected_dev->dev;
7039 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007040 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041 if (IS_ERR(rdev))
7042 continue;
7043
NeilBrownb2d444d2005-11-08 21:39:31 -08007044 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045 MD_BUG();
7046 continue;
7047 }
NeilBrownd0fae182008-03-04 14:29:31 -08007048 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007050 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007052
7053 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7054 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007055
7056 autorun_devices(part);
7057}
7058
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007059#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007060
7061static __exit void md_exit(void)
7062{
7063 mddev_t *mddev;
7064 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007065
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007066 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007067 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007068
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007069 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070 unregister_blkdev(mdp_major, "mdp");
7071 unregister_reboot_notifier(&md_notifier);
7072 unregister_sysctl_table(raid_table_header);
7073 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007074 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007076 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007077 }
7078}
7079
Dan Williams685784a2007-07-09 11:56:42 -07007080subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081module_exit(md_exit)
7082
NeilBrownf91de922005-11-08 21:39:36 -08007083static int get_ro(char *buffer, struct kernel_param *kp)
7084{
7085 return sprintf(buffer, "%d", start_readonly);
7086}
7087static int set_ro(const char *val, struct kernel_param *kp)
7088{
7089 char *e;
7090 int num = simple_strtoul(val, &e, 10);
7091 if (*val && (*e == '\0' || *e == '\n')) {
7092 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007093 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007094 }
7095 return -EINVAL;
7096}
7097
NeilBrown80ca3a42006-07-10 04:44:18 -07007098module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7099module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8e2006-01-06 00:20:15 -08007100
NeilBrownefeb53c2009-01-09 08:31:10 +11007101module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007102
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103EXPORT_SYMBOL(register_md_personality);
7104EXPORT_SYMBOL(unregister_md_personality);
7105EXPORT_SYMBOL(md_error);
7106EXPORT_SYMBOL(md_done_sync);
7107EXPORT_SYMBOL(md_write_start);
7108EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109EXPORT_SYMBOL(md_register_thread);
7110EXPORT_SYMBOL(md_unregister_thread);
7111EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112EXPORT_SYMBOL(md_check_recovery);
7113MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07007114MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007115MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);