blob: 93287f88f1f41f4231ffba7864cd170c948a8034 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/buffer_head.h> /* for invalidate_bdev */
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110042#include <linux/hdreg.h>
43#include <linux/proc_fs.h>
44#include <linux/random.h>
45#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070046#include <linux/file.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110047#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110048#include <linux/raid/md_p.h>
49#include <linux/raid/md_u.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110050#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110051#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#define DEBUG 0
54#define dprintk(x...) ((void)(DEBUG && printk(x)))
55
56
57#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110058static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
NeilBrown2604b702006-01-06 00:20:36 -080061static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static DEFINE_SPINLOCK(pers_lock);
63
Adrian Bunk5e563412006-06-26 00:27:42 -070064static void md_print_devices(void);
65
Bernd Schubert90b08712008-05-23 13:04:38 -070066static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
67
Adrian Bunk5e563412006-06-26 00:27:42 -070068#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*
71 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
72 * is 1000 KB/sec, so the extra system load does not show up that much.
73 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070074 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * subsystem is idle. There is also an 'absolute maximum' reconstruction
76 * speed limit - in case reconstruction slows down your system despite
77 * idle IO detection.
78 *
79 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080080 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
82
83static int sysctl_speed_limit_min = 1000;
84static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080085static inline int speed_min(mddev_t *mddev)
86{
87 return mddev->sync_speed_min ?
88 mddev->sync_speed_min : sysctl_speed_limit_min;
89}
90
91static inline int speed_max(mddev_t *mddev)
92{
93 return mddev->sync_speed_max ?
94 mddev->sync_speed_max : sysctl_speed_limit_max;
95}
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97static struct ctl_table_header *raid_table_header;
98
99static ctl_table raid_table[] = {
100 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 .procname = "speed_limit_min",
102 .data = &sysctl_speed_limit_min,
103 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700104 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800105 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 },
107 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 .procname = "speed_limit_max",
109 .data = &sysctl_speed_limit_max,
110 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700111 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800112 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800114 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};
116
117static ctl_table raid_dir_table[] = {
118 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .procname = "raid",
120 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700121 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 .child = raid_table,
123 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800124 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
127static ctl_table raid_root_table[] = {
128 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .procname = "dev",
130 .maxlen = 0,
131 .mode = 0555,
132 .child = raid_dir_table,
133 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800134 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700137static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
NeilBrownf91de922005-11-08 21:39:36 -0800139static int start_readonly;
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/*
NeilBrownd7603b72006-01-06 00:20:30 -0800142 * We have a system wide 'event count' that is incremented
143 * on any 'interesting' event, and readers of /proc/mdstat
144 * can use 'poll' or 'select' to find out when the event
145 * count increases.
146 *
147 * Events are:
148 * start array, stop array, error, add device, remove device,
149 * start build, activate spare
150 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800151static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800152static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800153void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800154{
155 atomic_inc(&md_event_count);
156 wake_up(&md_event_waiters);
157}
NeilBrown29269552006-03-27 01:18:10 -0800158EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800159
NeilBrownc331eb02006-05-30 21:27:13 -0700160/* Alternate version that can be called from interrupts
161 * when calling sysfs_notify isn't needed.
162 */
Adrian Bunk05381952006-06-26 00:28:01 -0700163static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700164{
165 atomic_inc(&md_event_count);
166 wake_up(&md_event_waiters);
167}
168
NeilBrownd7603b72006-01-06 00:20:30 -0800169/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * Enables to iterate over all existing md arrays
171 * all_mddevs_lock protects this list.
172 */
173static LIST_HEAD(all_mddevs);
174static DEFINE_SPINLOCK(all_mddevs_lock);
175
176
177/*
178 * iterates through all used mddevs in the system.
179 * We take care to grab the all_mddevs_lock whenever navigating
180 * the list, and to always hold a refcount when unlocked.
181 * Any code which breaks out of this loop while own
182 * a reference to the current mddev and must mddev_put it.
183 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800184#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 \
186 for (({ spin_lock(&all_mddevs_lock); \
187 tmp = all_mddevs.next; \
188 mddev = NULL;}); \
189 ({ if (tmp != &all_mddevs) \
190 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
191 spin_unlock(&all_mddevs_lock); \
192 if (mddev) mddev_put(mddev); \
193 mddev = list_entry(tmp, mddev_t, all_mddevs); \
194 tmp != &all_mddevs;}); \
195 ({ spin_lock(&all_mddevs_lock); \
196 tmp = tmp->next;}) \
197 )
198
199
NeilBrown409c57f2009-03-31 14:39:39 +1100200/* Rather than calling directly into the personality make_request function,
201 * IO requests come here first so that we can check if the device is
202 * being suspended pending a reconfiguration.
203 * We hold a refcount over the call to ->make_request. By the time that
204 * call has finished, the bio has been linked into some internal structure
205 * and so is visible to ->quiesce(), so we don't need the refcount any more.
206 */
207static int md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
NeilBrown409c57f2009-03-31 14:39:39 +1100209 mddev_t *mddev = q->queuedata;
210 int rv;
211 if (mddev == NULL || mddev->pers == NULL) {
212 bio_io_error(bio);
213 return 0;
214 }
215 rcu_read_lock();
NeilBrowna2826aa2009-12-14 12:49:49 +1100216 if (mddev->suspended || mddev->barrier) {
NeilBrown409c57f2009-03-31 14:39:39 +1100217 DEFINE_WAIT(__wait);
218 for (;;) {
219 prepare_to_wait(&mddev->sb_wait, &__wait,
220 TASK_UNINTERRUPTIBLE);
NeilBrowna2826aa2009-12-14 12:49:49 +1100221 if (!mddev->suspended && !mddev->barrier)
NeilBrown409c57f2009-03-31 14:39:39 +1100222 break;
223 rcu_read_unlock();
224 schedule();
225 rcu_read_lock();
226 }
227 finish_wait(&mddev->sb_wait, &__wait);
228 }
229 atomic_inc(&mddev->active_io);
230 rcu_read_unlock();
231 rv = mddev->pers->make_request(q, bio);
232 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
233 wake_up(&mddev->sb_wait);
234
235 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
NeilBrown409c57f2009-03-31 14:39:39 +1100238static void mddev_suspend(mddev_t *mddev)
239{
240 BUG_ON(mddev->suspended);
241 mddev->suspended = 1;
242 synchronize_rcu();
243 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
244 mddev->pers->quiesce(mddev, 1);
245 md_unregister_thread(mddev->thread);
246 mddev->thread = NULL;
247 /* we now know that no code is executing in the personality module,
248 * except possibly the tail end of a ->bi_end_io function, but that
249 * is certain to complete before the module has a chance to get
250 * unloaded
251 */
252}
253
254static void mddev_resume(mddev_t *mddev)
255{
256 mddev->suspended = 0;
257 wake_up(&mddev->sb_wait);
258 mddev->pers->quiesce(mddev, 0);
259}
260
NeilBrown3fa841d2009-09-23 18:10:29 +1000261int mddev_congested(mddev_t *mddev, int bits)
262{
NeilBrowna2826aa2009-12-14 12:49:49 +1100263 if (mddev->barrier)
264 return 1;
NeilBrown3fa841d2009-09-23 18:10:29 +1000265 return mddev->suspended;
266}
267EXPORT_SYMBOL(mddev_congested);
268
NeilBrowna2826aa2009-12-14 12:49:49 +1100269/*
270 * Generic barrier handling for md
271 */
272
273#define POST_REQUEST_BARRIER ((void*)1)
274
275static void md_end_barrier(struct bio *bio, int err)
276{
277 mdk_rdev_t *rdev = bio->bi_private;
278 mddev_t *mddev = rdev->mddev;
279 if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER)
280 set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags);
281
282 rdev_dec_pending(rdev, mddev);
283
284 if (atomic_dec_and_test(&mddev->flush_pending)) {
285 if (mddev->barrier == POST_REQUEST_BARRIER) {
286 /* This was a post-request barrier */
287 mddev->barrier = NULL;
288 wake_up(&mddev->sb_wait);
289 } else
290 /* The pre-request barrier has finished */
291 schedule_work(&mddev->barrier_work);
292 }
293 bio_put(bio);
294}
295
296static void submit_barriers(mddev_t *mddev)
297{
298 mdk_rdev_t *rdev;
299
300 rcu_read_lock();
301 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
302 if (rdev->raid_disk >= 0 &&
303 !test_bit(Faulty, &rdev->flags)) {
304 /* Take two references, one is dropped
305 * when request finishes, one after
306 * we reclaim rcu_read_lock
307 */
308 struct bio *bi;
309 atomic_inc(&rdev->nr_pending);
310 atomic_inc(&rdev->nr_pending);
311 rcu_read_unlock();
312 bi = bio_alloc(GFP_KERNEL, 0);
313 bi->bi_end_io = md_end_barrier;
314 bi->bi_private = rdev;
315 bi->bi_bdev = rdev->bdev;
316 atomic_inc(&mddev->flush_pending);
317 submit_bio(WRITE_BARRIER, bi);
318 rcu_read_lock();
319 rdev_dec_pending(rdev, mddev);
320 }
321 rcu_read_unlock();
322}
323
324static void md_submit_barrier(struct work_struct *ws)
325{
326 mddev_t *mddev = container_of(ws, mddev_t, barrier_work);
327 struct bio *bio = mddev->barrier;
328
329 atomic_set(&mddev->flush_pending, 1);
330
331 if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags))
332 bio_endio(bio, -EOPNOTSUPP);
333 else if (bio->bi_size == 0)
334 /* an empty barrier - all done */
335 bio_endio(bio, 0);
336 else {
337 bio->bi_rw &= ~(1<<BIO_RW_BARRIER);
338 if (mddev->pers->make_request(mddev->queue, bio))
339 generic_make_request(bio);
340 mddev->barrier = POST_REQUEST_BARRIER;
341 submit_barriers(mddev);
342 }
343 if (atomic_dec_and_test(&mddev->flush_pending)) {
344 mddev->barrier = NULL;
345 wake_up(&mddev->sb_wait);
346 }
347}
348
349void md_barrier_request(mddev_t *mddev, struct bio *bio)
350{
351 spin_lock_irq(&mddev->write_lock);
352 wait_event_lock_irq(mddev->sb_wait,
353 !mddev->barrier,
354 mddev->write_lock, /*nothing*/);
355 mddev->barrier = bio;
356 spin_unlock_irq(&mddev->write_lock);
357
358 atomic_set(&mddev->flush_pending, 1);
359 INIT_WORK(&mddev->barrier_work, md_submit_barrier);
360
361 submit_barriers(mddev);
362
363 if (atomic_dec_and_test(&mddev->flush_pending))
364 schedule_work(&mddev->barrier_work);
365}
366EXPORT_SYMBOL(md_barrier_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368static inline mddev_t *mddev_get(mddev_t *mddev)
369{
370 atomic_inc(&mddev->active);
371 return mddev;
372}
373
Dan Williams5fd3a172009-03-04 00:57:25 -0700374static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376static void mddev_put(mddev_t *mddev)
377{
378 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
379 return;
NeilBrownd3374822009-01-09 08:31:10 +1100380 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
381 !mddev->hold_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 list_del(&mddev->all_mddevs);
NeilBrownd3374822009-01-09 08:31:10 +1100383 if (mddev->gendisk) {
384 /* we did a probe so need to clean up.
385 * Call schedule_work inside the spinlock
386 * so that flush_scheduled_work() after
387 * mddev_find will succeed in waiting for the
388 * work to be done.
389 */
390 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
391 schedule_work(&mddev->del_work);
392 } else
393 kfree(mddev);
394 }
395 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
398static mddev_t * mddev_find(dev_t unit)
399{
400 mddev_t *mddev, *new = NULL;
401
402 retry:
403 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
NeilBrownefeb53c2009-01-09 08:31:10 +1100405 if (unit) {
406 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
407 if (mddev->unit == unit) {
408 mddev_get(mddev);
409 spin_unlock(&all_mddevs_lock);
410 kfree(new);
411 return mddev;
412 }
413
414 if (new) {
415 list_add(&new->all_mddevs, &all_mddevs);
416 spin_unlock(&all_mddevs_lock);
417 new->hold_active = UNTIL_IOCTL;
418 return new;
419 }
420 } else if (new) {
421 /* find an unused unit number */
422 static int next_minor = 512;
423 int start = next_minor;
424 int is_free = 0;
425 int dev = 0;
426 while (!is_free) {
427 dev = MKDEV(MD_MAJOR, next_minor);
428 next_minor++;
429 if (next_minor > MINORMASK)
430 next_minor = 0;
431 if (next_minor == start) {
432 /* Oh dear, all in use. */
433 spin_unlock(&all_mddevs_lock);
434 kfree(new);
435 return NULL;
436 }
437
438 is_free = 1;
439 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
440 if (mddev->unit == dev) {
441 is_free = 0;
442 break;
443 }
444 }
445 new->unit = dev;
446 new->md_minor = MINOR(dev);
447 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 list_add(&new->all_mddevs, &all_mddevs);
449 spin_unlock(&all_mddevs_lock);
450 return new;
451 }
452 spin_unlock(&all_mddevs_lock);
453
NeilBrown9ffae0c2006-01-06 00:20:32 -0800454 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (!new)
456 return NULL;
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 new->unit = unit;
459 if (MAJOR(unit) == MD_MAJOR)
460 new->md_minor = MINOR(unit);
461 else
462 new->md_minor = MINOR(unit) >> MdpMinorShift;
463
NeilBrownc8c00a62009-08-10 12:50:52 +1000464 mutex_init(&new->open_mutex);
NeilBrowndf5b89b2006-03-27 01:18:20 -0800465 mutex_init(&new->reconfig_mutex);
NeilBrownc3d97142009-12-14 12:49:52 +1100466 mutex_init(&new->bitmap_info.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 INIT_LIST_HEAD(&new->disks);
468 INIT_LIST_HEAD(&new->all_mddevs);
469 init_timer(&new->safemode_timer);
470 atomic_set(&new->active, 1);
NeilBrownf2ea68c2008-07-21 17:05:25 +1000471 atomic_set(&new->openers, 0);
NeilBrown409c57f2009-03-31 14:39:39 +1100472 atomic_set(&new->active_io, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700473 spin_lock_init(&new->write_lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100474 atomic_set(&new->flush_pending, 0);
NeilBrown3d310eb2005-06-21 17:17:26 -0700475 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700476 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700477 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000478 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800479 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700480 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 goto retry;
483}
484
485static inline int mddev_lock(mddev_t * mddev)
486{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800487 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
Dan Williamsb522adc2009-03-31 15:00:31 +1100490static inline int mddev_is_locked(mddev_t *mddev)
491{
492 return mutex_is_locked(&mddev->reconfig_mutex);
493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495static inline int mddev_trylock(mddev_t * mddev)
496{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800497 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
500static inline void mddev_unlock(mddev_t * mddev)
501{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800502 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
NeilBrown005eca52005-08-22 13:11:08 -0700504 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
NeilBrown2989ddb2006-01-06 00:20:43 -0800507static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100509 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100511 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (rdev->desc_nr == nr)
513 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return NULL;
516}
517
518static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
519{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 mdk_rdev_t *rdev;
521
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100522 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (rdev->bdev->bd_dev == dev)
524 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return NULL;
527}
528
NeilBrownd9d166c2006-01-06 00:20:51 -0800529static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800530{
531 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800532 list_for_each_entry(pers, &pers_list, list) {
533 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800534 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800535 if (strcmp(pers->name, clevel)==0)
536 return pers;
537 }
NeilBrown2604b702006-01-06 00:20:36 -0800538 return NULL;
539}
540
Andre Nollb73df2d2008-07-11 22:02:23 +1000541/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700542static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Andre Nollb73df2d2008-07-11 22:02:23 +1000544 sector_t num_sectors = bdev->bd_inode->i_size / 512;
545 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548static int alloc_disk_sb(mdk_rdev_t * rdev)
549{
550 if (rdev->sb_page)
551 MD_BUG();
552
553 rdev->sb_page = alloc_page(GFP_KERNEL);
554 if (!rdev->sb_page) {
555 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000556 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558
559 return 0;
560}
561
562static void free_disk_sb(mdk_rdev_t * rdev)
563{
564 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800565 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 rdev->sb_loaded = 0;
567 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000568 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100569 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571}
572
573
NeilBrown6712ecf2007-09-27 12:47:43 +0200574static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700575{
576 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800577 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700578
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700579 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
580 printk("md: super_written gets error=%d, uptodate=%d\n",
581 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
582 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800583 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700584 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700585
NeilBrowna9701a32005-11-08 21:39:34 -0800586 if (atomic_dec_and_test(&mddev->pending_writes))
587 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700588 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700589}
590
NeilBrown6712ecf2007-09-27 12:47:43 +0200591static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800592{
593 struct bio *bio2 = bio->bi_private;
594 mdk_rdev_t *rdev = bio2->bi_private;
595 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800596
597 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
598 error == -EOPNOTSUPP) {
599 unsigned long flags;
600 /* barriers don't appear to be supported :-( */
601 set_bit(BarriersNotsupp, &rdev->flags);
602 mddev->barriers_work = 0;
603 spin_lock_irqsave(&mddev->write_lock, flags);
604 bio2->bi_next = mddev->biolist;
605 mddev->biolist = bio2;
606 spin_unlock_irqrestore(&mddev->write_lock, flags);
607 wake_up(&mddev->sb_wait);
608 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200609 } else {
610 bio_put(bio2);
611 bio->bi_private = rdev;
612 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800613 }
NeilBrowna9701a32005-11-08 21:39:34 -0800614}
615
NeilBrown7bfa19f2005-06-21 17:17:28 -0700616void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
617 sector_t sector, int size, struct page *page)
618{
619 /* write first size bytes of page to sector of rdev
620 * Increment mddev->pending_writes before returning
621 * and decrement it on completion, waking up sb_wait
622 * if zero is reached.
623 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800624 *
625 * As we might need to resubmit the request if BIO_RW_BARRIER
626 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700627 */
628 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Jens Axboe93dbb392009-02-16 10:25:40 +0100629 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700630
631 bio->bi_bdev = rdev->bdev;
632 bio->bi_sector = sector;
633 bio_add_page(bio, page, size, 0);
634 bio->bi_private = rdev;
635 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800636 bio->bi_rw = rw;
637
NeilBrown7bfa19f2005-06-21 17:17:28 -0700638 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800639 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
640 struct bio *rbio;
641 rw |= (1<<BIO_RW_BARRIER);
642 rbio = bio_clone(bio, GFP_NOIO);
643 rbio->bi_private = bio;
644 rbio->bi_end_io = super_written_barrier;
645 submit_bio(rw, rbio);
646 } else
647 submit_bio(rw, bio);
648}
649
650void md_super_wait(mddev_t *mddev)
651{
652 /* wait for all superblock writes that were scheduled to complete.
653 * if any had to be retried (due to BARRIER problems), retry them
654 */
655 DEFINE_WAIT(wq);
656 for(;;) {
657 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
658 if (atomic_read(&mddev->pending_writes)==0)
659 break;
660 while (mddev->biolist) {
661 struct bio *bio;
662 spin_lock_irq(&mddev->write_lock);
663 bio = mddev->biolist;
664 mddev->biolist = bio->bi_next ;
665 bio->bi_next = NULL;
666 spin_unlock_irq(&mddev->write_lock);
667 submit_bio(bio->bi_rw, bio);
668 }
669 schedule();
670 }
671 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700672}
673
NeilBrown6712ecf2007-09-27 12:47:43 +0200674static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
NeilBrowna654b9d82005-06-21 17:17:27 -0700679int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 struct page *page, int rw)
681{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700682 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 struct completion event;
684 int ret;
685
Jens Axboe93dbb392009-02-16 10:25:40 +0100686 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 bio->bi_bdev = bdev;
689 bio->bi_sector = sector;
690 bio_add_page(bio, page, size, 0);
691 init_completion(&event);
692 bio->bi_private = &event;
693 bio->bi_end_io = bi_complete;
694 submit_bio(rw, bio);
695 wait_for_completion(&event);
696
697 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
698 bio_put(bio);
699 return ret;
700}
NeilBrowna8745db2006-01-06 00:20:34 -0800701EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
NeilBrown0002b272005-09-09 16:23:53 -0700703static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 char b[BDEVNAME_SIZE];
706 if (!rdev->sb_page) {
707 MD_BUG();
708 return -EINVAL;
709 }
710 if (rdev->sb_loaded)
711 return 0;
712
713
Andre Noll0f420352008-07-11 22:02:23 +1000714 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 goto fail;
716 rdev->sb_loaded = 1;
717 return 0;
718
719fail:
720 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
721 bdevname(rdev->bdev,b));
722 return -EINVAL;
723}
724
725static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
726{
Andre Noll05710462008-07-11 22:02:20 +1000727 return sb1->set_uuid0 == sb2->set_uuid0 &&
728 sb1->set_uuid1 == sb2->set_uuid1 &&
729 sb1->set_uuid2 == sb2->set_uuid2 &&
730 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
734{
735 int ret;
736 mdp_super_t *tmp1, *tmp2;
737
738 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
739 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
740
741 if (!tmp1 || !tmp2) {
742 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100743 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 goto abort;
745 }
746
747 *tmp1 = *sb1;
748 *tmp2 = *sb2;
749
750 /*
751 * nr_disks is not constant
752 */
753 tmp1->nr_disks = 0;
754 tmp2->nr_disks = 0;
755
Andre Nollce0c8e02008-07-11 22:02:20 +1000756 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700758 kfree(tmp1);
759 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return ret;
761}
762
NeilBrown4d167f02007-05-09 02:35:37 -0700763
764static u32 md_csum_fold(u32 csum)
765{
766 csum = (csum & 0xffff) + (csum >> 16);
767 return (csum & 0xffff) + (csum >> 16);
768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770static unsigned int calc_sb_csum(mdp_super_t * sb)
771{
NeilBrown4d167f02007-05-09 02:35:37 -0700772 u64 newcsum = 0;
773 u32 *sb32 = (u32*)sb;
774 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 unsigned int disk_csum, csum;
776
777 disk_csum = sb->sb_csum;
778 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700779
780 for (i = 0; i < MD_SB_BYTES/4 ; i++)
781 newcsum += sb32[i];
782 csum = (newcsum & 0xffffffff) + (newcsum>>32);
783
784
785#ifdef CONFIG_ALPHA
786 /* This used to use csum_partial, which was wrong for several
787 * reasons including that different results are returned on
788 * different architectures. It isn't critical that we get exactly
789 * the same return value as before (we always csum_fold before
790 * testing, and that removes any differences). However as we
791 * know that csum_partial always returned a 16bit value on
792 * alphas, do a fold to maximise conformity to previous behaviour.
793 */
794 sb->sb_csum = md_csum_fold(disk_csum);
795#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700797#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return csum;
799}
800
801
802/*
803 * Handle superblock details.
804 * We want to be able to handle multiple superblock formats
805 * so we have a common interface to them all, and an array of
806 * different handlers.
807 * We rely on user-space to write the initial superblock, and support
808 * reading and updating of superblocks.
809 * Interface methods are:
810 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
811 * loads and validates a superblock on dev.
812 * if refdev != NULL, compare superblocks on both devices
813 * Return:
814 * 0 - dev has a superblock that is compatible with refdev
815 * 1 - dev has a superblock that is compatible and newer than refdev
816 * so dev should be used as the refdev in future
817 * -EINVAL superblock incompatible or invalid
818 * -othererror e.g. -EIO
819 *
820 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
821 * Verify that dev is acceptable into mddev.
822 * The first time, mddev->raid_disks will be 0, and data from
823 * dev should be merged in. Subsequent calls check that dev
824 * is new enough. Return 0 or -EINVAL
825 *
826 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
827 * Update the superblock for rdev with data in mddev
828 * This does not write to disc.
829 *
830 */
831
832struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000833 char *name;
834 struct module *owner;
835 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
836 int minor_version);
837 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
838 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
839 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000840 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841};
842
843/*
Andre Noll0894cc32009-06-18 08:49:23 +1000844 * Check that the given mddev has no bitmap.
845 *
846 * This function is called from the run method of all personalities that do not
847 * support bitmaps. It prints an error message and returns non-zero if mddev
848 * has a bitmap. Otherwise, it returns 0.
849 *
850 */
851int md_check_no_bitmap(mddev_t *mddev)
852{
NeilBrownc3d97142009-12-14 12:49:52 +1100853 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000854 return 0;
855 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
856 mdname(mddev), mddev->pers->name);
857 return 1;
858}
859EXPORT_SYMBOL(md_check_no_bitmap);
860
861/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 * load_super for 0.90.0
863 */
864static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
865{
866 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
867 mdp_super_t *sb;
868 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 /*
Andre Noll0f420352008-07-11 22:02:23 +1000871 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 * it's at the end of the disk.
873 *
874 * It also happens to be a multiple of 4Kb.
875 */
Andre Noll0f420352008-07-11 22:02:23 +1000876 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
NeilBrown0002b272005-09-09 16:23:53 -0700878 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (ret) return ret;
880
881 ret = -EINVAL;
882
883 bdevname(rdev->bdev, b);
884 sb = (mdp_super_t*)page_address(rdev->sb_page);
885
886 if (sb->md_magic != MD_SB_MAGIC) {
887 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
888 b);
889 goto abort;
890 }
891
892 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800893 sb->minor_version < 90 ||
894 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
896 sb->major_version, sb->minor_version,
897 b);
898 goto abort;
899 }
900
901 if (sb->raid_disks <= 0)
902 goto abort;
903
NeilBrown4d167f02007-05-09 02:35:37 -0700904 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
906 b);
907 goto abort;
908 }
909
910 rdev->preferred_minor = sb->md_minor;
911 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700912 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 if (sb->level == LEVEL_MULTIPATH)
915 rdev->desc_nr = -1;
916 else
917 rdev->desc_nr = sb->this_disk.number;
918
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700919 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700921 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 __u64 ev1, ev2;
923 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
924 if (!uuid_equal(refsb, sb)) {
925 printk(KERN_WARNING "md: %s has different UUID to %s\n",
926 b, bdevname(refdev->bdev,b2));
927 goto abort;
928 }
929 if (!sb_equal(refsb, sb)) {
930 printk(KERN_WARNING "md: %s has same UUID"
931 " but different superblock to %s\n",
932 b, bdevname(refdev->bdev, b2));
933 goto abort;
934 }
935 ev1 = md_event(sb);
936 ev2 = md_event(refsb);
937 if (ev1 > ev2)
938 ret = 1;
939 else
940 ret = 0;
941 }
NeilBrown8190e752009-06-18 08:48:58 +1000942 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Andre Nolldd8ac332009-03-31 14:33:13 +1100944 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -0800945 /* "this cannot possibly happen" ... */
946 ret = -EINVAL;
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 abort:
949 return ret;
950}
951
952/*
953 * validate_super for 0.90.0
954 */
955static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
956{
957 mdp_disk_t *desc;
958 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700959 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
NeilBrown41158c72005-06-21 17:17:25 -0700961 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800962 clear_bit(Faulty, &rdev->flags);
963 clear_bit(In_sync, &rdev->flags);
964 clear_bit(WriteMostly, &rdev->flags);
965 clear_bit(BarriersNotsupp, &rdev->flags);
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (mddev->raid_disks == 0) {
968 mddev->major_version = 0;
969 mddev->minor_version = sb->minor_version;
970 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800971 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +1000972 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 mddev->ctime = sb->ctime;
974 mddev->utime = sb->utime;
975 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800976 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 mddev->layout = sb->layout;
978 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +1100979 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -0700980 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +1100981 mddev->bitmap_info.offset = 0;
982 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
NeilBrownf6705572006-03-27 01:18:11 -0800984 if (mddev->minor_version >= 91) {
985 mddev->reshape_position = sb->reshape_position;
986 mddev->delta_disks = sb->delta_disks;
987 mddev->new_level = sb->new_level;
988 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +1000989 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -0800990 } else {
991 mddev->reshape_position = MaxSector;
992 mddev->delta_disks = 0;
993 mddev->new_level = mddev->level;
994 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +1000995 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -0800996 }
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (sb->state & (1<<MD_SB_CLEAN))
999 mddev->recovery_cp = MaxSector;
1000 else {
1001 if (sb->events_hi == sb->cp_events_hi &&
1002 sb->events_lo == sb->cp_events_lo) {
1003 mddev->recovery_cp = sb->recovery_cp;
1004 } else
1005 mddev->recovery_cp = 0;
1006 }
1007
1008 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1009 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1010 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1011 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1012
1013 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001014
1015 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001016 mddev->bitmap_info.file == NULL)
1017 mddev->bitmap_info.offset =
1018 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001019
NeilBrown41158c72005-06-21 17:17:25 -07001020 } else if (mddev->pers == NULL) {
1021 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 ++ev1;
1023 if (ev1 < mddev->events)
1024 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001025 } else if (mddev->bitmap) {
1026 /* if adding to array with a bitmap, then we can accept an
1027 * older device ... but not too old.
1028 */
NeilBrown41158c72005-06-21 17:17:25 -07001029 if (ev1 < mddev->bitmap->events_cleared)
1030 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001031 } else {
1032 if (ev1 < mddev->events)
1033 /* just a hot-add of a new device, leave raid_disk at -1 */
1034 return 0;
1035 }
NeilBrown41158c72005-06-21 17:17:25 -07001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 desc = sb->disks + rdev->desc_nr;
1039
1040 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001041 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001042 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1043 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001044 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001046 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1047 /* active but not in sync implies recovery up to
1048 * reshape position. We don't know exactly where
1049 * that is, so set to zero for now */
1050 if (mddev->minor_version >= 91) {
1051 rdev->recovery_offset = 0;
1052 rdev->raid_disk = desc->raid_disk;
1053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001055 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1056 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001057 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001058 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return 0;
1060}
1061
1062/*
1063 * sync_super for 0.90.0
1064 */
1065static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1066{
1067 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 mdk_rdev_t *rdev2;
1069 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 /* make rdev->sb match mddev data..
1073 *
1074 * 1/ zero out disks
1075 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1076 * 3/ any empty disks < next_spare become removed
1077 *
1078 * disks[0] gets initialised to REMOVED because
1079 * we cannot be sure from other fields if it has
1080 * been initialised or not.
1081 */
1082 int i;
1083 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1084
NeilBrown61181562005-09-09 16:24:02 -07001085 rdev->sb_size = MD_SB_BYTES;
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 sb = (mdp_super_t*)page_address(rdev->sb_page);
1088
1089 memset(sb, 0, sizeof(*sb));
1090
1091 sb->md_magic = MD_SB_MAGIC;
1092 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 sb->patch_version = mddev->patch_version;
1094 sb->gvalid_words = 0; /* ignored */
1095 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1096 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1097 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1098 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1099
1100 sb->ctime = mddev->ctime;
1101 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001102 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 sb->raid_disks = mddev->raid_disks;
1104 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001105 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 sb->utime = mddev->utime;
1107 sb->state = 0;
1108 sb->events_hi = (mddev->events>>32);
1109 sb->events_lo = (u32)mddev->events;
1110
NeilBrownf6705572006-03-27 01:18:11 -08001111 if (mddev->reshape_position == MaxSector)
1112 sb->minor_version = 90;
1113 else {
1114 sb->minor_version = 91;
1115 sb->reshape_position = mddev->reshape_position;
1116 sb->new_level = mddev->new_level;
1117 sb->delta_disks = mddev->delta_disks;
1118 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001119 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001120 }
1121 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (mddev->in_sync)
1123 {
1124 sb->recovery_cp = mddev->recovery_cp;
1125 sb->cp_events_hi = (mddev->events>>32);
1126 sb->cp_events_lo = (u32)mddev->events;
1127 if (mddev->recovery_cp == MaxSector)
1128 sb->state = (1<< MD_SB_CLEAN);
1129 } else
1130 sb->recovery_cp = 0;
1131
1132 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001133 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
NeilBrownc3d97142009-12-14 12:49:52 +11001135 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001136 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001139 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001141 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001142 int is_active = test_bit(In_sync, &rdev2->flags);
1143
1144 if (rdev2->raid_disk >= 0 &&
1145 sb->minor_version >= 91)
1146 /* we have nowhere to store the recovery_offset,
1147 * but if it is not below the reshape_position,
1148 * we can piggy-back on that.
1149 */
1150 is_active = 1;
1151 if (rdev2->raid_disk < 0 ||
1152 test_bit(Faulty, &rdev2->flags))
1153 is_active = 0;
1154 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001155 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001157 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001158 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 d = &sb->disks[rdev2->desc_nr];
1160 nr_disks++;
1161 d->number = rdev2->desc_nr;
1162 d->major = MAJOR(rdev2->bdev->bd_dev);
1163 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001164 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 d->raid_disk = rdev2->raid_disk;
1166 else
1167 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001168 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001170 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001172 if (test_bit(In_sync, &rdev2->flags))
1173 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 active++;
1175 working++;
1176 } else {
1177 d->state = 0;
1178 spare++;
1179 working++;
1180 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001181 if (test_bit(WriteMostly, &rdev2->flags))
1182 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 /* now set the "removed" and "faulty" bits on any missing devices */
1185 for (i=0 ; i < mddev->raid_disks ; i++) {
1186 mdp_disk_t *d = &sb->disks[i];
1187 if (d->state == 0 && d->number == 0) {
1188 d->number = i;
1189 d->raid_disk = i;
1190 d->state = (1<<MD_DISK_REMOVED);
1191 d->state |= (1<<MD_DISK_FAULTY);
1192 failed++;
1193 }
1194 }
1195 sb->nr_disks = nr_disks;
1196 sb->active_disks = active;
1197 sb->working_disks = working;
1198 sb->failed_disks = failed;
1199 sb->spare_disks = spare;
1200
1201 sb->this_disk = sb->disks[rdev->desc_nr];
1202 sb->sb_csum = calc_sb_csum(sb);
1203}
1204
1205/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001206 * rdev_size_change for 0.90.0
1207 */
1208static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001209super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001210{
Andre Noll58c0fed2009-03-31 14:33:13 +11001211 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001212 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001213 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001214 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001215 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001216 if (!num_sectors || num_sectors > rdev->sb_start)
1217 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001218 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001219 rdev->sb_page);
1220 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001221 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001222}
1223
1224
1225/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 * version 1 superblock
1227 */
1228
NeilBrown1c05b4b2006-10-21 10:24:08 -07001229static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001231 __le32 disk_csum;
1232 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 unsigned long long newcsum;
1234 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001235 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 int i;
1237
1238 disk_csum = sb->sb_csum;
1239 sb->sb_csum = 0;
1240 newcsum = 0;
1241 for (i=0; size>=4; size -= 4 )
1242 newcsum += le32_to_cpu(*isuper++);
1243
1244 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001245 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1248 sb->sb_csum = disk_csum;
1249 return cpu_to_le32(csum);
1250}
1251
1252static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1253{
1254 struct mdp_superblock_1 *sb;
1255 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001256 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001258 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 /*
Andre Noll0f420352008-07-11 22:02:23 +10001261 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 * It is always aligned to a 4K boundary and
1263 * depeding on minor_version, it can be:
1264 * 0: At least 8K, but less than 12K, from end of device
1265 * 1: At start of device
1266 * 2: 4K from start of device.
1267 */
1268 switch(minor_version) {
1269 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001270 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1271 sb_start -= 8*2;
1272 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 break;
1274 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001275 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 break;
1277 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001278 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 break;
1280 default:
1281 return -EINVAL;
1282 }
Andre Noll0f420352008-07-11 22:02:23 +10001283 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
NeilBrown0002b272005-09-09 16:23:53 -07001285 /* superblock is rarely larger than 1K, but it can be larger,
1286 * and it is safe to read 4k, so we do that
1287 */
1288 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (ret) return ret;
1290
1291
1292 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1293
1294 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1295 sb->major_version != cpu_to_le32(1) ||
1296 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001297 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001298 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return -EINVAL;
1300
1301 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1302 printk("md: invalid superblock checksum on %s\n",
1303 bdevname(rdev->bdev,b));
1304 return -EINVAL;
1305 }
1306 if (le64_to_cpu(sb->data_size) < 10) {
1307 printk("md: data_size too small on %s\n",
1308 bdevname(rdev->bdev,b));
1309 return -EINVAL;
1310 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 rdev->preferred_minor = 0xffff;
1313 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001314 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
NeilBrown0002b272005-09-09 16:23:53 -07001316 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001317 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001318 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001319 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1320
1321 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001322 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001323 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001324
NeilBrown31b65a02006-07-10 04:44:14 -07001325 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1326 rdev->desc_nr = -1;
1327 else
1328 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1329
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001330 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001331 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001332 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 __u64 ev1, ev2;
1334 struct mdp_superblock_1 *refsb =
1335 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1336
1337 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1338 sb->level != refsb->level ||
1339 sb->layout != refsb->layout ||
1340 sb->chunksize != refsb->chunksize) {
1341 printk(KERN_WARNING "md: %s has strangely different"
1342 " superblock to %s\n",
1343 bdevname(rdev->bdev,b),
1344 bdevname(refdev->bdev,b2));
1345 return -EINVAL;
1346 }
1347 ev1 = le64_to_cpu(sb->events);
1348 ev2 = le64_to_cpu(refsb->events);
1349
1350 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001351 ret = 1;
1352 else
1353 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
NeilBrowna1801f82008-03-04 14:29:31 -08001355 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001356 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1357 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001359 rdev->sectors = rdev->sb_start;
1360 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001362 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001363 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001364 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001365 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366}
1367
1368static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1369{
1370 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001371 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
NeilBrown41158c72005-06-21 17:17:25 -07001373 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001374 clear_bit(Faulty, &rdev->flags);
1375 clear_bit(In_sync, &rdev->flags);
1376 clear_bit(WriteMostly, &rdev->flags);
1377 clear_bit(BarriersNotsupp, &rdev->flags);
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 if (mddev->raid_disks == 0) {
1380 mddev->major_version = 1;
1381 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001382 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001383 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1385 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1386 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001387 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 mddev->layout = le32_to_cpu(sb->layout);
1389 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001390 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001391 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001392 mddev->bitmap_info.offset = 0;
1393 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1396 memcpy(mddev->uuid, sb->set_uuid, 16);
1397
1398 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001399
NeilBrown71c08052005-09-09 16:23:51 -07001400 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001401 mddev->bitmap_info.file == NULL )
1402 mddev->bitmap_info.offset =
1403 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001404
NeilBrownf6705572006-03-27 01:18:11 -08001405 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1406 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1407 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1408 mddev->new_level = le32_to_cpu(sb->new_level);
1409 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001410 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001411 } else {
1412 mddev->reshape_position = MaxSector;
1413 mddev->delta_disks = 0;
1414 mddev->new_level = mddev->level;
1415 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001416 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001417 }
1418
NeilBrown41158c72005-06-21 17:17:25 -07001419 } else if (mddev->pers == NULL) {
1420 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 ++ev1;
1422 if (ev1 < mddev->events)
1423 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001424 } else if (mddev->bitmap) {
1425 /* If adding to array with a bitmap, then we can accept an
1426 * older device, but not too old.
1427 */
NeilBrown41158c72005-06-21 17:17:25 -07001428 if (ev1 < mddev->bitmap->events_cleared)
1429 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001430 } else {
1431 if (ev1 < mddev->events)
1432 /* just a hot-add of a new device, leave raid_disk at -1 */
1433 return 0;
1434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 if (mddev->level != LEVEL_MULTIPATH) {
1436 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001437 if (rdev->desc_nr < 0 ||
1438 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1439 role = 0xffff;
1440 rdev->desc_nr = -1;
1441 } else
1442 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 switch(role) {
1444 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 break;
1446 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001447 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 break;
1449 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001450 if ((le32_to_cpu(sb->feature_map) &
1451 MD_FEATURE_RECOVERY_OFFSET))
1452 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1453 else
1454 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 rdev->raid_disk = role;
1456 break;
1457 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001458 if (sb->devflags & WriteMostly1)
1459 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001460 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001461 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return 0;
1464}
1465
1466static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1467{
1468 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 mdk_rdev_t *rdev2;
1470 int max_dev, i;
1471 /* make rdev->sb match mddev and rdev data. */
1472
1473 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1474
1475 sb->feature_map = 0;
1476 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001477 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 memset(sb->pad1, 0, sizeof(sb->pad1));
1479 memset(sb->pad2, 0, sizeof(sb->pad2));
1480 memset(sb->pad3, 0, sizeof(sb->pad3));
1481
1482 sb->utime = cpu_to_le64((__u64)mddev->utime);
1483 sb->events = cpu_to_le64(mddev->events);
1484 if (mddev->in_sync)
1485 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1486 else
1487 sb->resync_offset = cpu_to_le64(0);
1488
NeilBrown1c05b4b2006-10-21 10:24:08 -07001489 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001490
NeilBrownf0ca3402006-02-02 14:28:04 -08001491 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001492 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001493 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001494 sb->level = cpu_to_le32(mddev->level);
1495 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001496
NeilBrownc3d97142009-12-14 12:49:52 +11001497 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1498 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001499 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001500 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001501
1502 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001503 !test_bit(In_sync, &rdev->flags)) {
NeilBrown97e4f422009-03-31 14:33:13 +11001504 if (rdev->recovery_offset > 0) {
1505 sb->feature_map |=
1506 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1507 sb->recovery_offset =
1508 cpu_to_le64(rdev->recovery_offset);
1509 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001510 }
1511
NeilBrownf6705572006-03-27 01:18:11 -08001512 if (mddev->reshape_position != MaxSector) {
1513 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1514 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1515 sb->new_layout = cpu_to_le32(mddev->new_layout);
1516 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1517 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001518 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001519 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001522 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (rdev2->desc_nr+1 > max_dev)
1524 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001525
NeilBrown70471da2009-08-03 10:59:57 +10001526 if (max_dev > le32_to_cpu(sb->max_dev)) {
1527 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001528 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001529 rdev->sb_size = max_dev * 2 + 256;
1530 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1531 if (rdev->sb_size & bmask)
1532 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 for (i=0; i<max_dev;i++)
1535 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1536
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001537 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001539 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001541 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001543 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1544 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 else
1546 sb->dev_roles[i] = cpu_to_le16(0xffff);
1547 }
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 sb->sb_csum = calc_sb_1_csum(sb);
1550}
1551
Chris Webb0cd17fe2008-06-28 08:31:46 +10001552static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001553super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001554{
1555 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001556 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001557 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001558 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001559 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001560 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001561 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1562 max_sectors -= rdev->data_offset;
1563 if (!num_sectors || num_sectors > max_sectors)
1564 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001565 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001566 /* minor version 0 with bitmap we can't move */
1567 return 0;
1568 } else {
1569 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001570 sector_t sb_start;
1571 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1572 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001573 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001574 if (!num_sectors || num_sectors > max_sectors)
1575 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001576 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001577 }
1578 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001579 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001580 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001581 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001582 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001583 rdev->sb_page);
1584 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001585 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001586}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Adrian Bunk75c96f82005-05-05 16:16:09 -07001588static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 [0] = {
1590 .name = "0.90.0",
1591 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001592 .load_super = super_90_load,
1593 .validate_super = super_90_validate,
1594 .sync_super = super_90_sync,
1595 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 },
1597 [1] = {
1598 .name = "md-1",
1599 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001600 .load_super = super_1_load,
1601 .validate_super = super_1_validate,
1602 .sync_super = super_1_sync,
1603 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 },
1605};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1608{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001609 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
NeilBrown4b809912008-07-21 17:05:25 +10001611 rcu_read_lock();
1612 rdev_for_each_rcu(rdev, mddev1)
1613 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001614 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001615 rdev2->bdev->bd_contains) {
1616 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001617 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001618 }
1619 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 return 0;
1621}
1622
1623static LIST_HEAD(pending_raid_disks);
1624
Andre Nollac5e7112009-08-03 10:59:47 +10001625/*
1626 * Try to register data integrity profile for an mddev
1627 *
1628 * This is called when an array is started and after a disk has been kicked
1629 * from the array. It only succeeds if all working and active component devices
1630 * are integrity capable with matching profiles.
1631 */
1632int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001633{
Andre Nollac5e7112009-08-03 10:59:47 +10001634 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001635
Andre Nollac5e7112009-08-03 10:59:47 +10001636 if (list_empty(&mddev->disks))
1637 return 0; /* nothing to do */
1638 if (blk_get_integrity(mddev->gendisk))
1639 return 0; /* already registered */
1640 list_for_each_entry(rdev, &mddev->disks, same_set) {
1641 /* skip spares and non-functional disks */
1642 if (test_bit(Faulty, &rdev->flags))
1643 continue;
1644 if (rdev->raid_disk < 0)
1645 continue;
1646 /*
1647 * If at least one rdev is not integrity capable, we can not
1648 * enable data integrity for the md device.
1649 */
1650 if (!bdev_get_integrity(rdev->bdev))
1651 return -EINVAL;
1652 if (!reference) {
1653 /* Use the first rdev as the reference */
1654 reference = rdev;
1655 continue;
1656 }
1657 /* does this rdev's profile match the reference profile? */
1658 if (blk_integrity_compare(reference->bdev->bd_disk,
1659 rdev->bdev->bd_disk) < 0)
1660 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001661 }
Andre Nollac5e7112009-08-03 10:59:47 +10001662 /*
1663 * All component devices are integrity capable and have matching
1664 * profiles, register the common profile for the md device.
1665 */
1666 if (blk_integrity_register(mddev->gendisk,
1667 bdev_get_integrity(reference->bdev)) != 0) {
1668 printk(KERN_ERR "md: failed to register integrity for %s\n",
1669 mdname(mddev));
1670 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001671 }
Andre Nollac5e7112009-08-03 10:59:47 +10001672 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1673 mdname(mddev));
1674 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001675}
Andre Nollac5e7112009-08-03 10:59:47 +10001676EXPORT_SYMBOL(md_integrity_register);
1677
1678/* Disable data integrity if non-capable/non-matching disk is being added */
1679void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1680{
1681 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1682 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1683
1684 if (!bi_mddev) /* nothing to do */
1685 return;
1686 if (rdev->raid_disk < 0) /* skip spares */
1687 return;
1688 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1689 rdev->bdev->bd_disk) >= 0)
1690 return;
1691 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1692 blk_integrity_unregister(mddev->gendisk);
1693}
1694EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1697{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001698 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001699 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001700 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001701 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703 if (rdev->mddev) {
1704 MD_BUG();
1705 return -EINVAL;
1706 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001707
1708 /* prevent duplicates */
1709 if (find_rdev(mddev, rdev->bdev->bd_dev))
1710 return -EEXIST;
1711
Andre Nolldd8ac332009-03-31 14:33:13 +11001712 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1713 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1714 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001715 if (mddev->pers) {
1716 /* Cannot change size, so fail
1717 * If mddev->level <= 0, then we don't care
1718 * about aligning sizes (e.g. linear)
1719 */
1720 if (mddev->level > 0)
1721 return -ENOSPC;
1722 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001723 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 /* Verify rdev->desc_nr is unique.
1727 * If it is -1, assign a free number, else
1728 * check number is not in use
1729 */
1730 if (rdev->desc_nr < 0) {
1731 int choice = 0;
1732 if (mddev->pers) choice = mddev->raid_disks;
1733 while (find_rdev_nr(mddev, choice))
1734 choice++;
1735 rdev->desc_nr = choice;
1736 } else {
1737 if (find_rdev_nr(mddev, rdev->desc_nr))
1738 return -EBUSY;
1739 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001740 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1741 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1742 mdname(mddev), mddev->max_disks);
1743 return -EBUSY;
1744 }
NeilBrown19133a42005-11-08 21:39:35 -08001745 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001746 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001747 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001750 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001751
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001752 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001753 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001754
Tejun Heo0762b8b2008-08-25 19:56:12 +09001755 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001756 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1757 kobject_del(&rdev->kobj);
1758 goto fail;
1759 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001760 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1761
NeilBrown4b809912008-07-21 17:05:25 +10001762 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001763 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001764
1765 /* May as well allow recovery to be retried once */
1766 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001769
1770 fail:
1771 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1772 b, mdname(mddev));
1773 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
NeilBrown177a99b2008-02-06 01:39:56 -08001776static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001777{
1778 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1779 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001780 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001781}
1782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1784{
1785 char b[BDEVNAME_SIZE];
1786 if (!rdev->mddev) {
1787 MD_BUG();
1788 return;
1789 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001790 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001791 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1793 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001794 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001795 sysfs_put(rdev->sysfs_state);
1796 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001797 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001798 * writing to 'remove' to "dev/state". We also need
1799 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001800 */
NeilBrown4b809912008-07-21 17:05:25 +10001801 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001802 INIT_WORK(&rdev->del_work, md_delayed_delete);
1803 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001804 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
1807/*
1808 * prevent the device from being mounted, repartitioned or
1809 * otherwise reused by a RAID array (or any other kernel
1810 * subsystem), by bd_claiming the device.
1811 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001812static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
1814 int err = 0;
1815 struct block_device *bdev;
1816 char b[BDEVNAME_SIZE];
1817
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001818 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (IS_ERR(bdev)) {
1820 printk(KERN_ERR "md: could not open %s.\n",
1821 __bdevname(dev, b));
1822 return PTR_ERR(bdev);
1823 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001824 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 if (err) {
1826 printk(KERN_ERR "md: could not bd_claim %s.\n",
1827 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001828 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 return err;
1830 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001831 if (!shared)
1832 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 rdev->bdev = bdev;
1834 return err;
1835}
1836
1837static void unlock_rdev(mdk_rdev_t *rdev)
1838{
1839 struct block_device *bdev = rdev->bdev;
1840 rdev->bdev = NULL;
1841 if (!bdev)
1842 MD_BUG();
1843 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001844 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845}
1846
1847void md_autodetect_dev(dev_t dev);
1848
1849static void export_rdev(mdk_rdev_t * rdev)
1850{
1851 char b[BDEVNAME_SIZE];
1852 printk(KERN_INFO "md: export_rdev(%s)\n",
1853 bdevname(rdev->bdev,b));
1854 if (rdev->mddev)
1855 MD_BUG();
1856 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001858 if (test_bit(AutoDetected, &rdev->flags))
1859 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860#endif
1861 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001862 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863}
1864
1865static void kick_rdev_from_array(mdk_rdev_t * rdev)
1866{
1867 unbind_rdev_from_array(rdev);
1868 export_rdev(rdev);
1869}
1870
1871static void export_array(mddev_t *mddev)
1872{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001873 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
NeilBrownd089c6a2008-02-06 01:39:59 -08001875 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 if (!rdev->mddev) {
1877 MD_BUG();
1878 continue;
1879 }
1880 kick_rdev_from_array(rdev);
1881 }
1882 if (!list_empty(&mddev->disks))
1883 MD_BUG();
1884 mddev->raid_disks = 0;
1885 mddev->major_version = 0;
1886}
1887
1888static void print_desc(mdp_disk_t *desc)
1889{
1890 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1891 desc->major,desc->minor,desc->raid_disk,desc->state);
1892}
1893
Cheng Renquancd2ac932009-01-09 08:31:08 +11001894static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
1896 int i;
1897
1898 printk(KERN_INFO
1899 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1900 sb->major_version, sb->minor_version, sb->patch_version,
1901 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1902 sb->ctime);
1903 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1904 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1905 sb->md_minor, sb->layout, sb->chunk_size);
1906 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1907 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1908 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1909 sb->failed_disks, sb->spare_disks,
1910 sb->sb_csum, (unsigned long)sb->events_lo);
1911
1912 printk(KERN_INFO);
1913 for (i = 0; i < MD_SB_DISKS; i++) {
1914 mdp_disk_t *desc;
1915
1916 desc = sb->disks + i;
1917 if (desc->number || desc->major || desc->minor ||
1918 desc->raid_disk || (desc->state && (desc->state != 4))) {
1919 printk(" D %2d: ", i);
1920 print_desc(desc);
1921 }
1922 }
1923 printk(KERN_INFO "md: THIS: ");
1924 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
1926
Cheng Renquancd2ac932009-01-09 08:31:08 +11001927static void print_sb_1(struct mdp_superblock_1 *sb)
1928{
1929 __u8 *uuid;
1930
1931 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001932 printk(KERN_INFO
1933 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1934 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1935 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001936 le32_to_cpu(sb->major_version),
1937 le32_to_cpu(sb->feature_map),
1938 uuid[0], uuid[1], uuid[2], uuid[3],
1939 uuid[4], uuid[5], uuid[6], uuid[7],
1940 uuid[8], uuid[9], uuid[10], uuid[11],
1941 uuid[12], uuid[13], uuid[14], uuid[15],
1942 sb->set_name,
1943 (unsigned long long)le64_to_cpu(sb->ctime)
1944 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1945
1946 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001947 printk(KERN_INFO
1948 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11001949 " RO:%llu\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001950 "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1951 ":%02x%02x%02x%02x%02x%02x\n"
1952 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1953 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001954 le32_to_cpu(sb->level),
1955 (unsigned long long)le64_to_cpu(sb->size),
1956 le32_to_cpu(sb->raid_disks),
1957 le32_to_cpu(sb->layout),
1958 le32_to_cpu(sb->chunksize),
1959 (unsigned long long)le64_to_cpu(sb->data_offset),
1960 (unsigned long long)le64_to_cpu(sb->data_size),
1961 (unsigned long long)le64_to_cpu(sb->super_offset),
1962 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1963 le32_to_cpu(sb->dev_number),
1964 uuid[0], uuid[1], uuid[2], uuid[3],
1965 uuid[4], uuid[5], uuid[6], uuid[7],
1966 uuid[8], uuid[9], uuid[10], uuid[11],
1967 uuid[12], uuid[13], uuid[14], uuid[15],
1968 sb->devflags,
1969 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1970 (unsigned long long)le64_to_cpu(sb->events),
1971 (unsigned long long)le64_to_cpu(sb->resync_offset),
1972 le32_to_cpu(sb->sb_csum),
1973 le32_to_cpu(sb->max_dev)
1974 );
1975}
1976
1977static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
1979 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11001980 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1981 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08001982 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1983 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11001985 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1986 switch (major_version) {
1987 case 0:
1988 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1989 break;
1990 case 1:
1991 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1992 break;
1993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 } else
1995 printk(KERN_INFO "md: no rdev superblock!\n");
1996}
1997
Adrian Bunk5e563412006-06-26 00:27:42 -07001998static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002000 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 mdk_rdev_t *rdev;
2002 mddev_t *mddev;
2003 char b[BDEVNAME_SIZE];
2004
2005 printk("\n");
2006 printk("md: **********************************\n");
2007 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2008 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002009 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
NeilBrown32a76272005-06-21 17:17:14 -07002011 if (mddev->bitmap)
2012 bitmap_print_sb(mddev->bitmap);
2013 else
2014 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002015 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 printk("<%s>", bdevname(rdev->bdev,b));
2017 printk("\n");
2018
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002019 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002020 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
2022 printk("md: **********************************\n");
2023 printk("\n");
2024}
2025
2026
NeilBrown42543762006-06-26 00:27:57 -07002027static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
NeilBrown42543762006-06-26 00:27:57 -07002029 /* Update each superblock (in-memory image), but
2030 * if we are allowed to, skip spares which already
2031 * have the right event counter, or have one earlier
2032 * (which would mean they aren't being marked as dirty
2033 * with the rest of the array)
2034 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
NeilBrown5e865102009-11-12 12:08:04 +11002037 /* First make sure individual recovery_offsets are correct */
2038 list_for_each_entry(rdev, &mddev->disks, same_set) {
2039 if (rdev->raid_disk >= 0 &&
2040 !test_bit(In_sync, &rdev->flags) &&
2041 mddev->curr_resync_completed > rdev->recovery_offset)
2042 rdev->recovery_offset = mddev->curr_resync_completed;
2043
2044 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002045 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002046 if (rdev->sb_events == mddev->events ||
2047 (nospares &&
2048 rdev->raid_disk < 0 &&
2049 (rdev->sb_events&1)==0 &&
2050 rdev->sb_events+1 == mddev->events)) {
2051 /* Don't update this superblock */
2052 rdev->sb_loaded = 2;
2053 } else {
2054 super_types[mddev->major_version].
2055 sync_super(mddev, rdev);
2056 rdev->sb_loaded = 1;
2057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 }
2059}
2060
NeilBrown850b2b42006-10-03 01:15:46 -07002061static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002064 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002065 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
NeilBrown1b57f132009-06-18 08:48:19 +10002067 mddev->utime = get_seconds();
NeilBrown8377bc82008-04-30 00:52:28 -07002068 if (mddev->external)
2069 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08002071 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002072
NeilBrown850b2b42006-10-03 01:15:46 -07002073 set_bit(MD_CHANGE_PENDING, &mddev->flags);
2074 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2075 force_change = 1;
2076 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2077 /* just a clean<-> dirty transition, possibly leave spares alone,
2078 * though if events isn't the right even/odd, we will have to do
2079 * spares after all
2080 */
2081 nospares = 1;
2082 if (force_change)
2083 nospares = 0;
2084 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002085 /* If the array is degraded, then skipping spares is both
2086 * dangerous and fairly pointless.
2087 * Dangerous because a device that was removed from the array
2088 * might have a event_count that still looks up-to-date,
2089 * so it can be re-added without a resync.
2090 * Pointless because if there are any spares to skip,
2091 * then a recovery will happen and soon that array won't
2092 * be degraded any more and the spare can go back to sleep then.
2093 */
NeilBrown850b2b42006-10-03 01:15:46 -07002094 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002095
NeilBrown06d91a52005-06-21 17:17:12 -07002096 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002097
2098 /* If this is just a dirty<->clean transition, and the array is clean
2099 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002100 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002101 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08002102 && (mddev->events & 1)
2103 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07002104 mddev->events--;
2105 else {
2106 /* otherwise we have to go forward and ... */
2107 mddev->events ++;
2108 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
NeilBrown51d56682009-08-13 09:54:02 +10002109 /* .. if the array isn't clean, an 'even' event must also go
2110 * to spares. */
2111 if ((mddev->events&1)==0)
NeilBrown42543762006-06-26 00:27:57 -07002112 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002113 } else {
NeilBrown51d56682009-08-13 09:54:02 +10002114 /* otherwise an 'odd' event must go to spares */
2115 if ((mddev->events&1))
NeilBrown42543762006-06-26 00:27:57 -07002116 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002117 }
2118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
2120 if (!mddev->events) {
2121 /*
2122 * oops, this 64-bit counter should never wrap.
2123 * Either we are in around ~1 trillion A.C., assuming
2124 * 1 reboot per second, or we have a bug:
2125 */
2126 MD_BUG();
2127 mddev->events --;
2128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 /*
2131 * do not write anything to disk if using
2132 * nonpersistent superblocks
2133 */
NeilBrown06d91a52005-06-21 17:17:12 -07002134 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08002135 if (!mddev->external)
2136 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2137
NeilBrowna9701a32005-11-08 21:39:34 -08002138 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002139 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return;
NeilBrown06d91a52005-06-21 17:17:12 -07002141 }
NeilBrowne6910632008-02-06 01:39:51 -08002142 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002143 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 dprintk(KERN_INFO
2146 "md: updating %s RAID superblock on device (in sync %d)\n",
2147 mdname(mddev),mddev->in_sync);
2148
NeilBrown4ad13662007-07-17 04:06:13 -07002149 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002150 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 char b[BDEVNAME_SIZE];
2152 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002153 if (rdev->sb_loaded != 1)
2154 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002155 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 dprintk("(skipping faulty ");
2157
2158 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002159 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002160 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002161 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002162 rdev->sb_page);
2163 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2164 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002165 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002166 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 } else
2169 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002170 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 /* only need to write one superblock... */
2172 break;
2173 }
NeilBrowna9701a32005-11-08 21:39:34 -08002174 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002175 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002176
NeilBrowna9701a32005-11-08 21:39:34 -08002177 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002178 if (mddev->in_sync != sync_req ||
2179 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002180 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002181 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002182 goto repeat;
2183 }
NeilBrown850b2b42006-10-03 01:15:46 -07002184 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002185 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002186 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002187 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2188 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190}
2191
Andre Noll7f6ce762008-03-23 18:34:54 +01002192/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002193 * We want to accept with case. For this we use cmd_match.
2194 */
2195static int cmd_match(const char *cmd, const char *str)
2196{
2197 /* See if cmd, written into a sysfs file, matches
2198 * str. They must either be the same, or cmd can
2199 * have a trailing newline
2200 */
2201 while (*cmd && *str && *cmd == *str) {
2202 cmd++;
2203 str++;
2204 }
2205 if (*cmd == '\n')
2206 cmd++;
2207 if (*str || *cmd)
2208 return 0;
2209 return 1;
2210}
2211
NeilBrown86e6ffd2005-11-08 21:39:24 -08002212struct rdev_sysfs_entry {
2213 struct attribute attr;
2214 ssize_t (*show)(mdk_rdev_t *, char *);
2215 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2216};
2217
2218static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002219state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002220{
2221 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002222 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002223
NeilBrownb2d444d2005-11-08 21:39:31 -08002224 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002225 len+= sprintf(page+len, "%sfaulty",sep);
2226 sep = ",";
2227 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002228 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002229 len += sprintf(page+len, "%sin_sync",sep);
2230 sep = ",";
2231 }
NeilBrownf6556752006-06-26 00:28:01 -07002232 if (test_bit(WriteMostly, &rdev->flags)) {
2233 len += sprintf(page+len, "%swrite_mostly",sep);
2234 sep = ",";
2235 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002236 if (test_bit(Blocked, &rdev->flags)) {
2237 len += sprintf(page+len, "%sblocked", sep);
2238 sep = ",";
2239 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002240 if (!test_bit(Faulty, &rdev->flags) &&
2241 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002242 len += sprintf(page+len, "%sspare", sep);
2243 sep = ",";
2244 }
2245 return len+sprintf(page+len, "\n");
2246}
2247
NeilBrown45dc2de2006-06-26 00:27:58 -07002248static ssize_t
2249state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2250{
2251 /* can write
2252 * faulty - simulates and error
2253 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002254 * writemostly - sets write_mostly
2255 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002256 * blocked - sets the Blocked flag
2257 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002258 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002259 */
2260 int err = -EINVAL;
2261 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2262 md_error(rdev->mddev, rdev);
2263 err = 0;
2264 } else if (cmd_match(buf, "remove")) {
2265 if (rdev->raid_disk >= 0)
2266 err = -EBUSY;
2267 else {
2268 mddev_t *mddev = rdev->mddev;
2269 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002270 if (mddev->pers)
2271 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002272 md_new_event(mddev);
2273 err = 0;
2274 }
NeilBrownf6556752006-06-26 00:28:01 -07002275 } else if (cmd_match(buf, "writemostly")) {
2276 set_bit(WriteMostly, &rdev->flags);
2277 err = 0;
2278 } else if (cmd_match(buf, "-writemostly")) {
2279 clear_bit(WriteMostly, &rdev->flags);
2280 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002281 } else if (cmd_match(buf, "blocked")) {
2282 set_bit(Blocked, &rdev->flags);
2283 err = 0;
2284 } else if (cmd_match(buf, "-blocked")) {
2285 clear_bit(Blocked, &rdev->flags);
2286 wake_up(&rdev->blocked_wait);
2287 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2288 md_wakeup_thread(rdev->mddev->thread);
2289
2290 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002291 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2292 set_bit(In_sync, &rdev->flags);
2293 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002294 }
NeilBrown3c0ee632008-10-21 13:25:28 +11002295 if (!err && rdev->sysfs_state)
2296 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002297 return err ? err : len;
2298}
NeilBrown80ca3a42006-07-10 04:44:18 -07002299static struct rdev_sysfs_entry rdev_state =
2300__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002301
2302static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002303errors_show(mdk_rdev_t *rdev, char *page)
2304{
2305 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2306}
2307
2308static ssize_t
2309errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2310{
2311 char *e;
2312 unsigned long n = simple_strtoul(buf, &e, 10);
2313 if (*buf && (*e == 0 || *e == '\n')) {
2314 atomic_set(&rdev->corrected_errors, n);
2315 return len;
2316 }
2317 return -EINVAL;
2318}
2319static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002320__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002321
NeilBrown014236d2006-01-06 00:20:55 -08002322static ssize_t
2323slot_show(mdk_rdev_t *rdev, char *page)
2324{
2325 if (rdev->raid_disk < 0)
2326 return sprintf(page, "none\n");
2327 else
2328 return sprintf(page, "%d\n", rdev->raid_disk);
2329}
2330
2331static ssize_t
2332slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2333{
2334 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002335 int err;
2336 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002337 int slot = simple_strtoul(buf, &e, 10);
2338 if (strncmp(buf, "none", 4)==0)
2339 slot = -1;
2340 else if (e==buf || (*e && *e!= '\n'))
2341 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002342 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002343 /* Setting 'slot' on an active array requires also
2344 * updating the 'rd%d' link, and communicating
2345 * with the personality with ->hot_*_disk.
2346 * For now we only support removing
2347 * failed/spare devices. This normally happens automatically,
2348 * but not when the metadata is externally managed.
2349 */
NeilBrownc303da62008-02-06 01:39:51 -08002350 if (rdev->raid_disk == -1)
2351 return -EEXIST;
2352 /* personality does all needed checks */
2353 if (rdev->mddev->pers->hot_add_disk == NULL)
2354 return -EINVAL;
2355 err = rdev->mddev->pers->
2356 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2357 if (err)
2358 return err;
2359 sprintf(nm, "rd%d", rdev->raid_disk);
2360 sysfs_remove_link(&rdev->mddev->kobj, nm);
2361 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2362 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002363 } else if (rdev->mddev->pers) {
2364 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002365 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002366 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002367 */
2368
2369 if (rdev->raid_disk != -1)
2370 return -EBUSY;
2371
2372 if (rdev->mddev->pers->hot_add_disk == NULL)
2373 return -EINVAL;
2374
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002375 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002376 if (rdev2->raid_disk == slot)
2377 return -EEXIST;
2378
2379 rdev->raid_disk = slot;
2380 if (test_bit(In_sync, &rdev->flags))
2381 rdev->saved_raid_disk = slot;
2382 else
2383 rdev->saved_raid_disk = -1;
2384 err = rdev->mddev->pers->
2385 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002386 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002387 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002388 return err;
Neil Brown52664732008-06-28 08:31:44 +10002389 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002390 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002391 sprintf(nm, "rd%d", rdev->raid_disk);
2392 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2393 printk(KERN_WARNING
2394 "md: cannot register "
2395 "%s for %s\n",
2396 nm, mdname(rdev->mddev));
2397
2398 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002399 } else {
2400 if (slot >= rdev->mddev->raid_disks)
2401 return -ENOSPC;
2402 rdev->raid_disk = slot;
2403 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002404 clear_bit(Faulty, &rdev->flags);
2405 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002406 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002407 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002408 }
NeilBrown014236d2006-01-06 00:20:55 -08002409 return len;
2410}
2411
2412
2413static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002414__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002415
NeilBrown93c8cad2006-01-06 00:20:56 -08002416static ssize_t
2417offset_show(mdk_rdev_t *rdev, char *page)
2418{
Andrew Morton6961ece2006-01-06 00:20:59 -08002419 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002420}
2421
2422static ssize_t
2423offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2424{
2425 char *e;
2426 unsigned long long offset = simple_strtoull(buf, &e, 10);
2427 if (e==buf || (*e && *e != '\n'))
2428 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002429 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002430 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002431 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002432 /* Must set offset before size, so overlap checks
2433 * can be sane */
2434 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002435 rdev->data_offset = offset;
2436 return len;
2437}
2438
2439static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002440__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002441
NeilBrown83303b62006-01-06 00:21:06 -08002442static ssize_t
2443rdev_size_show(mdk_rdev_t *rdev, char *page)
2444{
Andre Nolldd8ac332009-03-31 14:33:13 +11002445 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002446}
2447
NeilBrownc5d79ad2008-02-06 01:39:54 -08002448static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2449{
2450 /* check if two start/length pairs overlap */
2451 if (s1+l1 <= s2)
2452 return 0;
2453 if (s2+l2 <= s1)
2454 return 0;
2455 return 1;
2456}
2457
Dan Williamsb522adc2009-03-31 15:00:31 +11002458static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2459{
2460 unsigned long long blocks;
2461 sector_t new;
2462
2463 if (strict_strtoull(buf, 10, &blocks) < 0)
2464 return -EINVAL;
2465
2466 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2467 return -EINVAL; /* sector conversion overflow */
2468
2469 new = blocks * 2;
2470 if (new != blocks * 2)
2471 return -EINVAL; /* unsigned long long to sector_t overflow */
2472
2473 *sectors = new;
2474 return 0;
2475}
2476
NeilBrown83303b62006-01-06 00:21:06 -08002477static ssize_t
2478rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2479{
NeilBrown27c529b2008-03-04 14:29:33 -08002480 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002481 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002482 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002483
Dan Williamsb522adc2009-03-31 15:00:31 +11002484 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002485 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002486 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002487 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002488 sectors = super_types[my_mddev->major_version].
2489 rdev_size_change(rdev, sectors);
2490 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002491 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002492 } else if (!sectors)
2493 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2494 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002495 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002496 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002497 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002498
Andre Nolldd8ac332009-03-31 14:33:13 +11002499 rdev->sectors = sectors;
2500 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002501 /* need to check that all other rdevs with the same ->bdev
2502 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002503 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002504 * we have to change it back, we will have the lock again.
2505 */
2506 mddev_t *mddev;
2507 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002508 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002509
NeilBrown27c529b2008-03-04 14:29:33 -08002510 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002511 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002512 mdk_rdev_t *rdev2;
2513
2514 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002515 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002516 if (test_bit(AllReserved, &rdev2->flags) ||
2517 (rdev->bdev == rdev2->bdev &&
2518 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002519 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002520 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002521 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002522 overlap = 1;
2523 break;
2524 }
2525 mddev_unlock(mddev);
2526 if (overlap) {
2527 mddev_put(mddev);
2528 break;
2529 }
2530 }
NeilBrown27c529b2008-03-04 14:29:33 -08002531 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002532 if (overlap) {
2533 /* Someone else could have slipped in a size
2534 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002535 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002536 * safe, and trust userspace not to race with
2537 * itself
2538 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002539 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002540 return -EBUSY;
2541 }
2542 }
NeilBrown83303b62006-01-06 00:21:06 -08002543 return len;
2544}
2545
2546static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002547__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002548
NeilBrown86e6ffd2005-11-08 21:39:24 -08002549static struct attribute *rdev_default_attrs[] = {
2550 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002551 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002552 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002553 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002554 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002555 NULL,
2556};
2557static ssize_t
2558rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2559{
2560 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2561 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002562 mddev_t *mddev = rdev->mddev;
2563 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002564
2565 if (!entry->show)
2566 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002567
2568 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2569 if (!rv) {
2570 if (rdev->mddev == NULL)
2571 rv = -EBUSY;
2572 else
2573 rv = entry->show(rdev, page);
2574 mddev_unlock(mddev);
2575 }
2576 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002577}
2578
2579static ssize_t
2580rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2581 const char *page, size_t length)
2582{
2583 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2584 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002585 ssize_t rv;
2586 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002587
2588 if (!entry->store)
2589 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002590 if (!capable(CAP_SYS_ADMIN))
2591 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002592 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002593 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002594 if (rdev->mddev == NULL)
2595 rv = -EBUSY;
2596 else
2597 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002598 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002599 }
2600 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002601}
2602
2603static void rdev_free(struct kobject *ko)
2604{
2605 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2606 kfree(rdev);
2607}
2608static struct sysfs_ops rdev_sysfs_ops = {
2609 .show = rdev_attr_show,
2610 .store = rdev_attr_store,
2611};
2612static struct kobj_type rdev_ktype = {
2613 .release = rdev_free,
2614 .sysfs_ops = &rdev_sysfs_ops,
2615 .default_attrs = rdev_default_attrs,
2616};
2617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618/*
2619 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2620 *
2621 * mark the device faulty if:
2622 *
2623 * - the device is nonexistent (zero size)
2624 * - the device has no valid superblock
2625 *
2626 * a faulty rdev _never_ has rdev->sb set.
2627 */
2628static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2629{
2630 char b[BDEVNAME_SIZE];
2631 int err;
2632 mdk_rdev_t *rdev;
2633 sector_t size;
2634
NeilBrown9ffae0c2006-01-06 00:20:32 -08002635 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 if (!rdev) {
2637 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2638 return ERR_PTR(-ENOMEM);
2639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
2641 if ((err = alloc_disk_sb(rdev)))
2642 goto abort_free;
2643
NeilBrownc5d79ad2008-02-06 01:39:54 -08002644 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 if (err)
2646 goto abort_free;
2647
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002648 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002651 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002652 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002653 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002655 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002657 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002658 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
2660 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2661 if (!size) {
2662 printk(KERN_WARNING
2663 "md: %s has zero or unknown size, marking faulty!\n",
2664 bdevname(rdev->bdev,b));
2665 err = -EINVAL;
2666 goto abort_free;
2667 }
2668
2669 if (super_format >= 0) {
2670 err = super_types[super_format].
2671 load_super(rdev, NULL, super_minor);
2672 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002673 printk(KERN_WARNING
2674 "md: %s does not have a valid v%d.%d "
2675 "superblock, not importing!\n",
2676 bdevname(rdev->bdev,b),
2677 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 goto abort_free;
2679 }
2680 if (err < 0) {
2681 printk(KERN_WARNING
2682 "md: could not read %s's sb, not importing!\n",
2683 bdevname(rdev->bdev,b));
2684 goto abort_free;
2685 }
2686 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002689 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
2691 return rdev;
2692
2693abort_free:
2694 if (rdev->sb_page) {
2695 if (rdev->bdev)
2696 unlock_rdev(rdev);
2697 free_disk_sb(rdev);
2698 }
2699 kfree(rdev);
2700 return ERR_PTR(err);
2701}
2702
2703/*
2704 * Check a full RAID array for plausibility
2705 */
2706
2707
NeilBrowna757e642005-04-16 15:26:42 -07002708static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709{
2710 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002711 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 char b[BDEVNAME_SIZE];
2713
2714 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002715 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 switch (super_types[mddev->major_version].
2717 load_super(rdev, freshest, mddev->minor_version)) {
2718 case 1:
2719 freshest = rdev;
2720 break;
2721 case 0:
2722 break;
2723 default:
2724 printk( KERN_ERR \
2725 "md: fatal superblock inconsistency in %s"
2726 " -- removing from array\n",
2727 bdevname(rdev->bdev,b));
2728 kick_rdev_from_array(rdev);
2729 }
2730
2731
2732 super_types[mddev->major_version].
2733 validate_super(mddev, freshest);
2734
2735 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002736 rdev_for_each(rdev, tmp, mddev) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002737 if (rdev->desc_nr >= mddev->max_disks ||
2738 i > mddev->max_disks) {
2739 printk(KERN_WARNING
2740 "md: %s: %s: only %d devices permitted\n",
2741 mdname(mddev), bdevname(rdev->bdev, b),
2742 mddev->max_disks);
2743 kick_rdev_from_array(rdev);
2744 continue;
2745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 if (rdev != freshest)
2747 if (super_types[mddev->major_version].
2748 validate_super(mddev, rdev)) {
2749 printk(KERN_WARNING "md: kicking non-fresh %s"
2750 " from array!\n",
2751 bdevname(rdev->bdev,b));
2752 kick_rdev_from_array(rdev);
2753 continue;
2754 }
2755 if (mddev->level == LEVEL_MULTIPATH) {
2756 rdev->desc_nr = i++;
2757 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002758 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002759 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002760 rdev->raid_disk = -1;
2761 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 }
2763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764}
2765
NeilBrown72e02072009-12-14 12:49:55 +11002766/* Read a fixed-point number.
2767 * Numbers in sysfs attributes should be in "standard" units where
2768 * possible, so time should be in seconds.
2769 * However we internally use a a much smaller unit such as
2770 * milliseconds or jiffies.
2771 * This function takes a decimal number with a possible fractional
2772 * component, and produces an integer which is the result of
2773 * multiplying that number by 10^'scale'.
2774 * all without any floating-point arithmetic.
2775 */
2776int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2777{
2778 unsigned long result = 0;
2779 long decimals = -1;
2780 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2781 if (*cp == '.')
2782 decimals = 0;
2783 else if (decimals < scale) {
2784 unsigned int value;
2785 value = *cp - '0';
2786 result = result * 10 + value;
2787 if (decimals >= 0)
2788 decimals++;
2789 }
2790 cp++;
2791 }
2792 if (*cp == '\n')
2793 cp++;
2794 if (*cp)
2795 return -EINVAL;
2796 if (decimals < 0)
2797 decimals = 0;
2798 while (decimals < scale) {
2799 result *= 10;
2800 decimals ++;
2801 }
2802 *res = result;
2803 return 0;
2804}
2805
2806
NeilBrown19052c02008-08-05 15:54:13 +10002807static void md_safemode_timeout(unsigned long data);
2808
NeilBrowneae17012005-11-08 21:39:23 -08002809static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002810safe_delay_show(mddev_t *mddev, char *page)
2811{
2812 int msec = (mddev->safemode_delay*1000)/HZ;
2813 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2814}
2815static ssize_t
2816safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2817{
NeilBrown16f17b32006-06-26 00:27:37 -07002818 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07002819
NeilBrown72e02072009-12-14 12:49:55 +11002820 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002821 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07002822 if (msec == 0)
2823 mddev->safemode_delay = 0;
2824 else {
NeilBrown19052c02008-08-05 15:54:13 +10002825 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002826 mddev->safemode_delay = (msec*HZ)/1000;
2827 if (mddev->safemode_delay == 0)
2828 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002829 if (mddev->safemode_delay < old_delay)
2830 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002831 }
2832 return len;
2833}
2834static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002835__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002836
2837static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002838level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002839{
NeilBrown2604b702006-01-06 00:20:36 -08002840 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002841 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002842 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002843 else if (mddev->clevel[0])
2844 return sprintf(page, "%s\n", mddev->clevel);
2845 else if (mddev->level != LEVEL_NONE)
2846 return sprintf(page, "%d\n", mddev->level);
2847 else
2848 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002849}
2850
NeilBrownd9d166c2006-01-06 00:20:51 -08002851static ssize_t
2852level_store(mddev_t *mddev, const char *buf, size_t len)
2853{
NeilBrown245f46c2009-03-31 14:39:39 +11002854 char level[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002855 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002856 struct mdk_personality *pers;
2857 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002858 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002859
2860 if (mddev->pers == NULL) {
2861 if (len == 0)
2862 return 0;
2863 if (len >= sizeof(mddev->clevel))
2864 return -ENOSPC;
2865 strncpy(mddev->clevel, buf, len);
2866 if (mddev->clevel[len-1] == '\n')
2867 len--;
2868 mddev->clevel[len] = 0;
2869 mddev->level = LEVEL_NONE;
2870 return rv;
2871 }
2872
2873 /* request to change the personality. Need to ensure:
2874 * - array is not engaged in resync/recovery/reshape
2875 * - old personality can be suspended
2876 * - new personality will access other array.
2877 */
2878
2879 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownd9d166c2006-01-06 00:20:51 -08002880 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002881
2882 if (!mddev->pers->quiesce) {
2883 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2884 mdname(mddev), mddev->pers->name);
2885 return -EINVAL;
2886 }
2887
2888 /* Now find the new personality */
2889 if (len == 0 || len >= sizeof(level))
2890 return -EINVAL;
2891 strncpy(level, buf, len);
2892 if (level[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002893 len--;
NeilBrown245f46c2009-03-31 14:39:39 +11002894 level[len] = 0;
2895
2896 request_module("md-%s", level);
2897 spin_lock(&pers_lock);
2898 pers = find_pers(LEVEL_NONE, level);
2899 if (!pers || !try_module_get(pers->owner)) {
2900 spin_unlock(&pers_lock);
2901 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2902 return -EINVAL;
2903 }
2904 spin_unlock(&pers_lock);
2905
2906 if (pers == mddev->pers) {
2907 /* Nothing to do! */
2908 module_put(pers->owner);
2909 return rv;
2910 }
2911 if (!pers->takeover) {
2912 module_put(pers->owner);
2913 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2914 mdname(mddev), level);
2915 return -EINVAL;
2916 }
2917
2918 /* ->takeover must set new_* and/or delta_disks
2919 * if it succeeds, and may set them when it fails.
2920 */
2921 priv = pers->takeover(mddev);
2922 if (IS_ERR(priv)) {
2923 mddev->new_level = mddev->level;
2924 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002925 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002926 mddev->raid_disks -= mddev->delta_disks;
2927 mddev->delta_disks = 0;
2928 module_put(pers->owner);
2929 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2930 mdname(mddev), level);
2931 return PTR_ERR(priv);
2932 }
2933
2934 /* Looks like we have a winner */
2935 mddev_suspend(mddev);
2936 mddev->pers->stop(mddev);
2937 module_put(mddev->pers->owner);
NeilBrown3a981b02009-08-03 10:59:55 +10002938 /* Invalidate devices that are now superfluous */
2939 list_for_each_entry(rdev, &mddev->disks, same_set)
2940 if (rdev->raid_disk >= mddev->raid_disks) {
2941 rdev->raid_disk = -1;
2942 clear_bit(In_sync, &rdev->flags);
2943 }
NeilBrown245f46c2009-03-31 14:39:39 +11002944 mddev->pers = pers;
2945 mddev->private = priv;
2946 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2947 mddev->level = mddev->new_level;
2948 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002949 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002950 mddev->delta_disks = 0;
2951 pers->run(mddev);
2952 mddev_resume(mddev);
2953 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2954 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2955 md_wakeup_thread(mddev->thread);
NeilBrownd9d166c2006-01-06 00:20:51 -08002956 return rv;
2957}
2958
2959static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002960__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002961
NeilBrownd4dbd022006-06-26 00:27:59 -07002962
2963static ssize_t
2964layout_show(mddev_t *mddev, char *page)
2965{
2966 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002967 if (mddev->reshape_position != MaxSector &&
2968 mddev->layout != mddev->new_layout)
2969 return sprintf(page, "%d (%d)\n",
2970 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002971 return sprintf(page, "%d\n", mddev->layout);
2972}
2973
2974static ssize_t
2975layout_store(mddev_t *mddev, const char *buf, size_t len)
2976{
2977 char *e;
2978 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002979
2980 if (!*buf || (*e && *e != '\n'))
2981 return -EINVAL;
2982
NeilBrownb3546032009-03-31 14:56:41 +11002983 if (mddev->pers) {
2984 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10002985 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11002986 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10002987 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10002988 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10002989 if (err) {
2990 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11002991 return err;
NeilBrown597a7112009-06-18 08:47:42 +10002992 }
NeilBrownb3546032009-03-31 14:56:41 +11002993 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07002994 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11002995 if (mddev->reshape_position == MaxSector)
2996 mddev->layout = n;
2997 }
NeilBrownd4dbd022006-06-26 00:27:59 -07002998 return len;
2999}
3000static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003001__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003002
3003
NeilBrowneae17012005-11-08 21:39:23 -08003004static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003005raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003006{
NeilBrownbb636542005-11-08 21:39:45 -08003007 if (mddev->raid_disks == 0)
3008 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003009 if (mddev->reshape_position != MaxSector &&
3010 mddev->delta_disks != 0)
3011 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3012 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003013 return sprintf(page, "%d\n", mddev->raid_disks);
3014}
3015
NeilBrownda943b992006-01-06 00:20:54 -08003016static int update_raid_disks(mddev_t *mddev, int raid_disks);
3017
3018static ssize_t
3019raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3020{
NeilBrownda943b992006-01-06 00:20:54 -08003021 char *e;
3022 int rv = 0;
3023 unsigned long n = simple_strtoul(buf, &e, 10);
3024
3025 if (!*buf || (*e && *e != '\n'))
3026 return -EINVAL;
3027
3028 if (mddev->pers)
3029 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003030 else if (mddev->reshape_position != MaxSector) {
3031 int olddisks = mddev->raid_disks - mddev->delta_disks;
3032 mddev->delta_disks = n - olddisks;
3033 mddev->raid_disks = n;
3034 } else
NeilBrownda943b992006-01-06 00:20:54 -08003035 mddev->raid_disks = n;
3036 return rv ? rv : len;
3037}
3038static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003039__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003040
NeilBrown24dd4692005-11-08 21:39:26 -08003041static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003042chunk_size_show(mddev_t *mddev, char *page)
3043{
NeilBrown08a02ec2007-05-09 02:35:38 -07003044 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003045 mddev->chunk_sectors != mddev->new_chunk_sectors)
3046 return sprintf(page, "%d (%d)\n",
3047 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003048 mddev->chunk_sectors << 9);
3049 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003050}
3051
3052static ssize_t
3053chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3054{
NeilBrown3b343802006-01-06 00:20:47 -08003055 char *e;
3056 unsigned long n = simple_strtoul(buf, &e, 10);
3057
NeilBrown3b343802006-01-06 00:20:47 -08003058 if (!*buf || (*e && *e != '\n'))
3059 return -EINVAL;
3060
NeilBrownb3546032009-03-31 14:56:41 +11003061 if (mddev->pers) {
3062 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003063 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003064 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003065 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003066 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003067 if (err) {
3068 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003069 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003070 }
NeilBrownb3546032009-03-31 14:56:41 +11003071 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003072 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003073 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003074 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003075 }
NeilBrown3b343802006-01-06 00:20:47 -08003076 return len;
3077}
3078static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003079__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003080
NeilBrowna94213b2006-06-26 00:28:00 -07003081static ssize_t
3082resync_start_show(mddev_t *mddev, char *page)
3083{
NeilBrownd1a7c502009-03-31 15:24:32 +11003084 if (mddev->recovery_cp == MaxSector)
3085 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003086 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3087}
3088
3089static ssize_t
3090resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3091{
NeilBrowna94213b2006-06-26 00:28:00 -07003092 char *e;
3093 unsigned long long n = simple_strtoull(buf, &e, 10);
3094
3095 if (mddev->pers)
3096 return -EBUSY;
3097 if (!*buf || (*e && *e != '\n'))
3098 return -EINVAL;
3099
3100 mddev->recovery_cp = n;
3101 return len;
3102}
3103static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003104__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003105
NeilBrown9e653b62006-06-26 00:27:58 -07003106/*
3107 * The array state can be:
3108 *
3109 * clear
3110 * No devices, no size, no level
3111 * Equivalent to STOP_ARRAY ioctl
3112 * inactive
3113 * May have some settings, but array is not active
3114 * all IO results in error
3115 * When written, doesn't tear down array, but just stops it
3116 * suspended (not supported yet)
3117 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003118 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003119 * readonly
3120 * no resync can happen. no superblocks get written.
3121 * write requests fail
3122 * read-auto
3123 * like readonly, but behaves like 'clean' on a write request.
3124 *
3125 * clean - no pending writes, but otherwise active.
3126 * When written to inactive array, starts without resync
3127 * If a write request arrives then
3128 * if metadata is known, mark 'dirty' and switch to 'active'.
3129 * if not known, block and switch to write-pending
3130 * If written to an active array that has pending writes, then fails.
3131 * active
3132 * fully active: IO and resync can be happening.
3133 * When written to inactive array, starts with resync
3134 *
3135 * write-pending
3136 * clean, but writes are blocked waiting for 'active' to be written.
3137 *
3138 * active-idle
3139 * like active, but no writes have been seen for a while (100msec).
3140 *
3141 */
3142enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3143 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003144static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003145 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3146 "write-pending", "active-idle", NULL };
3147
3148static int match_word(const char *word, char **list)
3149{
3150 int n;
3151 for (n=0; list[n]; n++)
3152 if (cmd_match(word, list[n]))
3153 break;
3154 return n;
3155}
3156
3157static ssize_t
3158array_state_show(mddev_t *mddev, char *page)
3159{
3160 enum array_state st = inactive;
3161
3162 if (mddev->pers)
3163 switch(mddev->ro) {
3164 case 1:
3165 st = readonly;
3166 break;
3167 case 2:
3168 st = read_auto;
3169 break;
3170 case 0:
3171 if (mddev->in_sync)
3172 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08003173 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3174 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003175 else if (mddev->safemode)
3176 st = active_idle;
3177 else
3178 st = active;
3179 }
3180 else {
3181 if (list_empty(&mddev->disks) &&
3182 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003183 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003184 st = clear;
3185 else
3186 st = inactive;
3187 }
3188 return sprintf(page, "%s\n", array_states[st]);
3189}
3190
Neil Browndf5b20c2008-07-11 22:02:22 +10003191static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003192static int do_md_run(mddev_t * mddev);
3193static int restart_array(mddev_t *mddev);
3194
3195static ssize_t
3196array_state_store(mddev_t *mddev, const char *buf, size_t len)
3197{
3198 int err = -EINVAL;
3199 enum array_state st = match_word(buf, array_states);
3200 switch(st) {
3201 case bad_word:
3202 break;
3203 case clear:
3204 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003205 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003206 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003207 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003208 break;
3209 case inactive:
3210 /* stopping an active array */
3211 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003212 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003213 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003214 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003215 } else
3216 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003217 break;
3218 case suspended:
3219 break; /* not supported yet */
3220 case readonly:
3221 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10003222 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003223 else {
3224 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003225 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003226 err = do_md_run(mddev);
3227 }
3228 break;
3229 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003230 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003231 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10003232 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003233 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003234 err = restart_array(mddev);
3235 if (err == 0) {
3236 mddev->ro = 2;
3237 set_disk_ro(mddev->gendisk, 0);
3238 }
NeilBrown9e653b62006-06-26 00:27:58 -07003239 } else {
3240 mddev->ro = 2;
3241 err = do_md_run(mddev);
3242 }
3243 break;
3244 case clean:
3245 if (mddev->pers) {
3246 restart_array(mddev);
3247 spin_lock_irq(&mddev->write_lock);
3248 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003249 if (mddev->in_sync == 0) {
3250 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003251 if (mddev->safemode == 1)
3252 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003253 if (mddev->persistent)
3254 set_bit(MD_CHANGE_CLEAN,
3255 &mddev->flags);
3256 }
3257 err = 0;
3258 } else
3259 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003260 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003261 } else
3262 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003263 break;
3264 case active:
3265 if (mddev->pers) {
3266 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003267 if (mddev->external)
3268 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003269 wake_up(&mddev->sb_wait);
3270 err = 0;
3271 } else {
3272 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003273 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003274 err = do_md_run(mddev);
3275 }
3276 break;
3277 case write_pending:
3278 case active_idle:
3279 /* these cannot be set */
3280 break;
3281 }
3282 if (err)
3283 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003284 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003285 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003286 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003287 }
NeilBrown9e653b62006-06-26 00:27:58 -07003288}
NeilBrown80ca3a42006-07-10 04:44:18 -07003289static struct md_sysfs_entry md_array_state =
3290__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003291
NeilBrown6d7ff7382006-01-06 00:21:16 -08003292static ssize_t
3293null_show(mddev_t *mddev, char *page)
3294{
3295 return -EINVAL;
3296}
3297
3298static ssize_t
3299new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3300{
3301 /* buf must be %d:%d\n? giving major and minor numbers */
3302 /* The new device is added to the array.
3303 * If the array has a persistent superblock, we read the
3304 * superblock to initialise info and check validity.
3305 * Otherwise, only checking done is that in bind_rdev_to_array,
3306 * which mainly checks size.
3307 */
3308 char *e;
3309 int major = simple_strtoul(buf, &e, 10);
3310 int minor;
3311 dev_t dev;
3312 mdk_rdev_t *rdev;
3313 int err;
3314
3315 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3316 return -EINVAL;
3317 minor = simple_strtoul(e+1, &e, 10);
3318 if (*e && *e != '\n')
3319 return -EINVAL;
3320 dev = MKDEV(major, minor);
3321 if (major != MAJOR(dev) ||
3322 minor != MINOR(dev))
3323 return -EOVERFLOW;
3324
3325
3326 if (mddev->persistent) {
3327 rdev = md_import_device(dev, mddev->major_version,
3328 mddev->minor_version);
3329 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3330 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3331 mdk_rdev_t, same_set);
3332 err = super_types[mddev->major_version]
3333 .load_super(rdev, rdev0, mddev->minor_version);
3334 if (err < 0)
3335 goto out;
3336 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003337 } else if (mddev->external)
3338 rdev = md_import_device(dev, -2, -1);
3339 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003340 rdev = md_import_device(dev, -1, -1);
3341
3342 if (IS_ERR(rdev))
3343 return PTR_ERR(rdev);
3344 err = bind_rdev_to_array(rdev, mddev);
3345 out:
3346 if (err)
3347 export_rdev(rdev);
3348 return err ? err : len;
3349}
3350
3351static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003352__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003353
3354static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003355bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3356{
3357 char *end;
3358 unsigned long chunk, end_chunk;
3359
3360 if (!mddev->bitmap)
3361 goto out;
3362 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3363 while (*buf) {
3364 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3365 if (buf == end) break;
3366 if (*end == '-') { /* range */
3367 buf = end + 1;
3368 end_chunk = simple_strtoul(buf, &end, 0);
3369 if (buf == end) break;
3370 }
3371 if (*end && !isspace(*end)) break;
3372 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3373 buf = end;
3374 while (isspace(*buf)) buf++;
3375 }
3376 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3377out:
3378 return len;
3379}
3380
3381static struct md_sysfs_entry md_bitmap =
3382__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3383
3384static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003385size_show(mddev_t *mddev, char *page)
3386{
Andre Noll58c0fed2009-03-31 14:33:13 +11003387 return sprintf(page, "%llu\n",
3388 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003389}
3390
Andre Nolld71f9f82008-07-11 22:02:22 +10003391static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003392
3393static ssize_t
3394size_store(mddev_t *mddev, const char *buf, size_t len)
3395{
3396 /* If array is inactive, we can reduce the component size, but
3397 * not increase it (except from 0).
3398 * If array is active, we can try an on-line resize
3399 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003400 sector_t sectors;
3401 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003402
Andre Noll58c0fed2009-03-31 14:33:13 +11003403 if (err < 0)
3404 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003405 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003406 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003407 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003408 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003409 if (mddev->dev_sectors == 0 ||
3410 mddev->dev_sectors > sectors)
3411 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003412 else
3413 err = -ENOSPC;
3414 }
3415 return err ? err : len;
3416}
3417
3418static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003419__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003420
NeilBrown8bb93aa2006-01-06 00:20:50 -08003421
3422/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003423 * This is one of
3424 * 'none' for arrays with no metadata (good luck...)
3425 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003426 * or N.M for internally known formats
3427 */
3428static ssize_t
3429metadata_show(mddev_t *mddev, char *page)
3430{
3431 if (mddev->persistent)
3432 return sprintf(page, "%d.%d\n",
3433 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003434 else if (mddev->external)
3435 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003436 else
3437 return sprintf(page, "none\n");
3438}
3439
3440static ssize_t
3441metadata_store(mddev_t *mddev, const char *buf, size_t len)
3442{
3443 int major, minor;
3444 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003445 /* Changing the details of 'external' metadata is
3446 * always permitted. Otherwise there must be
3447 * no devices attached to the array.
3448 */
3449 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3450 ;
3451 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003452 return -EBUSY;
3453
3454 if (cmd_match(buf, "none")) {
3455 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003456 mddev->external = 0;
3457 mddev->major_version = 0;
3458 mddev->minor_version = 90;
3459 return len;
3460 }
3461 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003462 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003463 if (namelen >= sizeof(mddev->metadata_type))
3464 namelen = sizeof(mddev->metadata_type)-1;
3465 strncpy(mddev->metadata_type, buf+9, namelen);
3466 mddev->metadata_type[namelen] = 0;
3467 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3468 mddev->metadata_type[--namelen] = 0;
3469 mddev->persistent = 0;
3470 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003471 mddev->major_version = 0;
3472 mddev->minor_version = 90;
3473 return len;
3474 }
3475 major = simple_strtoul(buf, &e, 10);
3476 if (e==buf || *e != '.')
3477 return -EINVAL;
3478 buf = e+1;
3479 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003480 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003481 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003482 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003483 return -ENOENT;
3484 mddev->major_version = major;
3485 mddev->minor_version = minor;
3486 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003487 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003488 return len;
3489}
3490
3491static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003492__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003493
NeilBrowna35b0d62006-01-06 00:20:49 -08003494static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003495action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003496{
NeilBrown7eec3142005-11-08 21:39:44 -08003497 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003498 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3499 type = "frozen";
3500 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003501 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003502 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3503 type = "reshape";
3504 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003505 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3506 type = "resync";
3507 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3508 type = "check";
3509 else
3510 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003511 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003512 type = "recover";
3513 }
3514 return sprintf(page, "%s\n", type);
3515}
3516
3517static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003518action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003519{
NeilBrown7eec3142005-11-08 21:39:44 -08003520 if (!mddev->pers || !mddev->pers->sync_request)
3521 return -EINVAL;
3522
NeilBrownb6a9ce62009-05-26 09:41:17 +10003523 if (cmd_match(page, "frozen"))
3524 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3525 else
3526 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3527
3528 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003529 if (mddev->sync_thread) {
3530 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3531 md_unregister_thread(mddev->sync_thread);
3532 mddev->sync_thread = NULL;
3533 mddev->recovery = 0;
3534 }
NeilBrown03c902e2006-01-06 00:20:46 -08003535 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3536 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003537 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003538 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003539 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003540 else if (cmd_match(page, "recover")) {
3541 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3542 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3543 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003544 int err;
3545 if (mddev->pers->start_reshape == NULL)
3546 return -EINVAL;
3547 err = mddev->pers->start_reshape(mddev);
3548 if (err)
3549 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003550 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003551 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003552 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003553 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003554 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003555 return -EINVAL;
3556 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3557 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003558 }
NeilBrown03c902e2006-01-06 00:20:46 -08003559 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003560 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003561 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003562 return len;
3563}
3564
NeilBrown9d888832005-11-08 21:39:26 -08003565static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003566mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003567{
3568 return sprintf(page, "%llu\n",
3569 (unsigned long long) mddev->resync_mismatches);
3570}
3571
NeilBrown80ca3a42006-07-10 04:44:18 -07003572static struct md_sysfs_entry md_scan_mode =
3573__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003574
NeilBrown96de1e62005-11-08 21:39:39 -08003575
NeilBrown80ca3a42006-07-10 04:44:18 -07003576static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003577
NeilBrown88202a02006-01-06 00:21:36 -08003578static ssize_t
3579sync_min_show(mddev_t *mddev, char *page)
3580{
3581 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3582 mddev->sync_speed_min ? "local": "system");
3583}
3584
3585static ssize_t
3586sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3587{
3588 int min;
3589 char *e;
3590 if (strncmp(buf, "system", 6)==0) {
3591 mddev->sync_speed_min = 0;
3592 return len;
3593 }
3594 min = simple_strtoul(buf, &e, 10);
3595 if (buf == e || (*e && *e != '\n') || min <= 0)
3596 return -EINVAL;
3597 mddev->sync_speed_min = min;
3598 return len;
3599}
3600
3601static struct md_sysfs_entry md_sync_min =
3602__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3603
3604static ssize_t
3605sync_max_show(mddev_t *mddev, char *page)
3606{
3607 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3608 mddev->sync_speed_max ? "local": "system");
3609}
3610
3611static ssize_t
3612sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3613{
3614 int max;
3615 char *e;
3616 if (strncmp(buf, "system", 6)==0) {
3617 mddev->sync_speed_max = 0;
3618 return len;
3619 }
3620 max = simple_strtoul(buf, &e, 10);
3621 if (buf == e || (*e && *e != '\n') || max <= 0)
3622 return -EINVAL;
3623 mddev->sync_speed_max = max;
3624 return len;
3625}
3626
3627static struct md_sysfs_entry md_sync_max =
3628__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3629
Iustin Popd7f3d292007-10-16 23:30:54 -07003630static ssize_t
3631degraded_show(mddev_t *mddev, char *page)
3632{
3633 return sprintf(page, "%d\n", mddev->degraded);
3634}
3635static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003636
3637static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003638sync_force_parallel_show(mddev_t *mddev, char *page)
3639{
3640 return sprintf(page, "%d\n", mddev->parallel_resync);
3641}
3642
3643static ssize_t
3644sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3645{
3646 long n;
3647
3648 if (strict_strtol(buf, 10, &n))
3649 return -EINVAL;
3650
3651 if (n != 0 && n != 1)
3652 return -EINVAL;
3653
3654 mddev->parallel_resync = n;
3655
3656 if (mddev->sync_thread)
3657 wake_up(&resync_wait);
3658
3659 return len;
3660}
3661
3662/* force parallel resync, even with shared block devices */
3663static struct md_sysfs_entry md_sync_force_parallel =
3664__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3665 sync_force_parallel_show, sync_force_parallel_store);
3666
3667static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003668sync_speed_show(mddev_t *mddev, char *page)
3669{
3670 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003671 if (mddev->curr_resync == 0)
3672 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003673 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3674 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003675 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003676 db = resync - mddev->resync_mark_cnt;
3677 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003678}
3679
NeilBrown80ca3a42006-07-10 04:44:18 -07003680static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003681
3682static ssize_t
3683sync_completed_show(mddev_t *mddev, char *page)
3684{
Andre Noll58c0fed2009-03-31 14:33:13 +11003685 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003686
NeilBrownacb180b2009-04-14 16:28:34 +10003687 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3688 return sprintf(page, "none\n");
3689
NeilBrown88202a02006-01-06 00:21:36 -08003690 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003691 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003692 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003693 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003694
NeilBrownacb180b2009-04-14 16:28:34 +10003695 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003696 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003697}
3698
NeilBrown80ca3a42006-07-10 04:44:18 -07003699static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003700
NeilBrowne464eaf2006-03-27 01:18:14 -08003701static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003702min_sync_show(mddev_t *mddev, char *page)
3703{
3704 return sprintf(page, "%llu\n",
3705 (unsigned long long)mddev->resync_min);
3706}
3707static ssize_t
3708min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3709{
3710 unsigned long long min;
3711 if (strict_strtoull(buf, 10, &min))
3712 return -EINVAL;
3713 if (min > mddev->resync_max)
3714 return -EINVAL;
3715 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3716 return -EBUSY;
3717
3718 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003719 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003720 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003721 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003722 return -EINVAL;
3723 }
3724 mddev->resync_min = min;
3725
3726 return len;
3727}
3728
3729static struct md_sysfs_entry md_min_sync =
3730__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3731
3732static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003733max_sync_show(mddev_t *mddev, char *page)
3734{
3735 if (mddev->resync_max == MaxSector)
3736 return sprintf(page, "max\n");
3737 else
3738 return sprintf(page, "%llu\n",
3739 (unsigned long long)mddev->resync_max);
3740}
3741static ssize_t
3742max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3743{
3744 if (strncmp(buf, "max", 3) == 0)
3745 mddev->resync_max = MaxSector;
3746 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003747 unsigned long long max;
3748 if (strict_strtoull(buf, 10, &max))
3749 return -EINVAL;
3750 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003751 return -EINVAL;
3752 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003753 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003754 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3755 return -EBUSY;
3756
3757 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003758 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003759 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003760 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003761 return -EINVAL;
3762 }
3763 mddev->resync_max = max;
3764 }
3765 wake_up(&mddev->recovery_wait);
3766 return len;
3767}
3768
3769static struct md_sysfs_entry md_max_sync =
3770__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3771
3772static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003773suspend_lo_show(mddev_t *mddev, char *page)
3774{
3775 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3776}
3777
3778static ssize_t
3779suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3780{
3781 char *e;
3782 unsigned long long new = simple_strtoull(buf, &e, 10);
3783
NeilBrownb8d966e2009-07-01 11:14:04 +10003784 if (mddev->pers == NULL ||
3785 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003786 return -EINVAL;
3787 if (buf == e || (*e && *e != '\n'))
3788 return -EINVAL;
3789 if (new >= mddev->suspend_hi ||
3790 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3791 mddev->suspend_lo = new;
3792 mddev->pers->quiesce(mddev, 2);
3793 return len;
3794 } else
3795 return -EINVAL;
3796}
3797static struct md_sysfs_entry md_suspend_lo =
3798__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3799
3800
3801static ssize_t
3802suspend_hi_show(mddev_t *mddev, char *page)
3803{
3804 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3805}
3806
3807static ssize_t
3808suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3809{
3810 char *e;
3811 unsigned long long new = simple_strtoull(buf, &e, 10);
3812
NeilBrownb8d966e2009-07-01 11:14:04 +10003813 if (mddev->pers == NULL ||
3814 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003815 return -EINVAL;
3816 if (buf == e || (*e && *e != '\n'))
3817 return -EINVAL;
3818 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3819 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3820 mddev->suspend_hi = new;
3821 mddev->pers->quiesce(mddev, 1);
3822 mddev->pers->quiesce(mddev, 0);
3823 return len;
3824 } else
3825 return -EINVAL;
3826}
3827static struct md_sysfs_entry md_suspend_hi =
3828__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3829
NeilBrown08a02ec2007-05-09 02:35:38 -07003830static ssize_t
3831reshape_position_show(mddev_t *mddev, char *page)
3832{
3833 if (mddev->reshape_position != MaxSector)
3834 return sprintf(page, "%llu\n",
3835 (unsigned long long)mddev->reshape_position);
3836 strcpy(page, "none\n");
3837 return 5;
3838}
3839
3840static ssize_t
3841reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3842{
3843 char *e;
3844 unsigned long long new = simple_strtoull(buf, &e, 10);
3845 if (mddev->pers)
3846 return -EBUSY;
3847 if (buf == e || (*e && *e != '\n'))
3848 return -EINVAL;
3849 mddev->reshape_position = new;
3850 mddev->delta_disks = 0;
3851 mddev->new_level = mddev->level;
3852 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003853 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07003854 return len;
3855}
3856
3857static struct md_sysfs_entry md_reshape_position =
3858__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3859 reshape_position_store);
3860
Dan Williamsb522adc2009-03-31 15:00:31 +11003861static ssize_t
3862array_size_show(mddev_t *mddev, char *page)
3863{
3864 if (mddev->external_size)
3865 return sprintf(page, "%llu\n",
3866 (unsigned long long)mddev->array_sectors/2);
3867 else
3868 return sprintf(page, "default\n");
3869}
3870
3871static ssize_t
3872array_size_store(mddev_t *mddev, const char *buf, size_t len)
3873{
3874 sector_t sectors;
3875
3876 if (strncmp(buf, "default", 7) == 0) {
3877 if (mddev->pers)
3878 sectors = mddev->pers->size(mddev, 0, 0);
3879 else
3880 sectors = mddev->array_sectors;
3881
3882 mddev->external_size = 0;
3883 } else {
3884 if (strict_blocks_to_sectors(buf, &sectors) < 0)
3885 return -EINVAL;
3886 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10003887 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11003888
3889 mddev->external_size = 1;
3890 }
3891
3892 mddev->array_sectors = sectors;
3893 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10003894 if (mddev->pers)
3895 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11003896
3897 return len;
3898}
3899
3900static struct md_sysfs_entry md_array_size =
3901__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
3902 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08003903
NeilBrowneae17012005-11-08 21:39:23 -08003904static struct attribute *md_default_attrs[] = {
3905 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003906 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003907 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003908 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003909 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003910 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003911 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08003912 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003913 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003914 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003915 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11003916 &md_array_size.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003917 NULL,
3918};
3919
3920static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003921 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003922 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003923 &md_sync_min.attr,
3924 &md_sync_max.attr,
3925 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003926 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003927 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003928 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003929 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003930 &md_suspend_lo.attr,
3931 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003932 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003933 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003934 NULL,
3935};
NeilBrown411036f2005-11-08 21:39:40 -08003936static struct attribute_group md_redundancy_group = {
3937 .name = NULL,
3938 .attrs = md_redundancy_attrs,
3939};
3940
NeilBrowneae17012005-11-08 21:39:23 -08003941
3942static ssize_t
3943md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3944{
3945 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3946 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003947 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003948
3949 if (!entry->show)
3950 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003951 rv = mddev_lock(mddev);
3952 if (!rv) {
3953 rv = entry->show(mddev, page);
3954 mddev_unlock(mddev);
3955 }
NeilBrown96de1e62005-11-08 21:39:39 -08003956 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003957}
3958
3959static ssize_t
3960md_attr_store(struct kobject *kobj, struct attribute *attr,
3961 const char *page, size_t length)
3962{
3963 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3964 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003965 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003966
3967 if (!entry->store)
3968 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003969 if (!capable(CAP_SYS_ADMIN))
3970 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003971 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11003972 if (mddev->hold_active == UNTIL_IOCTL)
3973 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003974 if (!rv) {
3975 rv = entry->store(mddev, page, length);
3976 mddev_unlock(mddev);
3977 }
NeilBrown96de1e62005-11-08 21:39:39 -08003978 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003979}
3980
3981static void md_free(struct kobject *ko)
3982{
3983 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11003984
3985 if (mddev->sysfs_state)
3986 sysfs_put(mddev->sysfs_state);
3987
3988 if (mddev->gendisk) {
3989 del_gendisk(mddev->gendisk);
3990 put_disk(mddev->gendisk);
3991 }
3992 if (mddev->queue)
3993 blk_cleanup_queue(mddev->queue);
3994
NeilBrowneae17012005-11-08 21:39:23 -08003995 kfree(mddev);
3996}
3997
3998static struct sysfs_ops md_sysfs_ops = {
3999 .show = md_attr_show,
4000 .store = md_attr_store,
4001};
4002static struct kobj_type md_ktype = {
4003 .release = md_free,
4004 .sysfs_ops = &md_sysfs_ops,
4005 .default_attrs = md_default_attrs,
4006};
4007
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008int mdp_major = 0;
4009
Dan Williams5fd3a172009-03-04 00:57:25 -07004010static void mddev_delayed_delete(struct work_struct *ws)
4011{
4012 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4013
4014 if (mddev->private == &md_redundancy_group) {
4015 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
4016 if (mddev->sysfs_action)
4017 sysfs_put(mddev->sysfs_action);
4018 mddev->sysfs_action = NULL;
4019 mddev->private = NULL;
4020 }
4021 kobject_del(&mddev->kobj);
4022 kobject_put(&mddev->kobj);
4023}
4024
NeilBrownefeb53c2009-01-09 08:31:10 +11004025static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004027 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 mddev_t *mddev = mddev_find(dev);
4029 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004030 int partitioned;
4031 int shift;
4032 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004033 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
4035 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004036 return -ENODEV;
4037
4038 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4039 shift = partitioned ? MdpMinorShift : 0;
4040 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
NeilBrownd3374822009-01-09 08:31:10 +11004042 /* wait for any previous instance if this device
4043 * to be completed removed (mddev_delayed_delete).
4044 */
4045 flush_scheduled_work();
4046
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004047 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004048 error = -EEXIST;
4049 if (mddev->gendisk)
4050 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004051
4052 if (name) {
4053 /* Need to ensure that 'name' is not a duplicate.
4054 */
4055 mddev_t *mddev2;
4056 spin_lock(&all_mddevs_lock);
4057
4058 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4059 if (mddev2->gendisk &&
4060 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4061 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004062 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004063 }
4064 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 }
NeilBrown8b765392009-01-09 08:31:08 +11004066
NeilBrown0909dc42009-07-01 12:27:21 +10004067 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004068 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004069 if (!mddev->queue)
4070 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004071 mddev->queue->queuedata = mddev;
4072
NeilBrown8b765392009-01-09 08:31:08 +11004073 /* Can be unlocked because the queue is new: no concurrency */
4074 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4075
NeilBrown409c57f2009-03-31 14:39:39 +11004076 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004077
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 disk = alloc_disk(1 << shift);
4079 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004080 blk_cleanup_queue(mddev->queue);
4081 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004082 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004084 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004086 if (name)
4087 strcpy(disk->disk_name, name);
4088 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004090 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 disk->fops = &md_fops;
4093 disk->private_data = mddev;
4094 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004095 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004096 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004097 * remove it now.
4098 */
4099 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 add_disk(disk);
4101 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004102 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4103 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004104 if (error) {
4105 /* This isn't possible, but as kobject_init_and_add is marked
4106 * __must_check, we must do something with the result
4107 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004108 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4109 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004110 error = 0;
4111 }
4112 abort:
4113 mutex_unlock(&disks_mutex);
4114 if (!error) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004115 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11004116 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
4117 }
NeilBrownd3374822009-01-09 08:31:10 +11004118 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004119 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004120}
4121
4122static struct kobject *md_probe(dev_t dev, int *part, void *data)
4123{
4124 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 return NULL;
4126}
4127
NeilBrownefeb53c2009-01-09 08:31:10 +11004128static int add_named_array(const char *val, struct kernel_param *kp)
4129{
4130 /* val must be "md_*" where * is not all digits.
4131 * We allocate an array with a large free minor number, and
4132 * set the name to val. val must not already be an active name.
4133 */
4134 int len = strlen(val);
4135 char buf[DISK_NAME_LEN];
4136
4137 while (len && val[len-1] == '\n')
4138 len--;
4139 if (len >= DISK_NAME_LEN)
4140 return -E2BIG;
4141 strlcpy(buf, val, len+1);
4142 if (strncmp(buf, "md_", 3) != 0)
4143 return -EINVAL;
4144 return md_alloc(0, buf);
4145}
4146
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147static void md_safemode_timeout(unsigned long data)
4148{
4149 mddev_t *mddev = (mddev_t *) data;
4150
Neil Brown0fd62b82008-06-28 08:31:36 +10004151 if (!atomic_read(&mddev->writes_pending)) {
4152 mddev->safemode = 1;
4153 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11004154 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 md_wakeup_thread(mddev->thread);
4157}
4158
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004159static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160
4161static int do_md_run(mddev_t * mddev)
4162{
NeilBrown2604b702006-01-06 00:20:36 -08004163 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 mdk_rdev_t *rdev;
4165 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08004166 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
NeilBrowna757e642005-04-16 15:26:42 -07004168 if (list_empty(&mddev->disks))
4169 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
4172 if (mddev->pers)
4173 return -EBUSY;
4174
4175 /*
4176 * Analyze all RAID superblock(s)
4177 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004178 if (!mddev->raid_disks) {
4179 if (!mddev->persistent)
4180 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004181 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183
NeilBrownd9d166c2006-01-06 00:20:51 -08004184 if (mddev->level != LEVEL_NONE)
4185 request_module("md-level-%d", mddev->level);
4186 else if (mddev->clevel[0])
4187 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
4189 /*
4190 * Drop all container device buffers, from now on
4191 * the only valid external interface is through the md
4192 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004194 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004195 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 continue;
4197 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004198 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004199
4200 /* perform some consistency tests on the device.
4201 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004202 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004203 */
Andre Noll0f420352008-07-11 22:02:23 +10004204 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004205 if (mddev->dev_sectors &&
4206 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004207 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004208 printk("md: %s: data overlaps metadata\n",
4209 mdname(mddev));
4210 return -EINVAL;
4211 }
4212 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004213 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004214 > rdev->data_offset) {
4215 printk("md: %s: metadata overlaps data\n",
4216 mdname(mddev));
4217 return -EINVAL;
4218 }
4219 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004220 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 }
4222
4223 md_probe(mddev->unit, NULL, NULL);
4224 disk = mddev->gendisk;
4225 if (!disk)
4226 return -ENOMEM;
4227
4228 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004229 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004230 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004232 if (mddev->level != LEVEL_NONE)
4233 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4234 mddev->level);
4235 else
4236 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4237 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 return -EINVAL;
4239 }
NeilBrown2604b702006-01-06 00:20:36 -08004240 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004242 if (mddev->level != pers->level) {
4243 mddev->level = pers->level;
4244 mddev->new_level = pers->level;
4245 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004246 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247
NeilBrownf6705572006-03-27 01:18:11 -08004248 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004249 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004250 /* This personality cannot handle reshaping... */
4251 mddev->pers = NULL;
4252 module_put(pers->owner);
4253 return -EINVAL;
4254 }
4255
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004256 if (pers->sync_request) {
4257 /* Warn if this is a potentially silly
4258 * configuration.
4259 */
4260 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4261 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004262 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004263
4264 list_for_each_entry(rdev, &mddev->disks, same_set)
4265 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004266 if (rdev < rdev2 &&
4267 rdev->bdev->bd_contains ==
4268 rdev2->bdev->bd_contains) {
4269 printk(KERN_WARNING
4270 "%s: WARNING: %s appears to be"
4271 " on the same physical disk as"
4272 " %s.\n",
4273 mdname(mddev),
4274 bdevname(rdev->bdev,b),
4275 bdevname(rdev2->bdev,b2));
4276 warned = 1;
4277 }
4278 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004279
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004280 if (warned)
4281 printk(KERN_WARNING
4282 "True protection against single-disk"
4283 " failure might be compromised.\n");
4284 }
4285
NeilBrown657390d2005-08-26 18:34:16 -07004286 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004287 /* may be over-ridden by personality */
4288 mddev->resync_max_sectors = mddev->dev_sectors;
4289
NeilBrowna9701a32005-11-08 21:39:34 -08004290 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004291 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
NeilBrownf91de922005-11-08 21:39:36 -08004293 if (start_readonly)
4294 mddev->ro = 2; /* read-only, but switch on first write */
4295
NeilBrownb15c2e52006-01-06 00:20:16 -08004296 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004297 if (err)
4298 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004299 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4300 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4301 " but 'external_size' not in effect?\n", __func__);
4302 printk(KERN_ERR
4303 "md: invalid array_size %llu > default size %llu\n",
4304 (unsigned long long)mddev->array_sectors / 2,
4305 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4306 err = -EINVAL;
4307 mddev->pers->stop(mddev);
4308 }
4309 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004310 err = bitmap_create(mddev);
4311 if (err) {
4312 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4313 mdname(mddev), err);
4314 mddev->pers->stop(mddev);
4315 }
4316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 module_put(mddev->pers->owner);
4319 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004320 bitmap_destroy(mddev);
4321 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004323 if (mddev->pers->sync_request) {
4324 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4325 printk(KERN_WARNING
4326 "md: cannot register extra attributes for %s\n",
4327 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004328 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004329 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004330 mddev->ro = 0;
4331
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 atomic_set(&mddev->writes_pending,0);
4333 mddev->safemode = 0;
4334 mddev->safemode_timer.function = md_safemode_timeout;
4335 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004336 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004338
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004339 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004340 if (rdev->raid_disk >= 0) {
4341 char nm[20];
4342 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004343 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4344 printk("md: cannot register %s for %s\n",
4345 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004346 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
4348 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4349
NeilBrown850b2b42006-10-03 01:15:46 -07004350 if (mddev->flags)
4351 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
Andre Nollf233ea52008-07-21 17:05:22 +10004353 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004355 /* If there is a partially-recovered drive we need to
4356 * start recovery here. If we leave it to md_check_recovery,
4357 * it will remove the drives and not do the right thing
4358 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07004359 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004360 int spares = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004361 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004362 if (rdev->raid_disk >= 0 &&
4363 !test_bit(In_sync, &rdev->flags) &&
4364 !test_bit(Faulty, &rdev->flags))
4365 /* complete an interrupted recovery */
4366 spares++;
4367 if (spares && mddev->pers->sync_request) {
4368 mddev->recovery = 0;
4369 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4370 mddev->sync_thread = md_register_thread(md_do_sync,
4371 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10004372 "resync");
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004373 if (!mddev->sync_thread) {
4374 printk(KERN_ERR "%s: could not start resync"
4375 " thread...\n",
4376 mdname(mddev));
4377 /* leave the spares where they are, it shouldn't hurt */
4378 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07004379 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004380 }
4381 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07004382 md_wakeup_thread(mddev->thread);
4383 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004384
NeilBrown449aad32009-08-03 10:59:58 +10004385 revalidate_disk(mddev->gendisk);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004386 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08004387 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004388 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004389 if (mddev->sysfs_action)
4390 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004391 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09004392 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 return 0;
4394}
4395
4396static int restart_array(mddev_t *mddev)
4397{
4398 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399
Andre Noll80fab1d2008-07-11 22:02:21 +10004400 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004402 return -ENXIO;
4403 if (!mddev->pers)
4404 return -EINVAL;
4405 if (!mddev->ro)
4406 return -EBUSY;
4407 mddev->safemode = 0;
4408 mddev->ro = 0;
4409 set_disk_ro(disk, 0);
4410 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4411 mdname(mddev));
4412 /* Kick recovery or resync if necessary */
4413 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4414 md_wakeup_thread(mddev->thread);
4415 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004416 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004417 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418}
4419
NeilBrownacc55e22006-06-26 00:27:47 -07004420/* similar to deny_write_access, but accounts for our holding a reference
4421 * to the file ourselves */
4422static int deny_bitmap_write_access(struct file * file)
4423{
4424 struct inode *inode = file->f_mapping->host;
4425
4426 spin_lock(&inode->i_lock);
4427 if (atomic_read(&inode->i_writecount) > 1) {
4428 spin_unlock(&inode->i_lock);
4429 return -ETXTBSY;
4430 }
4431 atomic_set(&inode->i_writecount, -1);
4432 spin_unlock(&inode->i_lock);
4433
4434 return 0;
4435}
4436
4437static void restore_bitmap_write_access(struct file *file)
4438{
4439 struct inode *inode = file->f_mapping->host;
4440
4441 spin_lock(&inode->i_lock);
4442 atomic_set(&inode->i_writecount, 1);
4443 spin_unlock(&inode->i_lock);
4444}
4445
NeilBrown9e653b62006-06-26 00:27:58 -07004446/* mode:
4447 * 0 - completely stop and dis-assemble array
4448 * 1 - switch to readonly
4449 * 2 - stop but do not disassemble array
4450 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004451static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452{
4453 int err = 0;
4454 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004455 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456
NeilBrownc8c00a62009-08-10 12:50:52 +10004457 mutex_lock(&mddev->open_mutex);
NeilBrownf2ea68c2008-07-21 17:05:25 +10004458 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004459 printk("md: %s still in use.\n",mdname(mddev));
NeilBrownc8c00a62009-08-10 12:50:52 +10004460 err = -EBUSY;
4461 } else if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
4463 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004464 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4466 md_unregister_thread(mddev->sync_thread);
4467 mddev->sync_thread = NULL;
4468 }
4469
4470 del_timer_sync(&mddev->safemode_timer);
4471
NeilBrown9e653b62006-06-26 00:27:58 -07004472 switch(mode) {
4473 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004475 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 goto out;
4477 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004478 break;
4479 case 0: /* disassemble */
4480 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004481 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004482 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 if (mddev->ro)
4484 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004485
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004487 mddev->queue->merge_bvec_fn = NULL;
4488 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004489 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 module_put(mddev->pers->owner);
Dan Williams5fd3a172009-03-04 00:57:25 -07004491 if (mddev->pers->sync_request)
4492 mddev->private = &md_redundancy_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004494 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004495 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004496
NeilBrownc4647292009-05-07 12:51:06 +10004497 list_for_each_entry(rdev, &mddev->disks, same_set)
4498 if (rdev->raid_disk >= 0) {
4499 char nm[20];
4500 sprintf(nm, "rd%d", rdev->raid_disk);
4501 sysfs_remove_link(&mddev->kobj, nm);
4502 }
4503
NeilBrown0d4ca602006-12-10 02:20:44 -08004504 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004505 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08004506
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 if (mddev->ro)
4508 mddev->ro = 0;
4509 }
NeilBrown850b2b42006-10-03 01:15:46 -07004510 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 /* mark array as shutdown cleanly */
4512 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07004513 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 }
NeilBrown9e653b62006-06-26 00:27:58 -07004515 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004517 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown80ffb3c2009-08-18 10:35:26 +10004518 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 }
NeilBrownc8c00a62009-08-10 12:50:52 +10004520out:
4521 mutex_unlock(&mddev->open_mutex);
4522 if (err)
4523 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 /*
4525 * Free resources if final stop
4526 */
NeilBrown9e653b62006-06-26 00:27:58 -07004527 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004528
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4530
NeilBrown978f9462006-02-02 14:28:05 -08004531 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004532 if (mddev->bitmap_info.file) {
4533 restore_bitmap_write_access(mddev->bitmap_info.file);
4534 fput(mddev->bitmap_info.file);
4535 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004536 }
NeilBrownc3d97142009-12-14 12:49:52 +11004537 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004538
NeilBrown177a99b2008-02-06 01:39:56 -08004539 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07004540 flush_scheduled_work();
4541
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 export_array(mddev);
4543
Andre Nollf233ea52008-07-21 17:05:22 +10004544 mddev->array_sectors = 0;
Dan Williamsb522adc2009-03-31 15:00:31 +11004545 mddev->external_size = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004546 mddev->dev_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004547 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004548 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004549 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004550 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004551 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004552 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004553 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004554 mddev->level = LEVEL_NONE;
4555 mddev->clevel[0] = 0;
4556 mddev->flags = 0;
4557 mddev->ro = 0;
4558 mddev->metadata_type[0] = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10004559 mddev->chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004560 mddev->ctime = mddev->utime = 0;
4561 mddev->layout = 0;
4562 mddev->max_disks = 0;
4563 mddev->events = 0;
4564 mddev->delta_disks = 0;
4565 mddev->new_level = LEVEL_NONE;
4566 mddev->new_layout = 0;
Andre Noll664e7c42009-06-18 08:45:27 +10004567 mddev->new_chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004568 mddev->curr_resync = 0;
4569 mddev->resync_mismatches = 0;
4570 mddev->suspend_lo = mddev->suspend_hi = 0;
4571 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4572 mddev->recovery = 0;
4573 mddev->in_sync = 0;
4574 mddev->changed = 0;
4575 mddev->degraded = 0;
4576 mddev->barriers_work = 0;
4577 mddev->safemode = 0;
NeilBrownc3d97142009-12-14 12:49:52 +11004578 mddev->bitmap_info.offset = 0;
4579 mddev->bitmap_info.default_offset = 0;
NeilBrown42a04b52009-12-14 12:49:53 +11004580 mddev->bitmap_info.chunksize = 0;
4581 mddev->bitmap_info.daemon_sleep = 0;
4582 mddev->bitmap_info.max_write_behind = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004583 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004584 if (mddev->hold_active == UNTIL_STOP)
4585 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004586
NeilBrowna8a55c32006-06-26 00:27:39 -07004587 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4589 mdname(mddev));
4590 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004591 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004592 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004593 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 return err;
4595}
4596
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004597#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598static void autorun_array(mddev_t *mddev)
4599{
4600 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 int err;
4602
NeilBrowna757e642005-04-16 15:26:42 -07004603 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605
4606 printk(KERN_INFO "md: running: ");
4607
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004608 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 char b[BDEVNAME_SIZE];
4610 printk("<%s>", bdevname(rdev->bdev,b));
4611 }
4612 printk("\n");
4613
NeilBrownd710e132008-10-13 11:55:12 +11004614 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 if (err) {
4616 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004617 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 }
4619}
4620
4621/*
4622 * lets try to run arrays based on all disks that have arrived
4623 * until now. (those are in pending_raid_disks)
4624 *
4625 * the method: pick the first pending disk, collect all disks with
4626 * the same UUID, remove all from the pending list and put them into
4627 * the 'same_array' list. Then order this list based on superblock
4628 * update time (freshest comes first), kick out 'old' disks and
4629 * compare superblocks. If everything's fine then run it.
4630 *
4631 * If "unit" is allocated, then bump its reference count
4632 */
4633static void autorun_devices(int part)
4634{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004635 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 mddev_t *mddev;
4637 char b[BDEVNAME_SIZE];
4638
4639 printk(KERN_INFO "md: autorun ...\n");
4640 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004641 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004643 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 rdev0 = list_entry(pending_raid_disks.next,
4645 mdk_rdev_t, same_set);
4646
4647 printk(KERN_INFO "md: considering %s ...\n",
4648 bdevname(rdev0->bdev,b));
4649 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004650 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 if (super_90_load(rdev, rdev0, 0) >= 0) {
4652 printk(KERN_INFO "md: adding %s ...\n",
4653 bdevname(rdev->bdev,b));
4654 list_move(&rdev->same_set, &candidates);
4655 }
4656 /*
4657 * now we have a set of devices, with all of them having
4658 * mostly sane superblocks. It's time to allocate the
4659 * mddev.
4660 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004661 if (part) {
4662 dev = MKDEV(mdp_major,
4663 rdev0->preferred_minor << MdpMinorShift);
4664 unit = MINOR(dev) >> MdpMinorShift;
4665 } else {
4666 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4667 unit = MINOR(dev);
4668 }
4669 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4671 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4672 break;
4673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674
4675 md_probe(dev, NULL, NULL);
4676 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004677 if (!mddev || !mddev->gendisk) {
4678 if (mddev)
4679 mddev_put(mddev);
4680 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 "md: cannot allocate memory for md drive.\n");
4682 break;
4683 }
4684 if (mddev_lock(mddev))
4685 printk(KERN_WARNING "md: %s locked, cannot run\n",
4686 mdname(mddev));
4687 else if (mddev->raid_disks || mddev->major_version
4688 || !list_empty(&mddev->disks)) {
4689 printk(KERN_WARNING
4690 "md: %s already running, cannot run %s\n",
4691 mdname(mddev), bdevname(rdev0->bdev,b));
4692 mddev_unlock(mddev);
4693 } else {
4694 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004695 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004696 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 list_del_init(&rdev->same_set);
4698 if (bind_rdev_to_array(rdev, mddev))
4699 export_rdev(rdev);
4700 }
4701 autorun_array(mddev);
4702 mddev_unlock(mddev);
4703 }
4704 /* on success, candidates will be empty, on error
4705 * it won't...
4706 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004707 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004708 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 mddev_put(mddev);
4712 }
4713 printk(KERN_INFO "md: ... autorun DONE.\n");
4714}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004715#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717static int get_version(void __user * arg)
4718{
4719 mdu_version_t ver;
4720
4721 ver.major = MD_MAJOR_VERSION;
4722 ver.minor = MD_MINOR_VERSION;
4723 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4724
4725 if (copy_to_user(arg, &ver, sizeof(ver)))
4726 return -EFAULT;
4727
4728 return 0;
4729}
4730
4731static int get_array_info(mddev_t * mddev, void __user * arg)
4732{
4733 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004734 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
NeilBrowna9f326e2009-09-23 18:06:41 +10004737 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004738 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004740 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 failed++;
4742 else {
4743 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004744 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004745 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 else
4747 spare++;
4748 }
4749 }
4750
4751 info.major_version = mddev->major_version;
4752 info.minor_version = mddev->minor_version;
4753 info.patch_version = MD_PATCHLEVEL_VERSION;
4754 info.ctime = mddev->ctime;
4755 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004756 info.size = mddev->dev_sectors / 2;
4757 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004758 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 info.nr_disks = nr;
4760 info.raid_disks = mddev->raid_disks;
4761 info.md_minor = mddev->md_minor;
4762 info.not_persistent= !mddev->persistent;
4763
4764 info.utime = mddev->utime;
4765 info.state = 0;
4766 if (mddev->in_sync)
4767 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11004768 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07004769 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004770 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 info.working_disks = working;
4772 info.failed_disks = failed;
4773 info.spare_disks = spare;
4774
4775 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004776 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777
4778 if (copy_to_user(arg, &info, sizeof(info)))
4779 return -EFAULT;
4780
4781 return 0;
4782}
4783
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004784static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004785{
4786 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4787 char *ptr, *buf = NULL;
4788 int err = -ENOMEM;
4789
Dan Williamsb5470dc2008-06-27 21:44:04 -07004790 if (md_allow_write(mddev))
4791 file = kmalloc(sizeof(*file), GFP_NOIO);
4792 else
4793 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004794
NeilBrown32a76272005-06-21 17:17:14 -07004795 if (!file)
4796 goto out;
4797
4798 /* bitmap disabled, zero the first byte and copy out */
4799 if (!mddev->bitmap || !mddev->bitmap->file) {
4800 file->pathname[0] = '\0';
4801 goto copy_out;
4802 }
4803
4804 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4805 if (!buf)
4806 goto out;
4807
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004808 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4809 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004810 goto out;
4811
4812 strcpy(file->pathname, ptr);
4813
4814copy_out:
4815 err = 0;
4816 if (copy_to_user(arg, file, sizeof(*file)))
4817 err = -EFAULT;
4818out:
4819 kfree(buf);
4820 kfree(file);
4821 return err;
4822}
4823
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824static int get_disk_info(mddev_t * mddev, void __user * arg)
4825{
4826 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 mdk_rdev_t *rdev;
4828
4829 if (copy_from_user(&info, arg, sizeof(info)))
4830 return -EFAULT;
4831
Andre Noll26ef3792008-07-11 22:02:21 +10004832 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 if (rdev) {
4834 info.major = MAJOR(rdev->bdev->bd_dev);
4835 info.minor = MINOR(rdev->bdev->bd_dev);
4836 info.raid_disk = rdev->raid_disk;
4837 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004838 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004840 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 info.state |= (1<<MD_DISK_ACTIVE);
4842 info.state |= (1<<MD_DISK_SYNC);
4843 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004844 if (test_bit(WriteMostly, &rdev->flags))
4845 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 } else {
4847 info.major = info.minor = 0;
4848 info.raid_disk = -1;
4849 info.state = (1<<MD_DISK_REMOVED);
4850 }
4851
4852 if (copy_to_user(arg, &info, sizeof(info)))
4853 return -EFAULT;
4854
4855 return 0;
4856}
4857
4858static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4859{
4860 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4861 mdk_rdev_t *rdev;
4862 dev_t dev = MKDEV(info->major,info->minor);
4863
4864 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4865 return -EOVERFLOW;
4866
4867 if (!mddev->raid_disks) {
4868 int err;
4869 /* expecting a device which has a superblock */
4870 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4871 if (IS_ERR(rdev)) {
4872 printk(KERN_WARNING
4873 "md: md_import_device returned %ld\n",
4874 PTR_ERR(rdev));
4875 return PTR_ERR(rdev);
4876 }
4877 if (!list_empty(&mddev->disks)) {
4878 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4879 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10004880 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 .load_super(rdev, rdev0, mddev->minor_version);
4882 if (err < 0) {
4883 printk(KERN_WARNING
4884 "md: %s has different UUID to %s\n",
4885 bdevname(rdev->bdev,b),
4886 bdevname(rdev0->bdev,b2));
4887 export_rdev(rdev);
4888 return -EINVAL;
4889 }
4890 }
4891 err = bind_rdev_to_array(rdev, mddev);
4892 if (err)
4893 export_rdev(rdev);
4894 return err;
4895 }
4896
4897 /*
4898 * add_new_disk can be used once the array is assembled
4899 * to add "hot spares". They must already have a superblock
4900 * written
4901 */
4902 if (mddev->pers) {
4903 int err;
4904 if (!mddev->pers->hot_add_disk) {
4905 printk(KERN_WARNING
4906 "%s: personality does not support diskops!\n",
4907 mdname(mddev));
4908 return -EINVAL;
4909 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004910 if (mddev->persistent)
4911 rdev = md_import_device(dev, mddev->major_version,
4912 mddev->minor_version);
4913 else
4914 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 if (IS_ERR(rdev)) {
4916 printk(KERN_WARNING
4917 "md: md_import_device returned %ld\n",
4918 PTR_ERR(rdev));
4919 return PTR_ERR(rdev);
4920 }
NeilBrown41158c72005-06-21 17:17:25 -07004921 /* set save_raid_disk if appropriate */
4922 if (!mddev->persistent) {
4923 if (info->state & (1<<MD_DISK_SYNC) &&
4924 info->raid_disk < mddev->raid_disks)
4925 rdev->raid_disk = info->raid_disk;
4926 else
4927 rdev->raid_disk = -1;
4928 } else
4929 super_types[mddev->major_version].
4930 validate_super(mddev, rdev);
4931 rdev->saved_raid_disk = rdev->raid_disk;
4932
NeilBrownb2d444d2005-11-08 21:39:31 -08004933 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004934 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4935 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11004936 else
4937 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004938
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 rdev->raid_disk = -1;
4940 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004941 if (!err && !mddev->pers->hot_remove_disk) {
4942 /* If there is hot_add_disk but no hot_remove_disk
4943 * then added disks for geometry changes,
4944 * and should be added immediately.
4945 */
4946 super_types[mddev->major_version].
4947 validate_super(mddev, rdev);
4948 err = mddev->pers->hot_add_disk(mddev, rdev);
4949 if (err)
4950 unbind_rdev_from_array(rdev);
4951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 if (err)
4953 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004954 else
NeilBrown3c0ee632008-10-21 13:25:28 +11004955 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07004956
NeilBrown17571282006-12-10 02:20:52 -08004957 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004958 if (mddev->degraded)
4959 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004960 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004961 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 return err;
4963 }
4964
4965 /* otherwise, add_new_disk is only allowed
4966 * for major_version==0 superblocks
4967 */
4968 if (mddev->major_version != 0) {
4969 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4970 mdname(mddev));
4971 return -EINVAL;
4972 }
4973
4974 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4975 int err;
NeilBrownd710e132008-10-13 11:55:12 +11004976 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 if (IS_ERR(rdev)) {
4978 printk(KERN_WARNING
4979 "md: error, md_import_device() returned %ld\n",
4980 PTR_ERR(rdev));
4981 return PTR_ERR(rdev);
4982 }
4983 rdev->desc_nr = info->number;
4984 if (info->raid_disk < mddev->raid_disks)
4985 rdev->raid_disk = info->raid_disk;
4986 else
4987 rdev->raid_disk = -1;
4988
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004990 if (info->state & (1<<MD_DISK_SYNC))
4991 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004993 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4994 set_bit(WriteMostly, &rdev->flags);
4995
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 if (!mddev->persistent) {
4997 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10004998 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 } else
Andre Noll0f420352008-07-11 22:02:23 +10005000 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10005001 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002
NeilBrown2bf071b2006-01-06 00:20:55 -08005003 err = bind_rdev_to_array(rdev, mddev);
5004 if (err) {
5005 export_rdev(rdev);
5006 return err;
5007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 }
5009
5010 return 0;
5011}
5012
5013static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5014{
5015 char b[BDEVNAME_SIZE];
5016 mdk_rdev_t *rdev;
5017
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 rdev = find_rdev(mddev, dev);
5019 if (!rdev)
5020 return -ENXIO;
5021
5022 if (rdev->raid_disk >= 0)
5023 goto busy;
5024
5025 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005026 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005027 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028
5029 return 0;
5030busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005031 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 bdevname(rdev->bdev,b), mdname(mddev));
5033 return -EBUSY;
5034}
5035
5036static int hot_add_disk(mddev_t * mddev, dev_t dev)
5037{
5038 char b[BDEVNAME_SIZE];
5039 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 mdk_rdev_t *rdev;
5041
5042 if (!mddev->pers)
5043 return -ENODEV;
5044
5045 if (mddev->major_version != 0) {
5046 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5047 " version-0 superblocks.\n",
5048 mdname(mddev));
5049 return -EINVAL;
5050 }
5051 if (!mddev->pers->hot_add_disk) {
5052 printk(KERN_WARNING
5053 "%s: personality does not support diskops!\n",
5054 mdname(mddev));
5055 return -EINVAL;
5056 }
5057
NeilBrownd710e132008-10-13 11:55:12 +11005058 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 if (IS_ERR(rdev)) {
5060 printk(KERN_WARNING
5061 "md: error, md_import_device() returned %ld\n",
5062 PTR_ERR(rdev));
5063 return -EINVAL;
5064 }
5065
5066 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10005067 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 else
Andre Noll0f420352008-07-11 22:02:23 +10005069 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070
NeilBrown8190e752009-06-18 08:48:58 +10005071 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072
NeilBrownb2d444d2005-11-08 21:39:31 -08005073 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 printk(KERN_WARNING
5075 "md: can not hot-add faulty %s disk to %s!\n",
5076 bdevname(rdev->bdev,b), mdname(mddev));
5077 err = -EINVAL;
5078 goto abort_export;
5079 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005080 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005082 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005083 err = bind_rdev_to_array(rdev, mddev);
5084 if (err)
5085 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086
5087 /*
5088 * The rest should better be atomic, we can have disk failures
5089 * noticed in interrupt contexts ...
5090 */
5091
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 rdev->raid_disk = -1;
5093
NeilBrown850b2b42006-10-03 01:15:46 -07005094 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
5096 /*
5097 * Kick recovery, maybe this spare has to be added to the
5098 * array immediately.
5099 */
5100 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5101 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005102 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 return 0;
5104
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105abort_export:
5106 export_rdev(rdev);
5107 return err;
5108}
5109
NeilBrown32a76272005-06-21 17:17:14 -07005110static int set_bitmap_file(mddev_t *mddev, int fd)
5111{
5112 int err;
5113
NeilBrown36fa3062005-09-09 16:23:45 -07005114 if (mddev->pers) {
5115 if (!mddev->pers->quiesce)
5116 return -EBUSY;
5117 if (mddev->recovery || mddev->sync_thread)
5118 return -EBUSY;
5119 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005120 }
5121
NeilBrown36fa3062005-09-09 16:23:45 -07005122
5123 if (fd >= 0) {
5124 if (mddev->bitmap)
5125 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005126 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005127
NeilBrownc3d97142009-12-14 12:49:52 +11005128 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005129 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5130 mdname(mddev));
5131 return -EBADF;
5132 }
5133
NeilBrownc3d97142009-12-14 12:49:52 +11005134 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005135 if (err) {
5136 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5137 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005138 fput(mddev->bitmap_info.file);
5139 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005140 return err;
5141 }
NeilBrownc3d97142009-12-14 12:49:52 +11005142 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005143 } else if (mddev->bitmap == NULL)
5144 return -ENOENT; /* cannot remove what isn't there */
5145 err = 0;
5146 if (mddev->pers) {
5147 mddev->pers->quiesce(mddev, 1);
5148 if (fd >= 0)
5149 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005150 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005151 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005152 fd = -1; /* make sure to put the file */
5153 }
NeilBrown36fa3062005-09-09 16:23:45 -07005154 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005155 }
5156 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005157 if (mddev->bitmap_info.file) {
5158 restore_bitmap_write_access(mddev->bitmap_info.file);
5159 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005160 }
NeilBrownc3d97142009-12-14 12:49:52 +11005161 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005162 }
5163
NeilBrown32a76272005-06-21 17:17:14 -07005164 return err;
5165}
5166
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167/*
5168 * set_array_info is used two different ways
5169 * The original usage is when creating a new array.
5170 * In this usage, raid_disks is > 0 and it together with
5171 * level, size, not_persistent,layout,chunksize determine the
5172 * shape of the array.
5173 * This will always create an array with a type-0.90.0 superblock.
5174 * The newer usage is when assembling an array.
5175 * In this case raid_disks will be 0, and the major_version field is
5176 * use to determine which style super-blocks are to be found on the devices.
5177 * The minor and patch _version numbers are also kept incase the
5178 * super_block handler wishes to interpret them.
5179 */
5180static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5181{
5182
5183 if (info->raid_disks == 0) {
5184 /* just setting version number for superblock loading */
5185 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005186 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 super_types[info->major_version].name == NULL) {
5188 /* maybe try to auto-load a module? */
5189 printk(KERN_INFO
5190 "md: superblock version %d not known\n",
5191 info->major_version);
5192 return -EINVAL;
5193 }
5194 mddev->major_version = info->major_version;
5195 mddev->minor_version = info->minor_version;
5196 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005197 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 return 0;
5199 }
5200 mddev->major_version = MD_MAJOR_VERSION;
5201 mddev->minor_version = MD_MINOR_VERSION;
5202 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5203 mddev->ctime = get_seconds();
5204
5205 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005206 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005207 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 mddev->raid_disks = info->raid_disks;
5209 /* don't set md_minor, it is determined by which /dev/md* was
5210 * openned
5211 */
5212 if (info->state & (1<<MD_SB_CLEAN))
5213 mddev->recovery_cp = MaxSector;
5214 else
5215 mddev->recovery_cp = 0;
5216 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005217 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218
5219 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005220 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221
5222 mddev->max_disks = MD_SB_DISKS;
5223
NeilBrowne6910632008-02-06 01:39:51 -08005224 if (mddev->persistent)
5225 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005226 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227
NeilBrownc3d97142009-12-14 12:49:52 +11005228 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5229 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005230
NeilBrownf6705572006-03-27 01:18:11 -08005231 mddev->reshape_position = MaxSector;
5232
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 /*
5234 * Generate a 128 bit UUID
5235 */
5236 get_random_bytes(mddev->uuid, 16);
5237
NeilBrownf6705572006-03-27 01:18:11 -08005238 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005239 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005240 mddev->new_layout = mddev->layout;
5241 mddev->delta_disks = 0;
5242
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 return 0;
5244}
5245
Dan Williams1f403622009-03-31 14:59:03 +11005246void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5247{
Dan Williamsb522adc2009-03-31 15:00:31 +11005248 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5249
5250 if (mddev->external_size)
5251 return;
5252
Dan Williams1f403622009-03-31 14:59:03 +11005253 mddev->array_sectors = array_sectors;
5254}
5255EXPORT_SYMBOL(md_set_array_sectors);
5256
Andre Nolld71f9f82008-07-11 22:02:22 +10005257static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005258{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005259 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005260 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005261 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005262
5263 if (mddev->pers->resize == NULL)
5264 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005265 /* The "num_sectors" is the number of sectors of each device that
5266 * is used. This can only make sense for arrays with redundancy.
5267 * linear and raid0 always use whatever space is available. We can only
5268 * consider changing this number if no resync or reconstruction is
5269 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005270 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005271 * of each device. If num_sectors is zero, we find the largest size
5272 * that fits.
5273
NeilBrowna35b0d62006-01-06 00:20:49 -08005274 */
5275 if (mddev->sync_thread)
5276 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005277 if (mddev->bitmap)
5278 /* Sorry, cannot grow a bitmap yet, just remove it,
5279 * grow, and re-add.
5280 */
5281 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005282 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005283 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005284
Andre Nolld71f9f82008-07-11 22:02:22 +10005285 if (fit && (num_sectors == 0 || num_sectors > avail))
5286 num_sectors = avail;
5287 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005288 return -ENOSPC;
5289 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005290 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005291 if (!rv)
5292 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005293 return rv;
5294}
5295
NeilBrownda943b992006-01-06 00:20:54 -08005296static int update_raid_disks(mddev_t *mddev, int raid_disks)
5297{
5298 int rv;
5299 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005300 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005301 return -EINVAL;
5302 if (raid_disks <= 0 ||
5303 raid_disks >= mddev->max_disks)
5304 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005305 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005306 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005307 mddev->delta_disks = raid_disks - mddev->raid_disks;
5308
5309 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005310 return rv;
5311}
5312
5313
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314/*
5315 * update_array_info is used to change the configuration of an
5316 * on-line array.
5317 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5318 * fields in the info are checked against the array.
5319 * Any differences that cannot be handled will cause an error.
5320 * Normally, only one change can be managed at a time.
5321 */
5322static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5323{
5324 int rv = 0;
5325 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005326 int state = 0;
5327
5328 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005329 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005330 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331
5332 if (mddev->major_version != info->major_version ||
5333 mddev->minor_version != info->minor_version ||
5334/* mddev->patch_version != info->patch_version || */
5335 mddev->ctime != info->ctime ||
5336 mddev->level != info->level ||
5337/* mddev->layout != info->layout || */
5338 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005339 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005340 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5341 ((state^info->state) & 0xfffffe00)
5342 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 return -EINVAL;
5344 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005345 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5346 cnt++;
5347 if (mddev->raid_disks != info->raid_disks)
5348 cnt++;
5349 if (mddev->layout != info->layout)
5350 cnt++;
5351 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5352 cnt++;
5353 if (cnt == 0)
5354 return 0;
5355 if (cnt > 1)
5356 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357
5358 if (mddev->layout != info->layout) {
5359 /* Change layout
5360 * we don't need to do anything at the md level, the
5361 * personality will take care of it all.
5362 */
NeilBrown50ac1682009-06-18 08:47:55 +10005363 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005365 else {
5366 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005367 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005368 if (rv)
5369 mddev->new_layout = mddev->layout;
5370 return rv;
5371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005373 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005374 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375
NeilBrownda943b992006-01-06 00:20:54 -08005376 if (mddev->raid_disks != info->raid_disks)
5377 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378
NeilBrown36fa3062005-09-09 16:23:45 -07005379 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5380 if (mddev->pers->quiesce == NULL)
5381 return -EINVAL;
5382 if (mddev->recovery || mddev->sync_thread)
5383 return -EBUSY;
5384 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5385 /* add the bitmap */
5386 if (mddev->bitmap)
5387 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005388 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005389 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005390 mddev->bitmap_info.offset =
5391 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005392 mddev->pers->quiesce(mddev, 1);
5393 rv = bitmap_create(mddev);
5394 if (rv)
5395 bitmap_destroy(mddev);
5396 mddev->pers->quiesce(mddev, 0);
5397 } else {
5398 /* remove the bitmap */
5399 if (!mddev->bitmap)
5400 return -ENOENT;
5401 if (mddev->bitmap->file)
5402 return -EINVAL;
5403 mddev->pers->quiesce(mddev, 1);
5404 bitmap_destroy(mddev);
5405 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005406 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005407 }
5408 }
NeilBrown850b2b42006-10-03 01:15:46 -07005409 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 return rv;
5411}
5412
5413static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5414{
5415 mdk_rdev_t *rdev;
5416
5417 if (mddev->pers == NULL)
5418 return -ENODEV;
5419
5420 rdev = find_rdev(mddev, dev);
5421 if (!rdev)
5422 return -ENODEV;
5423
5424 md_error(mddev, rdev);
5425 return 0;
5426}
5427
Andre Noll2f9618c2008-04-25 18:57:58 +02005428/*
5429 * We have a problem here : there is no easy way to give a CHS
5430 * virtual geometry. We currently pretend that we have a 2 heads
5431 * 4 sectors (with a BIG number of cylinders...). This drives
5432 * dosfs just mad... ;-)
5433 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005434static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5435{
5436 mddev_t *mddev = bdev->bd_disk->private_data;
5437
5438 geo->heads = 2;
5439 geo->sectors = 4;
5440 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5441 return 0;
5442}
5443
Al Viroa39907f2008-03-02 10:31:15 -05005444static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 unsigned int cmd, unsigned long arg)
5446{
5447 int err = 0;
5448 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 mddev_t *mddev = NULL;
5450
5451 if (!capable(CAP_SYS_ADMIN))
5452 return -EACCES;
5453
5454 /*
5455 * Commands dealing with the RAID driver but not any
5456 * particular array:
5457 */
5458 switch (cmd)
5459 {
5460 case RAID_VERSION:
5461 err = get_version(argp);
5462 goto done;
5463
5464 case PRINT_RAID_DEBUG:
5465 err = 0;
5466 md_print_devices();
5467 goto done;
5468
5469#ifndef MODULE
5470 case RAID_AUTORUN:
5471 err = 0;
5472 autostart_arrays(arg);
5473 goto done;
5474#endif
5475 default:;
5476 }
5477
5478 /*
5479 * Commands creating/starting a new array:
5480 */
5481
Al Viroa39907f2008-03-02 10:31:15 -05005482 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483
5484 if (!mddev) {
5485 BUG();
5486 goto abort;
5487 }
5488
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 err = mddev_lock(mddev);
5490 if (err) {
5491 printk(KERN_INFO
5492 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5493 err, cmd);
5494 goto abort;
5495 }
5496
5497 switch (cmd)
5498 {
5499 case SET_ARRAY_INFO:
5500 {
5501 mdu_array_info_t info;
5502 if (!arg)
5503 memset(&info, 0, sizeof(info));
5504 else if (copy_from_user(&info, argp, sizeof(info))) {
5505 err = -EFAULT;
5506 goto abort_unlock;
5507 }
5508 if (mddev->pers) {
5509 err = update_array_info(mddev, &info);
5510 if (err) {
5511 printk(KERN_WARNING "md: couldn't update"
5512 " array info. %d\n", err);
5513 goto abort_unlock;
5514 }
5515 goto done_unlock;
5516 }
5517 if (!list_empty(&mddev->disks)) {
5518 printk(KERN_WARNING
5519 "md: array %s already has disks!\n",
5520 mdname(mddev));
5521 err = -EBUSY;
5522 goto abort_unlock;
5523 }
5524 if (mddev->raid_disks) {
5525 printk(KERN_WARNING
5526 "md: array %s already initialised!\n",
5527 mdname(mddev));
5528 err = -EBUSY;
5529 goto abort_unlock;
5530 }
5531 err = set_array_info(mddev, &info);
5532 if (err) {
5533 printk(KERN_WARNING "md: couldn't set"
5534 " array info. %d\n", err);
5535 goto abort_unlock;
5536 }
5537 }
5538 goto done_unlock;
5539
5540 default:;
5541 }
5542
5543 /*
5544 * Commands querying/configuring an existing array:
5545 */
NeilBrown32a76272005-06-21 17:17:14 -07005546 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005547 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005548 if ((!mddev->raid_disks && !mddev->external)
5549 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5550 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5551 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 err = -ENODEV;
5553 goto abort_unlock;
5554 }
5555
5556 /*
5557 * Commands even a read-only array can execute:
5558 */
5559 switch (cmd)
5560 {
5561 case GET_ARRAY_INFO:
5562 err = get_array_info(mddev, argp);
5563 goto done_unlock;
5564
NeilBrown32a76272005-06-21 17:17:14 -07005565 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005566 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005567 goto done_unlock;
5568
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 case GET_DISK_INFO:
5570 err = get_disk_info(mddev, argp);
5571 goto done_unlock;
5572
5573 case RESTART_ARRAY_RW:
5574 err = restart_array(mddev);
5575 goto done_unlock;
5576
5577 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005578 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 goto done_unlock;
5580
5581 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005582 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 goto done_unlock;
5584
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 }
5586
5587 /*
5588 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005589 * superblock, so we do not allow them on read-only arrays.
5590 * However non-MD ioctls (e.g. get-size) will still come through
5591 * here and hit the 'default' below, so only disallow
5592 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005594 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005595 if (mddev->ro == 2) {
5596 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005597 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005598 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5599 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005600 } else {
5601 err = -EROFS;
5602 goto abort_unlock;
5603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 }
5605
5606 switch (cmd)
5607 {
5608 case ADD_NEW_DISK:
5609 {
5610 mdu_disk_info_t info;
5611 if (copy_from_user(&info, argp, sizeof(info)))
5612 err = -EFAULT;
5613 else
5614 err = add_new_disk(mddev, &info);
5615 goto done_unlock;
5616 }
5617
5618 case HOT_REMOVE_DISK:
5619 err = hot_remove_disk(mddev, new_decode_dev(arg));
5620 goto done_unlock;
5621
5622 case HOT_ADD_DISK:
5623 err = hot_add_disk(mddev, new_decode_dev(arg));
5624 goto done_unlock;
5625
5626 case SET_DISK_FAULTY:
5627 err = set_disk_faulty(mddev, new_decode_dev(arg));
5628 goto done_unlock;
5629
5630 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005631 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 goto done_unlock;
5633
NeilBrown32a76272005-06-21 17:17:14 -07005634 case SET_BITMAP_FILE:
5635 err = set_bitmap_file(mddev, (int)arg);
5636 goto done_unlock;
5637
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 err = -EINVAL;
5640 goto abort_unlock;
5641 }
5642
5643done_unlock:
5644abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005645 if (mddev->hold_active == UNTIL_IOCTL &&
5646 err != -EINVAL)
5647 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 mddev_unlock(mddev);
5649
5650 return err;
5651done:
5652 if (err)
5653 MD_BUG();
5654abort:
5655 return err;
5656}
5657
Al Viroa39907f2008-03-02 10:31:15 -05005658static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659{
5660 /*
5661 * Succeed if we can lock the mddev, which confirms that
5662 * it isn't being stopped right now.
5663 */
NeilBrownd3374822009-01-09 08:31:10 +11005664 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 int err;
5666
NeilBrownd3374822009-01-09 08:31:10 +11005667 if (mddev->gendisk != bdev->bd_disk) {
5668 /* we are racing with mddev_put which is discarding this
5669 * bd_disk.
5670 */
5671 mddev_put(mddev);
5672 /* Wait until bdev->bd_disk is definitely gone */
5673 flush_scheduled_work();
5674 /* Then retry the open from the top */
5675 return -ERESTARTSYS;
5676 }
5677 BUG_ON(mddev != bdev->bd_disk->private_data);
5678
NeilBrownc8c00a62009-08-10 12:50:52 +10005679 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 goto out;
5681
5682 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005683 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005684 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685
Al Viroa39907f2008-03-02 10:31:15 -05005686 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 out:
5688 return err;
5689}
5690
Al Viroa39907f2008-03-02 10:31:15 -05005691static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692{
Al Viroa39907f2008-03-02 10:31:15 -05005693 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005695 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005696 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 mddev_put(mddev);
5698
5699 return 0;
5700}
5701
Linus Torvalds44ce62942007-05-09 18:51:36 -07005702static int md_media_changed(struct gendisk *disk)
5703{
5704 mddev_t *mddev = disk->private_data;
5705
5706 return mddev->changed;
5707}
5708
5709static int md_revalidate(struct gendisk *disk)
5710{
5711 mddev_t *mddev = disk->private_data;
5712
5713 mddev->changed = 0;
5714 return 0;
5715}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005716static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717{
5718 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005719 .open = md_open,
5720 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005721 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005722 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07005723 .media_changed = md_media_changed,
5724 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725};
5726
Adrian Bunk75c96f82005-05-05 16:16:09 -07005727static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728{
5729 mdk_thread_t *thread = arg;
5730
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 /*
5732 * md_thread is a 'system-thread', it's priority should be very
5733 * high. We avoid resource deadlocks individually in each
5734 * raid personality. (RAID5 does preallocation) We also use RR and
5735 * the very same RT priority as kswapd, thus we will never get
5736 * into a priority inversion deadlock.
5737 *
5738 * we definitely have to have equal or higher priority than
5739 * bdflush, otherwise bdflush will deadlock if there are too
5740 * many dirty RAID5 blocks.
5741 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742
NeilBrown6985c432005-10-19 21:23:47 -07005743 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005744 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745
NeilBrown93588e22005-11-15 00:09:12 -08005746 /* We need to wait INTERRUPTIBLE so that
5747 * we don't add to the load-average.
5748 * That means we need to be sure no signals are
5749 * pending
5750 */
5751 if (signal_pending(current))
5752 flush_signals(current);
5753
5754 wait_event_interruptible_timeout
5755 (thread->wqueue,
5756 test_bit(THREAD_WAKEUP, &thread->flags)
5757 || kthread_should_stop(),
5758 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759
5760 clear_bit(THREAD_WAKEUP, &thread->flags);
5761
NeilBrown787453c2005-11-08 21:39:43 -08005762 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005764
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 return 0;
5766}
5767
5768void md_wakeup_thread(mdk_thread_t *thread)
5769{
5770 if (thread) {
5771 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5772 set_bit(THREAD_WAKEUP, &thread->flags);
5773 wake_up(&thread->wqueue);
5774 }
5775}
5776
5777mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5778 const char *name)
5779{
5780 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781
NeilBrown9ffae0c2006-01-06 00:20:32 -08005782 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783 if (!thread)
5784 return NULL;
5785
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 init_waitqueue_head(&thread->wqueue);
5787
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 thread->run = run;
5789 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005790 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10005791 thread->tsk = kthread_run(md_thread, thread,
5792 "%s_%s",
5793 mdname(thread->mddev),
5794 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07005795 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 kfree(thread);
5797 return NULL;
5798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 return thread;
5800}
5801
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802void md_unregister_thread(mdk_thread_t *thread)
5803{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005804 if (!thread)
5805 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005806 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005807
5808 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 kfree(thread);
5810}
5811
5812void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5813{
5814 if (!mddev) {
5815 MD_BUG();
5816 return;
5817 }
5818
NeilBrownb2d444d2005-11-08 21:39:31 -08005819 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005821
5822 if (mddev->external)
5823 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005824/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5826 mdname(mddev),
5827 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5828 __builtin_return_address(0),__builtin_return_address(1),
5829 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005830*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005831 if (!mddev->pers)
5832 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 if (!mddev->pers->error_handler)
5834 return;
5835 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005836 if (mddev->degraded)
5837 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005838 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5840 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5841 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005842 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843}
5844
5845/* seq_file implementation /proc/mdstat */
5846
5847static void status_unused(struct seq_file *seq)
5848{
5849 int i = 0;
5850 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851
5852 seq_printf(seq, "unused devices: ");
5853
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005854 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 char b[BDEVNAME_SIZE];
5856 i++;
5857 seq_printf(seq, "%s ",
5858 bdevname(rdev->bdev,b));
5859 }
5860 if (!i)
5861 seq_printf(seq, "<none>");
5862
5863 seq_printf(seq, "\n");
5864}
5865
5866
5867static void status_resync(struct seq_file *seq, mddev_t * mddev)
5868{
NeilBrowndd71cf62009-05-07 12:49:35 +10005869 sector_t max_sectors, resync, res;
5870 unsigned long dt, db;
5871 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08005872 int scale;
5873 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874
NeilBrowndd71cf62009-05-07 12:49:35 +10005875 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876
5877 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10005878 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 else
NeilBrowndd71cf62009-05-07 12:49:35 +10005880 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881
5882 /*
5883 * Should not happen.
5884 */
NeilBrowndd71cf62009-05-07 12:49:35 +10005885 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 MD_BUG();
5887 return;
5888 }
NeilBrown4588b422006-03-27 01:18:04 -08005889 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10005890 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08005891 * u32, as those are the requirements for sector_div.
5892 * Thus 'scale' must be at least 10
5893 */
5894 scale = 10;
5895 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10005896 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08005897 scale++;
5898 }
5899 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10005900 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08005901
5902 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 {
NeilBrown4588b422006-03-27 01:18:04 -08005904 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 seq_printf(seq, "[");
5906 for (i = 0; i < x; i++)
5907 seq_printf(seq, "=");
5908 seq_printf(seq, ">");
5909 for (i = 0; i < y; i++)
5910 seq_printf(seq, ".");
5911 seq_printf(seq, "] ");
5912 }
NeilBrown4588b422006-03-27 01:18:04 -08005913 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005914 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5915 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005916 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5917 "check" :
5918 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5919 "resync" : "recovery"))),
5920 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10005921 (unsigned long long) resync/2,
5922 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923
5924 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 * dt: time from mark until now
5926 * db: blocks written from mark until now
5927 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10005928 *
5929 * rt is a sector_t, so could be 32bit or 64bit.
5930 * So we divide before multiply in case it is 32bit and close
5931 * to the limit.
5932 * We scale the divisor (db) by 32 to avoid loosing precision
5933 * near the end of resync when the number of remaining sectors
5934 * is close to 'db'.
5935 * We then divide rt by 32 after multiplying by db to compensate.
5936 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 */
5938 dt = ((jiffies - mddev->resync_mark) / HZ);
5939 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005940 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5941 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942
NeilBrowndd71cf62009-05-07 12:49:35 +10005943 rt = max_sectors - resync; /* number of remaining sectors */
5944 sector_div(rt, db/32+1);
5945 rt *= dt;
5946 rt >>= 5;
5947
5948 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
5949 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950
NeilBrownff4e8d92006-07-10 04:44:16 -07005951 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952}
5953
5954static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5955{
5956 struct list_head *tmp;
5957 loff_t l = *pos;
5958 mddev_t *mddev;
5959
5960 if (l >= 0x10000)
5961 return NULL;
5962 if (!l--)
5963 /* header */
5964 return (void*)1;
5965
5966 spin_lock(&all_mddevs_lock);
5967 list_for_each(tmp,&all_mddevs)
5968 if (!l--) {
5969 mddev = list_entry(tmp, mddev_t, all_mddevs);
5970 mddev_get(mddev);
5971 spin_unlock(&all_mddevs_lock);
5972 return mddev;
5973 }
5974 spin_unlock(&all_mddevs_lock);
5975 if (!l--)
5976 return (void*)2;/* tail */
5977 return NULL;
5978}
5979
5980static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5981{
5982 struct list_head *tmp;
5983 mddev_t *next_mddev, *mddev = v;
5984
5985 ++*pos;
5986 if (v == (void*)2)
5987 return NULL;
5988
5989 spin_lock(&all_mddevs_lock);
5990 if (v == (void*)1)
5991 tmp = all_mddevs.next;
5992 else
5993 tmp = mddev->all_mddevs.next;
5994 if (tmp != &all_mddevs)
5995 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5996 else {
5997 next_mddev = (void*)2;
5998 *pos = 0x10000;
5999 }
6000 spin_unlock(&all_mddevs_lock);
6001
6002 if (v != (void*)1)
6003 mddev_put(mddev);
6004 return next_mddev;
6005
6006}
6007
6008static void md_seq_stop(struct seq_file *seq, void *v)
6009{
6010 mddev_t *mddev = v;
6011
6012 if (mddev && v != (void*)1 && v != (void*)2)
6013 mddev_put(mddev);
6014}
6015
NeilBrownd7603b72006-01-06 00:20:30 -08006016struct mdstat_info {
6017 int event;
6018};
6019
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020static int md_seq_show(struct seq_file *seq, void *v)
6021{
6022 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006023 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006025 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006026 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027
6028 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006029 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030 seq_printf(seq, "Personalities : ");
6031 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006032 list_for_each_entry(pers, &pers_list, list)
6033 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034
6035 spin_unlock(&pers_lock);
6036 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006037 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 return 0;
6039 }
6040 if (v == (void*)2) {
6041 status_unused(seq);
6042 return 0;
6043 }
6044
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006045 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006047
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6049 seq_printf(seq, "%s : %sactive", mdname(mddev),
6050 mddev->pers ? "" : "in");
6051 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006052 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006054 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006055 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 seq_printf(seq, " %s", mddev->pers->name);
6057 }
6058
Andre Nolldd8ac332009-03-31 14:33:13 +11006059 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006060 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061 char b[BDEVNAME_SIZE];
6062 seq_printf(seq, " %s[%d]",
6063 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006064 if (test_bit(WriteMostly, &rdev->flags))
6065 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006066 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 seq_printf(seq, "(F)");
6068 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006069 } else if (rdev->raid_disk < 0)
6070 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006071 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 }
6073
6074 if (!list_empty(&mddev->disks)) {
6075 if (mddev->pers)
6076 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006077 (unsigned long long)
6078 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 else
6080 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006081 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006083 if (mddev->persistent) {
6084 if (mddev->major_version != 0 ||
6085 mddev->minor_version != 90) {
6086 seq_printf(seq," super %d.%d",
6087 mddev->major_version,
6088 mddev->minor_version);
6089 }
NeilBrowne6910632008-02-06 01:39:51 -08006090 } else if (mddev->external)
6091 seq_printf(seq, " super external:%s",
6092 mddev->metadata_type);
6093 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006094 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095
6096 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006097 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006099 if (mddev->pers->sync_request) {
6100 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006101 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006102 seq_printf(seq, "\n ");
6103 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6104 seq_printf(seq, "\tresync=DELAYED\n ");
6105 else if (mddev->recovery_cp < MaxSector)
6106 seq_printf(seq, "\tresync=PENDING\n ");
6107 }
NeilBrown32a76272005-06-21 17:17:14 -07006108 } else
6109 seq_printf(seq, "\n ");
6110
6111 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006112 unsigned long chunk_kb;
6113 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006114 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006115 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006116 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6117 "%lu%s chunk",
6118 bitmap->pages - bitmap->missing_pages,
6119 bitmap->pages,
6120 (bitmap->pages - bitmap->missing_pages)
6121 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006122 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006123 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006124 if (bitmap->file) {
6125 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006126 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006127 }
NeilBrown78d742d2005-06-21 17:17:15 -07006128
NeilBrown32a76272005-06-21 17:17:14 -07006129 seq_printf(seq, "\n");
6130 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 }
6132
6133 seq_printf(seq, "\n");
6134 }
6135 mddev_unlock(mddev);
6136
6137 return 0;
6138}
6139
Jan Engelhardt110518b2009-05-07 12:49:37 +10006140static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141 .start = md_seq_start,
6142 .next = md_seq_next,
6143 .stop = md_seq_stop,
6144 .show = md_seq_show,
6145};
6146
6147static int md_seq_open(struct inode *inode, struct file *file)
6148{
6149 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006150 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6151 if (mi == NULL)
6152 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153
6154 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006155 if (error)
6156 kfree(mi);
6157 else {
6158 struct seq_file *p = file->private_data;
6159 p->private = mi;
6160 mi->event = atomic_read(&md_event_count);
6161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162 return error;
6163}
6164
NeilBrownd7603b72006-01-06 00:20:30 -08006165static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6166{
6167 struct seq_file *m = filp->private_data;
6168 struct mdstat_info *mi = m->private;
6169 int mask;
6170
6171 poll_wait(filp, &md_event_waiters, wait);
6172
6173 /* always allow read */
6174 mask = POLLIN | POLLRDNORM;
6175
6176 if (mi->event != atomic_read(&md_event_count))
6177 mask |= POLLERR | POLLPRI;
6178 return mask;
6179}
6180
Arjan van de Venfa027c22007-02-12 00:55:33 -08006181static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006182 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 .open = md_seq_open,
6184 .read = seq_read,
6185 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006186 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006187 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188};
6189
NeilBrown2604b702006-01-06 00:20:36 -08006190int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006193 list_add_tail(&p->list, &pers_list);
6194 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 spin_unlock(&pers_lock);
6196 return 0;
6197}
6198
NeilBrown2604b702006-01-06 00:20:36 -08006199int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200{
NeilBrown2604b702006-01-06 00:20:36 -08006201 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006203 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 spin_unlock(&pers_lock);
6205 return 0;
6206}
6207
NeilBrowneea1bf32009-03-31 14:27:02 +11006208static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209{
6210 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006212 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213
6214 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006215 rcu_read_lock();
6216 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006218 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6219 (int)part_stat_read(&disk->part0, sectors[1]) -
6220 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006221 /* sync IO will cause sync_io to increase before the disk_stats
6222 * as sync_io is counted when a request starts, and
6223 * disk_stats is counted when it completes.
6224 * So resync activity will cause curr_events to be smaller than
6225 * when there was no such activity.
6226 * non-sync IO will cause disk_stat to increase without
6227 * increasing sync_io so curr_events will (eventually)
6228 * be larger than it was before. Once it becomes
6229 * substantially larger, the test below will cause
6230 * the array to appear non-idle, and resync will slow
6231 * down.
6232 * If there is a lot of outstanding resync activity when
6233 * we set last_event to curr_events, then all that activity
6234 * completing might cause the array to appear non-idle
6235 * and resync will be slowed down even though there might
6236 * not have been non-resync activity. This will only
6237 * happen once though. 'last_events' will soon reflect
6238 * the state where there is little or no outstanding
6239 * resync requests, and further resync activity will
6240 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006241 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006243 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244 rdev->last_events = curr_events;
6245 idle = 0;
6246 }
6247 }
NeilBrown4b809912008-07-21 17:05:25 +10006248 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 return idle;
6250}
6251
6252void md_done_sync(mddev_t *mddev, int blocks, int ok)
6253{
6254 /* another "blocks" (512byte) blocks have been synced */
6255 atomic_sub(blocks, &mddev->recovery_active);
6256 wake_up(&mddev->recovery_wait);
6257 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006258 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 md_wakeup_thread(mddev->thread);
6260 // stop recovery, signal do_sync ....
6261 }
6262}
6263
6264
NeilBrown06d91a52005-06-21 17:17:12 -07006265/* md_write_start(mddev, bi)
6266 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006267 * in superblock) before writing, schedule a superblock update
6268 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006269 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006270void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271{
Neil Brown0fd62b82008-06-28 08:31:36 +10006272 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006273 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006274 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006275
NeilBrownf91de922005-11-08 21:39:36 -08006276 BUG_ON(mddev->ro == 1);
6277 if (mddev->ro == 2) {
6278 /* need to switch to read/write */
6279 mddev->ro = 0;
6280 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6281 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006282 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006283 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006284 }
NeilBrown06d91a52005-06-21 17:17:12 -07006285 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006286 if (mddev->safemode == 1)
6287 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006288 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006289 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006290 if (mddev->in_sync) {
6291 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006292 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006293 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006294 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006295 }
NeilBrowna9701a32005-11-08 21:39:34 -08006296 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006297 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006298 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006299 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006300 wait_event(mddev->sb_wait,
6301 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6302 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303}
6304
6305void md_write_end(mddev_t *mddev)
6306{
6307 if (atomic_dec_and_test(&mddev->writes_pending)) {
6308 if (mddev->safemode == 2)
6309 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006310 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6312 }
6313}
6314
NeilBrown2a2275d2007-01-26 00:57:11 -08006315/* md_allow_write(mddev)
6316 * Calling this ensures that the array is marked 'active' so that writes
6317 * may proceed without blocking. It is important to call this before
6318 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6319 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006320 *
6321 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6322 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006323 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006324int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006325{
6326 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006327 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006328 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006329 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006330 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006331 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006332
6333 spin_lock_irq(&mddev->write_lock);
6334 if (mddev->in_sync) {
6335 mddev->in_sync = 0;
6336 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6337 if (mddev->safemode_delay &&
6338 mddev->safemode == 0)
6339 mddev->safemode = 1;
6340 spin_unlock_irq(&mddev->write_lock);
6341 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006342 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006343 } else
6344 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006345
6346 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6347 return -EAGAIN;
6348 else
6349 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006350}
6351EXPORT_SYMBOL_GPL(md_allow_write);
6352
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353#define SYNC_MARKS 10
6354#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006355void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356{
6357 mddev_t *mddev2;
6358 unsigned int currspeed = 0,
6359 window;
NeilBrown57afd892005-06-21 17:17:13 -07006360 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 unsigned long mark[SYNC_MARKS];
6362 sector_t mark_cnt[SYNC_MARKS];
6363 int last_mark,m;
6364 struct list_head *tmp;
6365 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006366 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006367 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006368 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369
6370 /* just incase thread restarts... */
6371 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6372 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006373 if (mddev->ro) /* never try to sync a read-only array */
6374 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375
NeilBrown61df9d92006-10-03 01:15:57 -07006376 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6377 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6378 desc = "data-check";
6379 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6380 desc = "requested-resync";
6381 else
6382 desc = "resync";
6383 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6384 desc = "reshape";
6385 else
6386 desc = "recovery";
6387
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388 /* we overload curr_resync somewhat here.
6389 * 0 == not engaged in resync at all
6390 * 2 == checking that there is no conflict with another sync
6391 * 1 == like 2, but have yielded to allow conflicting resync to
6392 * commense
6393 * other == active in resync - this many blocks
6394 *
6395 * Before starting a resync we must have set curr_resync to
6396 * 2, and then checked that every "conflicting" array has curr_resync
6397 * less than ours. When we find one that is the same or higher
6398 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6399 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6400 * This will mean we have to start checking from the beginning again.
6401 *
6402 */
6403
6404 do {
6405 mddev->curr_resync = 2;
6406
6407 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08006408 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07006409 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 goto skip;
6411 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08006412 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413 if (mddev2 == mddev)
6414 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006415 if (!mddev->parallel_resync
6416 && mddev2->curr_resync
6417 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418 DEFINE_WAIT(wq);
6419 if (mddev < mddev2 && mddev->curr_resync == 2) {
6420 /* arbitrarily yield */
6421 mddev->curr_resync = 1;
6422 wake_up(&resync_wait);
6423 }
6424 if (mddev > mddev2 && mddev->curr_resync == 1)
6425 /* no need to wait here, we can wait the next
6426 * time 'round when curr_resync == 2
6427 */
6428 continue;
NeilBrown97441972008-09-19 11:49:54 +10006429 /* We need to wait 'interruptible' so as not to
6430 * contribute to the load average, and not to
6431 * be caught by 'softlockup'
6432 */
6433 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006434 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006435 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006436 printk(KERN_INFO "md: delaying %s of %s"
6437 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006439 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006441 if (signal_pending(current))
6442 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443 schedule();
6444 finish_wait(&resync_wait, &wq);
6445 goto try_again;
6446 }
6447 finish_wait(&resync_wait, &wq);
6448 }
6449 }
6450 } while (mddev->curr_resync < 2);
6451
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006452 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006453 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006455 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456 */
6457 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006458 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006459 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006460 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6461 j = mddev->resync_min;
6462 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006463 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006464
NeilBrownccfcc3c2006-03-27 01:18:09 -08006465 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006466 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006467 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006468 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006469 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006470 j = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006471 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006472 if (rdev->raid_disk >= 0 &&
6473 !test_bit(Faulty, &rdev->flags) &&
6474 !test_bit(In_sync, &rdev->flags) &&
6475 rdev->recovery_offset < j)
6476 j = rdev->recovery_offset;
6477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478
NeilBrown61df9d92006-10-03 01:15:57 -07006479 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6480 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6481 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006482 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006483 "(but not more than %d KB/sec) for %s.\n",
6484 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485
NeilBrowneea1bf32009-03-31 14:27:02 +11006486 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006487
NeilBrown57afd892005-06-21 17:17:13 -07006488 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489 for (m = 0; m < SYNC_MARKS; m++) {
6490 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006491 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492 }
6493 last_mark = 0;
6494 mddev->resync_mark = mark[last_mark];
6495 mddev->resync_mark_cnt = mark_cnt[last_mark];
6496
6497 /*
6498 * Tune reconstruction:
6499 */
6500 window = 32*(PAGE_SIZE/512);
6501 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6502 window/2,(unsigned long long) max_sectors/2);
6503
6504 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505 last_check = 0;
6506
6507 if (j>2) {
6508 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006509 "md: resuming %s of %s from checkpoint.\n",
6510 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511 mddev->curr_resync = j;
6512 }
NeilBrownefa59332009-12-14 12:49:49 +11006513 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514
6515 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006516 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517
NeilBrown57afd892005-06-21 17:17:13 -07006518 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006519
NeilBrown7a91ee12009-05-26 12:57:21 +10006520 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6521 ((mddev->curr_resync > mddev->curr_resync_completed &&
6522 (mddev->curr_resync - mddev->curr_resync_completed)
6523 > (max_sectors >> 4)) ||
6524 (j - mddev->curr_resync_completed)*2
6525 >= mddev->resync_max - mddev->curr_resync_completed
6526 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006527 /* time to update curr_resync_completed */
6528 blk_unplug(mddev->queue);
6529 wait_event(mddev->recovery_wait,
6530 atomic_read(&mddev->recovery_active) == 0);
6531 mddev->curr_resync_completed =
6532 mddev->curr_resync;
6533 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006534 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006535 }
NeilBrownacb180b2009-04-14 16:28:34 +10006536
NeilBrowne62e58a2009-07-01 13:15:35 +10006537 while (j >= mddev->resync_max && !kthread_should_stop()) {
6538 /* As this condition is controlled by user-space,
6539 * we can block indefinitely, so use '_interruptible'
6540 * to avoid triggering warnings.
6541 */
6542 flush_signals(current); /* just in case */
6543 wait_event_interruptible(mddev->recovery_wait,
6544 mddev->resync_max > j
6545 || kthread_should_stop());
6546 }
NeilBrownacb180b2009-04-14 16:28:34 +10006547
6548 if (kthread_should_stop())
6549 goto interrupted;
6550
NeilBrown57afd892005-06-21 17:17:13 -07006551 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006552 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006553 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006554 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555 goto out;
6556 }
NeilBrown57afd892005-06-21 17:17:13 -07006557
6558 if (!skipped) { /* actual IO requested */
6559 io_sectors += sectors;
6560 atomic_add(sectors, &mddev->recovery_active);
6561 }
6562
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563 j += sectors;
6564 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006565 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006566 if (last_check == 0)
6567 /* this is the earliers that rebuilt will be
6568 * visible in /proc/mdstat
6569 */
6570 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006571
6572 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573 continue;
6574
NeilBrown57afd892005-06-21 17:17:13 -07006575 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576
NeilBrowndfc70642008-05-23 13:04:39 -07006577 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 break;
6579
6580 repeat:
6581 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6582 /* step marks */
6583 int next = (last_mark+1) % SYNC_MARKS;
6584
6585 mddev->resync_mark = mark[next];
6586 mddev->resync_mark_cnt = mark_cnt[next];
6587 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006588 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589 last_mark = next;
6590 }
6591
6592
NeilBrownc6207272008-02-06 01:39:52 -08006593 if (kthread_should_stop())
6594 goto interrupted;
6595
Linus Torvalds1da177e2005-04-16 15:20:36 -07006596
6597 /*
6598 * this loop exits only if either when we are slower than
6599 * the 'hard' speed limit, or the system was IO-idle for
6600 * a jiffy.
6601 * the system might be non-idle CPU-wise, but we only care
6602 * about not overloading the IO subsystem. (things like an
6603 * e2fsck being done on the RAID array should execute fast)
6604 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006605 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006606 cond_resched();
6607
NeilBrown57afd892005-06-21 17:17:13 -07006608 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6609 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610
NeilBrown88202a02006-01-06 00:21:36 -08006611 if (currspeed > speed_min(mddev)) {
6612 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006613 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006614 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615 goto repeat;
6616 }
6617 }
6618 }
NeilBrown61df9d92006-10-03 01:15:57 -07006619 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620 /*
6621 * this also signals 'finished resyncing' to md_stop
6622 */
6623 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006624 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625
6626 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6627
6628 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006629 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630
NeilBrowndfc70642008-05-23 13:04:39 -07006631 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006632 mddev->curr_resync > 2) {
6633 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6634 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6635 if (mddev->curr_resync >= mddev->recovery_cp) {
6636 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006637 "md: checkpointing %s of %s.\n",
6638 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006639 mddev->recovery_cp = mddev->curr_resync;
6640 }
6641 } else
6642 mddev->recovery_cp = MaxSector;
6643 } else {
6644 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6645 mddev->curr_resync = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006646 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006647 if (rdev->raid_disk >= 0 &&
6648 !test_bit(Faulty, &rdev->flags) &&
6649 !test_bit(In_sync, &rdev->flags) &&
6650 rdev->recovery_offset < mddev->curr_resync)
6651 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653 }
NeilBrown17571282006-12-10 02:20:52 -08006654 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006657 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6658 /* We completed so min/max setting can be forgotten if used. */
6659 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6660 mddev->resync_min = 0;
6661 mddev->resync_max = MaxSector;
6662 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6663 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006665 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6666 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006667 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668 wake_up(&resync_wait);
6669 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6670 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006671 return;
6672
6673 interrupted:
6674 /*
6675 * got a signal, exit.
6676 */
6677 printk(KERN_INFO
6678 "md: md_do_sync() got signal ... exiting\n");
6679 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6680 goto out;
6681
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682}
NeilBrown29269552006-03-27 01:18:10 -08006683EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684
6685
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006686static int remove_and_add_spares(mddev_t *mddev)
6687{
6688 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006689 int spares = 0;
6690
NeilBrown97e4f422009-03-31 14:33:13 +11006691 mddev->curr_resync_completed = 0;
6692
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006693 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006694 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006695 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006696 (test_bit(Faulty, &rdev->flags) ||
6697 ! test_bit(In_sync, &rdev->flags)) &&
6698 atomic_read(&rdev->nr_pending)==0) {
6699 if (mddev->pers->hot_remove_disk(
6700 mddev, rdev->raid_disk)==0) {
6701 char nm[20];
6702 sprintf(nm,"rd%d", rdev->raid_disk);
6703 sysfs_remove_link(&mddev->kobj, nm);
6704 rdev->raid_disk = -1;
6705 }
6706 }
6707
NeilBrown4044ba52009-01-09 08:31:11 +11006708 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006709 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006710 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006711 !test_bit(In_sync, &rdev->flags) &&
6712 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006713 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006714 if (rdev->raid_disk < 0
6715 && !test_bit(Faulty, &rdev->flags)) {
6716 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006717 if (mddev->pers->
6718 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006719 char nm[20];
6720 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006721 if (sysfs_create_link(&mddev->kobj,
6722 &rdev->kobj, nm))
6723 printk(KERN_WARNING
6724 "md: cannot register "
6725 "%s for %s\n",
6726 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006727 spares++;
6728 md_new_event(mddev);
6729 } else
6730 break;
6731 }
NeilBrowndfc70642008-05-23 13:04:39 -07006732 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006733 }
6734 return spares;
6735}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736/*
6737 * This routine is regularly called by all per-raid-array threads to
6738 * deal with generic issues like resync and super-block update.
6739 * Raid personalities that don't have a thread (linear/raid0) do not
6740 * need this as they never do any recovery or update the superblock.
6741 *
6742 * It does not do any resync itself, but rather "forks" off other threads
6743 * to do that as needed.
6744 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6745 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006746 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747 * and wakeups up this thread which will reap the thread and finish up.
6748 * This thread also removes any faulty devices (with nr_pending == 0).
6749 *
6750 * The overall approach is:
6751 * 1/ if the superblock needs updating, update it.
6752 * 2/ If a recovery thread is running, don't do anything else.
6753 * 3/ If recovery has finished, clean up, possibly marking spares active.
6754 * 4/ If there are any faulty devices, remove them.
6755 * 5/ If array is degraded, try to add spares devices
6756 * 6/ If array has spares or is not in-sync, start a resync thread.
6757 */
6758void md_check_recovery(mddev_t *mddev)
6759{
6760 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761
6762
NeilBrown5f404022005-06-21 17:17:16 -07006763 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11006764 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765
6766 if (mddev->ro)
6767 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006768
6769 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006770 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006771 printk(KERN_INFO "md: %s in immediate safe mode\n",
6772 mdname(mddev));
6773 mddev->safemode = 2;
6774 }
6775 flush_signals(current);
6776 }
6777
NeilBrownc89a8ee2008-08-05 15:54:13 +10006778 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6779 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006780 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006781 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006783 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006784 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006785 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6786 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 ))
6788 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006789
NeilBrowndf5b89b2006-03-27 01:18:20 -08006790 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006791 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006792
NeilBrownc89a8ee2008-08-05 15:54:13 +10006793 if (mddev->ro) {
6794 /* Only thing we do on a ro array is remove
6795 * failed devices.
6796 */
6797 remove_and_add_spares(mddev);
6798 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6799 goto unlock;
6800 }
6801
NeilBrown31a59e32008-04-30 00:52:30 -07006802 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006803 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006804 spin_lock_irq(&mddev->write_lock);
6805 if (mddev->safemode &&
6806 !atomic_read(&mddev->writes_pending) &&
6807 !mddev->in_sync &&
6808 mddev->recovery_cp == MaxSector) {
6809 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006810 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006811 if (mddev->persistent)
6812 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6813 }
6814 if (mddev->safemode == 1)
6815 mddev->safemode = 0;
6816 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006817 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006818 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006819 }
NeilBrownfca4d842005-06-21 17:17:11 -07006820
NeilBrown850b2b42006-10-03 01:15:46 -07006821 if (mddev->flags)
6822 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006823
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006824 list_for_each_entry(rdev, &mddev->disks, same_set)
Neil Brown52664732008-06-28 08:31:44 +10006825 if (test_and_clear_bit(StateChanged, &rdev->flags))
NeilBrown3c0ee632008-10-21 13:25:28 +11006826 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown52664732008-06-28 08:31:44 +10006827
NeilBrown06d91a52005-06-21 17:17:12 -07006828
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6830 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6831 /* resync/recovery still happening */
6832 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6833 goto unlock;
6834 }
6835 if (mddev->sync_thread) {
6836 /* resync has finished, collect result */
6837 md_unregister_thread(mddev->sync_thread);
6838 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006839 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6840 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841 /* success...*/
6842 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006843 if (mddev->pers->spare_active(mddev))
6844 sysfs_notify(&mddev->kobj, NULL,
6845 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846 }
NeilBrowncea9c222009-03-31 15:15:05 +11006847 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6848 mddev->pers->finish_reshape)
6849 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07006850 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006851
6852 /* if array is no-longer degraded, then any saved_raid_disk
6853 * information must be scrapped
6854 */
6855 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006856 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07006857 rdev->saved_raid_disk = -1;
6858
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859 mddev->recovery = 0;
6860 /* flag recovery needed just to double check */
6861 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11006862 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006863 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006864 goto unlock;
6865 }
Neil Brown72a23c22008-06-28 08:31:41 +10006866 /* Set RUNNING before clearing NEEDED to avoid
6867 * any transients in the value of "sync_action".
6868 */
6869 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6870 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006871 /* Clear some bits that don't mean anything, but
6872 * might be left set
6873 */
NeilBrown24dd4692005-11-08 21:39:26 -08006874 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6875 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006876
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006877 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6878 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879 /* no recovery is running.
6880 * remove any failed drives, then
6881 * add spares if possible.
6882 * Spare are also removed and re-added, to allow
6883 * the personality to fail the re-add.
6884 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006886 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10006887 if (mddev->pers->check_reshape == NULL ||
6888 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006889 /* Cannot proceed */
6890 goto unlock;
6891 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006892 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006893 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006894 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6895 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07006896 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006897 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006898 } else if (mddev->recovery_cp < MaxSector) {
6899 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006900 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006901 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6902 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006904
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006906 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6907 /* We are adding a device or devices to an array
6908 * which has the bitmap stored on all devices.
6909 * So make sure all bitmap pages get written
6910 */
6911 bitmap_write_all(mddev->bitmap);
6912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913 mddev->sync_thread = md_register_thread(md_do_sync,
6914 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006915 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916 if (!mddev->sync_thread) {
6917 printk(KERN_ERR "%s: could not start resync"
6918 " thread...\n",
6919 mdname(mddev));
6920 /* leave the spares where they are, it shouldn't hurt */
6921 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006922 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11006924 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006925 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 }
6927 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006928 if (!mddev->sync_thread) {
6929 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6930 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6931 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11006932 if (mddev->sysfs_action)
6933 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10006934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935 mddev_unlock(mddev);
6936 }
6937}
6938
Dan Williams6bfe0b42008-04-30 00:52:32 -07006939void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6940{
NeilBrown3c0ee632008-10-21 13:25:28 +11006941 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07006942 wait_event_timeout(rdev->blocked_wait,
6943 !test_bit(Blocked, &rdev->flags),
6944 msecs_to_jiffies(5000));
6945 rdev_dec_pending(rdev, mddev);
6946}
6947EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6948
Adrian Bunk75c96f82005-05-05 16:16:09 -07006949static int md_notify_reboot(struct notifier_block *this,
6950 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951{
6952 struct list_head *tmp;
6953 mddev_t *mddev;
6954
6955 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6956
6957 printk(KERN_INFO "md: stopping all md devices.\n");
6958
NeilBrown29ac4aa2008-02-06 01:39:58 -08006959 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006960 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10006961 /* Force a switch to readonly even array
6962 * appears to still be in use. Hence
6963 * the '100'.
6964 */
NeilBrownd710e132008-10-13 11:55:12 +11006965 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10006966 mddev_unlock(mddev);
6967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006968 /*
6969 * certain more exotic SCSI devices are known to be
6970 * volatile wrt too early system reboots. While the
6971 * right place to handle this issue is the given
6972 * driver, we do want to have a safe RAID driver ...
6973 */
6974 mdelay(1000*1);
6975 }
6976 return NOTIFY_DONE;
6977}
6978
Adrian Bunk75c96f82005-05-05 16:16:09 -07006979static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980 .notifier_call = md_notify_reboot,
6981 .next = NULL,
6982 .priority = INT_MAX, /* before any real devices */
6983};
6984
6985static void md_geninit(void)
6986{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6988
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07006989 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990}
6991
Adrian Bunk75c96f82005-05-05 16:16:09 -07006992static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006994 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995 return -1;
6996 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006997 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006998 return -1;
6999 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007000 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007001 md_probe, NULL, NULL);
7002 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003 md_probe, NULL, NULL);
7004
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007006 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007
7008 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007009 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010}
7011
7012
7013#ifndef MODULE
7014
7015/*
7016 * Searches all registered partitions for autorun RAID arrays
7017 * at boot time.
7018 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007019
7020static LIST_HEAD(all_detected_devices);
7021struct detected_devices_node {
7022 struct list_head list;
7023 dev_t dev;
7024};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007025
7026void md_autodetect_dev(dev_t dev)
7027{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007028 struct detected_devices_node *node_detected_dev;
7029
7030 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7031 if (node_detected_dev) {
7032 node_detected_dev->dev = dev;
7033 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7034 } else {
7035 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7036 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038}
7039
7040
7041static void autostart_arrays(int part)
7042{
7043 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007044 struct detected_devices_node *node_detected_dev;
7045 dev_t dev;
7046 int i_scanned, i_passed;
7047
7048 i_scanned = 0;
7049 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007050
7051 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7052
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007053 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7054 i_scanned++;
7055 node_detected_dev = list_entry(all_detected_devices.next,
7056 struct detected_devices_node, list);
7057 list_del(&node_detected_dev->list);
7058 dev = node_detected_dev->dev;
7059 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007060 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061 if (IS_ERR(rdev))
7062 continue;
7063
NeilBrownb2d444d2005-11-08 21:39:31 -08007064 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065 MD_BUG();
7066 continue;
7067 }
NeilBrownd0fae182008-03-04 14:29:31 -08007068 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007070 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007072
7073 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7074 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075
7076 autorun_devices(part);
7077}
7078
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007079#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080
7081static __exit void md_exit(void)
7082{
7083 mddev_t *mddev;
7084 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007085
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007086 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007087 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007089 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007090 unregister_blkdev(mdp_major, "mdp");
7091 unregister_reboot_notifier(&md_notifier);
7092 unregister_sysctl_table(raid_table_header);
7093 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007094 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007096 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097 }
7098}
7099
Dan Williams685784a2007-07-09 11:56:42 -07007100subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007101module_exit(md_exit)
7102
NeilBrownf91de922005-11-08 21:39:36 -08007103static int get_ro(char *buffer, struct kernel_param *kp)
7104{
7105 return sprintf(buffer, "%d", start_readonly);
7106}
7107static int set_ro(const char *val, struct kernel_param *kp)
7108{
7109 char *e;
7110 int num = simple_strtoul(val, &e, 10);
7111 if (*val && (*e == '\0' || *e == '\n')) {
7112 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007113 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007114 }
7115 return -EINVAL;
7116}
7117
NeilBrown80ca3a42006-07-10 04:44:18 -07007118module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7119module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007120
NeilBrownefeb53c2009-01-09 08:31:10 +11007121module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007122
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123EXPORT_SYMBOL(register_md_personality);
7124EXPORT_SYMBOL(unregister_md_personality);
7125EXPORT_SYMBOL(md_error);
7126EXPORT_SYMBOL(md_done_sync);
7127EXPORT_SYMBOL(md_write_start);
7128EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129EXPORT_SYMBOL(md_register_thread);
7130EXPORT_SYMBOL(md_unregister_thread);
7131EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132EXPORT_SYMBOL(md_check_recovery);
7133MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07007134MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007135MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);