blob: 43cf9cc9c1df3650c228ce01920645fb474f105a [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>
Arnd Bergmann6e9624b2010-08-07 18:25:34 +020039#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/buffer_head.h> /* for invalidate_bdev */
NeilBrownd7603b72006-01-06 00:20:30 -080041#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070042#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080043#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110044#include <linux/hdreg.h>
45#include <linux/proc_fs.h>
46#include <linux/random.h>
47#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070048#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110049#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110050#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/raid/md_p.h>
52#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110054#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110055#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#define DEBUG 0
58#define dprintk(x...) ((void)(DEBUG && printk(x)))
59
60
61#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110062static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#endif
64
NeilBrown2604b702006-01-06 00:20:36 -080065static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static DEFINE_SPINLOCK(pers_lock);
67
Adrian Bunk5e563412006-06-26 00:27:42 -070068static void md_print_devices(void);
69
Bernd Schubert90b08712008-05-23 13:04:38 -070070static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
71
Adrian Bunk5e563412006-06-26 00:27:42 -070072#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/*
Robert Becker1e509152009-12-14 12:49:58 +110075 * Default number of read corrections we'll attempt on an rdev
76 * before ejecting it from the array. We divide the read error
77 * count by 2 for every hour elapsed between read errors.
78 */
79#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
80/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
82 * is 1000 KB/sec, so the extra system load does not show up that much.
83 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070084 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * subsystem is idle. There is also an 'absolute maximum' reconstruction
86 * speed limit - in case reconstruction slows down your system despite
87 * idle IO detection.
88 *
89 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080090 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 */
92
93static int sysctl_speed_limit_min = 1000;
94static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080095static inline int speed_min(mddev_t *mddev)
96{
97 return mddev->sync_speed_min ?
98 mddev->sync_speed_min : sysctl_speed_limit_min;
99}
100
101static inline int speed_max(mddev_t *mddev)
102{
103 return mddev->sync_speed_max ?
104 mddev->sync_speed_max : sysctl_speed_limit_max;
105}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107static struct ctl_table_header *raid_table_header;
108
109static ctl_table raid_table[] = {
110 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 .procname = "speed_limit_min",
112 .data = &sysctl_speed_limit_min,
113 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700114 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800115 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 },
117 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 .procname = "speed_limit_max",
119 .data = &sysctl_speed_limit_max,
120 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700121 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800122 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800124 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
127static ctl_table raid_dir_table[] = {
128 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .procname = "raid",
130 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700131 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 .child = raid_table,
133 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800134 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
137static ctl_table raid_root_table[] = {
138 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 .procname = "dev",
140 .maxlen = 0,
141 .mode = 0555,
142 .child = raid_dir_table,
143 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800144 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145};
146
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700147static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
NeilBrownf91de922005-11-08 21:39:36 -0800149static int start_readonly;
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/*
NeilBrownd7603b72006-01-06 00:20:30 -0800152 * We have a system wide 'event count' that is incremented
153 * on any 'interesting' event, and readers of /proc/mdstat
154 * can use 'poll' or 'select' to find out when the event
155 * count increases.
156 *
157 * Events are:
158 * start array, stop array, error, add device, remove device,
159 * start build, activate spare
160 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800161static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800162static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800163void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800164{
165 atomic_inc(&md_event_count);
166 wake_up(&md_event_waiters);
167}
NeilBrown29269552006-03-27 01:18:10 -0800168EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800169
NeilBrownc331eb02006-05-30 21:27:13 -0700170/* Alternate version that can be called from interrupts
171 * when calling sysfs_notify isn't needed.
172 */
Adrian Bunk05381952006-06-26 00:28:01 -0700173static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700174{
175 atomic_inc(&md_event_count);
176 wake_up(&md_event_waiters);
177}
178
NeilBrownd7603b72006-01-06 00:20:30 -0800179/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * Enables to iterate over all existing md arrays
181 * all_mddevs_lock protects this list.
182 */
183static LIST_HEAD(all_mddevs);
184static DEFINE_SPINLOCK(all_mddevs_lock);
185
186
187/*
188 * iterates through all used mddevs in the system.
189 * We take care to grab the all_mddevs_lock whenever navigating
190 * the list, and to always hold a refcount when unlocked.
191 * Any code which breaks out of this loop while own
192 * a reference to the current mddev and must mddev_put it.
193 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800194#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 \
196 for (({ spin_lock(&all_mddevs_lock); \
197 tmp = all_mddevs.next; \
198 mddev = NULL;}); \
199 ({ if (tmp != &all_mddevs) \
200 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
201 spin_unlock(&all_mddevs_lock); \
202 if (mddev) mddev_put(mddev); \
203 mddev = list_entry(tmp, mddev_t, all_mddevs); \
204 tmp != &all_mddevs;}); \
205 ({ spin_lock(&all_mddevs_lock); \
206 tmp = tmp->next;}) \
207 )
208
209
NeilBrown409c57f2009-03-31 14:39:39 +1100210/* Rather than calling directly into the personality make_request function,
211 * IO requests come here first so that we can check if the device is
212 * being suspended pending a reconfiguration.
213 * We hold a refcount over the call to ->make_request. By the time that
214 * call has finished, the bio has been linked into some internal structure
215 * and so is visible to ->quiesce(), so we don't need the refcount any more.
216 */
217static int md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
NeilBrown49077322010-03-25 16:20:56 +1100219 const int rw = bio_data_dir(bio);
NeilBrown409c57f2009-03-31 14:39:39 +1100220 mddev_t *mddev = q->queuedata;
221 int rv;
NeilBrown49077322010-03-25 16:20:56 +1100222 int cpu;
223
NeilBrown409c57f2009-03-31 14:39:39 +1100224 if (mddev == NULL || mddev->pers == NULL) {
225 bio_io_error(bio);
226 return 0;
227 }
228 rcu_read_lock();
NeilBrowna2826aa2009-12-14 12:49:49 +1100229 if (mddev->suspended || mddev->barrier) {
NeilBrown409c57f2009-03-31 14:39:39 +1100230 DEFINE_WAIT(__wait);
231 for (;;) {
232 prepare_to_wait(&mddev->sb_wait, &__wait,
233 TASK_UNINTERRUPTIBLE);
NeilBrowna2826aa2009-12-14 12:49:49 +1100234 if (!mddev->suspended && !mddev->barrier)
NeilBrown409c57f2009-03-31 14:39:39 +1100235 break;
236 rcu_read_unlock();
237 schedule();
238 rcu_read_lock();
239 }
240 finish_wait(&mddev->sb_wait, &__wait);
241 }
242 atomic_inc(&mddev->active_io);
243 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100244
NeilBrown21a52c62010-04-01 15:02:13 +1100245 rv = mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100246
247 cpu = part_stat_lock();
248 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
249 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
250 bio_sectors(bio));
251 part_stat_unlock();
252
NeilBrown409c57f2009-03-31 14:39:39 +1100253 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
254 wake_up(&mddev->sb_wait);
255
256 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
NeilBrown9e35b992010-04-06 14:23:02 +1000259/* mddev_suspend makes sure no new requests are submitted
260 * to the device, and that any requests that have been submitted
261 * are completely handled.
262 * Once ->stop is called and completes, the module will be completely
263 * unused.
264 */
NeilBrown390ee602010-06-01 19:37:27 +1000265void mddev_suspend(mddev_t *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100266{
267 BUG_ON(mddev->suspended);
268 mddev->suspended = 1;
269 synchronize_rcu();
270 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
271 mddev->pers->quiesce(mddev, 1);
NeilBrown409c57f2009-03-31 14:39:39 +1100272}
NeilBrown390ee602010-06-01 19:37:27 +1000273EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100274
NeilBrown390ee602010-06-01 19:37:27 +1000275void mddev_resume(mddev_t *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100276{
277 mddev->suspended = 0;
278 wake_up(&mddev->sb_wait);
279 mddev->pers->quiesce(mddev, 0);
280}
NeilBrown390ee602010-06-01 19:37:27 +1000281EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100282
NeilBrown3fa841d2009-09-23 18:10:29 +1000283int mddev_congested(mddev_t *mddev, int bits)
284{
NeilBrowna2826aa2009-12-14 12:49:49 +1100285 if (mddev->barrier)
286 return 1;
NeilBrown3fa841d2009-09-23 18:10:29 +1000287 return mddev->suspended;
288}
289EXPORT_SYMBOL(mddev_congested);
290
NeilBrowna2826aa2009-12-14 12:49:49 +1100291/*
292 * Generic barrier handling for md
293 */
294
295#define POST_REQUEST_BARRIER ((void*)1)
296
297static void md_end_barrier(struct bio *bio, int err)
298{
299 mdk_rdev_t *rdev = bio->bi_private;
300 mddev_t *mddev = rdev->mddev;
301 if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER)
302 set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags);
303
304 rdev_dec_pending(rdev, mddev);
305
306 if (atomic_dec_and_test(&mddev->flush_pending)) {
307 if (mddev->barrier == POST_REQUEST_BARRIER) {
308 /* This was a post-request barrier */
309 mddev->barrier = NULL;
310 wake_up(&mddev->sb_wait);
311 } else
312 /* The pre-request barrier has finished */
313 schedule_work(&mddev->barrier_work);
314 }
315 bio_put(bio);
316}
317
318static void submit_barriers(mddev_t *mddev)
319{
320 mdk_rdev_t *rdev;
321
322 rcu_read_lock();
323 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
324 if (rdev->raid_disk >= 0 &&
325 !test_bit(Faulty, &rdev->flags)) {
326 /* Take two references, one is dropped
327 * when request finishes, one after
328 * we reclaim rcu_read_lock
329 */
330 struct bio *bi;
331 atomic_inc(&rdev->nr_pending);
332 atomic_inc(&rdev->nr_pending);
333 rcu_read_unlock();
334 bi = bio_alloc(GFP_KERNEL, 0);
335 bi->bi_end_io = md_end_barrier;
336 bi->bi_private = rdev;
337 bi->bi_bdev = rdev->bdev;
338 atomic_inc(&mddev->flush_pending);
339 submit_bio(WRITE_BARRIER, bi);
340 rcu_read_lock();
341 rdev_dec_pending(rdev, mddev);
342 }
343 rcu_read_unlock();
344}
345
346static void md_submit_barrier(struct work_struct *ws)
347{
348 mddev_t *mddev = container_of(ws, mddev_t, barrier_work);
349 struct bio *bio = mddev->barrier;
350
351 atomic_set(&mddev->flush_pending, 1);
352
353 if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags))
354 bio_endio(bio, -EOPNOTSUPP);
355 else if (bio->bi_size == 0)
356 /* an empty barrier - all done */
357 bio_endio(bio, 0);
358 else {
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200359 bio->bi_rw &= ~REQ_HARDBARRIER;
NeilBrown21a52c62010-04-01 15:02:13 +1100360 if (mddev->pers->make_request(mddev, bio))
NeilBrowna2826aa2009-12-14 12:49:49 +1100361 generic_make_request(bio);
362 mddev->barrier = POST_REQUEST_BARRIER;
363 submit_barriers(mddev);
364 }
365 if (atomic_dec_and_test(&mddev->flush_pending)) {
366 mddev->barrier = NULL;
367 wake_up(&mddev->sb_wait);
368 }
369}
370
371void md_barrier_request(mddev_t *mddev, struct bio *bio)
372{
373 spin_lock_irq(&mddev->write_lock);
374 wait_event_lock_irq(mddev->sb_wait,
375 !mddev->barrier,
376 mddev->write_lock, /*nothing*/);
377 mddev->barrier = bio;
378 spin_unlock_irq(&mddev->write_lock);
379
380 atomic_set(&mddev->flush_pending, 1);
381 INIT_WORK(&mddev->barrier_work, md_submit_barrier);
382
383 submit_barriers(mddev);
384
385 if (atomic_dec_and_test(&mddev->flush_pending))
386 schedule_work(&mddev->barrier_work);
387}
388EXPORT_SYMBOL(md_barrier_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100389
NeilBrown2ac87402010-06-01 19:37:29 +1000390/* Support for plugging.
391 * This mirrors the plugging support in request_queue, but does not
392 * require having a whole queue
393 */
394static void plugger_work(struct work_struct *work)
395{
396 struct plug_handle *plug =
397 container_of(work, struct plug_handle, unplug_work);
398 plug->unplug_fn(plug);
399}
400static void plugger_timeout(unsigned long data)
401{
402 struct plug_handle *plug = (void *)data;
403 kblockd_schedule_work(NULL, &plug->unplug_work);
404}
405void plugger_init(struct plug_handle *plug,
406 void (*unplug_fn)(struct plug_handle *))
407{
408 plug->unplug_flag = 0;
409 plug->unplug_fn = unplug_fn;
410 init_timer(&plug->unplug_timer);
411 plug->unplug_timer.function = plugger_timeout;
412 plug->unplug_timer.data = (unsigned long)plug;
413 INIT_WORK(&plug->unplug_work, plugger_work);
414}
415EXPORT_SYMBOL_GPL(plugger_init);
416
417void plugger_set_plug(struct plug_handle *plug)
418{
419 if (!test_and_set_bit(PLUGGED_FLAG, &plug->unplug_flag))
420 mod_timer(&plug->unplug_timer, jiffies + msecs_to_jiffies(3)+1);
421}
422EXPORT_SYMBOL_GPL(plugger_set_plug);
423
424int plugger_remove_plug(struct plug_handle *plug)
425{
426 if (test_and_clear_bit(PLUGGED_FLAG, &plug->unplug_flag)) {
427 del_timer(&plug->unplug_timer);
428 return 1;
429 } else
430 return 0;
431}
432EXPORT_SYMBOL_GPL(plugger_remove_plug);
433
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435static inline mddev_t *mddev_get(mddev_t *mddev)
436{
437 atomic_inc(&mddev->active);
438 return mddev;
439}
440
Dan Williams5fd3a172009-03-04 00:57:25 -0700441static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443static void mddev_put(mddev_t *mddev)
444{
445 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
446 return;
NeilBrownd3374822009-01-09 08:31:10 +1100447 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100448 mddev->ctime == 0 && !mddev->hold_active) {
449 /* Array is not configured at all, and not held active,
450 * so destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 list_del(&mddev->all_mddevs);
NeilBrownd3374822009-01-09 08:31:10 +1100452 if (mddev->gendisk) {
453 /* we did a probe so need to clean up.
454 * Call schedule_work inside the spinlock
455 * so that flush_scheduled_work() after
456 * mddev_find will succeed in waiting for the
457 * work to be done.
458 */
459 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
460 schedule_work(&mddev->del_work);
461 } else
462 kfree(mddev);
463 }
464 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
NeilBrown390ee602010-06-01 19:37:27 +1000467void mddev_init(mddev_t *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100468{
469 mutex_init(&mddev->open_mutex);
470 mutex_init(&mddev->reconfig_mutex);
471 mutex_init(&mddev->bitmap_info.mutex);
472 INIT_LIST_HEAD(&mddev->disks);
473 INIT_LIST_HEAD(&mddev->all_mddevs);
474 init_timer(&mddev->safemode_timer);
475 atomic_set(&mddev->active, 1);
476 atomic_set(&mddev->openers, 0);
477 atomic_set(&mddev->active_io, 0);
478 spin_lock_init(&mddev->write_lock);
479 atomic_set(&mddev->flush_pending, 0);
480 init_waitqueue_head(&mddev->sb_wait);
481 init_waitqueue_head(&mddev->recovery_wait);
482 mddev->reshape_position = MaxSector;
483 mddev->resync_min = 0;
484 mddev->resync_max = MaxSector;
485 mddev->level = LEVEL_NONE;
486}
NeilBrown390ee602010-06-01 19:37:27 +1000487EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489static mddev_t * mddev_find(dev_t unit)
490{
491 mddev_t *mddev, *new = NULL;
492
493 retry:
494 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
NeilBrownefeb53c2009-01-09 08:31:10 +1100496 if (unit) {
497 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
498 if (mddev->unit == unit) {
499 mddev_get(mddev);
500 spin_unlock(&all_mddevs_lock);
501 kfree(new);
502 return mddev;
503 }
504
505 if (new) {
506 list_add(&new->all_mddevs, &all_mddevs);
507 spin_unlock(&all_mddevs_lock);
508 new->hold_active = UNTIL_IOCTL;
509 return new;
510 }
511 } else if (new) {
512 /* find an unused unit number */
513 static int next_minor = 512;
514 int start = next_minor;
515 int is_free = 0;
516 int dev = 0;
517 while (!is_free) {
518 dev = MKDEV(MD_MAJOR, next_minor);
519 next_minor++;
520 if (next_minor > MINORMASK)
521 next_minor = 0;
522 if (next_minor == start) {
523 /* Oh dear, all in use. */
524 spin_unlock(&all_mddevs_lock);
525 kfree(new);
526 return NULL;
527 }
528
529 is_free = 1;
530 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
531 if (mddev->unit == dev) {
532 is_free = 0;
533 break;
534 }
535 }
536 new->unit = dev;
537 new->md_minor = MINOR(dev);
538 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 list_add(&new->all_mddevs, &all_mddevs);
540 spin_unlock(&all_mddevs_lock);
541 return new;
542 }
543 spin_unlock(&all_mddevs_lock);
544
NeilBrown9ffae0c2006-01-06 00:20:32 -0800545 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (!new)
547 return NULL;
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 new->unit = unit;
550 if (MAJOR(unit) == MD_MAJOR)
551 new->md_minor = MINOR(unit);
552 else
553 new->md_minor = MINOR(unit) >> MdpMinorShift;
554
NeilBrownfafd7fb2010-04-01 15:55:30 +1100555 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 goto retry;
558}
559
560static inline int mddev_lock(mddev_t * mddev)
561{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800562 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
Dan Williamsb522adc2009-03-31 15:00:31 +1100565static inline int mddev_is_locked(mddev_t *mddev)
566{
567 return mutex_is_locked(&mddev->reconfig_mutex);
568}
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570static inline int mddev_trylock(mddev_t * mddev)
571{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800572 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
NeilBrownb6eb1272010-04-15 10:13:47 +1000575static struct attribute_group md_redundancy_group;
576
NeilBrowna64c8762010-04-14 17:15:37 +1000577static void mddev_unlock(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
NeilBrowna64c8762010-04-14 17:15:37 +1000579 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000580 /* These cannot be removed under reconfig_mutex as
581 * an access to the files will try to take reconfig_mutex
582 * while holding the file unremovable, which leads to
583 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000584 * So hold set sysfs_active while the remove in happeing,
585 * and anything else which might set ->to_remove or my
586 * otherwise change the sysfs namespace will fail with
587 * -EBUSY if sysfs_active is still set.
588 * We set sysfs_active under reconfig_mutex and elsewhere
589 * test it under the same mutex to ensure its correct value
590 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000591 */
NeilBrowna64c8762010-04-14 17:15:37 +1000592 struct attribute_group *to_remove = mddev->to_remove;
593 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000594 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000595 mutex_unlock(&mddev->reconfig_mutex);
596
NeilBrown00bcb4a2010-06-01 19:37:23 +1000597 if (mddev->kobj.sd) {
598 if (to_remove != &md_redundancy_group)
599 sysfs_remove_group(&mddev->kobj, to_remove);
600 if (mddev->pers == NULL ||
601 mddev->pers->sync_request == NULL) {
602 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
603 if (mddev->sysfs_action)
604 sysfs_put(mddev->sysfs_action);
605 mddev->sysfs_action = NULL;
606 }
NeilBrowna64c8762010-04-14 17:15:37 +1000607 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000608 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000609 } else
610 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
NeilBrown005eca52005-08-22 13:11:08 -0700612 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
NeilBrown2989ddb2006-01-06 00:20:43 -0800615static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100617 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100619 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (rdev->desc_nr == nr)
621 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return NULL;
624}
625
626static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
627{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 mdk_rdev_t *rdev;
629
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100630 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (rdev->bdev->bd_dev == dev)
632 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return NULL;
635}
636
NeilBrownd9d166c2006-01-06 00:20:51 -0800637static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800638{
639 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800640 list_for_each_entry(pers, &pers_list, list) {
641 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800642 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800643 if (strcmp(pers->name, clevel)==0)
644 return pers;
645 }
NeilBrown2604b702006-01-06 00:20:36 -0800646 return NULL;
647}
648
Andre Nollb73df2d2008-07-11 22:02:23 +1000649/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700650static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Andre Nollb73df2d2008-07-11 22:02:23 +1000652 sector_t num_sectors = bdev->bd_inode->i_size / 512;
653 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656static int alloc_disk_sb(mdk_rdev_t * rdev)
657{
658 if (rdev->sb_page)
659 MD_BUG();
660
661 rdev->sb_page = alloc_page(GFP_KERNEL);
662 if (!rdev->sb_page) {
663 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000664 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666
667 return 0;
668}
669
670static void free_disk_sb(mdk_rdev_t * rdev)
671{
672 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800673 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 rdev->sb_loaded = 0;
675 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000676 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100677 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679}
680
681
NeilBrown6712ecf2007-09-27 12:47:43 +0200682static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700683{
684 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800685 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700686
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700687 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
688 printk("md: super_written gets error=%d, uptodate=%d\n",
689 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
690 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800691 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700692 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700693
NeilBrowna9701a32005-11-08 21:39:34 -0800694 if (atomic_dec_and_test(&mddev->pending_writes))
695 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700696 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700697}
698
NeilBrown6712ecf2007-09-27 12:47:43 +0200699static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800700{
701 struct bio *bio2 = bio->bi_private;
702 mdk_rdev_t *rdev = bio2->bi_private;
703 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800704
705 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
706 error == -EOPNOTSUPP) {
707 unsigned long flags;
708 /* barriers don't appear to be supported :-( */
709 set_bit(BarriersNotsupp, &rdev->flags);
710 mddev->barriers_work = 0;
711 spin_lock_irqsave(&mddev->write_lock, flags);
712 bio2->bi_next = mddev->biolist;
713 mddev->biolist = bio2;
714 spin_unlock_irqrestore(&mddev->write_lock, flags);
715 wake_up(&mddev->sb_wait);
716 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200717 } else {
718 bio_put(bio2);
719 bio->bi_private = rdev;
720 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800721 }
NeilBrowna9701a32005-11-08 21:39:34 -0800722}
723
NeilBrown7bfa19f2005-06-21 17:17:28 -0700724void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
725 sector_t sector, int size, struct page *page)
726{
727 /* write first size bytes of page to sector of rdev
728 * Increment mddev->pending_writes before returning
729 * and decrement it on completion, waking up sb_wait
730 * if zero is reached.
731 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800732 *
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200733 * As we might need to resubmit the request if REQ_HARDBARRIER
NeilBrowna9701a32005-11-08 21:39:34 -0800734 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700735 */
736 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200737 int rw = REQ_WRITE | REQ_SYNC | REQ_UNPLUG;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700738
739 bio->bi_bdev = rdev->bdev;
740 bio->bi_sector = sector;
741 bio_add_page(bio, page, size, 0);
742 bio->bi_private = rdev;
743 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800744 bio->bi_rw = rw;
745
NeilBrown7bfa19f2005-06-21 17:17:28 -0700746 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800747 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
748 struct bio *rbio;
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200749 rw |= REQ_HARDBARRIER;
NeilBrowna9701a32005-11-08 21:39:34 -0800750 rbio = bio_clone(bio, GFP_NOIO);
751 rbio->bi_private = bio;
752 rbio->bi_end_io = super_written_barrier;
753 submit_bio(rw, rbio);
754 } else
755 submit_bio(rw, bio);
756}
757
758void md_super_wait(mddev_t *mddev)
759{
760 /* wait for all superblock writes that were scheduled to complete.
761 * if any had to be retried (due to BARRIER problems), retry them
762 */
763 DEFINE_WAIT(wq);
764 for(;;) {
765 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
766 if (atomic_read(&mddev->pending_writes)==0)
767 break;
768 while (mddev->biolist) {
769 struct bio *bio;
770 spin_lock_irq(&mddev->write_lock);
771 bio = mddev->biolist;
772 mddev->biolist = bio->bi_next ;
773 bio->bi_next = NULL;
774 spin_unlock_irq(&mddev->write_lock);
775 submit_bio(bio->bi_rw, bio);
776 }
777 schedule();
778 }
779 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700780}
781
NeilBrown6712ecf2007-09-27 12:47:43 +0200782static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
NeilBrowna654b9d82005-06-21 17:17:27 -0700787int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 struct page *page, int rw)
789{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700790 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 struct completion event;
792 int ret;
793
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200794 rw |= REQ_SYNC | REQ_UNPLUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 bio->bi_bdev = bdev;
797 bio->bi_sector = sector;
798 bio_add_page(bio, page, size, 0);
799 init_completion(&event);
800 bio->bi_private = &event;
801 bio->bi_end_io = bi_complete;
802 submit_bio(rw, bio);
803 wait_for_completion(&event);
804
805 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
806 bio_put(bio);
807 return ret;
808}
NeilBrowna8745db2006-01-06 00:20:34 -0800809EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
NeilBrown0002b272005-09-09 16:23:53 -0700811static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 char b[BDEVNAME_SIZE];
814 if (!rdev->sb_page) {
815 MD_BUG();
816 return -EINVAL;
817 }
818 if (rdev->sb_loaded)
819 return 0;
820
821
Andre Noll0f420352008-07-11 22:02:23 +1000822 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 goto fail;
824 rdev->sb_loaded = 1;
825 return 0;
826
827fail:
828 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
829 bdevname(rdev->bdev,b));
830 return -EINVAL;
831}
832
833static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
834{
Andre Noll05710462008-07-11 22:02:20 +1000835 return sb1->set_uuid0 == sb2->set_uuid0 &&
836 sb1->set_uuid1 == sb2->set_uuid1 &&
837 sb1->set_uuid2 == sb2->set_uuid2 &&
838 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
842{
843 int ret;
844 mdp_super_t *tmp1, *tmp2;
845
846 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
847 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
848
849 if (!tmp1 || !tmp2) {
850 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100851 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 goto abort;
853 }
854
855 *tmp1 = *sb1;
856 *tmp2 = *sb2;
857
858 /*
859 * nr_disks is not constant
860 */
861 tmp1->nr_disks = 0;
862 tmp2->nr_disks = 0;
863
Andre Nollce0c8e02008-07-11 22:02:20 +1000864 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700866 kfree(tmp1);
867 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return ret;
869}
870
NeilBrown4d167f02007-05-09 02:35:37 -0700871
872static u32 md_csum_fold(u32 csum)
873{
874 csum = (csum & 0xffff) + (csum >> 16);
875 return (csum & 0xffff) + (csum >> 16);
876}
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878static unsigned int calc_sb_csum(mdp_super_t * sb)
879{
NeilBrown4d167f02007-05-09 02:35:37 -0700880 u64 newcsum = 0;
881 u32 *sb32 = (u32*)sb;
882 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 unsigned int disk_csum, csum;
884
885 disk_csum = sb->sb_csum;
886 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700887
888 for (i = 0; i < MD_SB_BYTES/4 ; i++)
889 newcsum += sb32[i];
890 csum = (newcsum & 0xffffffff) + (newcsum>>32);
891
892
893#ifdef CONFIG_ALPHA
894 /* This used to use csum_partial, which was wrong for several
895 * reasons including that different results are returned on
896 * different architectures. It isn't critical that we get exactly
897 * the same return value as before (we always csum_fold before
898 * testing, and that removes any differences). However as we
899 * know that csum_partial always returned a 16bit value on
900 * alphas, do a fold to maximise conformity to previous behaviour.
901 */
902 sb->sb_csum = md_csum_fold(disk_csum);
903#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700905#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return csum;
907}
908
909
910/*
911 * Handle superblock details.
912 * We want to be able to handle multiple superblock formats
913 * so we have a common interface to them all, and an array of
914 * different handlers.
915 * We rely on user-space to write the initial superblock, and support
916 * reading and updating of superblocks.
917 * Interface methods are:
918 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
919 * loads and validates a superblock on dev.
920 * if refdev != NULL, compare superblocks on both devices
921 * Return:
922 * 0 - dev has a superblock that is compatible with refdev
923 * 1 - dev has a superblock that is compatible and newer than refdev
924 * so dev should be used as the refdev in future
925 * -EINVAL superblock incompatible or invalid
926 * -othererror e.g. -EIO
927 *
928 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
929 * Verify that dev is acceptable into mddev.
930 * The first time, mddev->raid_disks will be 0, and data from
931 * dev should be merged in. Subsequent calls check that dev
932 * is new enough. Return 0 or -EINVAL
933 *
934 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
935 * Update the superblock for rdev with data in mddev
936 * This does not write to disc.
937 *
938 */
939
940struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000941 char *name;
942 struct module *owner;
943 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
944 int minor_version);
945 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
946 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
947 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000948 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949};
950
951/*
Andre Noll0894cc32009-06-18 08:49:23 +1000952 * Check that the given mddev has no bitmap.
953 *
954 * This function is called from the run method of all personalities that do not
955 * support bitmaps. It prints an error message and returns non-zero if mddev
956 * has a bitmap. Otherwise, it returns 0.
957 *
958 */
959int md_check_no_bitmap(mddev_t *mddev)
960{
NeilBrownc3d97142009-12-14 12:49:52 +1100961 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000962 return 0;
963 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
964 mdname(mddev), mddev->pers->name);
965 return 1;
966}
967EXPORT_SYMBOL(md_check_no_bitmap);
968
969/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 * load_super for 0.90.0
971 */
972static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
973{
974 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
975 mdp_super_t *sb;
976 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 /*
Andre Noll0f420352008-07-11 22:02:23 +1000979 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 * it's at the end of the disk.
981 *
982 * It also happens to be a multiple of 4Kb.
983 */
Andre Noll0f420352008-07-11 22:02:23 +1000984 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
NeilBrown0002b272005-09-09 16:23:53 -0700986 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (ret) return ret;
988
989 ret = -EINVAL;
990
991 bdevname(rdev->bdev, b);
992 sb = (mdp_super_t*)page_address(rdev->sb_page);
993
994 if (sb->md_magic != MD_SB_MAGIC) {
995 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
996 b);
997 goto abort;
998 }
999
1000 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001001 sb->minor_version < 90 ||
1002 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1004 sb->major_version, sb->minor_version,
1005 b);
1006 goto abort;
1007 }
1008
1009 if (sb->raid_disks <= 0)
1010 goto abort;
1011
NeilBrown4d167f02007-05-09 02:35:37 -07001012 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1014 b);
1015 goto abort;
1016 }
1017
1018 rdev->preferred_minor = sb->md_minor;
1019 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001020 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 if (sb->level == LEVEL_MULTIPATH)
1023 rdev->desc_nr = -1;
1024 else
1025 rdev->desc_nr = sb->this_disk.number;
1026
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001027 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001029 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 __u64 ev1, ev2;
1031 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
1032 if (!uuid_equal(refsb, sb)) {
1033 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1034 b, bdevname(refdev->bdev,b2));
1035 goto abort;
1036 }
1037 if (!sb_equal(refsb, sb)) {
1038 printk(KERN_WARNING "md: %s has same UUID"
1039 " but different superblock to %s\n",
1040 b, bdevname(refdev->bdev, b2));
1041 goto abort;
1042 }
1043 ev1 = md_event(sb);
1044 ev2 = md_event(refsb);
1045 if (ev1 > ev2)
1046 ret = 1;
1047 else
1048 ret = 0;
1049 }
NeilBrown8190e752009-06-18 08:48:58 +10001050 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Andre Nolldd8ac332009-03-31 14:33:13 +11001052 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001053 /* "this cannot possibly happen" ... */
1054 ret = -EINVAL;
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 abort:
1057 return ret;
1058}
1059
1060/*
1061 * validate_super for 0.90.0
1062 */
1063static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1064{
1065 mdp_disk_t *desc;
1066 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001067 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
NeilBrown41158c72005-06-21 17:17:25 -07001069 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001070 clear_bit(Faulty, &rdev->flags);
1071 clear_bit(In_sync, &rdev->flags);
1072 clear_bit(WriteMostly, &rdev->flags);
1073 clear_bit(BarriersNotsupp, &rdev->flags);
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (mddev->raid_disks == 0) {
1076 mddev->major_version = 0;
1077 mddev->minor_version = sb->minor_version;
1078 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001079 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001080 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 mddev->ctime = sb->ctime;
1082 mddev->utime = sb->utime;
1083 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001084 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 mddev->layout = sb->layout;
1086 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +11001087 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001088 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001089 mddev->bitmap_info.offset = 0;
1090 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
NeilBrownf6705572006-03-27 01:18:11 -08001092 if (mddev->minor_version >= 91) {
1093 mddev->reshape_position = sb->reshape_position;
1094 mddev->delta_disks = sb->delta_disks;
1095 mddev->new_level = sb->new_level;
1096 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001097 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001098 } else {
1099 mddev->reshape_position = MaxSector;
1100 mddev->delta_disks = 0;
1101 mddev->new_level = mddev->level;
1102 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001103 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001104 }
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (sb->state & (1<<MD_SB_CLEAN))
1107 mddev->recovery_cp = MaxSector;
1108 else {
1109 if (sb->events_hi == sb->cp_events_hi &&
1110 sb->events_lo == sb->cp_events_lo) {
1111 mddev->recovery_cp = sb->recovery_cp;
1112 } else
1113 mddev->recovery_cp = 0;
1114 }
1115
1116 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1117 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1118 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1119 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1120
1121 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001122
1123 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001124 mddev->bitmap_info.file == NULL)
1125 mddev->bitmap_info.offset =
1126 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001127
NeilBrown41158c72005-06-21 17:17:25 -07001128 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001129 /* Insist on good event counter while assembling, except
1130 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001132 if (sb->disks[rdev->desc_nr].state & (
1133 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1134 if (ev1 < mddev->events)
1135 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001136 } else if (mddev->bitmap) {
1137 /* if adding to array with a bitmap, then we can accept an
1138 * older device ... but not too old.
1139 */
NeilBrown41158c72005-06-21 17:17:25 -07001140 if (ev1 < mddev->bitmap->events_cleared)
1141 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001142 } else {
1143 if (ev1 < mddev->events)
1144 /* just a hot-add of a new device, leave raid_disk at -1 */
1145 return 0;
1146 }
NeilBrown41158c72005-06-21 17:17:25 -07001147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 desc = sb->disks + rdev->desc_nr;
1150
1151 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001152 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001153 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1154 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001155 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001157 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1158 /* active but not in sync implies recovery up to
1159 * reshape position. We don't know exactly where
1160 * that is, so set to zero for now */
1161 if (mddev->minor_version >= 91) {
1162 rdev->recovery_offset = 0;
1163 rdev->raid_disk = desc->raid_disk;
1164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001166 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1167 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001168 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001169 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return 0;
1171}
1172
1173/*
1174 * sync_super for 0.90.0
1175 */
1176static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1177{
1178 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 mdk_rdev_t *rdev2;
1180 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 /* make rdev->sb match mddev data..
1184 *
1185 * 1/ zero out disks
1186 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1187 * 3/ any empty disks < next_spare become removed
1188 *
1189 * disks[0] gets initialised to REMOVED because
1190 * we cannot be sure from other fields if it has
1191 * been initialised or not.
1192 */
1193 int i;
1194 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1195
NeilBrown61181562005-09-09 16:24:02 -07001196 rdev->sb_size = MD_SB_BYTES;
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 sb = (mdp_super_t*)page_address(rdev->sb_page);
1199
1200 memset(sb, 0, sizeof(*sb));
1201
1202 sb->md_magic = MD_SB_MAGIC;
1203 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 sb->patch_version = mddev->patch_version;
1205 sb->gvalid_words = 0; /* ignored */
1206 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1207 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1208 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1209 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1210
1211 sb->ctime = mddev->ctime;
1212 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001213 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 sb->raid_disks = mddev->raid_disks;
1215 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001216 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 sb->utime = mddev->utime;
1218 sb->state = 0;
1219 sb->events_hi = (mddev->events>>32);
1220 sb->events_lo = (u32)mddev->events;
1221
NeilBrownf6705572006-03-27 01:18:11 -08001222 if (mddev->reshape_position == MaxSector)
1223 sb->minor_version = 90;
1224 else {
1225 sb->minor_version = 91;
1226 sb->reshape_position = mddev->reshape_position;
1227 sb->new_level = mddev->new_level;
1228 sb->delta_disks = mddev->delta_disks;
1229 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001230 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001231 }
1232 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (mddev->in_sync)
1234 {
1235 sb->recovery_cp = mddev->recovery_cp;
1236 sb->cp_events_hi = (mddev->events>>32);
1237 sb->cp_events_lo = (u32)mddev->events;
1238 if (mddev->recovery_cp == MaxSector)
1239 sb->state = (1<< MD_SB_CLEAN);
1240 } else
1241 sb->recovery_cp = 0;
1242
1243 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001244 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
NeilBrownc3d97142009-12-14 12:49:52 +11001246 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001247 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001250 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001252 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001253 int is_active = test_bit(In_sync, &rdev2->flags);
1254
1255 if (rdev2->raid_disk >= 0 &&
1256 sb->minor_version >= 91)
1257 /* we have nowhere to store the recovery_offset,
1258 * but if it is not below the reshape_position,
1259 * we can piggy-back on that.
1260 */
1261 is_active = 1;
1262 if (rdev2->raid_disk < 0 ||
1263 test_bit(Faulty, &rdev2->flags))
1264 is_active = 0;
1265 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001266 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001268 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001269 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 d = &sb->disks[rdev2->desc_nr];
1271 nr_disks++;
1272 d->number = rdev2->desc_nr;
1273 d->major = MAJOR(rdev2->bdev->bd_dev);
1274 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001275 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 d->raid_disk = rdev2->raid_disk;
1277 else
1278 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001279 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001281 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001283 if (test_bit(In_sync, &rdev2->flags))
1284 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 active++;
1286 working++;
1287 } else {
1288 d->state = 0;
1289 spare++;
1290 working++;
1291 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001292 if (test_bit(WriteMostly, &rdev2->flags))
1293 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 /* now set the "removed" and "faulty" bits on any missing devices */
1296 for (i=0 ; i < mddev->raid_disks ; i++) {
1297 mdp_disk_t *d = &sb->disks[i];
1298 if (d->state == 0 && d->number == 0) {
1299 d->number = i;
1300 d->raid_disk = i;
1301 d->state = (1<<MD_DISK_REMOVED);
1302 d->state |= (1<<MD_DISK_FAULTY);
1303 failed++;
1304 }
1305 }
1306 sb->nr_disks = nr_disks;
1307 sb->active_disks = active;
1308 sb->working_disks = working;
1309 sb->failed_disks = failed;
1310 sb->spare_disks = spare;
1311
1312 sb->this_disk = sb->disks[rdev->desc_nr];
1313 sb->sb_csum = calc_sb_csum(sb);
1314}
1315
1316/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001317 * rdev_size_change for 0.90.0
1318 */
1319static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001320super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001321{
Andre Noll58c0fed2009-03-31 14:33:13 +11001322 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001323 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001324 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001325 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001326 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001327 if (!num_sectors || num_sectors > rdev->sb_start)
1328 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001329 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001330 rdev->sb_page);
1331 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001332 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001333}
1334
1335
1336/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 * version 1 superblock
1338 */
1339
NeilBrown1c05b4b2006-10-21 10:24:08 -07001340static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001342 __le32 disk_csum;
1343 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 unsigned long long newcsum;
1345 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001346 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 int i;
1348
1349 disk_csum = sb->sb_csum;
1350 sb->sb_csum = 0;
1351 newcsum = 0;
1352 for (i=0; size>=4; size -= 4 )
1353 newcsum += le32_to_cpu(*isuper++);
1354
1355 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001356 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1359 sb->sb_csum = disk_csum;
1360 return cpu_to_le32(csum);
1361}
1362
1363static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1364{
1365 struct mdp_superblock_1 *sb;
1366 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001367 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001369 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 /*
Andre Noll0f420352008-07-11 22:02:23 +10001372 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 * It is always aligned to a 4K boundary and
1374 * depeding on minor_version, it can be:
1375 * 0: At least 8K, but less than 12K, from end of device
1376 * 1: At start of device
1377 * 2: 4K from start of device.
1378 */
1379 switch(minor_version) {
1380 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001381 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1382 sb_start -= 8*2;
1383 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 break;
1385 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001386 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 break;
1388 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001389 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 break;
1391 default:
1392 return -EINVAL;
1393 }
Andre Noll0f420352008-07-11 22:02:23 +10001394 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
NeilBrown0002b272005-09-09 16:23:53 -07001396 /* superblock is rarely larger than 1K, but it can be larger,
1397 * and it is safe to read 4k, so we do that
1398 */
1399 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 if (ret) return ret;
1401
1402
1403 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1404
1405 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1406 sb->major_version != cpu_to_le32(1) ||
1407 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001408 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001409 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 return -EINVAL;
1411
1412 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1413 printk("md: invalid superblock checksum on %s\n",
1414 bdevname(rdev->bdev,b));
1415 return -EINVAL;
1416 }
1417 if (le64_to_cpu(sb->data_size) < 10) {
1418 printk("md: data_size too small on %s\n",
1419 bdevname(rdev->bdev,b));
1420 return -EINVAL;
1421 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 rdev->preferred_minor = 0xffff;
1424 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001425 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
NeilBrown0002b272005-09-09 16:23:53 -07001427 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001428 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001429 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001430 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1431
1432 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001433 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001434 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001435
NeilBrown31b65a02006-07-10 04:44:14 -07001436 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1437 rdev->desc_nr = -1;
1438 else
1439 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1440
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001441 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001442 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001443 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 __u64 ev1, ev2;
1445 struct mdp_superblock_1 *refsb =
1446 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1447
1448 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1449 sb->level != refsb->level ||
1450 sb->layout != refsb->layout ||
1451 sb->chunksize != refsb->chunksize) {
1452 printk(KERN_WARNING "md: %s has strangely different"
1453 " superblock to %s\n",
1454 bdevname(rdev->bdev,b),
1455 bdevname(refdev->bdev,b2));
1456 return -EINVAL;
1457 }
1458 ev1 = le64_to_cpu(sb->events);
1459 ev2 = le64_to_cpu(refsb->events);
1460
1461 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001462 ret = 1;
1463 else
1464 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
NeilBrowna1801f82008-03-04 14:29:31 -08001466 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001467 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1468 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001470 rdev->sectors = rdev->sb_start;
1471 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001473 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001474 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001475 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001476 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
1479static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1480{
1481 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001482 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
NeilBrown41158c72005-06-21 17:17:25 -07001484 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001485 clear_bit(Faulty, &rdev->flags);
1486 clear_bit(In_sync, &rdev->flags);
1487 clear_bit(WriteMostly, &rdev->flags);
1488 clear_bit(BarriersNotsupp, &rdev->flags);
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 if (mddev->raid_disks == 0) {
1491 mddev->major_version = 1;
1492 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001493 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001494 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1496 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1497 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001498 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 mddev->layout = le32_to_cpu(sb->layout);
1500 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001501 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001502 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001503 mddev->bitmap_info.offset = 0;
1504 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1507 memcpy(mddev->uuid, sb->set_uuid, 16);
1508
1509 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001510
NeilBrown71c08052005-09-09 16:23:51 -07001511 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001512 mddev->bitmap_info.file == NULL )
1513 mddev->bitmap_info.offset =
1514 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001515
NeilBrownf6705572006-03-27 01:18:11 -08001516 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1517 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1518 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1519 mddev->new_level = le32_to_cpu(sb->new_level);
1520 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001521 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001522 } else {
1523 mddev->reshape_position = MaxSector;
1524 mddev->delta_disks = 0;
1525 mddev->new_level = mddev->level;
1526 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001527 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001528 }
1529
NeilBrown41158c72005-06-21 17:17:25 -07001530 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001531 /* Insist of good event counter while assembling, except for
1532 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001534 if (rdev->desc_nr >= 0 &&
1535 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1536 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1537 if (ev1 < mddev->events)
1538 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001539 } else if (mddev->bitmap) {
1540 /* If adding to array with a bitmap, then we can accept an
1541 * older device, but not too old.
1542 */
NeilBrown41158c72005-06-21 17:17:25 -07001543 if (ev1 < mddev->bitmap->events_cleared)
1544 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001545 } else {
1546 if (ev1 < mddev->events)
1547 /* just a hot-add of a new device, leave raid_disk at -1 */
1548 return 0;
1549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (mddev->level != LEVEL_MULTIPATH) {
1551 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001552 if (rdev->desc_nr < 0 ||
1553 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1554 role = 0xffff;
1555 rdev->desc_nr = -1;
1556 } else
1557 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 switch(role) {
1559 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 break;
1561 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001562 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 break;
1564 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001565 if ((le32_to_cpu(sb->feature_map) &
1566 MD_FEATURE_RECOVERY_OFFSET))
1567 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1568 else
1569 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 rdev->raid_disk = role;
1571 break;
1572 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001573 if (sb->devflags & WriteMostly1)
1574 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001575 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001576 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return 0;
1579}
1580
1581static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1582{
1583 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 mdk_rdev_t *rdev2;
1585 int max_dev, i;
1586 /* make rdev->sb match mddev and rdev data. */
1587
1588 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1589
1590 sb->feature_map = 0;
1591 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001592 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 memset(sb->pad1, 0, sizeof(sb->pad1));
1594 memset(sb->pad2, 0, sizeof(sb->pad2));
1595 memset(sb->pad3, 0, sizeof(sb->pad3));
1596
1597 sb->utime = cpu_to_le64((__u64)mddev->utime);
1598 sb->events = cpu_to_le64(mddev->events);
1599 if (mddev->in_sync)
1600 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1601 else
1602 sb->resync_offset = cpu_to_le64(0);
1603
NeilBrown1c05b4b2006-10-21 10:24:08 -07001604 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001605
NeilBrownf0ca3402006-02-02 14:28:04 -08001606 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001607 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001608 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001609 sb->level = cpu_to_le32(mddev->level);
1610 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001611
NeilBrownc3d97142009-12-14 12:49:52 +11001612 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1613 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001614 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001615 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001616
1617 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001618 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001619 sb->feature_map |=
1620 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1621 sb->recovery_offset =
1622 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001623 }
1624
NeilBrownf6705572006-03-27 01:18:11 -08001625 if (mddev->reshape_position != MaxSector) {
1626 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1627 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1628 sb->new_layout = cpu_to_le32(mddev->new_layout);
1629 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1630 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001631 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001632 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001635 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (rdev2->desc_nr+1 > max_dev)
1637 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001638
NeilBrown70471da2009-08-03 10:59:57 +10001639 if (max_dev > le32_to_cpu(sb->max_dev)) {
1640 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001641 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001642 rdev->sb_size = max_dev * 2 + 256;
1643 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1644 if (rdev->sb_size & bmask)
1645 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 for (i=0; i<max_dev;i++)
1648 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1649
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001650 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001652 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001654 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001656 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001657 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 else
1659 sb->dev_roles[i] = cpu_to_le16(0xffff);
1660 }
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 sb->sb_csum = calc_sb_1_csum(sb);
1663}
1664
Chris Webb0cd17fe2008-06-28 08:31:46 +10001665static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001666super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001667{
1668 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001669 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001670 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001671 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001672 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001673 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001674 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1675 max_sectors -= rdev->data_offset;
1676 if (!num_sectors || num_sectors > max_sectors)
1677 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001678 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001679 /* minor version 0 with bitmap we can't move */
1680 return 0;
1681 } else {
1682 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001683 sector_t sb_start;
1684 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1685 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001686 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001687 if (!num_sectors || num_sectors > max_sectors)
1688 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001689 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001690 }
1691 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001692 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001693 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001694 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001695 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001696 rdev->sb_page);
1697 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001698 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001699}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Adrian Bunk75c96f82005-05-05 16:16:09 -07001701static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 [0] = {
1703 .name = "0.90.0",
1704 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001705 .load_super = super_90_load,
1706 .validate_super = super_90_validate,
1707 .sync_super = super_90_sync,
1708 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 },
1710 [1] = {
1711 .name = "md-1",
1712 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001713 .load_super = super_1_load,
1714 .validate_super = super_1_validate,
1715 .sync_super = super_1_sync,
1716 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 },
1718};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1721{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001722 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
NeilBrown4b809912008-07-21 17:05:25 +10001724 rcu_read_lock();
1725 rdev_for_each_rcu(rdev, mddev1)
1726 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001727 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001728 rdev2->bdev->bd_contains) {
1729 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001730 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001731 }
1732 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 return 0;
1734}
1735
1736static LIST_HEAD(pending_raid_disks);
1737
Andre Nollac5e7112009-08-03 10:59:47 +10001738/*
1739 * Try to register data integrity profile for an mddev
1740 *
1741 * This is called when an array is started and after a disk has been kicked
1742 * from the array. It only succeeds if all working and active component devices
1743 * are integrity capable with matching profiles.
1744 */
1745int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001746{
Andre Nollac5e7112009-08-03 10:59:47 +10001747 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001748
Andre Nollac5e7112009-08-03 10:59:47 +10001749 if (list_empty(&mddev->disks))
1750 return 0; /* nothing to do */
1751 if (blk_get_integrity(mddev->gendisk))
1752 return 0; /* already registered */
1753 list_for_each_entry(rdev, &mddev->disks, same_set) {
1754 /* skip spares and non-functional disks */
1755 if (test_bit(Faulty, &rdev->flags))
1756 continue;
1757 if (rdev->raid_disk < 0)
1758 continue;
1759 /*
1760 * If at least one rdev is not integrity capable, we can not
1761 * enable data integrity for the md device.
1762 */
1763 if (!bdev_get_integrity(rdev->bdev))
1764 return -EINVAL;
1765 if (!reference) {
1766 /* Use the first rdev as the reference */
1767 reference = rdev;
1768 continue;
1769 }
1770 /* does this rdev's profile match the reference profile? */
1771 if (blk_integrity_compare(reference->bdev->bd_disk,
1772 rdev->bdev->bd_disk) < 0)
1773 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001774 }
Andre Nollac5e7112009-08-03 10:59:47 +10001775 /*
1776 * All component devices are integrity capable and have matching
1777 * profiles, register the common profile for the md device.
1778 */
1779 if (blk_integrity_register(mddev->gendisk,
1780 bdev_get_integrity(reference->bdev)) != 0) {
1781 printk(KERN_ERR "md: failed to register integrity for %s\n",
1782 mdname(mddev));
1783 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001784 }
Andre Nollac5e7112009-08-03 10:59:47 +10001785 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1786 mdname(mddev));
1787 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001788}
Andre Nollac5e7112009-08-03 10:59:47 +10001789EXPORT_SYMBOL(md_integrity_register);
1790
1791/* Disable data integrity if non-capable/non-matching disk is being added */
1792void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1793{
1794 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1795 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1796
1797 if (!bi_mddev) /* nothing to do */
1798 return;
1799 if (rdev->raid_disk < 0) /* skip spares */
1800 return;
1801 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1802 rdev->bdev->bd_disk) >= 0)
1803 return;
1804 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1805 blk_integrity_unregister(mddev->gendisk);
1806}
1807EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1810{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001811 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001812 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001813 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001814 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 if (rdev->mddev) {
1817 MD_BUG();
1818 return -EINVAL;
1819 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001820
1821 /* prevent duplicates */
1822 if (find_rdev(mddev, rdev->bdev->bd_dev))
1823 return -EEXIST;
1824
Andre Nolldd8ac332009-03-31 14:33:13 +11001825 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1826 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1827 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001828 if (mddev->pers) {
1829 /* Cannot change size, so fail
1830 * If mddev->level <= 0, then we don't care
1831 * about aligning sizes (e.g. linear)
1832 */
1833 if (mddev->level > 0)
1834 return -ENOSPC;
1835 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001836 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 /* Verify rdev->desc_nr is unique.
1840 * If it is -1, assign a free number, else
1841 * check number is not in use
1842 */
1843 if (rdev->desc_nr < 0) {
1844 int choice = 0;
1845 if (mddev->pers) choice = mddev->raid_disks;
1846 while (find_rdev_nr(mddev, choice))
1847 choice++;
1848 rdev->desc_nr = choice;
1849 } else {
1850 if (find_rdev_nr(mddev, rdev->desc_nr))
1851 return -EBUSY;
1852 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001853 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1854 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1855 mdname(mddev), mddev->max_disks);
1856 return -EBUSY;
1857 }
NeilBrown19133a42005-11-08 21:39:35 -08001858 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001859 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001860 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001863 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001864
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001865 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001866 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001867
Tejun Heo0762b8b2008-08-25 19:56:12 +09001868 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10001869 if (sysfs_create_link(&rdev->kobj, ko, "block"))
1870 /* failure here is OK */;
1871 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11001872
NeilBrown4b809912008-07-21 17:05:25 +10001873 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001874 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001875
1876 /* May as well allow recovery to be retried once */
1877 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001880
1881 fail:
1882 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1883 b, mdname(mddev));
1884 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
1886
NeilBrown177a99b2008-02-06 01:39:56 -08001887static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001888{
1889 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1890 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001891 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001892}
1893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1895{
1896 char b[BDEVNAME_SIZE];
1897 if (!rdev->mddev) {
1898 MD_BUG();
1899 return;
1900 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001901 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001902 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1904 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001905 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001906 sysfs_put(rdev->sysfs_state);
1907 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001908 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001909 * writing to 'remove' to "dev/state". We also need
1910 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001911 */
NeilBrown4b809912008-07-21 17:05:25 +10001912 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001913 INIT_WORK(&rdev->del_work, md_delayed_delete);
1914 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001915 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916}
1917
1918/*
1919 * prevent the device from being mounted, repartitioned or
1920 * otherwise reused by a RAID array (or any other kernel
1921 * subsystem), by bd_claiming the device.
1922 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001923static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 int err = 0;
1926 struct block_device *bdev;
1927 char b[BDEVNAME_SIZE];
1928
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001929 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (IS_ERR(bdev)) {
1931 printk(KERN_ERR "md: could not open %s.\n",
1932 __bdevname(dev, b));
1933 return PTR_ERR(bdev);
1934 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001935 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 if (err) {
1937 printk(KERN_ERR "md: could not bd_claim %s.\n",
1938 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001939 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 return err;
1941 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001942 if (!shared)
1943 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 rdev->bdev = bdev;
1945 return err;
1946}
1947
1948static void unlock_rdev(mdk_rdev_t *rdev)
1949{
1950 struct block_device *bdev = rdev->bdev;
1951 rdev->bdev = NULL;
1952 if (!bdev)
1953 MD_BUG();
1954 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001955 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956}
1957
1958void md_autodetect_dev(dev_t dev);
1959
1960static void export_rdev(mdk_rdev_t * rdev)
1961{
1962 char b[BDEVNAME_SIZE];
1963 printk(KERN_INFO "md: export_rdev(%s)\n",
1964 bdevname(rdev->bdev,b));
1965 if (rdev->mddev)
1966 MD_BUG();
1967 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001969 if (test_bit(AutoDetected, &rdev->flags))
1970 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971#endif
1972 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001973 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974}
1975
1976static void kick_rdev_from_array(mdk_rdev_t * rdev)
1977{
1978 unbind_rdev_from_array(rdev);
1979 export_rdev(rdev);
1980}
1981
1982static void export_array(mddev_t *mddev)
1983{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001984 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
NeilBrownd089c6a2008-02-06 01:39:59 -08001986 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 if (!rdev->mddev) {
1988 MD_BUG();
1989 continue;
1990 }
1991 kick_rdev_from_array(rdev);
1992 }
1993 if (!list_empty(&mddev->disks))
1994 MD_BUG();
1995 mddev->raid_disks = 0;
1996 mddev->major_version = 0;
1997}
1998
1999static void print_desc(mdp_disk_t *desc)
2000{
2001 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2002 desc->major,desc->minor,desc->raid_disk,desc->state);
2003}
2004
Cheng Renquancd2ac932009-01-09 08:31:08 +11002005static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
2007 int i;
2008
2009 printk(KERN_INFO
2010 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2011 sb->major_version, sb->minor_version, sb->patch_version,
2012 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2013 sb->ctime);
2014 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2015 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2016 sb->md_minor, sb->layout, sb->chunk_size);
2017 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2018 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2019 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2020 sb->failed_disks, sb->spare_disks,
2021 sb->sb_csum, (unsigned long)sb->events_lo);
2022
2023 printk(KERN_INFO);
2024 for (i = 0; i < MD_SB_DISKS; i++) {
2025 mdp_disk_t *desc;
2026
2027 desc = sb->disks + i;
2028 if (desc->number || desc->major || desc->minor ||
2029 desc->raid_disk || (desc->state && (desc->state != 4))) {
2030 printk(" D %2d: ", i);
2031 print_desc(desc);
2032 }
2033 }
2034 printk(KERN_INFO "md: THIS: ");
2035 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036}
2037
Cheng Renquancd2ac932009-01-09 08:31:08 +11002038static void print_sb_1(struct mdp_superblock_1 *sb)
2039{
2040 __u8 *uuid;
2041
2042 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002043 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002044 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002045 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002046 le32_to_cpu(sb->major_version),
2047 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002048 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002049 sb->set_name,
2050 (unsigned long long)le64_to_cpu(sb->ctime)
2051 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2052
2053 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002054 printk(KERN_INFO
2055 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002056 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002057 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002058 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2059 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002060 le32_to_cpu(sb->level),
2061 (unsigned long long)le64_to_cpu(sb->size),
2062 le32_to_cpu(sb->raid_disks),
2063 le32_to_cpu(sb->layout),
2064 le32_to_cpu(sb->chunksize),
2065 (unsigned long long)le64_to_cpu(sb->data_offset),
2066 (unsigned long long)le64_to_cpu(sb->data_size),
2067 (unsigned long long)le64_to_cpu(sb->super_offset),
2068 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2069 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002070 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002071 sb->devflags,
2072 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2073 (unsigned long long)le64_to_cpu(sb->events),
2074 (unsigned long long)le64_to_cpu(sb->resync_offset),
2075 le32_to_cpu(sb->sb_csum),
2076 le32_to_cpu(sb->max_dev)
2077 );
2078}
2079
2080static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
2082 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002083 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2084 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002085 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2086 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002088 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2089 switch (major_version) {
2090 case 0:
2091 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
2092 break;
2093 case 1:
2094 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
2095 break;
2096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 } else
2098 printk(KERN_INFO "md: no rdev superblock!\n");
2099}
2100
Adrian Bunk5e563412006-06-26 00:27:42 -07002101static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002103 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 mdk_rdev_t *rdev;
2105 mddev_t *mddev;
2106 char b[BDEVNAME_SIZE];
2107
2108 printk("\n");
2109 printk("md: **********************************\n");
2110 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2111 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002112 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
NeilBrown32a76272005-06-21 17:17:14 -07002114 if (mddev->bitmap)
2115 bitmap_print_sb(mddev->bitmap);
2116 else
2117 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002118 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 printk("<%s>", bdevname(rdev->bdev,b));
2120 printk("\n");
2121
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002122 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002123 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
2125 printk("md: **********************************\n");
2126 printk("\n");
2127}
2128
2129
NeilBrown42543762006-06-26 00:27:57 -07002130static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
NeilBrown42543762006-06-26 00:27:57 -07002132 /* Update each superblock (in-memory image), but
2133 * if we are allowed to, skip spares which already
2134 * have the right event counter, or have one earlier
2135 * (which would mean they aren't being marked as dirty
2136 * with the rest of the array)
2137 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 mdk_rdev_t *rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002139 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002140 if (rdev->sb_events == mddev->events ||
2141 (nospares &&
2142 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002143 rdev->sb_events+1 == mddev->events)) {
2144 /* Don't update this superblock */
2145 rdev->sb_loaded = 2;
2146 } else {
2147 super_types[mddev->major_version].
2148 sync_super(mddev, rdev);
2149 rdev->sb_loaded = 1;
2150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 }
2152}
2153
NeilBrown850b2b42006-10-03 01:15:46 -07002154static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002157 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002158 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002161 /* First make sure individual recovery_offsets are correct */
2162 list_for_each_entry(rdev, &mddev->disks, same_set) {
2163 if (rdev->raid_disk >= 0 &&
2164 mddev->delta_disks >= 0 &&
2165 !test_bit(In_sync, &rdev->flags) &&
2166 mddev->curr_resync_completed > rdev->recovery_offset)
2167 rdev->recovery_offset = mddev->curr_resync_completed;
2168
2169 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002170 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002171 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002172 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002173 wake_up(&mddev->sb_wait);
2174 return;
2175 }
2176
NeilBrowna9701a32005-11-08 21:39:34 -08002177 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002178
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002179 mddev->utime = get_seconds();
2180
NeilBrown850b2b42006-10-03 01:15:46 -07002181 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2182 force_change = 1;
2183 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2184 /* just a clean<-> dirty transition, possibly leave spares alone,
2185 * though if events isn't the right even/odd, we will have to do
2186 * spares after all
2187 */
2188 nospares = 1;
2189 if (force_change)
2190 nospares = 0;
2191 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002192 /* If the array is degraded, then skipping spares is both
2193 * dangerous and fairly pointless.
2194 * Dangerous because a device that was removed from the array
2195 * might have a event_count that still looks up-to-date,
2196 * so it can be re-added without a resync.
2197 * Pointless because if there are any spares to skip,
2198 * then a recovery will happen and soon that array won't
2199 * be degraded any more and the spare can go back to sleep then.
2200 */
NeilBrown850b2b42006-10-03 01:15:46 -07002201 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002202
NeilBrown06d91a52005-06-21 17:17:12 -07002203 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002204
2205 /* If this is just a dirty<->clean transition, and the array is clean
2206 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002207 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002208 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002209 && mddev->can_decrease_events
2210 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002211 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002212 mddev->can_decrease_events = 0;
2213 } else {
NeilBrown42543762006-06-26 00:27:57 -07002214 /* otherwise we have to go forward and ... */
2215 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002216 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
2219 if (!mddev->events) {
2220 /*
2221 * oops, this 64-bit counter should never wrap.
2222 * Either we are in around ~1 trillion A.C., assuming
2223 * 1 reboot per second, or we have a bug:
2224 */
2225 MD_BUG();
2226 mddev->events --;
2227 }
NeilBrowne6910632008-02-06 01:39:51 -08002228 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002229 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
2231 dprintk(KERN_INFO
2232 "md: updating %s RAID superblock on device (in sync %d)\n",
2233 mdname(mddev),mddev->in_sync);
2234
NeilBrown4ad13662007-07-17 04:06:13 -07002235 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002236 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 char b[BDEVNAME_SIZE];
2238 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002239 if (rdev->sb_loaded != 1)
2240 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002241 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 dprintk("(skipping faulty ");
2243
2244 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002245 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002246 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002247 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002248 rdev->sb_page);
2249 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2250 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002251 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002252 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 } else
2255 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002256 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 /* only need to write one superblock... */
2258 break;
2259 }
NeilBrowna9701a32005-11-08 21:39:34 -08002260 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002261 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002262
NeilBrowna9701a32005-11-08 21:39:34 -08002263 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002264 if (mddev->in_sync != sync_req ||
2265 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002266 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002267 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002268 goto repeat;
2269 }
NeilBrown850b2b42006-10-03 01:15:46 -07002270 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002271 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002272 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002273 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2274 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276}
2277
Andre Noll7f6ce762008-03-23 18:34:54 +01002278/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002279 * We want to accept with case. For this we use cmd_match.
2280 */
2281static int cmd_match(const char *cmd, const char *str)
2282{
2283 /* See if cmd, written into a sysfs file, matches
2284 * str. They must either be the same, or cmd can
2285 * have a trailing newline
2286 */
2287 while (*cmd && *str && *cmd == *str) {
2288 cmd++;
2289 str++;
2290 }
2291 if (*cmd == '\n')
2292 cmd++;
2293 if (*str || *cmd)
2294 return 0;
2295 return 1;
2296}
2297
NeilBrown86e6ffd2005-11-08 21:39:24 -08002298struct rdev_sysfs_entry {
2299 struct attribute attr;
2300 ssize_t (*show)(mdk_rdev_t *, char *);
2301 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2302};
2303
2304static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002305state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002306{
2307 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002308 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002309
NeilBrownb2d444d2005-11-08 21:39:31 -08002310 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002311 len+= sprintf(page+len, "%sfaulty",sep);
2312 sep = ",";
2313 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002314 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002315 len += sprintf(page+len, "%sin_sync",sep);
2316 sep = ",";
2317 }
NeilBrownf6556752006-06-26 00:28:01 -07002318 if (test_bit(WriteMostly, &rdev->flags)) {
2319 len += sprintf(page+len, "%swrite_mostly",sep);
2320 sep = ",";
2321 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002322 if (test_bit(Blocked, &rdev->flags)) {
2323 len += sprintf(page+len, "%sblocked", sep);
2324 sep = ",";
2325 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002326 if (!test_bit(Faulty, &rdev->flags) &&
2327 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002328 len += sprintf(page+len, "%sspare", sep);
2329 sep = ",";
2330 }
2331 return len+sprintf(page+len, "\n");
2332}
2333
NeilBrown45dc2de2006-06-26 00:27:58 -07002334static ssize_t
2335state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2336{
2337 /* can write
2338 * faulty - simulates and error
2339 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002340 * writemostly - sets write_mostly
2341 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002342 * blocked - sets the Blocked flag
2343 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002344 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002345 */
2346 int err = -EINVAL;
2347 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2348 md_error(rdev->mddev, rdev);
2349 err = 0;
2350 } else if (cmd_match(buf, "remove")) {
2351 if (rdev->raid_disk >= 0)
2352 err = -EBUSY;
2353 else {
2354 mddev_t *mddev = rdev->mddev;
2355 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002356 if (mddev->pers)
2357 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002358 md_new_event(mddev);
2359 err = 0;
2360 }
NeilBrownf6556752006-06-26 00:28:01 -07002361 } else if (cmd_match(buf, "writemostly")) {
2362 set_bit(WriteMostly, &rdev->flags);
2363 err = 0;
2364 } else if (cmd_match(buf, "-writemostly")) {
2365 clear_bit(WriteMostly, &rdev->flags);
2366 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002367 } else if (cmd_match(buf, "blocked")) {
2368 set_bit(Blocked, &rdev->flags);
2369 err = 0;
2370 } else if (cmd_match(buf, "-blocked")) {
2371 clear_bit(Blocked, &rdev->flags);
2372 wake_up(&rdev->blocked_wait);
2373 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2374 md_wakeup_thread(rdev->mddev->thread);
2375
2376 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002377 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2378 set_bit(In_sync, &rdev->flags);
2379 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002380 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002381 if (!err)
2382 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002383 return err ? err : len;
2384}
NeilBrown80ca3a42006-07-10 04:44:18 -07002385static struct rdev_sysfs_entry rdev_state =
2386__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002387
2388static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002389errors_show(mdk_rdev_t *rdev, char *page)
2390{
2391 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2392}
2393
2394static ssize_t
2395errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2396{
2397 char *e;
2398 unsigned long n = simple_strtoul(buf, &e, 10);
2399 if (*buf && (*e == 0 || *e == '\n')) {
2400 atomic_set(&rdev->corrected_errors, n);
2401 return len;
2402 }
2403 return -EINVAL;
2404}
2405static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002406__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002407
NeilBrown014236d2006-01-06 00:20:55 -08002408static ssize_t
2409slot_show(mdk_rdev_t *rdev, char *page)
2410{
2411 if (rdev->raid_disk < 0)
2412 return sprintf(page, "none\n");
2413 else
2414 return sprintf(page, "%d\n", rdev->raid_disk);
2415}
2416
2417static ssize_t
2418slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2419{
2420 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002421 int err;
2422 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002423 int slot = simple_strtoul(buf, &e, 10);
2424 if (strncmp(buf, "none", 4)==0)
2425 slot = -1;
2426 else if (e==buf || (*e && *e!= '\n'))
2427 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002428 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002429 /* Setting 'slot' on an active array requires also
2430 * updating the 'rd%d' link, and communicating
2431 * with the personality with ->hot_*_disk.
2432 * For now we only support removing
2433 * failed/spare devices. This normally happens automatically,
2434 * but not when the metadata is externally managed.
2435 */
NeilBrownc303da62008-02-06 01:39:51 -08002436 if (rdev->raid_disk == -1)
2437 return -EEXIST;
2438 /* personality does all needed checks */
2439 if (rdev->mddev->pers->hot_add_disk == NULL)
2440 return -EINVAL;
2441 err = rdev->mddev->pers->
2442 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2443 if (err)
2444 return err;
2445 sprintf(nm, "rd%d", rdev->raid_disk);
2446 sysfs_remove_link(&rdev->mddev->kobj, nm);
Maciej Trelab7103102010-04-14 16:58:16 +10002447 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002448 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2449 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002450 } else if (rdev->mddev->pers) {
2451 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002452 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002453 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002454 */
2455
2456 if (rdev->raid_disk != -1)
2457 return -EBUSY;
2458
2459 if (rdev->mddev->pers->hot_add_disk == NULL)
2460 return -EINVAL;
2461
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002462 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002463 if (rdev2->raid_disk == slot)
2464 return -EEXIST;
2465
2466 rdev->raid_disk = slot;
2467 if (test_bit(In_sync, &rdev->flags))
2468 rdev->saved_raid_disk = slot;
2469 else
2470 rdev->saved_raid_disk = -1;
2471 err = rdev->mddev->pers->
2472 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002473 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002474 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002475 return err;
Neil Brown52664732008-06-28 08:31:44 +10002476 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002477 sysfs_notify_dirent_safe(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002478 sprintf(nm, "rd%d", rdev->raid_disk);
2479 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002480 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002481 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002482 } else {
2483 if (slot >= rdev->mddev->raid_disks)
2484 return -ENOSPC;
2485 rdev->raid_disk = slot;
2486 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002487 clear_bit(Faulty, &rdev->flags);
2488 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002489 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002490 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002491 }
NeilBrown014236d2006-01-06 00:20:55 -08002492 return len;
2493}
2494
2495
2496static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002497__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002498
NeilBrown93c8cad2006-01-06 00:20:56 -08002499static ssize_t
2500offset_show(mdk_rdev_t *rdev, char *page)
2501{
Andrew Morton6961ece2006-01-06 00:20:59 -08002502 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002503}
2504
2505static ssize_t
2506offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2507{
2508 char *e;
2509 unsigned long long offset = simple_strtoull(buf, &e, 10);
2510 if (e==buf || (*e && *e != '\n'))
2511 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002512 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002513 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002514 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002515 /* Must set offset before size, so overlap checks
2516 * can be sane */
2517 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002518 rdev->data_offset = offset;
2519 return len;
2520}
2521
2522static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002523__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002524
NeilBrown83303b62006-01-06 00:21:06 -08002525static ssize_t
2526rdev_size_show(mdk_rdev_t *rdev, char *page)
2527{
Andre Nolldd8ac332009-03-31 14:33:13 +11002528 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002529}
2530
NeilBrownc5d79ad2008-02-06 01:39:54 -08002531static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2532{
2533 /* check if two start/length pairs overlap */
2534 if (s1+l1 <= s2)
2535 return 0;
2536 if (s2+l2 <= s1)
2537 return 0;
2538 return 1;
2539}
2540
Dan Williamsb522adc2009-03-31 15:00:31 +11002541static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2542{
2543 unsigned long long blocks;
2544 sector_t new;
2545
2546 if (strict_strtoull(buf, 10, &blocks) < 0)
2547 return -EINVAL;
2548
2549 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2550 return -EINVAL; /* sector conversion overflow */
2551
2552 new = blocks * 2;
2553 if (new != blocks * 2)
2554 return -EINVAL; /* unsigned long long to sector_t overflow */
2555
2556 *sectors = new;
2557 return 0;
2558}
2559
NeilBrown83303b62006-01-06 00:21:06 -08002560static ssize_t
2561rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2562{
NeilBrown27c529b2008-03-04 14:29:33 -08002563 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002564 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002565 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002566
Dan Williamsb522adc2009-03-31 15:00:31 +11002567 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002568 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002569 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002570 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002571 sectors = super_types[my_mddev->major_version].
2572 rdev_size_change(rdev, sectors);
2573 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002574 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002575 } else if (!sectors)
2576 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2577 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002578 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002579 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002580 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002581
Andre Nolldd8ac332009-03-31 14:33:13 +11002582 rdev->sectors = sectors;
2583 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002584 /* need to check that all other rdevs with the same ->bdev
2585 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002586 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002587 * we have to change it back, we will have the lock again.
2588 */
2589 mddev_t *mddev;
2590 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002591 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002592
NeilBrown27c529b2008-03-04 14:29:33 -08002593 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002594 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002595 mdk_rdev_t *rdev2;
2596
2597 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002598 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002599 if (test_bit(AllReserved, &rdev2->flags) ||
2600 (rdev->bdev == rdev2->bdev &&
2601 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002602 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002603 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002604 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002605 overlap = 1;
2606 break;
2607 }
2608 mddev_unlock(mddev);
2609 if (overlap) {
2610 mddev_put(mddev);
2611 break;
2612 }
2613 }
NeilBrown27c529b2008-03-04 14:29:33 -08002614 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002615 if (overlap) {
2616 /* Someone else could have slipped in a size
2617 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002618 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002619 * safe, and trust userspace not to race with
2620 * itself
2621 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002622 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002623 return -EBUSY;
2624 }
2625 }
NeilBrown83303b62006-01-06 00:21:06 -08002626 return len;
2627}
2628
2629static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002630__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002631
Dan Williams06e3c812009-12-12 21:17:12 -07002632
2633static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2634{
2635 unsigned long long recovery_start = rdev->recovery_offset;
2636
2637 if (test_bit(In_sync, &rdev->flags) ||
2638 recovery_start == MaxSector)
2639 return sprintf(page, "none\n");
2640
2641 return sprintf(page, "%llu\n", recovery_start);
2642}
2643
2644static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2645{
2646 unsigned long long recovery_start;
2647
2648 if (cmd_match(buf, "none"))
2649 recovery_start = MaxSector;
2650 else if (strict_strtoull(buf, 10, &recovery_start))
2651 return -EINVAL;
2652
2653 if (rdev->mddev->pers &&
2654 rdev->raid_disk >= 0)
2655 return -EBUSY;
2656
2657 rdev->recovery_offset = recovery_start;
2658 if (recovery_start == MaxSector)
2659 set_bit(In_sync, &rdev->flags);
2660 else
2661 clear_bit(In_sync, &rdev->flags);
2662 return len;
2663}
2664
2665static struct rdev_sysfs_entry rdev_recovery_start =
2666__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2667
NeilBrown86e6ffd2005-11-08 21:39:24 -08002668static struct attribute *rdev_default_attrs[] = {
2669 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002670 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002671 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002672 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002673 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002674 &rdev_recovery_start.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002675 NULL,
2676};
2677static ssize_t
2678rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2679{
2680 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2681 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002682 mddev_t *mddev = rdev->mddev;
2683 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002684
2685 if (!entry->show)
2686 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002687
2688 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2689 if (!rv) {
2690 if (rdev->mddev == NULL)
2691 rv = -EBUSY;
2692 else
2693 rv = entry->show(rdev, page);
2694 mddev_unlock(mddev);
2695 }
2696 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002697}
2698
2699static ssize_t
2700rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2701 const char *page, size_t length)
2702{
2703 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2704 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002705 ssize_t rv;
2706 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002707
2708 if (!entry->store)
2709 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002710 if (!capable(CAP_SYS_ADMIN))
2711 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002712 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002713 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002714 if (rdev->mddev == NULL)
2715 rv = -EBUSY;
2716 else
2717 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002718 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002719 }
2720 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002721}
2722
2723static void rdev_free(struct kobject *ko)
2724{
2725 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2726 kfree(rdev);
2727}
Emese Revfy52cf25d2010-01-19 02:58:23 +01002728static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002729 .show = rdev_attr_show,
2730 .store = rdev_attr_store,
2731};
2732static struct kobj_type rdev_ktype = {
2733 .release = rdev_free,
2734 .sysfs_ops = &rdev_sysfs_ops,
2735 .default_attrs = rdev_default_attrs,
2736};
2737
NeilBrowne8bb9a82010-06-01 19:37:26 +10002738void md_rdev_init(mdk_rdev_t *rdev)
2739{
2740 rdev->desc_nr = -1;
2741 rdev->saved_raid_disk = -1;
2742 rdev->raid_disk = -1;
2743 rdev->flags = 0;
2744 rdev->data_offset = 0;
2745 rdev->sb_events = 0;
2746 rdev->last_read_error.tv_sec = 0;
2747 rdev->last_read_error.tv_nsec = 0;
2748 atomic_set(&rdev->nr_pending, 0);
2749 atomic_set(&rdev->read_errors, 0);
2750 atomic_set(&rdev->corrected_errors, 0);
2751
2752 INIT_LIST_HEAD(&rdev->same_set);
2753 init_waitqueue_head(&rdev->blocked_wait);
2754}
2755EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756/*
2757 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2758 *
2759 * mark the device faulty if:
2760 *
2761 * - the device is nonexistent (zero size)
2762 * - the device has no valid superblock
2763 *
2764 * a faulty rdev _never_ has rdev->sb set.
2765 */
2766static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2767{
2768 char b[BDEVNAME_SIZE];
2769 int err;
2770 mdk_rdev_t *rdev;
2771 sector_t size;
2772
NeilBrown9ffae0c2006-01-06 00:20:32 -08002773 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 if (!rdev) {
2775 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2776 return ERR_PTR(-ENOMEM);
2777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
NeilBrowne8bb9a82010-06-01 19:37:26 +10002779 md_rdev_init(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 if ((err = alloc_disk_sb(rdev)))
2781 goto abort_free;
2782
NeilBrownc5d79ad2008-02-06 01:39:54 -08002783 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 if (err)
2785 goto abort_free;
2786
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002787 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002788
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2790 if (!size) {
2791 printk(KERN_WARNING
2792 "md: %s has zero or unknown size, marking faulty!\n",
2793 bdevname(rdev->bdev,b));
2794 err = -EINVAL;
2795 goto abort_free;
2796 }
2797
2798 if (super_format >= 0) {
2799 err = super_types[super_format].
2800 load_super(rdev, NULL, super_minor);
2801 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002802 printk(KERN_WARNING
2803 "md: %s does not have a valid v%d.%d "
2804 "superblock, not importing!\n",
2805 bdevname(rdev->bdev,b),
2806 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 goto abort_free;
2808 }
2809 if (err < 0) {
2810 printk(KERN_WARNING
2811 "md: could not read %s's sb, not importing!\n",
2812 bdevname(rdev->bdev,b));
2813 goto abort_free;
2814 }
2815 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002816
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 return rdev;
2818
2819abort_free:
2820 if (rdev->sb_page) {
2821 if (rdev->bdev)
2822 unlock_rdev(rdev);
2823 free_disk_sb(rdev);
2824 }
2825 kfree(rdev);
2826 return ERR_PTR(err);
2827}
2828
2829/*
2830 * Check a full RAID array for plausibility
2831 */
2832
2833
NeilBrowna757e642005-04-16 15:26:42 -07002834static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835{
2836 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002837 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 char b[BDEVNAME_SIZE];
2839
2840 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002841 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 switch (super_types[mddev->major_version].
2843 load_super(rdev, freshest, mddev->minor_version)) {
2844 case 1:
2845 freshest = rdev;
2846 break;
2847 case 0:
2848 break;
2849 default:
2850 printk( KERN_ERR \
2851 "md: fatal superblock inconsistency in %s"
2852 " -- removing from array\n",
2853 bdevname(rdev->bdev,b));
2854 kick_rdev_from_array(rdev);
2855 }
2856
2857
2858 super_types[mddev->major_version].
2859 validate_super(mddev, freshest);
2860
2861 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002862 rdev_for_each(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10002863 if (mddev->max_disks &&
2864 (rdev->desc_nr >= mddev->max_disks ||
2865 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002866 printk(KERN_WARNING
2867 "md: %s: %s: only %d devices permitted\n",
2868 mdname(mddev), bdevname(rdev->bdev, b),
2869 mddev->max_disks);
2870 kick_rdev_from_array(rdev);
2871 continue;
2872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 if (rdev != freshest)
2874 if (super_types[mddev->major_version].
2875 validate_super(mddev, rdev)) {
2876 printk(KERN_WARNING "md: kicking non-fresh %s"
2877 " from array!\n",
2878 bdevname(rdev->bdev,b));
2879 kick_rdev_from_array(rdev);
2880 continue;
2881 }
2882 if (mddev->level == LEVEL_MULTIPATH) {
2883 rdev->desc_nr = i++;
2884 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002885 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002886 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002887 rdev->raid_disk = -1;
2888 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 }
2890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891}
2892
NeilBrown72e02072009-12-14 12:49:55 +11002893/* Read a fixed-point number.
2894 * Numbers in sysfs attributes should be in "standard" units where
2895 * possible, so time should be in seconds.
2896 * However we internally use a a much smaller unit such as
2897 * milliseconds or jiffies.
2898 * This function takes a decimal number with a possible fractional
2899 * component, and produces an integer which is the result of
2900 * multiplying that number by 10^'scale'.
2901 * all without any floating-point arithmetic.
2902 */
2903int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2904{
2905 unsigned long result = 0;
2906 long decimals = -1;
2907 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2908 if (*cp == '.')
2909 decimals = 0;
2910 else if (decimals < scale) {
2911 unsigned int value;
2912 value = *cp - '0';
2913 result = result * 10 + value;
2914 if (decimals >= 0)
2915 decimals++;
2916 }
2917 cp++;
2918 }
2919 if (*cp == '\n')
2920 cp++;
2921 if (*cp)
2922 return -EINVAL;
2923 if (decimals < 0)
2924 decimals = 0;
2925 while (decimals < scale) {
2926 result *= 10;
2927 decimals ++;
2928 }
2929 *res = result;
2930 return 0;
2931}
2932
2933
NeilBrown19052c02008-08-05 15:54:13 +10002934static void md_safemode_timeout(unsigned long data);
2935
NeilBrowneae17012005-11-08 21:39:23 -08002936static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002937safe_delay_show(mddev_t *mddev, char *page)
2938{
2939 int msec = (mddev->safemode_delay*1000)/HZ;
2940 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2941}
2942static ssize_t
2943safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2944{
NeilBrown16f17b32006-06-26 00:27:37 -07002945 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07002946
NeilBrown72e02072009-12-14 12:49:55 +11002947 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002948 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07002949 if (msec == 0)
2950 mddev->safemode_delay = 0;
2951 else {
NeilBrown19052c02008-08-05 15:54:13 +10002952 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002953 mddev->safemode_delay = (msec*HZ)/1000;
2954 if (mddev->safemode_delay == 0)
2955 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002956 if (mddev->safemode_delay < old_delay)
2957 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002958 }
2959 return len;
2960}
2961static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002962__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002963
2964static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002965level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002966{
NeilBrown2604b702006-01-06 00:20:36 -08002967 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002968 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002969 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002970 else if (mddev->clevel[0])
2971 return sprintf(page, "%s\n", mddev->clevel);
2972 else if (mddev->level != LEVEL_NONE)
2973 return sprintf(page, "%d\n", mddev->level);
2974 else
2975 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002976}
2977
NeilBrownd9d166c2006-01-06 00:20:51 -08002978static ssize_t
2979level_store(mddev_t *mddev, const char *buf, size_t len)
2980{
Dan Williamsf2859af2010-05-02 10:04:16 -07002981 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002982 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002983 struct mdk_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07002984 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11002985 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002986 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002987
2988 if (mddev->pers == NULL) {
2989 if (len == 0)
2990 return 0;
2991 if (len >= sizeof(mddev->clevel))
2992 return -ENOSPC;
2993 strncpy(mddev->clevel, buf, len);
2994 if (mddev->clevel[len-1] == '\n')
2995 len--;
2996 mddev->clevel[len] = 0;
2997 mddev->level = LEVEL_NONE;
2998 return rv;
2999 }
3000
3001 /* request to change the personality. Need to ensure:
3002 * - array is not engaged in resync/recovery/reshape
3003 * - old personality can be suspended
3004 * - new personality will access other array.
3005 */
3006
NeilBrownbb4f1e92010-08-08 21:18:03 +10003007 if (mddev->sync_thread ||
3008 mddev->reshape_position != MaxSector ||
3009 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003010 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003011
3012 if (!mddev->pers->quiesce) {
3013 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3014 mdname(mddev), mddev->pers->name);
3015 return -EINVAL;
3016 }
3017
3018 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003019 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003020 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003021 strncpy(clevel, buf, len);
3022 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003023 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003024 clevel[len] = 0;
3025 if (strict_strtol(clevel, 10, &level))
3026 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003027
Dan Williamsf2859af2010-05-02 10:04:16 -07003028 if (request_module("md-%s", clevel) != 0)
3029 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003030 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003031 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003032 if (!pers || !try_module_get(pers->owner)) {
3033 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003034 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003035 return -EINVAL;
3036 }
3037 spin_unlock(&pers_lock);
3038
3039 if (pers == mddev->pers) {
3040 /* Nothing to do! */
3041 module_put(pers->owner);
3042 return rv;
3043 }
3044 if (!pers->takeover) {
3045 module_put(pers->owner);
3046 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003047 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003048 return -EINVAL;
3049 }
3050
NeilBrowne93f68a2010-06-15 09:36:03 +01003051 list_for_each_entry(rdev, &mddev->disks, same_set)
3052 rdev->new_raid_disk = rdev->raid_disk;
3053
NeilBrown245f46c2009-03-31 14:39:39 +11003054 /* ->takeover must set new_* and/or delta_disks
3055 * if it succeeds, and may set them when it fails.
3056 */
3057 priv = pers->takeover(mddev);
3058 if (IS_ERR(priv)) {
3059 mddev->new_level = mddev->level;
3060 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003061 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003062 mddev->raid_disks -= mddev->delta_disks;
3063 mddev->delta_disks = 0;
3064 module_put(pers->owner);
3065 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003066 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003067 return PTR_ERR(priv);
3068 }
3069
3070 /* Looks like we have a winner */
3071 mddev_suspend(mddev);
3072 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003073
3074 if (mddev->pers->sync_request == NULL &&
3075 pers->sync_request != NULL) {
3076 /* need to add the md_redundancy_group */
3077 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3078 printk(KERN_WARNING
3079 "md: cannot register extra attributes for %s\n",
3080 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003081 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003082 }
3083 if (mddev->pers->sync_request != NULL &&
3084 pers->sync_request == NULL) {
3085 /* need to remove the md_redundancy_group */
3086 if (mddev->to_remove == NULL)
3087 mddev->to_remove = &md_redundancy_group;
3088 }
3089
Trela Maciej54071b32010-03-08 16:02:42 +11003090 if (mddev->pers->sync_request == NULL &&
3091 mddev->external) {
3092 /* We are converting from a no-redundancy array
3093 * to a redundancy array and metadata is managed
3094 * externally so we need to be sure that writes
3095 * won't block due to a need to transition
3096 * clean->dirty
3097 * until external management is started.
3098 */
3099 mddev->in_sync = 0;
3100 mddev->safemode_delay = 0;
3101 mddev->safemode = 0;
3102 }
3103
NeilBrowne93f68a2010-06-15 09:36:03 +01003104 list_for_each_entry(rdev, &mddev->disks, same_set) {
3105 char nm[20];
3106 if (rdev->raid_disk < 0)
3107 continue;
3108 if (rdev->new_raid_disk > mddev->raid_disks)
3109 rdev->new_raid_disk = -1;
3110 if (rdev->new_raid_disk == rdev->raid_disk)
3111 continue;
3112 sprintf(nm, "rd%d", rdev->raid_disk);
3113 sysfs_remove_link(&mddev->kobj, nm);
3114 }
3115 list_for_each_entry(rdev, &mddev->disks, same_set) {
3116 if (rdev->raid_disk < 0)
3117 continue;
3118 if (rdev->new_raid_disk == rdev->raid_disk)
3119 continue;
3120 rdev->raid_disk = rdev->new_raid_disk;
3121 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003122 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003123 else {
3124 char nm[20];
3125 sprintf(nm, "rd%d", rdev->raid_disk);
3126 if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3127 printk("md: cannot register %s for %s after level change\n",
3128 nm, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003129 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003130 }
3131
3132 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003133 mddev->pers = pers;
3134 mddev->private = priv;
3135 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3136 mddev->level = mddev->new_level;
3137 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003138 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003139 mddev->delta_disks = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003140 if (mddev->pers->sync_request == NULL) {
3141 /* this is now an array without redundancy, so
3142 * it must always be in_sync
3143 */
3144 mddev->in_sync = 1;
3145 del_timer_sync(&mddev->safemode_timer);
3146 }
NeilBrown245f46c2009-03-31 14:39:39 +11003147 pers->run(mddev);
3148 mddev_resume(mddev);
3149 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3150 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3151 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003152 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003153 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003154 return rv;
3155}
3156
3157static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003158__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003159
NeilBrownd4dbd022006-06-26 00:27:59 -07003160
3161static ssize_t
3162layout_show(mddev_t *mddev, char *page)
3163{
3164 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003165 if (mddev->reshape_position != MaxSector &&
3166 mddev->layout != mddev->new_layout)
3167 return sprintf(page, "%d (%d)\n",
3168 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003169 return sprintf(page, "%d\n", mddev->layout);
3170}
3171
3172static ssize_t
3173layout_store(mddev_t *mddev, const char *buf, size_t len)
3174{
3175 char *e;
3176 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003177
3178 if (!*buf || (*e && *e != '\n'))
3179 return -EINVAL;
3180
NeilBrownb3546032009-03-31 14:56:41 +11003181 if (mddev->pers) {
3182 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003183 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003184 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003185 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003186 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003187 if (err) {
3188 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003189 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003190 }
NeilBrownb3546032009-03-31 14:56:41 +11003191 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003192 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003193 if (mddev->reshape_position == MaxSector)
3194 mddev->layout = n;
3195 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003196 return len;
3197}
3198static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003199__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003200
3201
NeilBrowneae17012005-11-08 21:39:23 -08003202static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003203raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003204{
NeilBrownbb636542005-11-08 21:39:45 -08003205 if (mddev->raid_disks == 0)
3206 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003207 if (mddev->reshape_position != MaxSector &&
3208 mddev->delta_disks != 0)
3209 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3210 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003211 return sprintf(page, "%d\n", mddev->raid_disks);
3212}
3213
NeilBrownda943b992006-01-06 00:20:54 -08003214static int update_raid_disks(mddev_t *mddev, int raid_disks);
3215
3216static ssize_t
3217raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3218{
NeilBrownda943b992006-01-06 00:20:54 -08003219 char *e;
3220 int rv = 0;
3221 unsigned long n = simple_strtoul(buf, &e, 10);
3222
3223 if (!*buf || (*e && *e != '\n'))
3224 return -EINVAL;
3225
3226 if (mddev->pers)
3227 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003228 else if (mddev->reshape_position != MaxSector) {
3229 int olddisks = mddev->raid_disks - mddev->delta_disks;
3230 mddev->delta_disks = n - olddisks;
3231 mddev->raid_disks = n;
3232 } else
NeilBrownda943b992006-01-06 00:20:54 -08003233 mddev->raid_disks = n;
3234 return rv ? rv : len;
3235}
3236static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003237__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003238
NeilBrown24dd4692005-11-08 21:39:26 -08003239static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003240chunk_size_show(mddev_t *mddev, char *page)
3241{
NeilBrown08a02ec2007-05-09 02:35:38 -07003242 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003243 mddev->chunk_sectors != mddev->new_chunk_sectors)
3244 return sprintf(page, "%d (%d)\n",
3245 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003246 mddev->chunk_sectors << 9);
3247 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003248}
3249
3250static ssize_t
3251chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3252{
NeilBrown3b343802006-01-06 00:20:47 -08003253 char *e;
3254 unsigned long n = simple_strtoul(buf, &e, 10);
3255
NeilBrown3b343802006-01-06 00:20:47 -08003256 if (!*buf || (*e && *e != '\n'))
3257 return -EINVAL;
3258
NeilBrownb3546032009-03-31 14:56:41 +11003259 if (mddev->pers) {
3260 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003261 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003262 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003263 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003264 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003265 if (err) {
3266 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003267 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003268 }
NeilBrownb3546032009-03-31 14:56:41 +11003269 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003270 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003271 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003272 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003273 }
NeilBrown3b343802006-01-06 00:20:47 -08003274 return len;
3275}
3276static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003277__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003278
NeilBrowna94213b2006-06-26 00:28:00 -07003279static ssize_t
3280resync_start_show(mddev_t *mddev, char *page)
3281{
NeilBrownd1a7c502009-03-31 15:24:32 +11003282 if (mddev->recovery_cp == MaxSector)
3283 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003284 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3285}
3286
3287static ssize_t
3288resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3289{
NeilBrowna94213b2006-06-26 00:28:00 -07003290 char *e;
3291 unsigned long long n = simple_strtoull(buf, &e, 10);
3292
3293 if (mddev->pers)
3294 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003295 if (cmd_match(buf, "none"))
3296 n = MaxSector;
3297 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003298 return -EINVAL;
3299
3300 mddev->recovery_cp = n;
3301 return len;
3302}
3303static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003304__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003305
NeilBrown9e653b62006-06-26 00:27:58 -07003306/*
3307 * The array state can be:
3308 *
3309 * clear
3310 * No devices, no size, no level
3311 * Equivalent to STOP_ARRAY ioctl
3312 * inactive
3313 * May have some settings, but array is not active
3314 * all IO results in error
3315 * When written, doesn't tear down array, but just stops it
3316 * suspended (not supported yet)
3317 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003318 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003319 * readonly
3320 * no resync can happen. no superblocks get written.
3321 * write requests fail
3322 * read-auto
3323 * like readonly, but behaves like 'clean' on a write request.
3324 *
3325 * clean - no pending writes, but otherwise active.
3326 * When written to inactive array, starts without resync
3327 * If a write request arrives then
3328 * if metadata is known, mark 'dirty' and switch to 'active'.
3329 * if not known, block and switch to write-pending
3330 * If written to an active array that has pending writes, then fails.
3331 * active
3332 * fully active: IO and resync can be happening.
3333 * When written to inactive array, starts with resync
3334 *
3335 * write-pending
3336 * clean, but writes are blocked waiting for 'active' to be written.
3337 *
3338 * active-idle
3339 * like active, but no writes have been seen for a while (100msec).
3340 *
3341 */
3342enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3343 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003344static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003345 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3346 "write-pending", "active-idle", NULL };
3347
3348static int match_word(const char *word, char **list)
3349{
3350 int n;
3351 for (n=0; list[n]; n++)
3352 if (cmd_match(word, list[n]))
3353 break;
3354 return n;
3355}
3356
3357static ssize_t
3358array_state_show(mddev_t *mddev, char *page)
3359{
3360 enum array_state st = inactive;
3361
3362 if (mddev->pers)
3363 switch(mddev->ro) {
3364 case 1:
3365 st = readonly;
3366 break;
3367 case 2:
3368 st = read_auto;
3369 break;
3370 case 0:
3371 if (mddev->in_sync)
3372 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003373 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003374 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003375 else if (mddev->safemode)
3376 st = active_idle;
3377 else
3378 st = active;
3379 }
3380 else {
3381 if (list_empty(&mddev->disks) &&
3382 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003383 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003384 st = clear;
3385 else
3386 st = inactive;
3387 }
3388 return sprintf(page, "%s\n", array_states[st]);
3389}
3390
Neil Browndf5b20c2008-07-11 22:02:22 +10003391static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrowna4bd82d2010-03-29 13:23:10 +11003392static int md_set_readonly(mddev_t * mddev, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003393static int do_md_run(mddev_t * mddev);
3394static int restart_array(mddev_t *mddev);
3395
3396static ssize_t
3397array_state_store(mddev_t *mddev, const char *buf, size_t len)
3398{
3399 int err = -EINVAL;
3400 enum array_state st = match_word(buf, array_states);
3401 switch(st) {
3402 case bad_word:
3403 break;
3404 case clear:
3405 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003406 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003407 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003408 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003409 break;
3410 case inactive:
3411 /* stopping an active array */
3412 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003413 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003414 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003415 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003416 } else
3417 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003418 break;
3419 case suspended:
3420 break; /* not supported yet */
3421 case readonly:
3422 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003423 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003424 else {
3425 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003426 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003427 err = do_md_run(mddev);
3428 }
3429 break;
3430 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003431 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003432 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003433 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003434 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003435 err = restart_array(mddev);
3436 if (err == 0) {
3437 mddev->ro = 2;
3438 set_disk_ro(mddev->gendisk, 0);
3439 }
NeilBrown9e653b62006-06-26 00:27:58 -07003440 } else {
3441 mddev->ro = 2;
3442 err = do_md_run(mddev);
3443 }
3444 break;
3445 case clean:
3446 if (mddev->pers) {
3447 restart_array(mddev);
3448 spin_lock_irq(&mddev->write_lock);
3449 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003450 if (mddev->in_sync == 0) {
3451 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003452 if (mddev->safemode == 1)
3453 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003454 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003455 }
3456 err = 0;
3457 } else
3458 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003459 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003460 } else
3461 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003462 break;
3463 case active:
3464 if (mddev->pers) {
3465 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003466 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003467 wake_up(&mddev->sb_wait);
3468 err = 0;
3469 } else {
3470 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003471 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003472 err = do_md_run(mddev);
3473 }
3474 break;
3475 case write_pending:
3476 case active_idle:
3477 /* these cannot be set */
3478 break;
3479 }
3480 if (err)
3481 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003482 else {
NeilBrown00bcb4a2010-06-01 19:37:23 +10003483 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003484 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003485 }
NeilBrown9e653b62006-06-26 00:27:58 -07003486}
NeilBrown80ca3a42006-07-10 04:44:18 -07003487static struct md_sysfs_entry md_array_state =
3488__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003489
NeilBrown6d7ff732006-01-06 00:21:16 -08003490static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003491max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3492 return sprintf(page, "%d\n",
3493 atomic_read(&mddev->max_corr_read_errors));
3494}
3495
3496static ssize_t
3497max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3498{
3499 char *e;
3500 unsigned long n = simple_strtoul(buf, &e, 10);
3501
3502 if (*buf && (*e == 0 || *e == '\n')) {
3503 atomic_set(&mddev->max_corr_read_errors, n);
3504 return len;
3505 }
3506 return -EINVAL;
3507}
3508
3509static struct md_sysfs_entry max_corr_read_errors =
3510__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3511 max_corrected_read_errors_store);
3512
3513static ssize_t
NeilBrown6d7ff732006-01-06 00:21:16 -08003514null_show(mddev_t *mddev, char *page)
3515{
3516 return -EINVAL;
3517}
3518
3519static ssize_t
3520new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3521{
3522 /* buf must be %d:%d\n? giving major and minor numbers */
3523 /* The new device is added to the array.
3524 * If the array has a persistent superblock, we read the
3525 * superblock to initialise info and check validity.
3526 * Otherwise, only checking done is that in bind_rdev_to_array,
3527 * which mainly checks size.
3528 */
3529 char *e;
3530 int major = simple_strtoul(buf, &e, 10);
3531 int minor;
3532 dev_t dev;
3533 mdk_rdev_t *rdev;
3534 int err;
3535
3536 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3537 return -EINVAL;
3538 minor = simple_strtoul(e+1, &e, 10);
3539 if (*e && *e != '\n')
3540 return -EINVAL;
3541 dev = MKDEV(major, minor);
3542 if (major != MAJOR(dev) ||
3543 minor != MINOR(dev))
3544 return -EOVERFLOW;
3545
3546
3547 if (mddev->persistent) {
3548 rdev = md_import_device(dev, mddev->major_version,
3549 mddev->minor_version);
3550 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3551 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3552 mdk_rdev_t, same_set);
3553 err = super_types[mddev->major_version]
3554 .load_super(rdev, rdev0, mddev->minor_version);
3555 if (err < 0)
3556 goto out;
3557 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003558 } else if (mddev->external)
3559 rdev = md_import_device(dev, -2, -1);
3560 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003561 rdev = md_import_device(dev, -1, -1);
3562
3563 if (IS_ERR(rdev))
3564 return PTR_ERR(rdev);
3565 err = bind_rdev_to_array(rdev, mddev);
3566 out:
3567 if (err)
3568 export_rdev(rdev);
3569 return err ? err : len;
3570}
3571
3572static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003573__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003574
3575static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003576bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3577{
3578 char *end;
3579 unsigned long chunk, end_chunk;
3580
3581 if (!mddev->bitmap)
3582 goto out;
3583 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3584 while (*buf) {
3585 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3586 if (buf == end) break;
3587 if (*end == '-') { /* range */
3588 buf = end + 1;
3589 end_chunk = simple_strtoul(buf, &end, 0);
3590 if (buf == end) break;
3591 }
3592 if (*end && !isspace(*end)) break;
3593 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003594 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003595 }
3596 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3597out:
3598 return len;
3599}
3600
3601static struct md_sysfs_entry md_bitmap =
3602__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3603
3604static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003605size_show(mddev_t *mddev, char *page)
3606{
Andre Noll58c0fed2009-03-31 14:33:13 +11003607 return sprintf(page, "%llu\n",
3608 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003609}
3610
Andre Nolld71f9f82008-07-11 22:02:22 +10003611static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003612
3613static ssize_t
3614size_store(mddev_t *mddev, const char *buf, size_t len)
3615{
3616 /* If array is inactive, we can reduce the component size, but
3617 * not increase it (except from 0).
3618 * If array is active, we can try an on-line resize
3619 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003620 sector_t sectors;
3621 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003622
Andre Noll58c0fed2009-03-31 14:33:13 +11003623 if (err < 0)
3624 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003625 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003626 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003627 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003628 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003629 if (mddev->dev_sectors == 0 ||
3630 mddev->dev_sectors > sectors)
3631 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003632 else
3633 err = -ENOSPC;
3634 }
3635 return err ? err : len;
3636}
3637
3638static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003639__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003640
NeilBrown8bb93aa2006-01-06 00:20:50 -08003641
3642/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003643 * This is one of
3644 * 'none' for arrays with no metadata (good luck...)
3645 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003646 * or N.M for internally known formats
3647 */
3648static ssize_t
3649metadata_show(mddev_t *mddev, char *page)
3650{
3651 if (mddev->persistent)
3652 return sprintf(page, "%d.%d\n",
3653 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003654 else if (mddev->external)
3655 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003656 else
3657 return sprintf(page, "none\n");
3658}
3659
3660static ssize_t
3661metadata_store(mddev_t *mddev, const char *buf, size_t len)
3662{
3663 int major, minor;
3664 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003665 /* Changing the details of 'external' metadata is
3666 * always permitted. Otherwise there must be
3667 * no devices attached to the array.
3668 */
3669 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3670 ;
3671 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003672 return -EBUSY;
3673
3674 if (cmd_match(buf, "none")) {
3675 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003676 mddev->external = 0;
3677 mddev->major_version = 0;
3678 mddev->minor_version = 90;
3679 return len;
3680 }
3681 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003682 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003683 if (namelen >= sizeof(mddev->metadata_type))
3684 namelen = sizeof(mddev->metadata_type)-1;
3685 strncpy(mddev->metadata_type, buf+9, namelen);
3686 mddev->metadata_type[namelen] = 0;
3687 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3688 mddev->metadata_type[--namelen] = 0;
3689 mddev->persistent = 0;
3690 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003691 mddev->major_version = 0;
3692 mddev->minor_version = 90;
3693 return len;
3694 }
3695 major = simple_strtoul(buf, &e, 10);
3696 if (e==buf || *e != '.')
3697 return -EINVAL;
3698 buf = e+1;
3699 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003700 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003701 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003702 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003703 return -ENOENT;
3704 mddev->major_version = major;
3705 mddev->minor_version = minor;
3706 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003707 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003708 return len;
3709}
3710
3711static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003712__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003713
NeilBrowna35b0d62006-01-06 00:20:49 -08003714static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003715action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003716{
NeilBrown7eec3142005-11-08 21:39:44 -08003717 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003718 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3719 type = "frozen";
3720 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003721 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003722 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3723 type = "reshape";
3724 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003725 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3726 type = "resync";
3727 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3728 type = "check";
3729 else
3730 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003731 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003732 type = "recover";
3733 }
3734 return sprintf(page, "%s\n", type);
3735}
3736
3737static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003738action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003739{
NeilBrown7eec3142005-11-08 21:39:44 -08003740 if (!mddev->pers || !mddev->pers->sync_request)
3741 return -EINVAL;
3742
NeilBrownb6a9ce62009-05-26 09:41:17 +10003743 if (cmd_match(page, "frozen"))
3744 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3745 else
3746 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3747
3748 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003749 if (mddev->sync_thread) {
3750 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3751 md_unregister_thread(mddev->sync_thread);
3752 mddev->sync_thread = NULL;
3753 mddev->recovery = 0;
3754 }
NeilBrown03c902e2006-01-06 00:20:46 -08003755 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3756 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003757 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003758 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003759 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003760 else if (cmd_match(page, "recover")) {
3761 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3762 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3763 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003764 int err;
3765 if (mddev->pers->start_reshape == NULL)
3766 return -EINVAL;
3767 err = mddev->pers->start_reshape(mddev);
3768 if (err)
3769 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003770 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003771 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003772 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003773 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003774 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003775 return -EINVAL;
3776 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3777 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003778 }
NeilBrown03c902e2006-01-06 00:20:46 -08003779 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003780 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10003781 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003782 return len;
3783}
3784
NeilBrown9d888832005-11-08 21:39:26 -08003785static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003786mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003787{
3788 return sprintf(page, "%llu\n",
3789 (unsigned long long) mddev->resync_mismatches);
3790}
3791
NeilBrown80ca3a42006-07-10 04:44:18 -07003792static struct md_sysfs_entry md_scan_mode =
3793__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003794
NeilBrown96de1e62005-11-08 21:39:39 -08003795
NeilBrown80ca3a42006-07-10 04:44:18 -07003796static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003797
NeilBrown88202a02006-01-06 00:21:36 -08003798static ssize_t
3799sync_min_show(mddev_t *mddev, char *page)
3800{
3801 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3802 mddev->sync_speed_min ? "local": "system");
3803}
3804
3805static ssize_t
3806sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3807{
3808 int min;
3809 char *e;
3810 if (strncmp(buf, "system", 6)==0) {
3811 mddev->sync_speed_min = 0;
3812 return len;
3813 }
3814 min = simple_strtoul(buf, &e, 10);
3815 if (buf == e || (*e && *e != '\n') || min <= 0)
3816 return -EINVAL;
3817 mddev->sync_speed_min = min;
3818 return len;
3819}
3820
3821static struct md_sysfs_entry md_sync_min =
3822__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3823
3824static ssize_t
3825sync_max_show(mddev_t *mddev, char *page)
3826{
3827 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3828 mddev->sync_speed_max ? "local": "system");
3829}
3830
3831static ssize_t
3832sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3833{
3834 int max;
3835 char *e;
3836 if (strncmp(buf, "system", 6)==0) {
3837 mddev->sync_speed_max = 0;
3838 return len;
3839 }
3840 max = simple_strtoul(buf, &e, 10);
3841 if (buf == e || (*e && *e != '\n') || max <= 0)
3842 return -EINVAL;
3843 mddev->sync_speed_max = max;
3844 return len;
3845}
3846
3847static struct md_sysfs_entry md_sync_max =
3848__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3849
Iustin Popd7f3d292007-10-16 23:30:54 -07003850static ssize_t
3851degraded_show(mddev_t *mddev, char *page)
3852{
3853 return sprintf(page, "%d\n", mddev->degraded);
3854}
3855static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003856
3857static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003858sync_force_parallel_show(mddev_t *mddev, char *page)
3859{
3860 return sprintf(page, "%d\n", mddev->parallel_resync);
3861}
3862
3863static ssize_t
3864sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3865{
3866 long n;
3867
3868 if (strict_strtol(buf, 10, &n))
3869 return -EINVAL;
3870
3871 if (n != 0 && n != 1)
3872 return -EINVAL;
3873
3874 mddev->parallel_resync = n;
3875
3876 if (mddev->sync_thread)
3877 wake_up(&resync_wait);
3878
3879 return len;
3880}
3881
3882/* force parallel resync, even with shared block devices */
3883static struct md_sysfs_entry md_sync_force_parallel =
3884__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3885 sync_force_parallel_show, sync_force_parallel_store);
3886
3887static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003888sync_speed_show(mddev_t *mddev, char *page)
3889{
3890 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003891 if (mddev->curr_resync == 0)
3892 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003893 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3894 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003895 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003896 db = resync - mddev->resync_mark_cnt;
3897 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003898}
3899
NeilBrown80ca3a42006-07-10 04:44:18 -07003900static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003901
3902static ssize_t
3903sync_completed_show(mddev_t *mddev, char *page)
3904{
Andre Noll58c0fed2009-03-31 14:33:13 +11003905 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003906
NeilBrownacb180b2009-04-14 16:28:34 +10003907 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3908 return sprintf(page, "none\n");
3909
NeilBrown88202a02006-01-06 00:21:36 -08003910 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003911 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003912 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003913 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003914
NeilBrownacb180b2009-04-14 16:28:34 +10003915 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003916 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003917}
3918
NeilBrown80ca3a42006-07-10 04:44:18 -07003919static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003920
NeilBrowne464eaf2006-03-27 01:18:14 -08003921static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003922min_sync_show(mddev_t *mddev, char *page)
3923{
3924 return sprintf(page, "%llu\n",
3925 (unsigned long long)mddev->resync_min);
3926}
3927static ssize_t
3928min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3929{
3930 unsigned long long min;
3931 if (strict_strtoull(buf, 10, &min))
3932 return -EINVAL;
3933 if (min > mddev->resync_max)
3934 return -EINVAL;
3935 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3936 return -EBUSY;
3937
3938 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003939 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003940 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003941 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003942 return -EINVAL;
3943 }
3944 mddev->resync_min = min;
3945
3946 return len;
3947}
3948
3949static struct md_sysfs_entry md_min_sync =
3950__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3951
3952static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003953max_sync_show(mddev_t *mddev, char *page)
3954{
3955 if (mddev->resync_max == MaxSector)
3956 return sprintf(page, "max\n");
3957 else
3958 return sprintf(page, "%llu\n",
3959 (unsigned long long)mddev->resync_max);
3960}
3961static ssize_t
3962max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3963{
3964 if (strncmp(buf, "max", 3) == 0)
3965 mddev->resync_max = MaxSector;
3966 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003967 unsigned long long max;
3968 if (strict_strtoull(buf, 10, &max))
3969 return -EINVAL;
3970 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003971 return -EINVAL;
3972 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003973 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003974 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3975 return -EBUSY;
3976
3977 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003978 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003979 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003980 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003981 return -EINVAL;
3982 }
3983 mddev->resync_max = max;
3984 }
3985 wake_up(&mddev->recovery_wait);
3986 return len;
3987}
3988
3989static struct md_sysfs_entry md_max_sync =
3990__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3991
3992static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003993suspend_lo_show(mddev_t *mddev, char *page)
3994{
3995 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3996}
3997
3998static ssize_t
3999suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
4000{
4001 char *e;
4002 unsigned long long new = simple_strtoull(buf, &e, 10);
4003
NeilBrownb8d966e2009-07-01 11:14:04 +10004004 if (mddev->pers == NULL ||
4005 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004006 return -EINVAL;
4007 if (buf == e || (*e && *e != '\n'))
4008 return -EINVAL;
4009 if (new >= mddev->suspend_hi ||
4010 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
4011 mddev->suspend_lo = new;
4012 mddev->pers->quiesce(mddev, 2);
4013 return len;
4014 } else
4015 return -EINVAL;
4016}
4017static struct md_sysfs_entry md_suspend_lo =
4018__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4019
4020
4021static ssize_t
4022suspend_hi_show(mddev_t *mddev, char *page)
4023{
4024 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4025}
4026
4027static ssize_t
4028suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
4029{
4030 char *e;
4031 unsigned long long new = simple_strtoull(buf, &e, 10);
4032
NeilBrownb8d966e2009-07-01 11:14:04 +10004033 if (mddev->pers == NULL ||
4034 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004035 return -EINVAL;
4036 if (buf == e || (*e && *e != '\n'))
4037 return -EINVAL;
4038 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
4039 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
4040 mddev->suspend_hi = new;
4041 mddev->pers->quiesce(mddev, 1);
4042 mddev->pers->quiesce(mddev, 0);
4043 return len;
4044 } else
4045 return -EINVAL;
4046}
4047static struct md_sysfs_entry md_suspend_hi =
4048__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4049
NeilBrown08a02ec2007-05-09 02:35:38 -07004050static ssize_t
4051reshape_position_show(mddev_t *mddev, char *page)
4052{
4053 if (mddev->reshape_position != MaxSector)
4054 return sprintf(page, "%llu\n",
4055 (unsigned long long)mddev->reshape_position);
4056 strcpy(page, "none\n");
4057 return 5;
4058}
4059
4060static ssize_t
4061reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
4062{
4063 char *e;
4064 unsigned long long new = simple_strtoull(buf, &e, 10);
4065 if (mddev->pers)
4066 return -EBUSY;
4067 if (buf == e || (*e && *e != '\n'))
4068 return -EINVAL;
4069 mddev->reshape_position = new;
4070 mddev->delta_disks = 0;
4071 mddev->new_level = mddev->level;
4072 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004073 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004074 return len;
4075}
4076
4077static struct md_sysfs_entry md_reshape_position =
4078__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4079 reshape_position_store);
4080
Dan Williamsb522adc2009-03-31 15:00:31 +11004081static ssize_t
4082array_size_show(mddev_t *mddev, char *page)
4083{
4084 if (mddev->external_size)
4085 return sprintf(page, "%llu\n",
4086 (unsigned long long)mddev->array_sectors/2);
4087 else
4088 return sprintf(page, "default\n");
4089}
4090
4091static ssize_t
4092array_size_store(mddev_t *mddev, const char *buf, size_t len)
4093{
4094 sector_t sectors;
4095
4096 if (strncmp(buf, "default", 7) == 0) {
4097 if (mddev->pers)
4098 sectors = mddev->pers->size(mddev, 0, 0);
4099 else
4100 sectors = mddev->array_sectors;
4101
4102 mddev->external_size = 0;
4103 } else {
4104 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4105 return -EINVAL;
4106 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004107 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004108
4109 mddev->external_size = 1;
4110 }
4111
4112 mddev->array_sectors = sectors;
4113 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004114 if (mddev->pers)
4115 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11004116
4117 return len;
4118}
4119
4120static struct md_sysfs_entry md_array_size =
4121__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4122 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004123
NeilBrowneae17012005-11-08 21:39:23 -08004124static struct attribute *md_default_attrs[] = {
4125 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004126 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004127 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004128 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004129 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004130 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004131 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004132 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004133 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004134 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004135 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004136 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004137 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004138 NULL,
4139};
4140
4141static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004142 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004143 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004144 &md_sync_min.attr,
4145 &md_sync_max.attr,
4146 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004147 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004148 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004149 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004150 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004151 &md_suspend_lo.attr,
4152 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004153 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004154 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004155 NULL,
4156};
NeilBrown411036f2005-11-08 21:39:40 -08004157static struct attribute_group md_redundancy_group = {
4158 .name = NULL,
4159 .attrs = md_redundancy_attrs,
4160};
4161
NeilBrowneae17012005-11-08 21:39:23 -08004162
4163static ssize_t
4164md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4165{
4166 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4167 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004168 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004169
4170 if (!entry->show)
4171 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004172 rv = mddev_lock(mddev);
4173 if (!rv) {
4174 rv = entry->show(mddev, page);
4175 mddev_unlock(mddev);
4176 }
NeilBrown96de1e62005-11-08 21:39:39 -08004177 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004178}
4179
4180static ssize_t
4181md_attr_store(struct kobject *kobj, struct attribute *attr,
4182 const char *page, size_t length)
4183{
4184 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4185 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004186 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004187
4188 if (!entry->store)
4189 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004190 if (!capable(CAP_SYS_ADMIN))
4191 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004192 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004193 if (mddev->hold_active == UNTIL_IOCTL)
4194 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004195 if (!rv) {
4196 rv = entry->store(mddev, page, length);
4197 mddev_unlock(mddev);
4198 }
NeilBrown96de1e62005-11-08 21:39:39 -08004199 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004200}
4201
4202static void md_free(struct kobject *ko)
4203{
4204 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004205
4206 if (mddev->sysfs_state)
4207 sysfs_put(mddev->sysfs_state);
4208
4209 if (mddev->gendisk) {
4210 del_gendisk(mddev->gendisk);
4211 put_disk(mddev->gendisk);
4212 }
4213 if (mddev->queue)
4214 blk_cleanup_queue(mddev->queue);
4215
NeilBrowneae17012005-11-08 21:39:23 -08004216 kfree(mddev);
4217}
4218
Emese Revfy52cf25d2010-01-19 02:58:23 +01004219static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004220 .show = md_attr_show,
4221 .store = md_attr_store,
4222};
4223static struct kobj_type md_ktype = {
4224 .release = md_free,
4225 .sysfs_ops = &md_sysfs_ops,
4226 .default_attrs = md_default_attrs,
4227};
4228
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229int mdp_major = 0;
4230
Dan Williams5fd3a172009-03-04 00:57:25 -07004231static void mddev_delayed_delete(struct work_struct *ws)
4232{
4233 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4234
NeilBrown43a70502009-12-14 12:49:55 +11004235 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004236 kobject_del(&mddev->kobj);
4237 kobject_put(&mddev->kobj);
4238}
4239
NeilBrownefeb53c2009-01-09 08:31:10 +11004240static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004242 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 mddev_t *mddev = mddev_find(dev);
4244 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004245 int partitioned;
4246 int shift;
4247 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004248 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
4250 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004251 return -ENODEV;
4252
4253 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4254 shift = partitioned ? MdpMinorShift : 0;
4255 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
NeilBrownd3374822009-01-09 08:31:10 +11004257 /* wait for any previous instance if this device
4258 * to be completed removed (mddev_delayed_delete).
4259 */
4260 flush_scheduled_work();
4261
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004262 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004263 error = -EEXIST;
4264 if (mddev->gendisk)
4265 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004266
4267 if (name) {
4268 /* Need to ensure that 'name' is not a duplicate.
4269 */
4270 mddev_t *mddev2;
4271 spin_lock(&all_mddevs_lock);
4272
4273 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4274 if (mddev2->gendisk &&
4275 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4276 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004277 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004278 }
4279 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 }
NeilBrown8b765392009-01-09 08:31:08 +11004281
NeilBrown0909dc42009-07-01 12:27:21 +10004282 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004283 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004284 if (!mddev->queue)
4285 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004286 mddev->queue->queuedata = mddev;
4287
NeilBrown8b765392009-01-09 08:31:08 +11004288 /* Can be unlocked because the queue is new: no concurrency */
4289 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4290
NeilBrown409c57f2009-03-31 14:39:39 +11004291 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004292
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 disk = alloc_disk(1 << shift);
4294 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004295 blk_cleanup_queue(mddev->queue);
4296 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004297 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004299 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004301 if (name)
4302 strcpy(disk->disk_name, name);
4303 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004305 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 disk->fops = &md_fops;
4308 disk->private_data = mddev;
4309 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004310 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004311 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004312 * remove it now.
4313 */
4314 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 add_disk(disk);
4316 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004317 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4318 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004319 if (error) {
4320 /* This isn't possible, but as kobject_init_and_add is marked
4321 * __must_check, we must do something with the result
4322 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004323 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4324 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004325 error = 0;
4326 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004327 if (mddev->kobj.sd &&
4328 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004329 printk(KERN_DEBUG "pointless warning\n");
NeilBrown0909dc42009-07-01 12:27:21 +10004330 abort:
4331 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004332 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004333 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004334 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004335 }
NeilBrownd3374822009-01-09 08:31:10 +11004336 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004337 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004338}
4339
4340static struct kobject *md_probe(dev_t dev, int *part, void *data)
4341{
4342 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 return NULL;
4344}
4345
NeilBrownefeb53c2009-01-09 08:31:10 +11004346static int add_named_array(const char *val, struct kernel_param *kp)
4347{
4348 /* val must be "md_*" where * is not all digits.
4349 * We allocate an array with a large free minor number, and
4350 * set the name to val. val must not already be an active name.
4351 */
4352 int len = strlen(val);
4353 char buf[DISK_NAME_LEN];
4354
4355 while (len && val[len-1] == '\n')
4356 len--;
4357 if (len >= DISK_NAME_LEN)
4358 return -E2BIG;
4359 strlcpy(buf, val, len+1);
4360 if (strncmp(buf, "md_", 3) != 0)
4361 return -EINVAL;
4362 return md_alloc(0, buf);
4363}
4364
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365static void md_safemode_timeout(unsigned long data)
4366{
4367 mddev_t *mddev = (mddev_t *) data;
4368
Neil Brown0fd62b82008-06-28 08:31:36 +10004369 if (!atomic_read(&mddev->writes_pending)) {
4370 mddev->safemode = 1;
4371 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004372 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 md_wakeup_thread(mddev->thread);
4375}
4376
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004377static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378
NeilBrown390ee602010-06-01 19:37:27 +10004379int md_run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380{
NeilBrown2604b702006-01-06 00:20:36 -08004381 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 mdk_rdev_t *rdev;
NeilBrown2604b702006-01-06 00:20:36 -08004383 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
NeilBrowna757e642005-04-16 15:26:42 -07004385 if (list_empty(&mddev->disks))
4386 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
4389 if (mddev->pers)
4390 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004391 /* Cannot run until previous stop completes properly */
4392 if (mddev->sysfs_active)
4393 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004394
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 /*
4396 * Analyze all RAID superblock(s)
4397 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004398 if (!mddev->raid_disks) {
4399 if (!mddev->persistent)
4400 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004401 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403
NeilBrownd9d166c2006-01-06 00:20:51 -08004404 if (mddev->level != LEVEL_NONE)
4405 request_module("md-level-%d", mddev->level);
4406 else if (mddev->clevel[0])
4407 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408
4409 /*
4410 * Drop all container device buffers, from now on
4411 * the only valid external interface is through the md
4412 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004414 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004415 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 continue;
4417 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004418 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004419
4420 /* perform some consistency tests on the device.
4421 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004422 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004423 */
Andre Noll0f420352008-07-11 22:02:23 +10004424 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004425 if (mddev->dev_sectors &&
4426 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004427 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004428 printk("md: %s: data overlaps metadata\n",
4429 mdname(mddev));
4430 return -EINVAL;
4431 }
4432 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004433 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004434 > rdev->data_offset) {
4435 printk("md: %s: metadata overlaps data\n",
4436 mdname(mddev));
4437 return -EINVAL;
4438 }
4439 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004440 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 }
4442
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004444 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004445 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004447 if (mddev->level != LEVEL_NONE)
4448 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4449 mddev->level);
4450 else
4451 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4452 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 return -EINVAL;
4454 }
NeilBrown2604b702006-01-06 00:20:36 -08004455 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004457 if (mddev->level != pers->level) {
4458 mddev->level = pers->level;
4459 mddev->new_level = pers->level;
4460 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004461 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
NeilBrownf6705572006-03-27 01:18:11 -08004463 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004464 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004465 /* This personality cannot handle reshaping... */
4466 mddev->pers = NULL;
4467 module_put(pers->owner);
4468 return -EINVAL;
4469 }
4470
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004471 if (pers->sync_request) {
4472 /* Warn if this is a potentially silly
4473 * configuration.
4474 */
4475 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4476 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004477 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004478
4479 list_for_each_entry(rdev, &mddev->disks, same_set)
4480 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004481 if (rdev < rdev2 &&
4482 rdev->bdev->bd_contains ==
4483 rdev2->bdev->bd_contains) {
4484 printk(KERN_WARNING
4485 "%s: WARNING: %s appears to be"
4486 " on the same physical disk as"
4487 " %s.\n",
4488 mdname(mddev),
4489 bdevname(rdev->bdev,b),
4490 bdevname(rdev2->bdev,b2));
4491 warned = 1;
4492 }
4493 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004494
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004495 if (warned)
4496 printk(KERN_WARNING
4497 "True protection against single-disk"
4498 " failure might be compromised.\n");
4499 }
4500
NeilBrown657390d2005-08-26 18:34:16 -07004501 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004502 /* may be over-ridden by personality */
4503 mddev->resync_max_sectors = mddev->dev_sectors;
4504
NeilBrowna9701a32005-11-08 21:39:34 -08004505 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004506 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507
NeilBrown0f9552b2009-12-30 12:08:50 +11004508 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004509 mddev->ro = 2; /* read-only, but switch on first write */
4510
NeilBrownb15c2e52006-01-06 00:20:16 -08004511 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004512 if (err)
4513 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004514 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4515 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4516 " but 'external_size' not in effect?\n", __func__);
4517 printk(KERN_ERR
4518 "md: invalid array_size %llu > default size %llu\n",
4519 (unsigned long long)mddev->array_sectors / 2,
4520 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4521 err = -EINVAL;
4522 mddev->pers->stop(mddev);
4523 }
4524 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004525 err = bitmap_create(mddev);
4526 if (err) {
4527 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4528 mdname(mddev), err);
4529 mddev->pers->stop(mddev);
4530 }
4531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 module_put(mddev->pers->owner);
4534 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004535 bitmap_destroy(mddev);
4536 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004538 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004539 if (mddev->kobj.sd &&
4540 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004541 printk(KERN_WARNING
4542 "md: cannot register extra attributes for %s\n",
4543 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004544 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004545 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004546 mddev->ro = 0;
4547
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004549 atomic_set(&mddev->max_corr_read_errors,
4550 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 mddev->safemode = 0;
4552 mddev->safemode_timer.function = md_safemode_timeout;
4553 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004554 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004556
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004557 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004558 if (rdev->raid_disk >= 0) {
4559 char nm[20];
4560 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004561 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004562 /* failure here is OK */;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564
4565 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4566
NeilBrown850b2b42006-10-03 01:15:46 -07004567 if (mddev->flags)
4568 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569
NeilBrown0b8c9de2006-07-10 04:44:15 -07004570 md_wakeup_thread(mddev->thread);
4571 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004572
NeilBrownd7603b72006-01-06 00:20:30 -08004573 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004574 sysfs_notify_dirent_safe(mddev->sysfs_state);
4575 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004576 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 return 0;
4578}
NeilBrown390ee602010-06-01 19:37:27 +10004579EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580
NeilBrownfe60b012010-03-29 11:10:42 +11004581static int do_md_run(mddev_t *mddev)
4582{
4583 int err;
4584
4585 err = md_run(mddev);
4586 if (err)
4587 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10004588 err = bitmap_load(mddev);
4589 if (err) {
4590 bitmap_destroy(mddev);
4591 goto out;
4592 }
NeilBrownfe60b012010-03-29 11:10:42 +11004593 set_capacity(mddev->gendisk, mddev->array_sectors);
4594 revalidate_disk(mddev->gendisk);
4595 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4596out:
4597 return err;
4598}
4599
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600static int restart_array(mddev_t *mddev)
4601{
4602 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603
Andre Noll80fab1d2008-07-11 22:02:21 +10004604 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004606 return -ENXIO;
4607 if (!mddev->pers)
4608 return -EINVAL;
4609 if (!mddev->ro)
4610 return -EBUSY;
4611 mddev->safemode = 0;
4612 mddev->ro = 0;
4613 set_disk_ro(disk, 0);
4614 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4615 mdname(mddev));
4616 /* Kick recovery or resync if necessary */
4617 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4618 md_wakeup_thread(mddev->thread);
4619 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004620 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004621 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622}
4623
NeilBrownacc55e22006-06-26 00:27:47 -07004624/* similar to deny_write_access, but accounts for our holding a reference
4625 * to the file ourselves */
4626static int deny_bitmap_write_access(struct file * file)
4627{
4628 struct inode *inode = file->f_mapping->host;
4629
4630 spin_lock(&inode->i_lock);
4631 if (atomic_read(&inode->i_writecount) > 1) {
4632 spin_unlock(&inode->i_lock);
4633 return -ETXTBSY;
4634 }
4635 atomic_set(&inode->i_writecount, -1);
4636 spin_unlock(&inode->i_lock);
4637
4638 return 0;
4639}
4640
NeilBrown43a70502009-12-14 12:49:55 +11004641void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004642{
4643 struct inode *inode = file->f_mapping->host;
4644
4645 spin_lock(&inode->i_lock);
4646 atomic_set(&inode->i_writecount, 1);
4647 spin_unlock(&inode->i_lock);
4648}
4649
NeilBrown6177b472010-03-29 11:37:13 +11004650static void md_clean(mddev_t *mddev)
4651{
4652 mddev->array_sectors = 0;
4653 mddev->external_size = 0;
4654 mddev->dev_sectors = 0;
4655 mddev->raid_disks = 0;
4656 mddev->recovery_cp = 0;
4657 mddev->resync_min = 0;
4658 mddev->resync_max = MaxSector;
4659 mddev->reshape_position = MaxSector;
4660 mddev->external = 0;
4661 mddev->persistent = 0;
4662 mddev->level = LEVEL_NONE;
4663 mddev->clevel[0] = 0;
4664 mddev->flags = 0;
4665 mddev->ro = 0;
4666 mddev->metadata_type[0] = 0;
4667 mddev->chunk_sectors = 0;
4668 mddev->ctime = mddev->utime = 0;
4669 mddev->layout = 0;
4670 mddev->max_disks = 0;
4671 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10004672 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004673 mddev->delta_disks = 0;
4674 mddev->new_level = LEVEL_NONE;
4675 mddev->new_layout = 0;
4676 mddev->new_chunk_sectors = 0;
4677 mddev->curr_resync = 0;
4678 mddev->resync_mismatches = 0;
4679 mddev->suspend_lo = mddev->suspend_hi = 0;
4680 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4681 mddev->recovery = 0;
4682 mddev->in_sync = 0;
4683 mddev->degraded = 0;
4684 mddev->barriers_work = 0;
4685 mddev->safemode = 0;
4686 mddev->bitmap_info.offset = 0;
4687 mddev->bitmap_info.default_offset = 0;
4688 mddev->bitmap_info.chunksize = 0;
4689 mddev->bitmap_info.daemon_sleep = 0;
4690 mddev->bitmap_info.max_write_behind = 0;
NeilBrown252ac522010-06-01 19:37:29 +10004691 mddev->plug = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004692}
4693
NeilBrown390ee602010-06-01 19:37:27 +10004694void md_stop_writes(mddev_t *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11004695{
4696 if (mddev->sync_thread) {
4697 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4698 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4699 md_unregister_thread(mddev->sync_thread);
4700 mddev->sync_thread = NULL;
4701 }
4702
4703 del_timer_sync(&mddev->safemode_timer);
4704
4705 bitmap_flush(mddev);
4706 md_super_wait(mddev);
4707
4708 if (!mddev->in_sync || mddev->flags) {
4709 /* mark array as shutdown cleanly */
4710 mddev->in_sync = 1;
4711 md_update_sb(mddev, 1);
4712 }
4713}
NeilBrown390ee602010-06-01 19:37:27 +10004714EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11004715
NeilBrown390ee602010-06-01 19:37:27 +10004716void md_stop(mddev_t *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11004717{
4718 mddev->pers->stop(mddev);
4719 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4720 mddev->to_remove = &md_redundancy_group;
4721 module_put(mddev->pers->owner);
4722 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11004723 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11004724}
NeilBrown390ee602010-06-01 19:37:27 +10004725EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11004726
NeilBrowna4bd82d2010-03-29 13:23:10 +11004727static int md_set_readonly(mddev_t *mddev, int is_open)
4728{
4729 int err = 0;
4730 mutex_lock(&mddev->open_mutex);
4731 if (atomic_read(&mddev->openers) > is_open) {
4732 printk("md: %s still in use.\n",mdname(mddev));
4733 err = -EBUSY;
4734 goto out;
4735 }
4736 if (mddev->pers) {
4737 md_stop_writes(mddev);
4738
4739 err = -ENXIO;
4740 if (mddev->ro==1)
4741 goto out;
4742 mddev->ro = 1;
4743 set_disk_ro(mddev->gendisk, 1);
4744 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004745 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004746 err = 0;
4747 }
4748out:
4749 mutex_unlock(&mddev->open_mutex);
4750 return err;
4751}
4752
NeilBrown9e653b62006-06-26 00:27:58 -07004753/* mode:
4754 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07004755 * 2 - stop but do not disassemble array
4756 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004757static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004760 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761
NeilBrownc8c00a62009-08-10 12:50:52 +10004762 mutex_lock(&mddev->open_mutex);
NeilBrownbb4f1e92010-08-08 21:18:03 +10004763 if (atomic_read(&mddev->openers) > is_open ||
4764 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004765 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10004766 mutex_unlock(&mddev->open_mutex);
4767 return -EBUSY;
4768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769
NeilBrown6e17b022010-08-07 21:41:19 +10004770 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11004771 if (mddev->ro)
4772 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004773
NeilBrown390ee602010-06-01 19:37:27 +10004774 md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004775 md_stop(mddev);
4776 mddev->queue->merge_bvec_fn = NULL;
4777 mddev->queue->unplug_fn = NULL;
4778 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004779
NeilBrowna4bd82d2010-03-29 13:23:10 +11004780 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10004781 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004782
NeilBrowna4bd82d2010-03-29 13:23:10 +11004783 list_for_each_entry(rdev, &mddev->disks, same_set)
4784 if (rdev->raid_disk >= 0) {
4785 char nm[20];
4786 sprintf(nm, "rd%d", rdev->raid_disk);
4787 sysfs_remove_link(&mddev->kobj, nm);
4788 }
NeilBrownc4647292009-05-07 12:51:06 +10004789
NeilBrowna4bd82d2010-03-29 13:23:10 +11004790 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10004791 mutex_unlock(&mddev->open_mutex);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004792 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08004793
NeilBrowna4bd82d2010-03-29 13:23:10 +11004794 if (mddev->ro)
4795 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10004796 } else
4797 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 /*
4799 * Free resources if final stop
4800 */
NeilBrown9e653b62006-06-26 00:27:58 -07004801 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4803
NeilBrown978f9462006-02-02 14:28:05 -08004804 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004805 if (mddev->bitmap_info.file) {
4806 restore_bitmap_write_access(mddev->bitmap_info.file);
4807 fput(mddev->bitmap_info.file);
4808 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004809 }
NeilBrownc3d97142009-12-14 12:49:52 +11004810 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004811
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 export_array(mddev);
4813
NeilBrown6177b472010-03-29 11:37:13 +11004814 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11004815 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004816 if (mddev->hold_active == UNTIL_STOP)
4817 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11004818 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004819 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004820 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004821 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10004822 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823}
4824
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004825#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826static void autorun_array(mddev_t *mddev)
4827{
4828 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 int err;
4830
NeilBrowna757e642005-04-16 15:26:42 -07004831 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
4834 printk(KERN_INFO "md: running: ");
4835
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004836 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 char b[BDEVNAME_SIZE];
4838 printk("<%s>", bdevname(rdev->bdev,b));
4839 }
4840 printk("\n");
4841
NeilBrownd710e132008-10-13 11:55:12 +11004842 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 if (err) {
4844 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004845 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 }
4847}
4848
4849/*
4850 * lets try to run arrays based on all disks that have arrived
4851 * until now. (those are in pending_raid_disks)
4852 *
4853 * the method: pick the first pending disk, collect all disks with
4854 * the same UUID, remove all from the pending list and put them into
4855 * the 'same_array' list. Then order this list based on superblock
4856 * update time (freshest comes first), kick out 'old' disks and
4857 * compare superblocks. If everything's fine then run it.
4858 *
4859 * If "unit" is allocated, then bump its reference count
4860 */
4861static void autorun_devices(int part)
4862{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004863 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 mddev_t *mddev;
4865 char b[BDEVNAME_SIZE];
4866
4867 printk(KERN_INFO "md: autorun ...\n");
4868 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004869 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004871 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 rdev0 = list_entry(pending_raid_disks.next,
4873 mdk_rdev_t, same_set);
4874
4875 printk(KERN_INFO "md: considering %s ...\n",
4876 bdevname(rdev0->bdev,b));
4877 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004878 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 if (super_90_load(rdev, rdev0, 0) >= 0) {
4880 printk(KERN_INFO "md: adding %s ...\n",
4881 bdevname(rdev->bdev,b));
4882 list_move(&rdev->same_set, &candidates);
4883 }
4884 /*
4885 * now we have a set of devices, with all of them having
4886 * mostly sane superblocks. It's time to allocate the
4887 * mddev.
4888 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004889 if (part) {
4890 dev = MKDEV(mdp_major,
4891 rdev0->preferred_minor << MdpMinorShift);
4892 unit = MINOR(dev) >> MdpMinorShift;
4893 } else {
4894 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4895 unit = MINOR(dev);
4896 }
4897 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4899 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4900 break;
4901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902
4903 md_probe(dev, NULL, NULL);
4904 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004905 if (!mddev || !mddev->gendisk) {
4906 if (mddev)
4907 mddev_put(mddev);
4908 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 "md: cannot allocate memory for md drive.\n");
4910 break;
4911 }
4912 if (mddev_lock(mddev))
4913 printk(KERN_WARNING "md: %s locked, cannot run\n",
4914 mdname(mddev));
4915 else if (mddev->raid_disks || mddev->major_version
4916 || !list_empty(&mddev->disks)) {
4917 printk(KERN_WARNING
4918 "md: %s already running, cannot run %s\n",
4919 mdname(mddev), bdevname(rdev0->bdev,b));
4920 mddev_unlock(mddev);
4921 } else {
4922 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004923 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004924 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 list_del_init(&rdev->same_set);
4926 if (bind_rdev_to_array(rdev, mddev))
4927 export_rdev(rdev);
4928 }
4929 autorun_array(mddev);
4930 mddev_unlock(mddev);
4931 }
4932 /* on success, candidates will be empty, on error
4933 * it won't...
4934 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004935 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004936 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 mddev_put(mddev);
4940 }
4941 printk(KERN_INFO "md: ... autorun DONE.\n");
4942}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004943#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945static int get_version(void __user * arg)
4946{
4947 mdu_version_t ver;
4948
4949 ver.major = MD_MAJOR_VERSION;
4950 ver.minor = MD_MINOR_VERSION;
4951 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4952
4953 if (copy_to_user(arg, &ver, sizeof(ver)))
4954 return -EFAULT;
4955
4956 return 0;
4957}
4958
4959static int get_array_info(mddev_t * mddev, void __user * arg)
4960{
4961 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004962 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964
NeilBrowna9f326e2009-09-23 18:06:41 +10004965 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004966 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004968 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 failed++;
4970 else {
4971 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004972 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004973 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 else
4975 spare++;
4976 }
4977 }
4978
4979 info.major_version = mddev->major_version;
4980 info.minor_version = mddev->minor_version;
4981 info.patch_version = MD_PATCHLEVEL_VERSION;
4982 info.ctime = mddev->ctime;
4983 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004984 info.size = mddev->dev_sectors / 2;
4985 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004986 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 info.nr_disks = nr;
4988 info.raid_disks = mddev->raid_disks;
4989 info.md_minor = mddev->md_minor;
4990 info.not_persistent= !mddev->persistent;
4991
4992 info.utime = mddev->utime;
4993 info.state = 0;
4994 if (mddev->in_sync)
4995 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11004996 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07004997 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004998 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 info.working_disks = working;
5000 info.failed_disks = failed;
5001 info.spare_disks = spare;
5002
5003 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005004 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005
5006 if (copy_to_user(arg, &info, sizeof(info)))
5007 return -EFAULT;
5008
5009 return 0;
5010}
5011
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005012static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005013{
5014 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5015 char *ptr, *buf = NULL;
5016 int err = -ENOMEM;
5017
Dan Williamsb5470dc2008-06-27 21:44:04 -07005018 if (md_allow_write(mddev))
5019 file = kmalloc(sizeof(*file), GFP_NOIO);
5020 else
5021 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005022
NeilBrown32a76272005-06-21 17:17:14 -07005023 if (!file)
5024 goto out;
5025
5026 /* bitmap disabled, zero the first byte and copy out */
5027 if (!mddev->bitmap || !mddev->bitmap->file) {
5028 file->pathname[0] = '\0';
5029 goto copy_out;
5030 }
5031
5032 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5033 if (!buf)
5034 goto out;
5035
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005036 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5037 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005038 goto out;
5039
5040 strcpy(file->pathname, ptr);
5041
5042copy_out:
5043 err = 0;
5044 if (copy_to_user(arg, file, sizeof(*file)))
5045 err = -EFAULT;
5046out:
5047 kfree(buf);
5048 kfree(file);
5049 return err;
5050}
5051
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052static int get_disk_info(mddev_t * mddev, void __user * arg)
5053{
5054 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 mdk_rdev_t *rdev;
5056
5057 if (copy_from_user(&info, arg, sizeof(info)))
5058 return -EFAULT;
5059
Andre Noll26ef3792008-07-11 22:02:21 +10005060 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 if (rdev) {
5062 info.major = MAJOR(rdev->bdev->bd_dev);
5063 info.minor = MINOR(rdev->bdev->bd_dev);
5064 info.raid_disk = rdev->raid_disk;
5065 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005066 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005068 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 info.state |= (1<<MD_DISK_ACTIVE);
5070 info.state |= (1<<MD_DISK_SYNC);
5071 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005072 if (test_bit(WriteMostly, &rdev->flags))
5073 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 } else {
5075 info.major = info.minor = 0;
5076 info.raid_disk = -1;
5077 info.state = (1<<MD_DISK_REMOVED);
5078 }
5079
5080 if (copy_to_user(arg, &info, sizeof(info)))
5081 return -EFAULT;
5082
5083 return 0;
5084}
5085
5086static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5087{
5088 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5089 mdk_rdev_t *rdev;
5090 dev_t dev = MKDEV(info->major,info->minor);
5091
5092 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5093 return -EOVERFLOW;
5094
5095 if (!mddev->raid_disks) {
5096 int err;
5097 /* expecting a device which has a superblock */
5098 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5099 if (IS_ERR(rdev)) {
5100 printk(KERN_WARNING
5101 "md: md_import_device returned %ld\n",
5102 PTR_ERR(rdev));
5103 return PTR_ERR(rdev);
5104 }
5105 if (!list_empty(&mddev->disks)) {
5106 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5107 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005108 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 .load_super(rdev, rdev0, mddev->minor_version);
5110 if (err < 0) {
5111 printk(KERN_WARNING
5112 "md: %s has different UUID to %s\n",
5113 bdevname(rdev->bdev,b),
5114 bdevname(rdev0->bdev,b2));
5115 export_rdev(rdev);
5116 return -EINVAL;
5117 }
5118 }
5119 err = bind_rdev_to_array(rdev, mddev);
5120 if (err)
5121 export_rdev(rdev);
5122 return err;
5123 }
5124
5125 /*
5126 * add_new_disk can be used once the array is assembled
5127 * to add "hot spares". They must already have a superblock
5128 * written
5129 */
5130 if (mddev->pers) {
5131 int err;
5132 if (!mddev->pers->hot_add_disk) {
5133 printk(KERN_WARNING
5134 "%s: personality does not support diskops!\n",
5135 mdname(mddev));
5136 return -EINVAL;
5137 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005138 if (mddev->persistent)
5139 rdev = md_import_device(dev, mddev->major_version,
5140 mddev->minor_version);
5141 else
5142 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 if (IS_ERR(rdev)) {
5144 printk(KERN_WARNING
5145 "md: md_import_device returned %ld\n",
5146 PTR_ERR(rdev));
5147 return PTR_ERR(rdev);
5148 }
NeilBrown41158c72005-06-21 17:17:25 -07005149 /* set save_raid_disk if appropriate */
5150 if (!mddev->persistent) {
5151 if (info->state & (1<<MD_DISK_SYNC) &&
5152 info->raid_disk < mddev->raid_disks)
5153 rdev->raid_disk = info->raid_disk;
5154 else
5155 rdev->raid_disk = -1;
5156 } else
5157 super_types[mddev->major_version].
5158 validate_super(mddev, rdev);
5159 rdev->saved_raid_disk = rdev->raid_disk;
5160
NeilBrownb2d444d2005-11-08 21:39:31 -08005161 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005162 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5163 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005164 else
5165 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005166
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 rdev->raid_disk = -1;
5168 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005169 if (!err && !mddev->pers->hot_remove_disk) {
5170 /* If there is hot_add_disk but no hot_remove_disk
5171 * then added disks for geometry changes,
5172 * and should be added immediately.
5173 */
5174 super_types[mddev->major_version].
5175 validate_super(mddev, rdev);
5176 err = mddev->pers->hot_add_disk(mddev, rdev);
5177 if (err)
5178 unbind_rdev_from_array(rdev);
5179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 if (err)
5181 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005182 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005183 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005184
NeilBrown17571282006-12-10 02:20:52 -08005185 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005186 if (mddev->degraded)
5187 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005188 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07005189 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 return err;
5191 }
5192
5193 /* otherwise, add_new_disk is only allowed
5194 * for major_version==0 superblocks
5195 */
5196 if (mddev->major_version != 0) {
5197 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5198 mdname(mddev));
5199 return -EINVAL;
5200 }
5201
5202 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5203 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005204 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 if (IS_ERR(rdev)) {
5206 printk(KERN_WARNING
5207 "md: error, md_import_device() returned %ld\n",
5208 PTR_ERR(rdev));
5209 return PTR_ERR(rdev);
5210 }
5211 rdev->desc_nr = info->number;
5212 if (info->raid_disk < mddev->raid_disks)
5213 rdev->raid_disk = info->raid_disk;
5214 else
5215 rdev->raid_disk = -1;
5216
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005218 if (info->state & (1<<MD_DISK_SYNC))
5219 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005221 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5222 set_bit(WriteMostly, &rdev->flags);
5223
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 if (!mddev->persistent) {
5225 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10005226 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 } else
Andre Noll0f420352008-07-11 22:02:23 +10005228 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10005229 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230
NeilBrown2bf071b2006-01-06 00:20:55 -08005231 err = bind_rdev_to_array(rdev, mddev);
5232 if (err) {
5233 export_rdev(rdev);
5234 return err;
5235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 }
5237
5238 return 0;
5239}
5240
5241static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5242{
5243 char b[BDEVNAME_SIZE];
5244 mdk_rdev_t *rdev;
5245
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 rdev = find_rdev(mddev, dev);
5247 if (!rdev)
5248 return -ENXIO;
5249
5250 if (rdev->raid_disk >= 0)
5251 goto busy;
5252
5253 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005254 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005255 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256
5257 return 0;
5258busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005259 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 bdevname(rdev->bdev,b), mdname(mddev));
5261 return -EBUSY;
5262}
5263
5264static int hot_add_disk(mddev_t * mddev, dev_t dev)
5265{
5266 char b[BDEVNAME_SIZE];
5267 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 mdk_rdev_t *rdev;
5269
5270 if (!mddev->pers)
5271 return -ENODEV;
5272
5273 if (mddev->major_version != 0) {
5274 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5275 " version-0 superblocks.\n",
5276 mdname(mddev));
5277 return -EINVAL;
5278 }
5279 if (!mddev->pers->hot_add_disk) {
5280 printk(KERN_WARNING
5281 "%s: personality does not support diskops!\n",
5282 mdname(mddev));
5283 return -EINVAL;
5284 }
5285
NeilBrownd710e132008-10-13 11:55:12 +11005286 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 if (IS_ERR(rdev)) {
5288 printk(KERN_WARNING
5289 "md: error, md_import_device() returned %ld\n",
5290 PTR_ERR(rdev));
5291 return -EINVAL;
5292 }
5293
5294 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10005295 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 else
Andre Noll0f420352008-07-11 22:02:23 +10005297 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298
NeilBrown8190e752009-06-18 08:48:58 +10005299 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300
NeilBrownb2d444d2005-11-08 21:39:31 -08005301 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 printk(KERN_WARNING
5303 "md: can not hot-add faulty %s disk to %s!\n",
5304 bdevname(rdev->bdev,b), mdname(mddev));
5305 err = -EINVAL;
5306 goto abort_export;
5307 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005308 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005310 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005311 err = bind_rdev_to_array(rdev, mddev);
5312 if (err)
5313 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314
5315 /*
5316 * The rest should better be atomic, we can have disk failures
5317 * noticed in interrupt contexts ...
5318 */
5319
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 rdev->raid_disk = -1;
5321
NeilBrown850b2b42006-10-03 01:15:46 -07005322 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323
5324 /*
5325 * Kick recovery, maybe this spare has to be added to the
5326 * array immediately.
5327 */
5328 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5329 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005330 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 return 0;
5332
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333abort_export:
5334 export_rdev(rdev);
5335 return err;
5336}
5337
NeilBrown32a76272005-06-21 17:17:14 -07005338static int set_bitmap_file(mddev_t *mddev, int fd)
5339{
5340 int err;
5341
NeilBrown36fa3062005-09-09 16:23:45 -07005342 if (mddev->pers) {
5343 if (!mddev->pers->quiesce)
5344 return -EBUSY;
5345 if (mddev->recovery || mddev->sync_thread)
5346 return -EBUSY;
5347 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005348 }
5349
NeilBrown36fa3062005-09-09 16:23:45 -07005350
5351 if (fd >= 0) {
5352 if (mddev->bitmap)
5353 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005354 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005355
NeilBrownc3d97142009-12-14 12:49:52 +11005356 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005357 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5358 mdname(mddev));
5359 return -EBADF;
5360 }
5361
NeilBrownc3d97142009-12-14 12:49:52 +11005362 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005363 if (err) {
5364 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5365 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005366 fput(mddev->bitmap_info.file);
5367 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005368 return err;
5369 }
NeilBrownc3d97142009-12-14 12:49:52 +11005370 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005371 } else if (mddev->bitmap == NULL)
5372 return -ENOENT; /* cannot remove what isn't there */
5373 err = 0;
5374 if (mddev->pers) {
5375 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005376 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005377 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005378 if (!err)
5379 err = bitmap_load(mddev);
5380 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005381 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005382 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005383 fd = -1; /* make sure to put the file */
5384 }
NeilBrown36fa3062005-09-09 16:23:45 -07005385 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005386 }
5387 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005388 if (mddev->bitmap_info.file) {
5389 restore_bitmap_write_access(mddev->bitmap_info.file);
5390 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005391 }
NeilBrownc3d97142009-12-14 12:49:52 +11005392 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005393 }
5394
NeilBrown32a76272005-06-21 17:17:14 -07005395 return err;
5396}
5397
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398/*
5399 * set_array_info is used two different ways
5400 * The original usage is when creating a new array.
5401 * In this usage, raid_disks is > 0 and it together with
5402 * level, size, not_persistent,layout,chunksize determine the
5403 * shape of the array.
5404 * This will always create an array with a type-0.90.0 superblock.
5405 * The newer usage is when assembling an array.
5406 * In this case raid_disks will be 0, and the major_version field is
5407 * use to determine which style super-blocks are to be found on the devices.
5408 * The minor and patch _version numbers are also kept incase the
5409 * super_block handler wishes to interpret them.
5410 */
5411static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5412{
5413
5414 if (info->raid_disks == 0) {
5415 /* just setting version number for superblock loading */
5416 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005417 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 super_types[info->major_version].name == NULL) {
5419 /* maybe try to auto-load a module? */
5420 printk(KERN_INFO
5421 "md: superblock version %d not known\n",
5422 info->major_version);
5423 return -EINVAL;
5424 }
5425 mddev->major_version = info->major_version;
5426 mddev->minor_version = info->minor_version;
5427 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005428 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005429 /* ensure mddev_put doesn't delete this now that there
5430 * is some minimal configuration.
5431 */
5432 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 return 0;
5434 }
5435 mddev->major_version = MD_MAJOR_VERSION;
5436 mddev->minor_version = MD_MINOR_VERSION;
5437 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5438 mddev->ctime = get_seconds();
5439
5440 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005441 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005442 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 mddev->raid_disks = info->raid_disks;
5444 /* don't set md_minor, it is determined by which /dev/md* was
5445 * openned
5446 */
5447 if (info->state & (1<<MD_SB_CLEAN))
5448 mddev->recovery_cp = MaxSector;
5449 else
5450 mddev->recovery_cp = 0;
5451 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005452 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453
5454 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005455 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456
5457 mddev->max_disks = MD_SB_DISKS;
5458
NeilBrowne6910632008-02-06 01:39:51 -08005459 if (mddev->persistent)
5460 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005461 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462
NeilBrownc3d97142009-12-14 12:49:52 +11005463 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5464 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005465
NeilBrownf6705572006-03-27 01:18:11 -08005466 mddev->reshape_position = MaxSector;
5467
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 /*
5469 * Generate a 128 bit UUID
5470 */
5471 get_random_bytes(mddev->uuid, 16);
5472
NeilBrownf6705572006-03-27 01:18:11 -08005473 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005474 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005475 mddev->new_layout = mddev->layout;
5476 mddev->delta_disks = 0;
5477
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 return 0;
5479}
5480
Dan Williams1f403622009-03-31 14:59:03 +11005481void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5482{
Dan Williamsb522adc2009-03-31 15:00:31 +11005483 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5484
5485 if (mddev->external_size)
5486 return;
5487
Dan Williams1f403622009-03-31 14:59:03 +11005488 mddev->array_sectors = array_sectors;
5489}
5490EXPORT_SYMBOL(md_set_array_sectors);
5491
Andre Nolld71f9f82008-07-11 22:02:22 +10005492static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005493{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005494 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005495 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005496 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005497
5498 if (mddev->pers->resize == NULL)
5499 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005500 /* The "num_sectors" is the number of sectors of each device that
5501 * is used. This can only make sense for arrays with redundancy.
5502 * linear and raid0 always use whatever space is available. We can only
5503 * consider changing this number if no resync or reconstruction is
5504 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005505 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005506 * of each device. If num_sectors is zero, we find the largest size
5507 * that fits.
5508
NeilBrowna35b0d62006-01-06 00:20:49 -08005509 */
5510 if (mddev->sync_thread)
5511 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005512 if (mddev->bitmap)
5513 /* Sorry, cannot grow a bitmap yet, just remove it,
5514 * grow, and re-add.
5515 */
5516 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005517 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005518 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005519
Andre Nolld71f9f82008-07-11 22:02:22 +10005520 if (fit && (num_sectors == 0 || num_sectors > avail))
5521 num_sectors = avail;
5522 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005523 return -ENOSPC;
5524 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005525 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005526 if (!rv)
5527 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005528 return rv;
5529}
5530
NeilBrownda943b992006-01-06 00:20:54 -08005531static int update_raid_disks(mddev_t *mddev, int raid_disks)
5532{
5533 int rv;
5534 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005535 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005536 return -EINVAL;
5537 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005538 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08005539 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005540 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005541 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005542 mddev->delta_disks = raid_disks - mddev->raid_disks;
5543
5544 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005545 return rv;
5546}
5547
5548
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549/*
5550 * update_array_info is used to change the configuration of an
5551 * on-line array.
5552 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5553 * fields in the info are checked against the array.
5554 * Any differences that cannot be handled will cause an error.
5555 * Normally, only one change can be managed at a time.
5556 */
5557static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5558{
5559 int rv = 0;
5560 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005561 int state = 0;
5562
5563 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005564 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005565 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
5567 if (mddev->major_version != info->major_version ||
5568 mddev->minor_version != info->minor_version ||
5569/* mddev->patch_version != info->patch_version || */
5570 mddev->ctime != info->ctime ||
5571 mddev->level != info->level ||
5572/* mddev->layout != info->layout || */
5573 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005574 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005575 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5576 ((state^info->state) & 0xfffffe00)
5577 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 return -EINVAL;
5579 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005580 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5581 cnt++;
5582 if (mddev->raid_disks != info->raid_disks)
5583 cnt++;
5584 if (mddev->layout != info->layout)
5585 cnt++;
5586 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5587 cnt++;
5588 if (cnt == 0)
5589 return 0;
5590 if (cnt > 1)
5591 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592
5593 if (mddev->layout != info->layout) {
5594 /* Change layout
5595 * we don't need to do anything at the md level, the
5596 * personality will take care of it all.
5597 */
NeilBrown50ac1682009-06-18 08:47:55 +10005598 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005600 else {
5601 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005602 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005603 if (rv)
5604 mddev->new_layout = mddev->layout;
5605 return rv;
5606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005608 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005609 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
NeilBrownda943b992006-01-06 00:20:54 -08005611 if (mddev->raid_disks != info->raid_disks)
5612 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613
NeilBrown36fa3062005-09-09 16:23:45 -07005614 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5615 if (mddev->pers->quiesce == NULL)
5616 return -EINVAL;
5617 if (mddev->recovery || mddev->sync_thread)
5618 return -EBUSY;
5619 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5620 /* add the bitmap */
5621 if (mddev->bitmap)
5622 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005623 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005624 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005625 mddev->bitmap_info.offset =
5626 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005627 mddev->pers->quiesce(mddev, 1);
5628 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005629 if (!rv)
5630 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07005631 if (rv)
5632 bitmap_destroy(mddev);
5633 mddev->pers->quiesce(mddev, 0);
5634 } else {
5635 /* remove the bitmap */
5636 if (!mddev->bitmap)
5637 return -ENOENT;
5638 if (mddev->bitmap->file)
5639 return -EINVAL;
5640 mddev->pers->quiesce(mddev, 1);
5641 bitmap_destroy(mddev);
5642 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005643 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005644 }
5645 }
NeilBrown850b2b42006-10-03 01:15:46 -07005646 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 return rv;
5648}
5649
5650static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5651{
5652 mdk_rdev_t *rdev;
5653
5654 if (mddev->pers == NULL)
5655 return -ENODEV;
5656
5657 rdev = find_rdev(mddev, dev);
5658 if (!rdev)
5659 return -ENODEV;
5660
5661 md_error(mddev, rdev);
5662 return 0;
5663}
5664
Andre Noll2f9618c2008-04-25 18:57:58 +02005665/*
5666 * We have a problem here : there is no easy way to give a CHS
5667 * virtual geometry. We currently pretend that we have a 2 heads
5668 * 4 sectors (with a BIG number of cylinders...). This drives
5669 * dosfs just mad... ;-)
5670 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005671static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5672{
5673 mddev_t *mddev = bdev->bd_disk->private_data;
5674
5675 geo->heads = 2;
5676 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11005677 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005678 return 0;
5679}
5680
Al Viroa39907f2008-03-02 10:31:15 -05005681static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 unsigned int cmd, unsigned long arg)
5683{
5684 int err = 0;
5685 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10005687 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688
5689 if (!capable(CAP_SYS_ADMIN))
5690 return -EACCES;
5691
5692 /*
5693 * Commands dealing with the RAID driver but not any
5694 * particular array:
5695 */
5696 switch (cmd)
5697 {
5698 case RAID_VERSION:
5699 err = get_version(argp);
5700 goto done;
5701
5702 case PRINT_RAID_DEBUG:
5703 err = 0;
5704 md_print_devices();
5705 goto done;
5706
5707#ifndef MODULE
5708 case RAID_AUTORUN:
5709 err = 0;
5710 autostart_arrays(arg);
5711 goto done;
5712#endif
5713 default:;
5714 }
5715
5716 /*
5717 * Commands creating/starting a new array:
5718 */
5719
Al Viroa39907f2008-03-02 10:31:15 -05005720 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721
5722 if (!mddev) {
5723 BUG();
5724 goto abort;
5725 }
5726
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 err = mddev_lock(mddev);
5728 if (err) {
5729 printk(KERN_INFO
5730 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5731 err, cmd);
5732 goto abort;
5733 }
5734
5735 switch (cmd)
5736 {
5737 case SET_ARRAY_INFO:
5738 {
5739 mdu_array_info_t info;
5740 if (!arg)
5741 memset(&info, 0, sizeof(info));
5742 else if (copy_from_user(&info, argp, sizeof(info))) {
5743 err = -EFAULT;
5744 goto abort_unlock;
5745 }
5746 if (mddev->pers) {
5747 err = update_array_info(mddev, &info);
5748 if (err) {
5749 printk(KERN_WARNING "md: couldn't update"
5750 " array info. %d\n", err);
5751 goto abort_unlock;
5752 }
5753 goto done_unlock;
5754 }
5755 if (!list_empty(&mddev->disks)) {
5756 printk(KERN_WARNING
5757 "md: array %s already has disks!\n",
5758 mdname(mddev));
5759 err = -EBUSY;
5760 goto abort_unlock;
5761 }
5762 if (mddev->raid_disks) {
5763 printk(KERN_WARNING
5764 "md: array %s already initialised!\n",
5765 mdname(mddev));
5766 err = -EBUSY;
5767 goto abort_unlock;
5768 }
5769 err = set_array_info(mddev, &info);
5770 if (err) {
5771 printk(KERN_WARNING "md: couldn't set"
5772 " array info. %d\n", err);
5773 goto abort_unlock;
5774 }
5775 }
5776 goto done_unlock;
5777
5778 default:;
5779 }
5780
5781 /*
5782 * Commands querying/configuring an existing array:
5783 */
NeilBrown32a76272005-06-21 17:17:14 -07005784 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005785 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005786 if ((!mddev->raid_disks && !mddev->external)
5787 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5788 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5789 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 err = -ENODEV;
5791 goto abort_unlock;
5792 }
5793
5794 /*
5795 * Commands even a read-only array can execute:
5796 */
5797 switch (cmd)
5798 {
5799 case GET_ARRAY_INFO:
5800 err = get_array_info(mddev, argp);
5801 goto done_unlock;
5802
NeilBrown32a76272005-06-21 17:17:14 -07005803 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005804 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005805 goto done_unlock;
5806
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 case GET_DISK_INFO:
5808 err = get_disk_info(mddev, argp);
5809 goto done_unlock;
5810
5811 case RESTART_ARRAY_RW:
5812 err = restart_array(mddev);
5813 goto done_unlock;
5814
5815 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005816 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 goto done_unlock;
5818
5819 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11005820 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 goto done_unlock;
5822
Dan Williamse2218352010-05-12 08:25:37 +10005823 case BLKROSET:
5824 if (get_user(ro, (int __user *)(arg))) {
5825 err = -EFAULT;
5826 goto done_unlock;
5827 }
5828 err = -EINVAL;
5829
5830 /* if the bdev is going readonly the value of mddev->ro
5831 * does not matter, no writes are coming
5832 */
5833 if (ro)
5834 goto done_unlock;
5835
5836 /* are we are already prepared for writes? */
5837 if (mddev->ro != 1)
5838 goto done_unlock;
5839
5840 /* transitioning to readauto need only happen for
5841 * arrays that call md_write_start
5842 */
5843 if (mddev->pers) {
5844 err = restart_array(mddev);
5845 if (err == 0) {
5846 mddev->ro = 2;
5847 set_disk_ro(mddev->gendisk, 0);
5848 }
5849 }
5850 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 }
5852
5853 /*
5854 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005855 * superblock, so we do not allow them on read-only arrays.
5856 * However non-MD ioctls (e.g. get-size) will still come through
5857 * here and hit the 'default' below, so only disallow
5858 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005860 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005861 if (mddev->ro == 2) {
5862 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005863 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005864 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5865 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005866 } else {
5867 err = -EROFS;
5868 goto abort_unlock;
5869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 }
5871
5872 switch (cmd)
5873 {
5874 case ADD_NEW_DISK:
5875 {
5876 mdu_disk_info_t info;
5877 if (copy_from_user(&info, argp, sizeof(info)))
5878 err = -EFAULT;
5879 else
5880 err = add_new_disk(mddev, &info);
5881 goto done_unlock;
5882 }
5883
5884 case HOT_REMOVE_DISK:
5885 err = hot_remove_disk(mddev, new_decode_dev(arg));
5886 goto done_unlock;
5887
5888 case HOT_ADD_DISK:
5889 err = hot_add_disk(mddev, new_decode_dev(arg));
5890 goto done_unlock;
5891
5892 case SET_DISK_FAULTY:
5893 err = set_disk_faulty(mddev, new_decode_dev(arg));
5894 goto done_unlock;
5895
5896 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005897 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 goto done_unlock;
5899
NeilBrown32a76272005-06-21 17:17:14 -07005900 case SET_BITMAP_FILE:
5901 err = set_bitmap_file(mddev, (int)arg);
5902 goto done_unlock;
5903
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 err = -EINVAL;
5906 goto abort_unlock;
5907 }
5908
5909done_unlock:
5910abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005911 if (mddev->hold_active == UNTIL_IOCTL &&
5912 err != -EINVAL)
5913 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 mddev_unlock(mddev);
5915
5916 return err;
5917done:
5918 if (err)
5919 MD_BUG();
5920abort:
5921 return err;
5922}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005923#ifdef CONFIG_COMPAT
5924static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5925 unsigned int cmd, unsigned long arg)
5926{
5927 switch (cmd) {
5928 case HOT_REMOVE_DISK:
5929 case HOT_ADD_DISK:
5930 case SET_DISK_FAULTY:
5931 case SET_BITMAP_FILE:
5932 /* These take in integer arg, do not convert */
5933 break;
5934 default:
5935 arg = (unsigned long)compat_ptr(arg);
5936 break;
5937 }
5938
5939 return md_ioctl(bdev, mode, cmd, arg);
5940}
5941#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942
Al Viroa39907f2008-03-02 10:31:15 -05005943static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944{
5945 /*
5946 * Succeed if we can lock the mddev, which confirms that
5947 * it isn't being stopped right now.
5948 */
NeilBrownd3374822009-01-09 08:31:10 +11005949 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 int err;
5951
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02005952 lock_kernel();
NeilBrownd3374822009-01-09 08:31:10 +11005953 if (mddev->gendisk != bdev->bd_disk) {
5954 /* we are racing with mddev_put which is discarding this
5955 * bd_disk.
5956 */
5957 mddev_put(mddev);
5958 /* Wait until bdev->bd_disk is definitely gone */
5959 flush_scheduled_work();
5960 /* Then retry the open from the top */
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02005961 unlock_kernel();
NeilBrownd3374822009-01-09 08:31:10 +11005962 return -ERESTARTSYS;
5963 }
5964 BUG_ON(mddev != bdev->bd_disk->private_data);
5965
NeilBrownc8c00a62009-08-10 12:50:52 +10005966 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967 goto out;
5968
5969 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005970 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005971 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972
NeilBrownf3b99be2010-06-24 13:31:03 +10005973 check_disk_size_change(mddev->gendisk, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 out:
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02005975 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 return err;
5977}
5978
Al Viroa39907f2008-03-02 10:31:15 -05005979static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980{
Al Viroa39907f2008-03-02 10:31:15 -05005981 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005983 BUG_ON(!mddev);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02005984 lock_kernel();
NeilBrownf2ea68c2008-07-21 17:05:25 +10005985 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 mddev_put(mddev);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02005987 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988
5989 return 0;
5990}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005991static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992{
5993 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005994 .open = md_open,
5995 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005996 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005997#ifdef CONFIG_COMPAT
5998 .compat_ioctl = md_compat_ioctl,
5999#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006000 .getgeo = md_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001};
6002
Adrian Bunk75c96f82005-05-05 16:16:09 -07006003static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004{
6005 mdk_thread_t *thread = arg;
6006
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 /*
6008 * md_thread is a 'system-thread', it's priority should be very
6009 * high. We avoid resource deadlocks individually in each
6010 * raid personality. (RAID5 does preallocation) We also use RR and
6011 * the very same RT priority as kswapd, thus we will never get
6012 * into a priority inversion deadlock.
6013 *
6014 * we definitely have to have equal or higher priority than
6015 * bdflush, otherwise bdflush will deadlock if there are too
6016 * many dirty RAID5 blocks.
6017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018
NeilBrown6985c432005-10-19 21:23:47 -07006019 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006020 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021
NeilBrown93588e22005-11-15 00:09:12 -08006022 /* We need to wait INTERRUPTIBLE so that
6023 * we don't add to the load-average.
6024 * That means we need to be sure no signals are
6025 * pending
6026 */
6027 if (signal_pending(current))
6028 flush_signals(current);
6029
6030 wait_event_interruptible_timeout
6031 (thread->wqueue,
6032 test_bit(THREAD_WAKEUP, &thread->flags)
6033 || kthread_should_stop(),
6034 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035
6036 clear_bit(THREAD_WAKEUP, &thread->flags);
6037
NeilBrown787453c2005-11-08 21:39:43 -08006038 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006040
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041 return 0;
6042}
6043
6044void md_wakeup_thread(mdk_thread_t *thread)
6045{
6046 if (thread) {
6047 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
6048 set_bit(THREAD_WAKEUP, &thread->flags);
6049 wake_up(&thread->wqueue);
6050 }
6051}
6052
6053mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
6054 const char *name)
6055{
6056 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057
NeilBrown9ffae0c2006-01-06 00:20:32 -08006058 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 if (!thread)
6060 return NULL;
6061
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 init_waitqueue_head(&thread->wqueue);
6063
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 thread->run = run;
6065 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006066 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006067 thread->tsk = kthread_run(md_thread, thread,
6068 "%s_%s",
6069 mdname(thread->mddev),
6070 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006071 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 kfree(thread);
6073 return NULL;
6074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 return thread;
6076}
6077
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078void md_unregister_thread(mdk_thread_t *thread)
6079{
NeilBrowne0cf8f02009-03-31 14:39:39 +11006080 if (!thread)
6081 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07006082 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07006083
6084 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 kfree(thread);
6086}
6087
6088void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6089{
6090 if (!mddev) {
6091 MD_BUG();
6092 return;
6093 }
6094
NeilBrownb2d444d2005-11-08 21:39:31 -08006095 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006097
6098 if (mddev->external)
6099 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07006100/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6102 mdname(mddev),
6103 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6104 __builtin_return_address(0),__builtin_return_address(1),
6105 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07006106*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07006107 if (!mddev->pers)
6108 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109 if (!mddev->pers->error_handler)
6110 return;
6111 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006112 if (mddev->degraded)
6113 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006114 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6116 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6117 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006118 if (mddev->event_work.func)
6119 schedule_work(&mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006120 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121}
6122
6123/* seq_file implementation /proc/mdstat */
6124
6125static void status_unused(struct seq_file *seq)
6126{
6127 int i = 0;
6128 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129
6130 seq_printf(seq, "unused devices: ");
6131
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006132 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 char b[BDEVNAME_SIZE];
6134 i++;
6135 seq_printf(seq, "%s ",
6136 bdevname(rdev->bdev,b));
6137 }
6138 if (!i)
6139 seq_printf(seq, "<none>");
6140
6141 seq_printf(seq, "\n");
6142}
6143
6144
6145static void status_resync(struct seq_file *seq, mddev_t * mddev)
6146{
NeilBrowndd71cf62009-05-07 12:49:35 +10006147 sector_t max_sectors, resync, res;
6148 unsigned long dt, db;
6149 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006150 int scale;
6151 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152
NeilBrowndd71cf62009-05-07 12:49:35 +10006153 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154
6155 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006156 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006158 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159
6160 /*
6161 * Should not happen.
6162 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006163 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164 MD_BUG();
6165 return;
6166 }
NeilBrown4588b422006-03-27 01:18:04 -08006167 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006168 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006169 * u32, as those are the requirements for sector_div.
6170 * Thus 'scale' must be at least 10
6171 */
6172 scale = 10;
6173 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006174 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006175 scale++;
6176 }
6177 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006178 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006179
6180 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 {
NeilBrown4588b422006-03-27 01:18:04 -08006182 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 seq_printf(seq, "[");
6184 for (i = 0; i < x; i++)
6185 seq_printf(seq, "=");
6186 seq_printf(seq, ">");
6187 for (i = 0; i < y; i++)
6188 seq_printf(seq, ".");
6189 seq_printf(seq, "] ");
6190 }
NeilBrown4588b422006-03-27 01:18:04 -08006191 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006192 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6193 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006194 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6195 "check" :
6196 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6197 "resync" : "recovery"))),
6198 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006199 (unsigned long long) resync/2,
6200 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201
6202 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203 * dt: time from mark until now
6204 * db: blocks written from mark until now
6205 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006206 *
6207 * rt is a sector_t, so could be 32bit or 64bit.
6208 * So we divide before multiply in case it is 32bit and close
6209 * to the limit.
6210 * We scale the divisor (db) by 32 to avoid loosing precision
6211 * near the end of resync when the number of remaining sectors
6212 * is close to 'db'.
6213 * We then divide rt by 32 after multiplying by db to compensate.
6214 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215 */
6216 dt = ((jiffies - mddev->resync_mark) / HZ);
6217 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006218 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6219 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220
NeilBrowndd71cf62009-05-07 12:49:35 +10006221 rt = max_sectors - resync; /* number of remaining sectors */
6222 sector_div(rt, db/32+1);
6223 rt *= dt;
6224 rt >>= 5;
6225
6226 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6227 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228
NeilBrownff4e8d92006-07-10 04:44:16 -07006229 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230}
6231
6232static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6233{
6234 struct list_head *tmp;
6235 loff_t l = *pos;
6236 mddev_t *mddev;
6237
6238 if (l >= 0x10000)
6239 return NULL;
6240 if (!l--)
6241 /* header */
6242 return (void*)1;
6243
6244 spin_lock(&all_mddevs_lock);
6245 list_for_each(tmp,&all_mddevs)
6246 if (!l--) {
6247 mddev = list_entry(tmp, mddev_t, all_mddevs);
6248 mddev_get(mddev);
6249 spin_unlock(&all_mddevs_lock);
6250 return mddev;
6251 }
6252 spin_unlock(&all_mddevs_lock);
6253 if (!l--)
6254 return (void*)2;/* tail */
6255 return NULL;
6256}
6257
6258static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6259{
6260 struct list_head *tmp;
6261 mddev_t *next_mddev, *mddev = v;
6262
6263 ++*pos;
6264 if (v == (void*)2)
6265 return NULL;
6266
6267 spin_lock(&all_mddevs_lock);
6268 if (v == (void*)1)
6269 tmp = all_mddevs.next;
6270 else
6271 tmp = mddev->all_mddevs.next;
6272 if (tmp != &all_mddevs)
6273 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6274 else {
6275 next_mddev = (void*)2;
6276 *pos = 0x10000;
6277 }
6278 spin_unlock(&all_mddevs_lock);
6279
6280 if (v != (void*)1)
6281 mddev_put(mddev);
6282 return next_mddev;
6283
6284}
6285
6286static void md_seq_stop(struct seq_file *seq, void *v)
6287{
6288 mddev_t *mddev = v;
6289
6290 if (mddev && v != (void*)1 && v != (void*)2)
6291 mddev_put(mddev);
6292}
6293
NeilBrownd7603b72006-01-06 00:20:30 -08006294struct mdstat_info {
6295 int event;
6296};
6297
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298static int md_seq_show(struct seq_file *seq, void *v)
6299{
6300 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006301 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006303 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006304 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305
6306 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006307 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 seq_printf(seq, "Personalities : ");
6309 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006310 list_for_each_entry(pers, &pers_list, list)
6311 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312
6313 spin_unlock(&pers_lock);
6314 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006315 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316 return 0;
6317 }
6318 if (v == (void*)2) {
6319 status_unused(seq);
6320 return 0;
6321 }
6322
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006323 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006325
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6327 seq_printf(seq, "%s : %sactive", mdname(mddev),
6328 mddev->pers ? "" : "in");
6329 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006330 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006332 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006333 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 seq_printf(seq, " %s", mddev->pers->name);
6335 }
6336
Andre Nolldd8ac332009-03-31 14:33:13 +11006337 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006338 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339 char b[BDEVNAME_SIZE];
6340 seq_printf(seq, " %s[%d]",
6341 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006342 if (test_bit(WriteMostly, &rdev->flags))
6343 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006344 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345 seq_printf(seq, "(F)");
6346 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006347 } else if (rdev->raid_disk < 0)
6348 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006349 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 }
6351
6352 if (!list_empty(&mddev->disks)) {
6353 if (mddev->pers)
6354 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006355 (unsigned long long)
6356 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357 else
6358 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006359 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006361 if (mddev->persistent) {
6362 if (mddev->major_version != 0 ||
6363 mddev->minor_version != 90) {
6364 seq_printf(seq," super %d.%d",
6365 mddev->major_version,
6366 mddev->minor_version);
6367 }
NeilBrowne6910632008-02-06 01:39:51 -08006368 } else if (mddev->external)
6369 seq_printf(seq, " super external:%s",
6370 mddev->metadata_type);
6371 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006372 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373
6374 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006375 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006377 if (mddev->pers->sync_request) {
6378 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006379 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006380 seq_printf(seq, "\n ");
6381 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6382 seq_printf(seq, "\tresync=DELAYED\n ");
6383 else if (mddev->recovery_cp < MaxSector)
6384 seq_printf(seq, "\tresync=PENDING\n ");
6385 }
NeilBrown32a76272005-06-21 17:17:14 -07006386 } else
6387 seq_printf(seq, "\n ");
6388
6389 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006390 unsigned long chunk_kb;
6391 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006392 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006393 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006394 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6395 "%lu%s chunk",
6396 bitmap->pages - bitmap->missing_pages,
6397 bitmap->pages,
6398 (bitmap->pages - bitmap->missing_pages)
6399 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006400 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006401 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006402 if (bitmap->file) {
6403 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006404 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006405 }
NeilBrown78d742d2005-06-21 17:17:15 -07006406
NeilBrown32a76272005-06-21 17:17:14 -07006407 seq_printf(seq, "\n");
6408 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 }
6410
6411 seq_printf(seq, "\n");
6412 }
6413 mddev_unlock(mddev);
6414
6415 return 0;
6416}
6417
Jan Engelhardt110518b2009-05-07 12:49:37 +10006418static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 .start = md_seq_start,
6420 .next = md_seq_next,
6421 .stop = md_seq_stop,
6422 .show = md_seq_show,
6423};
6424
6425static int md_seq_open(struct inode *inode, struct file *file)
6426{
6427 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006428 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6429 if (mi == NULL)
6430 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431
6432 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006433 if (error)
6434 kfree(mi);
6435 else {
6436 struct seq_file *p = file->private_data;
6437 p->private = mi;
6438 mi->event = atomic_read(&md_event_count);
6439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 return error;
6441}
6442
NeilBrownd7603b72006-01-06 00:20:30 -08006443static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6444{
6445 struct seq_file *m = filp->private_data;
6446 struct mdstat_info *mi = m->private;
6447 int mask;
6448
6449 poll_wait(filp, &md_event_waiters, wait);
6450
6451 /* always allow read */
6452 mask = POLLIN | POLLRDNORM;
6453
6454 if (mi->event != atomic_read(&md_event_count))
6455 mask |= POLLERR | POLLPRI;
6456 return mask;
6457}
6458
Arjan van de Venfa027c22007-02-12 00:55:33 -08006459static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006460 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461 .open = md_seq_open,
6462 .read = seq_read,
6463 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006464 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006465 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466};
6467
NeilBrown2604b702006-01-06 00:20:36 -08006468int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006471 list_add_tail(&p->list, &pers_list);
6472 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473 spin_unlock(&pers_lock);
6474 return 0;
6475}
6476
NeilBrown2604b702006-01-06 00:20:36 -08006477int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478{
NeilBrown2604b702006-01-06 00:20:36 -08006479 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006481 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482 spin_unlock(&pers_lock);
6483 return 0;
6484}
6485
NeilBrowneea1bf32009-03-31 14:27:02 +11006486static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487{
6488 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006490 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491
6492 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006493 rcu_read_lock();
6494 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006496 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6497 (int)part_stat_read(&disk->part0, sectors[1]) -
6498 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006499 /* sync IO will cause sync_io to increase before the disk_stats
6500 * as sync_io is counted when a request starts, and
6501 * disk_stats is counted when it completes.
6502 * So resync activity will cause curr_events to be smaller than
6503 * when there was no such activity.
6504 * non-sync IO will cause disk_stat to increase without
6505 * increasing sync_io so curr_events will (eventually)
6506 * be larger than it was before. Once it becomes
6507 * substantially larger, the test below will cause
6508 * the array to appear non-idle, and resync will slow
6509 * down.
6510 * If there is a lot of outstanding resync activity when
6511 * we set last_event to curr_events, then all that activity
6512 * completing might cause the array to appear non-idle
6513 * and resync will be slowed down even though there might
6514 * not have been non-resync activity. This will only
6515 * happen once though. 'last_events' will soon reflect
6516 * the state where there is little or no outstanding
6517 * resync requests, and further resync activity will
6518 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006519 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006521 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522 rdev->last_events = curr_events;
6523 idle = 0;
6524 }
6525 }
NeilBrown4b809912008-07-21 17:05:25 +10006526 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527 return idle;
6528}
6529
6530void md_done_sync(mddev_t *mddev, int blocks, int ok)
6531{
6532 /* another "blocks" (512byte) blocks have been synced */
6533 atomic_sub(blocks, &mddev->recovery_active);
6534 wake_up(&mddev->recovery_wait);
6535 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006536 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537 md_wakeup_thread(mddev->thread);
6538 // stop recovery, signal do_sync ....
6539 }
6540}
6541
6542
NeilBrown06d91a52005-06-21 17:17:12 -07006543/* md_write_start(mddev, bi)
6544 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006545 * in superblock) before writing, schedule a superblock update
6546 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006547 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006548void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549{
Neil Brown0fd62b82008-06-28 08:31:36 +10006550 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006551 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006552 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006553
NeilBrownf91de922005-11-08 21:39:36 -08006554 BUG_ON(mddev->ro == 1);
6555 if (mddev->ro == 2) {
6556 /* need to switch to read/write */
6557 mddev->ro = 0;
6558 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6559 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006560 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006561 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006562 }
NeilBrown06d91a52005-06-21 17:17:12 -07006563 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006564 if (mddev->safemode == 1)
6565 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006566 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006567 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006568 if (mddev->in_sync) {
6569 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006570 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006571 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006572 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006573 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006574 }
NeilBrowna9701a32005-11-08 21:39:34 -08006575 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006576 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006577 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10006578 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006579 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07006580 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581}
6582
6583void md_write_end(mddev_t *mddev)
6584{
6585 if (atomic_dec_and_test(&mddev->writes_pending)) {
6586 if (mddev->safemode == 2)
6587 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006588 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6590 }
6591}
6592
NeilBrown2a2275d2007-01-26 00:57:11 -08006593/* md_allow_write(mddev)
6594 * Calling this ensures that the array is marked 'active' so that writes
6595 * may proceed without blocking. It is important to call this before
6596 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6597 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006598 *
6599 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6600 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006601 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006602int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006603{
6604 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006605 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006606 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006607 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006608 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006609 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006610
6611 spin_lock_irq(&mddev->write_lock);
6612 if (mddev->in_sync) {
6613 mddev->in_sync = 0;
6614 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006615 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08006616 if (mddev->safemode_delay &&
6617 mddev->safemode == 0)
6618 mddev->safemode = 1;
6619 spin_unlock_irq(&mddev->write_lock);
6620 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006621 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006622 } else
6623 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006624
NeilBrown070dc6d2010-08-30 17:33:34 +10006625 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07006626 return -EAGAIN;
6627 else
6628 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006629}
6630EXPORT_SYMBOL_GPL(md_allow_write);
6631
NeilBrownb63d7c22010-06-01 19:37:33 +10006632void md_unplug(mddev_t *mddev)
NeilBrown252ac522010-06-01 19:37:29 +10006633{
6634 if (mddev->queue)
6635 blk_unplug(mddev->queue);
6636 if (mddev->plug)
6637 mddev->plug->unplug_fn(mddev->plug);
6638}
6639
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640#define SYNC_MARKS 10
6641#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006642void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643{
6644 mddev_t *mddev2;
6645 unsigned int currspeed = 0,
6646 window;
NeilBrown57afd892005-06-21 17:17:13 -07006647 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648 unsigned long mark[SYNC_MARKS];
6649 sector_t mark_cnt[SYNC_MARKS];
6650 int last_mark,m;
6651 struct list_head *tmp;
6652 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006653 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006654 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006655 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656
6657 /* just incase thread restarts... */
6658 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6659 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006660 if (mddev->ro) /* never try to sync a read-only array */
6661 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662
NeilBrown61df9d92006-10-03 01:15:57 -07006663 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6664 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6665 desc = "data-check";
6666 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6667 desc = "requested-resync";
6668 else
6669 desc = "resync";
6670 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6671 desc = "reshape";
6672 else
6673 desc = "recovery";
6674
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675 /* we overload curr_resync somewhat here.
6676 * 0 == not engaged in resync at all
6677 * 2 == checking that there is no conflict with another sync
6678 * 1 == like 2, but have yielded to allow conflicting resync to
6679 * commense
6680 * other == active in resync - this many blocks
6681 *
6682 * Before starting a resync we must have set curr_resync to
6683 * 2, and then checked that every "conflicting" array has curr_resync
6684 * less than ours. When we find one that is the same or higher
6685 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6686 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6687 * This will mean we have to start checking from the beginning again.
6688 *
6689 */
6690
6691 do {
6692 mddev->curr_resync = 2;
6693
6694 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006695 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006696 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006697
6698 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006699 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006700 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701 if (mddev2 == mddev)
6702 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006703 if (!mddev->parallel_resync
6704 && mddev2->curr_resync
6705 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706 DEFINE_WAIT(wq);
6707 if (mddev < mddev2 && mddev->curr_resync == 2) {
6708 /* arbitrarily yield */
6709 mddev->curr_resync = 1;
6710 wake_up(&resync_wait);
6711 }
6712 if (mddev > mddev2 && mddev->curr_resync == 1)
6713 /* no need to wait here, we can wait the next
6714 * time 'round when curr_resync == 2
6715 */
6716 continue;
NeilBrown97441972008-09-19 11:49:54 +10006717 /* We need to wait 'interruptible' so as not to
6718 * contribute to the load average, and not to
6719 * be caught by 'softlockup'
6720 */
6721 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006722 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006723 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006724 printk(KERN_INFO "md: delaying %s of %s"
6725 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006727 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006729 if (signal_pending(current))
6730 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731 schedule();
6732 finish_wait(&resync_wait, &wq);
6733 goto try_again;
6734 }
6735 finish_wait(&resync_wait, &wq);
6736 }
6737 }
6738 } while (mddev->curr_resync < 2);
6739
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006740 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006741 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006743 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744 */
6745 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006746 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006747 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006748 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6749 j = mddev->resync_min;
6750 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006751 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006752
NeilBrownccfcc3c2006-03-27 01:18:09 -08006753 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006754 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006755 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006757 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006758 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006759 rcu_read_lock();
6760 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006761 if (rdev->raid_disk >= 0 &&
6762 !test_bit(Faulty, &rdev->flags) &&
6763 !test_bit(In_sync, &rdev->flags) &&
6764 rdev->recovery_offset < j)
6765 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006766 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768
NeilBrown61df9d92006-10-03 01:15:57 -07006769 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6770 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6771 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006772 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006773 "(but not more than %d KB/sec) for %s.\n",
6774 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775
NeilBrowneea1bf32009-03-31 14:27:02 +11006776 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006777
NeilBrown57afd892005-06-21 17:17:13 -07006778 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779 for (m = 0; m < SYNC_MARKS; m++) {
6780 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006781 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 }
6783 last_mark = 0;
6784 mddev->resync_mark = mark[last_mark];
6785 mddev->resync_mark_cnt = mark_cnt[last_mark];
6786
6787 /*
6788 * Tune reconstruction:
6789 */
6790 window = 32*(PAGE_SIZE/512);
6791 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6792 window/2,(unsigned long long) max_sectors/2);
6793
6794 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795 last_check = 0;
6796
6797 if (j>2) {
6798 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006799 "md: resuming %s of %s from checkpoint.\n",
6800 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801 mddev->curr_resync = j;
6802 }
NeilBrownefa59332009-12-14 12:49:49 +11006803 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804
6805 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006806 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807
NeilBrown57afd892005-06-21 17:17:13 -07006808 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006809
NeilBrown7a91ee12009-05-26 12:57:21 +10006810 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6811 ((mddev->curr_resync > mddev->curr_resync_completed &&
6812 (mddev->curr_resync - mddev->curr_resync_completed)
6813 > (max_sectors >> 4)) ||
6814 (j - mddev->curr_resync_completed)*2
6815 >= mddev->resync_max - mddev->curr_resync_completed
6816 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006817 /* time to update curr_resync_completed */
NeilBrown252ac522010-06-01 19:37:29 +10006818 md_unplug(mddev);
NeilBrown97e4f422009-03-31 14:33:13 +11006819 wait_event(mddev->recovery_wait,
6820 atomic_read(&mddev->recovery_active) == 0);
6821 mddev->curr_resync_completed =
6822 mddev->curr_resync;
NeilBrown070dc6d2010-08-30 17:33:34 +10006823 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006824 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006825 }
NeilBrownacb180b2009-04-14 16:28:34 +10006826
NeilBrowne62e58a2009-07-01 13:15:35 +10006827 while (j >= mddev->resync_max && !kthread_should_stop()) {
6828 /* As this condition is controlled by user-space,
6829 * we can block indefinitely, so use '_interruptible'
6830 * to avoid triggering warnings.
6831 */
6832 flush_signals(current); /* just in case */
6833 wait_event_interruptible(mddev->recovery_wait,
6834 mddev->resync_max > j
6835 || kthread_should_stop());
6836 }
NeilBrownacb180b2009-04-14 16:28:34 +10006837
6838 if (kthread_should_stop())
6839 goto interrupted;
6840
NeilBrown57afd892005-06-21 17:17:13 -07006841 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006842 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006843 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006844 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845 goto out;
6846 }
NeilBrown57afd892005-06-21 17:17:13 -07006847
6848 if (!skipped) { /* actual IO requested */
6849 io_sectors += sectors;
6850 atomic_add(sectors, &mddev->recovery_active);
6851 }
6852
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853 j += sectors;
6854 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006855 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006856 if (last_check == 0)
6857 /* this is the earliers that rebuilt will be
6858 * visible in /proc/mdstat
6859 */
6860 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006861
6862 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863 continue;
6864
NeilBrown57afd892005-06-21 17:17:13 -07006865 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866
NeilBrowndfc70642008-05-23 13:04:39 -07006867 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868 break;
6869
6870 repeat:
6871 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6872 /* step marks */
6873 int next = (last_mark+1) % SYNC_MARKS;
6874
6875 mddev->resync_mark = mark[next];
6876 mddev->resync_mark_cnt = mark_cnt[next];
6877 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006878 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879 last_mark = next;
6880 }
6881
6882
NeilBrownc6207272008-02-06 01:39:52 -08006883 if (kthread_should_stop())
6884 goto interrupted;
6885
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886
6887 /*
6888 * this loop exits only if either when we are slower than
6889 * the 'hard' speed limit, or the system was IO-idle for
6890 * a jiffy.
6891 * the system might be non-idle CPU-wise, but we only care
6892 * about not overloading the IO subsystem. (things like an
6893 * e2fsck being done on the RAID array should execute fast)
6894 */
NeilBrown252ac522010-06-01 19:37:29 +10006895 md_unplug(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896 cond_resched();
6897
NeilBrown57afd892005-06-21 17:17:13 -07006898 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6899 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900
NeilBrown88202a02006-01-06 00:21:36 -08006901 if (currspeed > speed_min(mddev)) {
6902 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006903 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006904 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905 goto repeat;
6906 }
6907 }
6908 }
NeilBrown61df9d92006-10-03 01:15:57 -07006909 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910 /*
6911 * this also signals 'finished resyncing' to md_stop
6912 */
6913 out:
NeilBrown252ac522010-06-01 19:37:29 +10006914 md_unplug(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915
6916 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6917
6918 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006919 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920
NeilBrowndfc70642008-05-23 13:04:39 -07006921 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006922 mddev->curr_resync > 2) {
6923 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6924 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6925 if (mddev->curr_resync >= mddev->recovery_cp) {
6926 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006927 "md: checkpointing %s of %s.\n",
6928 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006929 mddev->recovery_cp = mddev->curr_resync;
6930 }
6931 } else
6932 mddev->recovery_cp = MaxSector;
6933 } else {
6934 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6935 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006936 rcu_read_lock();
6937 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006938 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10006939 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006940 !test_bit(Faulty, &rdev->flags) &&
6941 !test_bit(In_sync, &rdev->flags) &&
6942 rdev->recovery_offset < mddev->curr_resync)
6943 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07006944 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946 }
NeilBrown17571282006-12-10 02:20:52 -08006947 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948
Linus Torvalds1da177e2005-04-16 15:20:36 -07006949 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006950 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6951 /* We completed so min/max setting can be forgotten if used. */
6952 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6953 mddev->resync_min = 0;
6954 mddev->resync_max = MaxSector;
6955 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6956 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006958 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6959 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006960 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006961 wake_up(&resync_wait);
6962 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6963 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006964 return;
6965
6966 interrupted:
6967 /*
6968 * got a signal, exit.
6969 */
6970 printk(KERN_INFO
6971 "md: md_do_sync() got signal ... exiting\n");
6972 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6973 goto out;
6974
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975}
NeilBrown29269552006-03-27 01:18:10 -08006976EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977
6978
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006979static int remove_and_add_spares(mddev_t *mddev)
6980{
6981 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006982 int spares = 0;
6983
NeilBrown97e4f422009-03-31 14:33:13 +11006984 mddev->curr_resync_completed = 0;
6985
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006986 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006987 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006988 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006989 (test_bit(Faulty, &rdev->flags) ||
6990 ! test_bit(In_sync, &rdev->flags)) &&
6991 atomic_read(&rdev->nr_pending)==0) {
6992 if (mddev->pers->hot_remove_disk(
6993 mddev, rdev->raid_disk)==0) {
6994 char nm[20];
6995 sprintf(nm,"rd%d", rdev->raid_disk);
6996 sysfs_remove_link(&mddev->kobj, nm);
6997 rdev->raid_disk = -1;
6998 }
6999 }
7000
NeilBrown4044ba52009-01-09 08:31:11 +11007001 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007002 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07007003 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07007004 !test_bit(In_sync, &rdev->flags) &&
7005 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07007006 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007007 if (rdev->raid_disk < 0
7008 && !test_bit(Faulty, &rdev->flags)) {
7009 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10007010 if (mddev->pers->
7011 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007012 char nm[20];
7013 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08007014 if (sysfs_create_link(&mddev->kobj,
7015 &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10007016 /* failure here is OK */;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007017 spares++;
7018 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11007019 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007020 } else
7021 break;
7022 }
NeilBrowndfc70642008-05-23 13:04:39 -07007023 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007024 }
7025 return spares;
7026}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027/*
7028 * This routine is regularly called by all per-raid-array threads to
7029 * deal with generic issues like resync and super-block update.
7030 * Raid personalities that don't have a thread (linear/raid0) do not
7031 * need this as they never do any recovery or update the superblock.
7032 *
7033 * It does not do any resync itself, but rather "forks" off other threads
7034 * to do that as needed.
7035 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7036 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007037 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038 * and wakeups up this thread which will reap the thread and finish up.
7039 * This thread also removes any faulty devices (with nr_pending == 0).
7040 *
7041 * The overall approach is:
7042 * 1/ if the superblock needs updating, update it.
7043 * 2/ If a recovery thread is running, don't do anything else.
7044 * 3/ If recovery has finished, clean up, possibly marking spares active.
7045 * 4/ If there are any faulty devices, remove them.
7046 * 5/ If array is degraded, try to add spares devices
7047 * 6/ If array has spares or is not in-sync, start a resync thread.
7048 */
7049void md_check_recovery(mddev_t *mddev)
7050{
7051 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052
7053
NeilBrown5f404022005-06-21 17:17:16 -07007054 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007055 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056
7057 if (mddev->ro)
7058 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007059
7060 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007061 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007062 printk(KERN_INFO "md: %s in immediate safe mode\n",
7063 mdname(mddev));
7064 mddev->safemode = 2;
7065 }
7066 flush_signals(current);
7067 }
7068
NeilBrownc89a8ee2008-08-05 15:54:13 +10007069 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7070 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08007072 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007074 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007075 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007076 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7077 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007078 ))
7079 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007080
NeilBrowndf5b89b2006-03-27 01:18:20 -08007081 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007082 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007083
NeilBrownc89a8ee2008-08-05 15:54:13 +10007084 if (mddev->ro) {
7085 /* Only thing we do on a ro array is remove
7086 * failed devices.
7087 */
7088 remove_and_add_spares(mddev);
7089 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7090 goto unlock;
7091 }
7092
NeilBrown31a59e32008-04-30 00:52:30 -07007093 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007094 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007095 spin_lock_irq(&mddev->write_lock);
7096 if (mddev->safemode &&
7097 !atomic_read(&mddev->writes_pending) &&
7098 !mddev->in_sync &&
7099 mddev->recovery_cp == MaxSector) {
7100 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007101 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007102 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007103 }
7104 if (mddev->safemode == 1)
7105 mddev->safemode = 0;
7106 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007107 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007108 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007109 }
NeilBrownfca4d842005-06-21 17:17:11 -07007110
NeilBrown850b2b42006-10-03 01:15:46 -07007111 if (mddev->flags)
7112 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007113
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7115 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7116 /* resync/recovery still happening */
7117 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7118 goto unlock;
7119 }
7120 if (mddev->sync_thread) {
7121 /* resync has finished, collect result */
7122 md_unregister_thread(mddev->sync_thread);
7123 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07007124 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7125 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126 /* success...*/
7127 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10007128 if (mddev->pers->spare_active(mddev))
7129 sysfs_notify(&mddev->kobj, NULL,
7130 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131 }
NeilBrowncea9c222009-03-31 15:15:05 +11007132 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7133 mddev->pers->finish_reshape)
7134 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07007135 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07007136
7137 /* if array is no-longer degraded, then any saved_raid_disk
7138 * information must be scrapped
7139 */
7140 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007141 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07007142 rdev->saved_raid_disk = -1;
7143
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144 mddev->recovery = 0;
7145 /* flag recovery needed just to double check */
7146 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007147 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007148 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149 goto unlock;
7150 }
Neil Brown72a23c22008-06-28 08:31:41 +10007151 /* Set RUNNING before clearing NEEDED to avoid
7152 * any transients in the value of "sync_action".
7153 */
7154 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7155 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007156 /* Clear some bits that don't mean anything, but
7157 * might be left set
7158 */
NeilBrown24dd4692005-11-08 21:39:26 -08007159 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7160 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007162 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7163 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164 /* no recovery is running.
7165 * remove any failed drives, then
7166 * add spares if possible.
7167 * Spare are also removed and re-added, to allow
7168 * the personality to fail the re-add.
7169 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007171 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007172 if (mddev->pers->check_reshape == NULL ||
7173 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007174 /* Cannot proceed */
7175 goto unlock;
7176 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007177 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007178 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007179 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7180 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007181 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007182 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007183 } else if (mddev->recovery_cp < MaxSector) {
7184 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007185 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007186 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7187 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007189
Linus Torvalds1da177e2005-04-16 15:20:36 -07007190 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007191 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7192 /* We are adding a device or devices to an array
7193 * which has the bitmap stored on all devices.
7194 * So make sure all bitmap pages get written
7195 */
7196 bitmap_write_all(mddev->bitmap);
7197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198 mddev->sync_thread = md_register_thread(md_do_sync,
7199 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007200 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201 if (!mddev->sync_thread) {
7202 printk(KERN_ERR "%s: could not start resync"
7203 " thread...\n",
7204 mdname(mddev));
7205 /* leave the spares where they are, it shouldn't hurt */
7206 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08007207 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007208 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007209 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007210 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211 }
7212 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007213 if (!mddev->sync_thread) {
7214 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7215 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7216 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007217 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007218 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220 mddev_unlock(mddev);
7221 }
7222}
7223
Dan Williams6bfe0b42008-04-30 00:52:32 -07007224void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7225{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007226 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007227 wait_event_timeout(rdev->blocked_wait,
7228 !test_bit(Blocked, &rdev->flags),
7229 msecs_to_jiffies(5000));
7230 rdev_dec_pending(rdev, mddev);
7231}
7232EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7233
Adrian Bunk75c96f82005-05-05 16:16:09 -07007234static int md_notify_reboot(struct notifier_block *this,
7235 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236{
7237 struct list_head *tmp;
7238 mddev_t *mddev;
7239
7240 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7241
7242 printk(KERN_INFO "md: stopping all md devices.\n");
7243
NeilBrown29ac4aa2008-02-06 01:39:58 -08007244 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007245 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007246 /* Force a switch to readonly even array
7247 * appears to still be in use. Hence
7248 * the '100'.
7249 */
NeilBrowna4bd82d2010-03-29 13:23:10 +11007250 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007251 mddev_unlock(mddev);
7252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253 /*
7254 * certain more exotic SCSI devices are known to be
7255 * volatile wrt too early system reboots. While the
7256 * right place to handle this issue is the given
7257 * driver, we do want to have a safe RAID driver ...
7258 */
7259 mdelay(1000*1);
7260 }
7261 return NOTIFY_DONE;
7262}
7263
Adrian Bunk75c96f82005-05-05 16:16:09 -07007264static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007265 .notifier_call = md_notify_reboot,
7266 .next = NULL,
7267 .priority = INT_MAX, /* before any real devices */
7268};
7269
7270static void md_geninit(void)
7271{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7273
Denis V. Lunevc7705f32008-04-29 01:02:35 -07007274 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007275}
7276
Adrian Bunk75c96f82005-05-05 16:16:09 -07007277static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007278{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007279 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280 return -1;
7281 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007282 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283 return -1;
7284 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007285 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007286 md_probe, NULL, NULL);
7287 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288 md_probe, NULL, NULL);
7289
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007291 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292
7293 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007294 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007295}
7296
7297
7298#ifndef MODULE
7299
7300/*
7301 * Searches all registered partitions for autorun RAID arrays
7302 * at boot time.
7303 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007304
7305static LIST_HEAD(all_detected_devices);
7306struct detected_devices_node {
7307 struct list_head list;
7308 dev_t dev;
7309};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007310
7311void md_autodetect_dev(dev_t dev)
7312{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007313 struct detected_devices_node *node_detected_dev;
7314
7315 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7316 if (node_detected_dev) {
7317 node_detected_dev->dev = dev;
7318 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7319 } else {
7320 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7321 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007323}
7324
7325
7326static void autostart_arrays(int part)
7327{
7328 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007329 struct detected_devices_node *node_detected_dev;
7330 dev_t dev;
7331 int i_scanned, i_passed;
7332
7333 i_scanned = 0;
7334 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007335
7336 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7337
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007338 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7339 i_scanned++;
7340 node_detected_dev = list_entry(all_detected_devices.next,
7341 struct detected_devices_node, list);
7342 list_del(&node_detected_dev->list);
7343 dev = node_detected_dev->dev;
7344 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007345 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007346 if (IS_ERR(rdev))
7347 continue;
7348
NeilBrownb2d444d2005-11-08 21:39:31 -08007349 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007350 MD_BUG();
7351 continue;
7352 }
NeilBrownd0fae182008-03-04 14:29:31 -08007353 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007354 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007355 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007356 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007357
7358 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7359 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007360
7361 autorun_devices(part);
7362}
7363
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007364#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007365
7366static __exit void md_exit(void)
7367{
7368 mddev_t *mddev;
7369 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007370
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007371 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007372 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007373
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007374 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007375 unregister_blkdev(mdp_major, "mdp");
7376 unregister_reboot_notifier(&md_notifier);
7377 unregister_sysctl_table(raid_table_header);
7378 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007379 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007380 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007381 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007382 }
7383}
7384
Dan Williams685784a2007-07-09 11:56:42 -07007385subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007386module_exit(md_exit)
7387
NeilBrownf91de922005-11-08 21:39:36 -08007388static int get_ro(char *buffer, struct kernel_param *kp)
7389{
7390 return sprintf(buffer, "%d", start_readonly);
7391}
7392static int set_ro(const char *val, struct kernel_param *kp)
7393{
7394 char *e;
7395 int num = simple_strtoul(val, &e, 10);
7396 if (*val && (*e == '\0' || *e == '\n')) {
7397 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007398 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007399 }
7400 return -EINVAL;
7401}
7402
NeilBrown80ca3a42006-07-10 04:44:18 -07007403module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7404module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007405
NeilBrownefeb53c2009-01-09 08:31:10 +11007406module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007407
Linus Torvalds1da177e2005-04-16 15:20:36 -07007408EXPORT_SYMBOL(register_md_personality);
7409EXPORT_SYMBOL(unregister_md_personality);
7410EXPORT_SYMBOL(md_error);
7411EXPORT_SYMBOL(md_done_sync);
7412EXPORT_SYMBOL(md_write_start);
7413EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007414EXPORT_SYMBOL(md_register_thread);
7415EXPORT_SYMBOL(md_unregister_thread);
7416EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007417EXPORT_SYMBOL(md_check_recovery);
7418MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007419MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007420MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007421MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);