blob: 3640f025cb72ea73fe3c550a218fa32327df75d6 [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();
Tejun Heoe9c74692010-09-03 11:56:18 +0200229 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100230 DEFINE_WAIT(__wait);
231 for (;;) {
232 prepare_to_wait(&mddev->sb_wait, &__wait,
233 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200234 if (!mddev->suspended)
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{
285 return mddev->suspended;
286}
287EXPORT_SYMBOL(mddev_congested);
288
NeilBrowna2826aa2009-12-14 12:49:49 +1100289/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200290 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100291 */
292
Tejun Heoe9c74692010-09-03 11:56:18 +0200293static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100294{
295 mdk_rdev_t *rdev = bio->bi_private;
296 mddev_t *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100297
298 rdev_dec_pending(rdev, mddev);
299
300 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200301 /* The pre-request flush has finished */
302 schedule_work(&mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100303 }
304 bio_put(bio);
305}
306
Tejun Heoe9c74692010-09-03 11:56:18 +0200307static void submit_flushes(mddev_t *mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100308{
309 mdk_rdev_t *rdev;
310
311 rcu_read_lock();
312 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
313 if (rdev->raid_disk >= 0 &&
314 !test_bit(Faulty, &rdev->flags)) {
315 /* Take two references, one is dropped
316 * when request finishes, one after
317 * we reclaim rcu_read_lock
318 */
319 struct bio *bi;
320 atomic_inc(&rdev->nr_pending);
321 atomic_inc(&rdev->nr_pending);
322 rcu_read_unlock();
323 bi = bio_alloc(GFP_KERNEL, 0);
Tejun Heoe9c74692010-09-03 11:56:18 +0200324 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100325 bi->bi_private = rdev;
326 bi->bi_bdev = rdev->bdev;
327 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200328 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100329 rcu_read_lock();
330 rdev_dec_pending(rdev, mddev);
331 }
332 rcu_read_unlock();
333}
334
Tejun Heoe9c74692010-09-03 11:56:18 +0200335static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100336{
Tejun Heoe9c74692010-09-03 11:56:18 +0200337 mddev_t *mddev = container_of(ws, mddev_t, flush_work);
338 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100339
340 atomic_set(&mddev->flush_pending, 1);
341
Tejun Heoe9c74692010-09-03 11:56:18 +0200342 if (bio->bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100343 /* an empty barrier - all done */
344 bio_endio(bio, 0);
345 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200346 bio->bi_rw &= ~REQ_FLUSH;
NeilBrown21a52c62010-04-01 15:02:13 +1100347 if (mddev->pers->make_request(mddev, bio))
NeilBrowna2826aa2009-12-14 12:49:49 +1100348 generic_make_request(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100349 }
350 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200351 mddev->flush_bio = NULL;
NeilBrowna2826aa2009-12-14 12:49:49 +1100352 wake_up(&mddev->sb_wait);
353 }
354}
355
Tejun Heoe9c74692010-09-03 11:56:18 +0200356void md_flush_request(mddev_t *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100357{
358 spin_lock_irq(&mddev->write_lock);
359 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200360 !mddev->flush_bio,
NeilBrowna2826aa2009-12-14 12:49:49 +1100361 mddev->write_lock, /*nothing*/);
Tejun Heoe9c74692010-09-03 11:56:18 +0200362 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100363 spin_unlock_irq(&mddev->write_lock);
364
365 atomic_set(&mddev->flush_pending, 1);
Tejun Heoe9c74692010-09-03 11:56:18 +0200366 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
NeilBrowna2826aa2009-12-14 12:49:49 +1100367
Tejun Heoe9c74692010-09-03 11:56:18 +0200368 submit_flushes(mddev);
NeilBrowna2826aa2009-12-14 12:49:49 +1100369
370 if (atomic_dec_and_test(&mddev->flush_pending))
Tejun Heoe9c74692010-09-03 11:56:18 +0200371 schedule_work(&mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100372}
Tejun Heoe9c74692010-09-03 11:56:18 +0200373EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100374
NeilBrown2ac87402010-06-01 19:37:29 +1000375/* Support for plugging.
376 * This mirrors the plugging support in request_queue, but does not
377 * require having a whole queue
378 */
379static void plugger_work(struct work_struct *work)
380{
381 struct plug_handle *plug =
382 container_of(work, struct plug_handle, unplug_work);
383 plug->unplug_fn(plug);
384}
385static void plugger_timeout(unsigned long data)
386{
387 struct plug_handle *plug = (void *)data;
388 kblockd_schedule_work(NULL, &plug->unplug_work);
389}
390void plugger_init(struct plug_handle *plug,
391 void (*unplug_fn)(struct plug_handle *))
392{
393 plug->unplug_flag = 0;
394 plug->unplug_fn = unplug_fn;
395 init_timer(&plug->unplug_timer);
396 plug->unplug_timer.function = plugger_timeout;
397 plug->unplug_timer.data = (unsigned long)plug;
398 INIT_WORK(&plug->unplug_work, plugger_work);
399}
400EXPORT_SYMBOL_GPL(plugger_init);
401
402void plugger_set_plug(struct plug_handle *plug)
403{
404 if (!test_and_set_bit(PLUGGED_FLAG, &plug->unplug_flag))
405 mod_timer(&plug->unplug_timer, jiffies + msecs_to_jiffies(3)+1);
406}
407EXPORT_SYMBOL_GPL(plugger_set_plug);
408
409int plugger_remove_plug(struct plug_handle *plug)
410{
411 if (test_and_clear_bit(PLUGGED_FLAG, &plug->unplug_flag)) {
412 del_timer(&plug->unplug_timer);
413 return 1;
414 } else
415 return 0;
416}
417EXPORT_SYMBOL_GPL(plugger_remove_plug);
418
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420static inline mddev_t *mddev_get(mddev_t *mddev)
421{
422 atomic_inc(&mddev->active);
423 return mddev;
424}
425
Dan Williams5fd3a172009-03-04 00:57:25 -0700426static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428static void mddev_put(mddev_t *mddev)
429{
430 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
431 return;
NeilBrownd3374822009-01-09 08:31:10 +1100432 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100433 mddev->ctime == 0 && !mddev->hold_active) {
434 /* Array is not configured at all, and not held active,
435 * so destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 list_del(&mddev->all_mddevs);
NeilBrownd3374822009-01-09 08:31:10 +1100437 if (mddev->gendisk) {
438 /* we did a probe so need to clean up.
439 * Call schedule_work inside the spinlock
440 * so that flush_scheduled_work() after
441 * mddev_find will succeed in waiting for the
442 * work to be done.
443 */
444 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
445 schedule_work(&mddev->del_work);
446 } else
447 kfree(mddev);
448 }
449 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
NeilBrown390ee602010-06-01 19:37:27 +1000452void mddev_init(mddev_t *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100453{
454 mutex_init(&mddev->open_mutex);
455 mutex_init(&mddev->reconfig_mutex);
456 mutex_init(&mddev->bitmap_info.mutex);
457 INIT_LIST_HEAD(&mddev->disks);
458 INIT_LIST_HEAD(&mddev->all_mddevs);
459 init_timer(&mddev->safemode_timer);
460 atomic_set(&mddev->active, 1);
461 atomic_set(&mddev->openers, 0);
462 atomic_set(&mddev->active_io, 0);
463 spin_lock_init(&mddev->write_lock);
464 atomic_set(&mddev->flush_pending, 0);
465 init_waitqueue_head(&mddev->sb_wait);
466 init_waitqueue_head(&mddev->recovery_wait);
467 mddev->reshape_position = MaxSector;
468 mddev->resync_min = 0;
469 mddev->resync_max = MaxSector;
470 mddev->level = LEVEL_NONE;
471}
NeilBrown390ee602010-06-01 19:37:27 +1000472EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474static mddev_t * mddev_find(dev_t unit)
475{
476 mddev_t *mddev, *new = NULL;
477
478 retry:
479 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
NeilBrownefeb53c2009-01-09 08:31:10 +1100481 if (unit) {
482 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
483 if (mddev->unit == unit) {
484 mddev_get(mddev);
485 spin_unlock(&all_mddevs_lock);
486 kfree(new);
487 return mddev;
488 }
489
490 if (new) {
491 list_add(&new->all_mddevs, &all_mddevs);
492 spin_unlock(&all_mddevs_lock);
493 new->hold_active = UNTIL_IOCTL;
494 return new;
495 }
496 } else if (new) {
497 /* find an unused unit number */
498 static int next_minor = 512;
499 int start = next_minor;
500 int is_free = 0;
501 int dev = 0;
502 while (!is_free) {
503 dev = MKDEV(MD_MAJOR, next_minor);
504 next_minor++;
505 if (next_minor > MINORMASK)
506 next_minor = 0;
507 if (next_minor == start) {
508 /* Oh dear, all in use. */
509 spin_unlock(&all_mddevs_lock);
510 kfree(new);
511 return NULL;
512 }
513
514 is_free = 1;
515 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
516 if (mddev->unit == dev) {
517 is_free = 0;
518 break;
519 }
520 }
521 new->unit = dev;
522 new->md_minor = MINOR(dev);
523 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 list_add(&new->all_mddevs, &all_mddevs);
525 spin_unlock(&all_mddevs_lock);
526 return new;
527 }
528 spin_unlock(&all_mddevs_lock);
529
NeilBrown9ffae0c2006-01-06 00:20:32 -0800530 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (!new)
532 return NULL;
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 new->unit = unit;
535 if (MAJOR(unit) == MD_MAJOR)
536 new->md_minor = MINOR(unit);
537 else
538 new->md_minor = MINOR(unit) >> MdpMinorShift;
539
NeilBrownfafd7fb2010-04-01 15:55:30 +1100540 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 goto retry;
543}
544
545static inline int mddev_lock(mddev_t * mddev)
546{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800547 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
Dan Williamsb522adc2009-03-31 15:00:31 +1100550static inline int mddev_is_locked(mddev_t *mddev)
551{
552 return mutex_is_locked(&mddev->reconfig_mutex);
553}
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555static inline int mddev_trylock(mddev_t * mddev)
556{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800557 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
NeilBrownb6eb1272010-04-15 10:13:47 +1000560static struct attribute_group md_redundancy_group;
561
NeilBrowna64c8762010-04-14 17:15:37 +1000562static void mddev_unlock(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
NeilBrowna64c8762010-04-14 17:15:37 +1000564 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000565 /* These cannot be removed under reconfig_mutex as
566 * an access to the files will try to take reconfig_mutex
567 * while holding the file unremovable, which leads to
568 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000569 * So hold set sysfs_active while the remove in happeing,
570 * and anything else which might set ->to_remove or my
571 * otherwise change the sysfs namespace will fail with
572 * -EBUSY if sysfs_active is still set.
573 * We set sysfs_active under reconfig_mutex and elsewhere
574 * test it under the same mutex to ensure its correct value
575 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000576 */
NeilBrowna64c8762010-04-14 17:15:37 +1000577 struct attribute_group *to_remove = mddev->to_remove;
578 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000579 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000580 mutex_unlock(&mddev->reconfig_mutex);
581
NeilBrown00bcb4a2010-06-01 19:37:23 +1000582 if (mddev->kobj.sd) {
583 if (to_remove != &md_redundancy_group)
584 sysfs_remove_group(&mddev->kobj, to_remove);
585 if (mddev->pers == NULL ||
586 mddev->pers->sync_request == NULL) {
587 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
588 if (mddev->sysfs_action)
589 sysfs_put(mddev->sysfs_action);
590 mddev->sysfs_action = NULL;
591 }
NeilBrowna64c8762010-04-14 17:15:37 +1000592 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000593 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000594 } else
595 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
NeilBrown005eca52005-08-22 13:11:08 -0700597 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
NeilBrown2989ddb2006-01-06 00:20:43 -0800600static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100602 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100604 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (rdev->desc_nr == nr)
606 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return NULL;
609}
610
611static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
612{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 mdk_rdev_t *rdev;
614
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100615 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (rdev->bdev->bd_dev == dev)
617 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return NULL;
620}
621
NeilBrownd9d166c2006-01-06 00:20:51 -0800622static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800623{
624 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800625 list_for_each_entry(pers, &pers_list, list) {
626 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800627 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800628 if (strcmp(pers->name, clevel)==0)
629 return pers;
630 }
NeilBrown2604b702006-01-06 00:20:36 -0800631 return NULL;
632}
633
Andre Nollb73df2d2008-07-11 22:02:23 +1000634/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700635static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Andre Nollb73df2d2008-07-11 22:02:23 +1000637 sector_t num_sectors = bdev->bd_inode->i_size / 512;
638 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641static int alloc_disk_sb(mdk_rdev_t * rdev)
642{
643 if (rdev->sb_page)
644 MD_BUG();
645
646 rdev->sb_page = alloc_page(GFP_KERNEL);
647 if (!rdev->sb_page) {
648 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000649 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651
652 return 0;
653}
654
655static void free_disk_sb(mdk_rdev_t * rdev)
656{
657 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800658 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 rdev->sb_loaded = 0;
660 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000661 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100662 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664}
665
666
NeilBrown6712ecf2007-09-27 12:47:43 +0200667static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700668{
669 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800670 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700671
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700672 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
673 printk("md: super_written gets error=%d, uptodate=%d\n",
674 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
675 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800676 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700677 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700678
NeilBrowna9701a32005-11-08 21:39:34 -0800679 if (atomic_dec_and_test(&mddev->pending_writes))
680 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700681 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700682}
683
684void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
685 sector_t sector, int size, struct page *page)
686{
687 /* write first size bytes of page to sector of rdev
688 * Increment mddev->pending_writes before returning
689 * and decrement it on completion, waking up sb_wait
690 * if zero is reached.
691 * If an error occurred, call md_error
692 */
693 struct bio *bio = bio_alloc(GFP_NOIO, 1);
694
695 bio->bi_bdev = rdev->bdev;
696 bio->bi_sector = sector;
697 bio_add_page(bio, page, size, 0);
698 bio->bi_private = rdev;
699 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800700
NeilBrown7bfa19f2005-06-21 17:17:28 -0700701 atomic_inc(&mddev->pending_writes);
Tejun Heoe9c74692010-09-03 11:56:18 +0200702 submit_bio(REQ_WRITE | REQ_SYNC | REQ_UNPLUG | REQ_FLUSH | REQ_FUA,
703 bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800704}
705
706void md_super_wait(mddev_t *mddev)
707{
Tejun Heoe9c74692010-09-03 11:56:18 +0200708 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800709 DEFINE_WAIT(wq);
710 for(;;) {
711 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
712 if (atomic_read(&mddev->pending_writes)==0)
713 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800714 schedule();
715 }
716 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700717}
718
NeilBrown6712ecf2007-09-27 12:47:43 +0200719static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
NeilBrowna654b9d82005-06-21 17:17:27 -0700724int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 struct page *page, int rw)
726{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700727 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 struct completion event;
729 int ret;
730
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200731 rw |= REQ_SYNC | REQ_UNPLUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 bio->bi_bdev = bdev;
734 bio->bi_sector = sector;
735 bio_add_page(bio, page, size, 0);
736 init_completion(&event);
737 bio->bi_private = &event;
738 bio->bi_end_io = bi_complete;
739 submit_bio(rw, bio);
740 wait_for_completion(&event);
741
742 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
743 bio_put(bio);
744 return ret;
745}
NeilBrowna8745db2006-01-06 00:20:34 -0800746EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
NeilBrown0002b272005-09-09 16:23:53 -0700748static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
750 char b[BDEVNAME_SIZE];
751 if (!rdev->sb_page) {
752 MD_BUG();
753 return -EINVAL;
754 }
755 if (rdev->sb_loaded)
756 return 0;
757
758
Andre Noll0f420352008-07-11 22:02:23 +1000759 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 goto fail;
761 rdev->sb_loaded = 1;
762 return 0;
763
764fail:
765 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
766 bdevname(rdev->bdev,b));
767 return -EINVAL;
768}
769
770static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
771{
Andre Noll05710462008-07-11 22:02:20 +1000772 return sb1->set_uuid0 == sb2->set_uuid0 &&
773 sb1->set_uuid1 == sb2->set_uuid1 &&
774 sb1->set_uuid2 == sb2->set_uuid2 &&
775 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
779{
780 int ret;
781 mdp_super_t *tmp1, *tmp2;
782
783 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
784 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
785
786 if (!tmp1 || !tmp2) {
787 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100788 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 goto abort;
790 }
791
792 *tmp1 = *sb1;
793 *tmp2 = *sb2;
794
795 /*
796 * nr_disks is not constant
797 */
798 tmp1->nr_disks = 0;
799 tmp2->nr_disks = 0;
800
Andre Nollce0c8e02008-07-11 22:02:20 +1000801 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700803 kfree(tmp1);
804 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return ret;
806}
807
NeilBrown4d167f02007-05-09 02:35:37 -0700808
809static u32 md_csum_fold(u32 csum)
810{
811 csum = (csum & 0xffff) + (csum >> 16);
812 return (csum & 0xffff) + (csum >> 16);
813}
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815static unsigned int calc_sb_csum(mdp_super_t * sb)
816{
NeilBrown4d167f02007-05-09 02:35:37 -0700817 u64 newcsum = 0;
818 u32 *sb32 = (u32*)sb;
819 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 unsigned int disk_csum, csum;
821
822 disk_csum = sb->sb_csum;
823 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700824
825 for (i = 0; i < MD_SB_BYTES/4 ; i++)
826 newcsum += sb32[i];
827 csum = (newcsum & 0xffffffff) + (newcsum>>32);
828
829
830#ifdef CONFIG_ALPHA
831 /* This used to use csum_partial, which was wrong for several
832 * reasons including that different results are returned on
833 * different architectures. It isn't critical that we get exactly
834 * the same return value as before (we always csum_fold before
835 * testing, and that removes any differences). However as we
836 * know that csum_partial always returned a 16bit value on
837 * alphas, do a fold to maximise conformity to previous behaviour.
838 */
839 sb->sb_csum = md_csum_fold(disk_csum);
840#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700842#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return csum;
844}
845
846
847/*
848 * Handle superblock details.
849 * We want to be able to handle multiple superblock formats
850 * so we have a common interface to them all, and an array of
851 * different handlers.
852 * We rely on user-space to write the initial superblock, and support
853 * reading and updating of superblocks.
854 * Interface methods are:
855 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
856 * loads and validates a superblock on dev.
857 * if refdev != NULL, compare superblocks on both devices
858 * Return:
859 * 0 - dev has a superblock that is compatible with refdev
860 * 1 - dev has a superblock that is compatible and newer than refdev
861 * so dev should be used as the refdev in future
862 * -EINVAL superblock incompatible or invalid
863 * -othererror e.g. -EIO
864 *
865 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
866 * Verify that dev is acceptable into mddev.
867 * The first time, mddev->raid_disks will be 0, and data from
868 * dev should be merged in. Subsequent calls check that dev
869 * is new enough. Return 0 or -EINVAL
870 *
871 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
872 * Update the superblock for rdev with data in mddev
873 * This does not write to disc.
874 *
875 */
876
877struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000878 char *name;
879 struct module *owner;
880 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
881 int minor_version);
882 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
883 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
884 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000885 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886};
887
888/*
Andre Noll0894cc32009-06-18 08:49:23 +1000889 * Check that the given mddev has no bitmap.
890 *
891 * This function is called from the run method of all personalities that do not
892 * support bitmaps. It prints an error message and returns non-zero if mddev
893 * has a bitmap. Otherwise, it returns 0.
894 *
895 */
896int md_check_no_bitmap(mddev_t *mddev)
897{
NeilBrownc3d97142009-12-14 12:49:52 +1100898 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000899 return 0;
900 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
901 mdname(mddev), mddev->pers->name);
902 return 1;
903}
904EXPORT_SYMBOL(md_check_no_bitmap);
905
906/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 * load_super for 0.90.0
908 */
909static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
910{
911 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
912 mdp_super_t *sb;
913 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 /*
Andre Noll0f420352008-07-11 22:02:23 +1000916 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 * it's at the end of the disk.
918 *
919 * It also happens to be a multiple of 4Kb.
920 */
Andre Noll0f420352008-07-11 22:02:23 +1000921 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
NeilBrown0002b272005-09-09 16:23:53 -0700923 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (ret) return ret;
925
926 ret = -EINVAL;
927
928 bdevname(rdev->bdev, b);
929 sb = (mdp_super_t*)page_address(rdev->sb_page);
930
931 if (sb->md_magic != MD_SB_MAGIC) {
932 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
933 b);
934 goto abort;
935 }
936
937 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800938 sb->minor_version < 90 ||
939 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
941 sb->major_version, sb->minor_version,
942 b);
943 goto abort;
944 }
945
946 if (sb->raid_disks <= 0)
947 goto abort;
948
NeilBrown4d167f02007-05-09 02:35:37 -0700949 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
951 b);
952 goto abort;
953 }
954
955 rdev->preferred_minor = sb->md_minor;
956 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700957 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 if (sb->level == LEVEL_MULTIPATH)
960 rdev->desc_nr = -1;
961 else
962 rdev->desc_nr = sb->this_disk.number;
963
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700964 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700966 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 __u64 ev1, ev2;
968 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
969 if (!uuid_equal(refsb, sb)) {
970 printk(KERN_WARNING "md: %s has different UUID to %s\n",
971 b, bdevname(refdev->bdev,b2));
972 goto abort;
973 }
974 if (!sb_equal(refsb, sb)) {
975 printk(KERN_WARNING "md: %s has same UUID"
976 " but different superblock to %s\n",
977 b, bdevname(refdev->bdev, b2));
978 goto abort;
979 }
980 ev1 = md_event(sb);
981 ev2 = md_event(refsb);
982 if (ev1 > ev2)
983 ret = 1;
984 else
985 ret = 0;
986 }
NeilBrown8190e752009-06-18 08:48:58 +1000987 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Andre Nolldd8ac332009-03-31 14:33:13 +1100989 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -0800990 /* "this cannot possibly happen" ... */
991 ret = -EINVAL;
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 abort:
994 return ret;
995}
996
997/*
998 * validate_super for 0.90.0
999 */
1000static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1001{
1002 mdp_disk_t *desc;
1003 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001004 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
NeilBrown41158c72005-06-21 17:17:25 -07001006 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001007 clear_bit(Faulty, &rdev->flags);
1008 clear_bit(In_sync, &rdev->flags);
1009 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (mddev->raid_disks == 0) {
1012 mddev->major_version = 0;
1013 mddev->minor_version = sb->minor_version;
1014 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001015 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001016 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 mddev->ctime = sb->ctime;
1018 mddev->utime = sb->utime;
1019 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001020 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 mddev->layout = sb->layout;
1022 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +11001023 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001024 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001025 mddev->bitmap_info.offset = 0;
1026 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
NeilBrownf6705572006-03-27 01:18:11 -08001028 if (mddev->minor_version >= 91) {
1029 mddev->reshape_position = sb->reshape_position;
1030 mddev->delta_disks = sb->delta_disks;
1031 mddev->new_level = sb->new_level;
1032 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001033 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001034 } else {
1035 mddev->reshape_position = MaxSector;
1036 mddev->delta_disks = 0;
1037 mddev->new_level = mddev->level;
1038 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001039 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001040 }
1041
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 if (sb->state & (1<<MD_SB_CLEAN))
1043 mddev->recovery_cp = MaxSector;
1044 else {
1045 if (sb->events_hi == sb->cp_events_hi &&
1046 sb->events_lo == sb->cp_events_lo) {
1047 mddev->recovery_cp = sb->recovery_cp;
1048 } else
1049 mddev->recovery_cp = 0;
1050 }
1051
1052 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1053 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1054 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1055 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1056
1057 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001058
1059 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001060 mddev->bitmap_info.file == NULL)
1061 mddev->bitmap_info.offset =
1062 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001063
NeilBrown41158c72005-06-21 17:17:25 -07001064 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001065 /* Insist on good event counter while assembling, except
1066 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001068 if (sb->disks[rdev->desc_nr].state & (
1069 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1070 if (ev1 < mddev->events)
1071 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001072 } else if (mddev->bitmap) {
1073 /* if adding to array with a bitmap, then we can accept an
1074 * older device ... but not too old.
1075 */
NeilBrown41158c72005-06-21 17:17:25 -07001076 if (ev1 < mddev->bitmap->events_cleared)
1077 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001078 } else {
1079 if (ev1 < mddev->events)
1080 /* just a hot-add of a new device, leave raid_disk at -1 */
1081 return 0;
1082 }
NeilBrown41158c72005-06-21 17:17:25 -07001083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 desc = sb->disks + rdev->desc_nr;
1086
1087 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001088 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001089 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1090 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001091 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001093 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1094 /* active but not in sync implies recovery up to
1095 * reshape position. We don't know exactly where
1096 * that is, so set to zero for now */
1097 if (mddev->minor_version >= 91) {
1098 rdev->recovery_offset = 0;
1099 rdev->raid_disk = desc->raid_disk;
1100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001102 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1103 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001104 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001105 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return 0;
1107}
1108
1109/*
1110 * sync_super for 0.90.0
1111 */
1112static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1113{
1114 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 mdk_rdev_t *rdev2;
1116 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 /* make rdev->sb match mddev data..
1120 *
1121 * 1/ zero out disks
1122 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1123 * 3/ any empty disks < next_spare become removed
1124 *
1125 * disks[0] gets initialised to REMOVED because
1126 * we cannot be sure from other fields if it has
1127 * been initialised or not.
1128 */
1129 int i;
1130 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1131
NeilBrown61181562005-09-09 16:24:02 -07001132 rdev->sb_size = MD_SB_BYTES;
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 sb = (mdp_super_t*)page_address(rdev->sb_page);
1135
1136 memset(sb, 0, sizeof(*sb));
1137
1138 sb->md_magic = MD_SB_MAGIC;
1139 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 sb->patch_version = mddev->patch_version;
1141 sb->gvalid_words = 0; /* ignored */
1142 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1143 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1144 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1145 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1146
1147 sb->ctime = mddev->ctime;
1148 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001149 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 sb->raid_disks = mddev->raid_disks;
1151 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001152 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 sb->utime = mddev->utime;
1154 sb->state = 0;
1155 sb->events_hi = (mddev->events>>32);
1156 sb->events_lo = (u32)mddev->events;
1157
NeilBrownf6705572006-03-27 01:18:11 -08001158 if (mddev->reshape_position == MaxSector)
1159 sb->minor_version = 90;
1160 else {
1161 sb->minor_version = 91;
1162 sb->reshape_position = mddev->reshape_position;
1163 sb->new_level = mddev->new_level;
1164 sb->delta_disks = mddev->delta_disks;
1165 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001166 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001167 }
1168 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (mddev->in_sync)
1170 {
1171 sb->recovery_cp = mddev->recovery_cp;
1172 sb->cp_events_hi = (mddev->events>>32);
1173 sb->cp_events_lo = (u32)mddev->events;
1174 if (mddev->recovery_cp == MaxSector)
1175 sb->state = (1<< MD_SB_CLEAN);
1176 } else
1177 sb->recovery_cp = 0;
1178
1179 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001180 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
NeilBrownc3d97142009-12-14 12:49:52 +11001182 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001183 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001186 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001188 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001189 int is_active = test_bit(In_sync, &rdev2->flags);
1190
1191 if (rdev2->raid_disk >= 0 &&
1192 sb->minor_version >= 91)
1193 /* we have nowhere to store the recovery_offset,
1194 * but if it is not below the reshape_position,
1195 * we can piggy-back on that.
1196 */
1197 is_active = 1;
1198 if (rdev2->raid_disk < 0 ||
1199 test_bit(Faulty, &rdev2->flags))
1200 is_active = 0;
1201 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001202 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001204 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001205 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 d = &sb->disks[rdev2->desc_nr];
1207 nr_disks++;
1208 d->number = rdev2->desc_nr;
1209 d->major = MAJOR(rdev2->bdev->bd_dev);
1210 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001211 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 d->raid_disk = rdev2->raid_disk;
1213 else
1214 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001215 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001217 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001219 if (test_bit(In_sync, &rdev2->flags))
1220 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 active++;
1222 working++;
1223 } else {
1224 d->state = 0;
1225 spare++;
1226 working++;
1227 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001228 if (test_bit(WriteMostly, &rdev2->flags))
1229 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 /* now set the "removed" and "faulty" bits on any missing devices */
1232 for (i=0 ; i < mddev->raid_disks ; i++) {
1233 mdp_disk_t *d = &sb->disks[i];
1234 if (d->state == 0 && d->number == 0) {
1235 d->number = i;
1236 d->raid_disk = i;
1237 d->state = (1<<MD_DISK_REMOVED);
1238 d->state |= (1<<MD_DISK_FAULTY);
1239 failed++;
1240 }
1241 }
1242 sb->nr_disks = nr_disks;
1243 sb->active_disks = active;
1244 sb->working_disks = working;
1245 sb->failed_disks = failed;
1246 sb->spare_disks = spare;
1247
1248 sb->this_disk = sb->disks[rdev->desc_nr];
1249 sb->sb_csum = calc_sb_csum(sb);
1250}
1251
1252/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001253 * rdev_size_change for 0.90.0
1254 */
1255static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001256super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001257{
Andre Noll58c0fed2009-03-31 14:33:13 +11001258 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001259 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001260 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001261 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001262 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001263 if (!num_sectors || num_sectors > rdev->sb_start)
1264 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001265 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001266 rdev->sb_page);
1267 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001268 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001269}
1270
1271
1272/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 * version 1 superblock
1274 */
1275
NeilBrown1c05b4b2006-10-21 10:24:08 -07001276static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001278 __le32 disk_csum;
1279 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 unsigned long long newcsum;
1281 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001282 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 int i;
1284
1285 disk_csum = sb->sb_csum;
1286 sb->sb_csum = 0;
1287 newcsum = 0;
1288 for (i=0; size>=4; size -= 4 )
1289 newcsum += le32_to_cpu(*isuper++);
1290
1291 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001292 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
1294 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1295 sb->sb_csum = disk_csum;
1296 return cpu_to_le32(csum);
1297}
1298
1299static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1300{
1301 struct mdp_superblock_1 *sb;
1302 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001303 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001305 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 /*
Andre Noll0f420352008-07-11 22:02:23 +10001308 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 * It is always aligned to a 4K boundary and
1310 * depeding on minor_version, it can be:
1311 * 0: At least 8K, but less than 12K, from end of device
1312 * 1: At start of device
1313 * 2: 4K from start of device.
1314 */
1315 switch(minor_version) {
1316 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001317 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1318 sb_start -= 8*2;
1319 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 break;
1321 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001322 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 break;
1324 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001325 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 break;
1327 default:
1328 return -EINVAL;
1329 }
Andre Noll0f420352008-07-11 22:02:23 +10001330 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
NeilBrown0002b272005-09-09 16:23:53 -07001332 /* superblock is rarely larger than 1K, but it can be larger,
1333 * and it is safe to read 4k, so we do that
1334 */
1335 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if (ret) return ret;
1337
1338
1339 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1340
1341 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1342 sb->major_version != cpu_to_le32(1) ||
1343 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001344 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001345 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return -EINVAL;
1347
1348 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1349 printk("md: invalid superblock checksum on %s\n",
1350 bdevname(rdev->bdev,b));
1351 return -EINVAL;
1352 }
1353 if (le64_to_cpu(sb->data_size) < 10) {
1354 printk("md: data_size too small on %s\n",
1355 bdevname(rdev->bdev,b));
1356 return -EINVAL;
1357 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 rdev->preferred_minor = 0xffff;
1360 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001361 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
NeilBrown0002b272005-09-09 16:23:53 -07001363 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001364 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001365 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001366 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1367
1368 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001369 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001370 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001371
NeilBrown31b65a02006-07-10 04:44:14 -07001372 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1373 rdev->desc_nr = -1;
1374 else
1375 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1376
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001377 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001378 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001379 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 __u64 ev1, ev2;
1381 struct mdp_superblock_1 *refsb =
1382 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1383
1384 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1385 sb->level != refsb->level ||
1386 sb->layout != refsb->layout ||
1387 sb->chunksize != refsb->chunksize) {
1388 printk(KERN_WARNING "md: %s has strangely different"
1389 " superblock to %s\n",
1390 bdevname(rdev->bdev,b),
1391 bdevname(refdev->bdev,b2));
1392 return -EINVAL;
1393 }
1394 ev1 = le64_to_cpu(sb->events);
1395 ev2 = le64_to_cpu(refsb->events);
1396
1397 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001398 ret = 1;
1399 else
1400 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
NeilBrowna1801f82008-03-04 14:29:31 -08001402 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001403 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1404 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001406 rdev->sectors = rdev->sb_start;
1407 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001409 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001410 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001411 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001412 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
1415static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1416{
1417 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001418 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
NeilBrown41158c72005-06-21 17:17:25 -07001420 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001421 clear_bit(Faulty, &rdev->flags);
1422 clear_bit(In_sync, &rdev->flags);
1423 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 if (mddev->raid_disks == 0) {
1426 mddev->major_version = 1;
1427 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001428 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001429 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1431 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1432 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001433 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 mddev->layout = le32_to_cpu(sb->layout);
1435 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001436 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001437 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001438 mddev->bitmap_info.offset = 0;
1439 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1442 memcpy(mddev->uuid, sb->set_uuid, 16);
1443
1444 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001445
NeilBrown71c08052005-09-09 16:23:51 -07001446 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001447 mddev->bitmap_info.file == NULL )
1448 mddev->bitmap_info.offset =
1449 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001450
NeilBrownf6705572006-03-27 01:18:11 -08001451 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1452 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1453 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1454 mddev->new_level = le32_to_cpu(sb->new_level);
1455 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001456 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001457 } else {
1458 mddev->reshape_position = MaxSector;
1459 mddev->delta_disks = 0;
1460 mddev->new_level = mddev->level;
1461 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001462 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001463 }
1464
NeilBrown41158c72005-06-21 17:17:25 -07001465 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001466 /* Insist of good event counter while assembling, except for
1467 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001469 if (rdev->desc_nr >= 0 &&
1470 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1471 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1472 if (ev1 < mddev->events)
1473 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001474 } else if (mddev->bitmap) {
1475 /* If adding to array with a bitmap, then we can accept an
1476 * older device, but not too old.
1477 */
NeilBrown41158c72005-06-21 17:17:25 -07001478 if (ev1 < mddev->bitmap->events_cleared)
1479 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001480 } else {
1481 if (ev1 < mddev->events)
1482 /* just a hot-add of a new device, leave raid_disk at -1 */
1483 return 0;
1484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 if (mddev->level != LEVEL_MULTIPATH) {
1486 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001487 if (rdev->desc_nr < 0 ||
1488 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1489 role = 0xffff;
1490 rdev->desc_nr = -1;
1491 } else
1492 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 switch(role) {
1494 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 break;
1496 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001497 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 break;
1499 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001500 if ((le32_to_cpu(sb->feature_map) &
1501 MD_FEATURE_RECOVERY_OFFSET))
1502 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1503 else
1504 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 rdev->raid_disk = role;
1506 break;
1507 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001508 if (sb->devflags & WriteMostly1)
1509 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001510 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001511 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 return 0;
1514}
1515
1516static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1517{
1518 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 mdk_rdev_t *rdev2;
1520 int max_dev, i;
1521 /* make rdev->sb match mddev and rdev data. */
1522
1523 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1524
1525 sb->feature_map = 0;
1526 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001527 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 memset(sb->pad1, 0, sizeof(sb->pad1));
1529 memset(sb->pad2, 0, sizeof(sb->pad2));
1530 memset(sb->pad3, 0, sizeof(sb->pad3));
1531
1532 sb->utime = cpu_to_le64((__u64)mddev->utime);
1533 sb->events = cpu_to_le64(mddev->events);
1534 if (mddev->in_sync)
1535 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1536 else
1537 sb->resync_offset = cpu_to_le64(0);
1538
NeilBrown1c05b4b2006-10-21 10:24:08 -07001539 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001540
NeilBrownf0ca3402006-02-02 14:28:04 -08001541 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001542 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001543 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001544 sb->level = cpu_to_le32(mddev->level);
1545 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001546
NeilBrownc3d97142009-12-14 12:49:52 +11001547 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1548 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001549 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001550 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001551
1552 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001553 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001554 sb->feature_map |=
1555 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1556 sb->recovery_offset =
1557 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001558 }
1559
NeilBrownf6705572006-03-27 01:18:11 -08001560 if (mddev->reshape_position != MaxSector) {
1561 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1562 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1563 sb->new_layout = cpu_to_le32(mddev->new_layout);
1564 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1565 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001566 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001567 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001570 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 if (rdev2->desc_nr+1 > max_dev)
1572 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001573
NeilBrown70471da2009-08-03 10:59:57 +10001574 if (max_dev > le32_to_cpu(sb->max_dev)) {
1575 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001576 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001577 rdev->sb_size = max_dev * 2 + 256;
1578 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1579 if (rdev->sb_size & bmask)
1580 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 for (i=0; i<max_dev;i++)
1583 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1584
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001585 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001587 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001589 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001591 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001592 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 else
1594 sb->dev_roles[i] = cpu_to_le16(0xffff);
1595 }
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 sb->sb_csum = calc_sb_1_csum(sb);
1598}
1599
Chris Webb0cd17fe2008-06-28 08:31:46 +10001600static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001601super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001602{
1603 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001604 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001605 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001606 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001607 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001608 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001609 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1610 max_sectors -= rdev->data_offset;
1611 if (!num_sectors || num_sectors > max_sectors)
1612 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001613 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001614 /* minor version 0 with bitmap we can't move */
1615 return 0;
1616 } else {
1617 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001618 sector_t sb_start;
1619 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1620 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001621 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001622 if (!num_sectors || num_sectors > max_sectors)
1623 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001624 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001625 }
1626 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001627 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001628 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001629 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001630 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001631 rdev->sb_page);
1632 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001633 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001634}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Adrian Bunk75c96f82005-05-05 16:16:09 -07001636static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 [0] = {
1638 .name = "0.90.0",
1639 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001640 .load_super = super_90_load,
1641 .validate_super = super_90_validate,
1642 .sync_super = super_90_sync,
1643 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 },
1645 [1] = {
1646 .name = "md-1",
1647 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001648 .load_super = super_1_load,
1649 .validate_super = super_1_validate,
1650 .sync_super = super_1_sync,
1651 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 },
1653};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1656{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001657 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
NeilBrown4b809912008-07-21 17:05:25 +10001659 rcu_read_lock();
1660 rdev_for_each_rcu(rdev, mddev1)
1661 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001662 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001663 rdev2->bdev->bd_contains) {
1664 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001665 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001666 }
1667 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return 0;
1669}
1670
1671static LIST_HEAD(pending_raid_disks);
1672
Andre Nollac5e7112009-08-03 10:59:47 +10001673/*
1674 * Try to register data integrity profile for an mddev
1675 *
1676 * This is called when an array is started and after a disk has been kicked
1677 * from the array. It only succeeds if all working and active component devices
1678 * are integrity capable with matching profiles.
1679 */
1680int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001681{
Andre Nollac5e7112009-08-03 10:59:47 +10001682 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001683
Andre Nollac5e7112009-08-03 10:59:47 +10001684 if (list_empty(&mddev->disks))
1685 return 0; /* nothing to do */
1686 if (blk_get_integrity(mddev->gendisk))
1687 return 0; /* already registered */
1688 list_for_each_entry(rdev, &mddev->disks, same_set) {
1689 /* skip spares and non-functional disks */
1690 if (test_bit(Faulty, &rdev->flags))
1691 continue;
1692 if (rdev->raid_disk < 0)
1693 continue;
1694 /*
1695 * If at least one rdev is not integrity capable, we can not
1696 * enable data integrity for the md device.
1697 */
1698 if (!bdev_get_integrity(rdev->bdev))
1699 return -EINVAL;
1700 if (!reference) {
1701 /* Use the first rdev as the reference */
1702 reference = rdev;
1703 continue;
1704 }
1705 /* does this rdev's profile match the reference profile? */
1706 if (blk_integrity_compare(reference->bdev->bd_disk,
1707 rdev->bdev->bd_disk) < 0)
1708 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001709 }
Andre Nollac5e7112009-08-03 10:59:47 +10001710 /*
1711 * All component devices are integrity capable and have matching
1712 * profiles, register the common profile for the md device.
1713 */
1714 if (blk_integrity_register(mddev->gendisk,
1715 bdev_get_integrity(reference->bdev)) != 0) {
1716 printk(KERN_ERR "md: failed to register integrity for %s\n",
1717 mdname(mddev));
1718 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001719 }
Andre Nollac5e7112009-08-03 10:59:47 +10001720 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1721 mdname(mddev));
1722 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001723}
Andre Nollac5e7112009-08-03 10:59:47 +10001724EXPORT_SYMBOL(md_integrity_register);
1725
1726/* Disable data integrity if non-capable/non-matching disk is being added */
1727void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1728{
1729 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1730 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1731
1732 if (!bi_mddev) /* nothing to do */
1733 return;
1734 if (rdev->raid_disk < 0) /* skip spares */
1735 return;
1736 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1737 rdev->bdev->bd_disk) >= 0)
1738 return;
1739 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1740 blk_integrity_unregister(mddev->gendisk);
1741}
1742EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1745{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001746 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001747 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001748 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001749 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 if (rdev->mddev) {
1752 MD_BUG();
1753 return -EINVAL;
1754 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001755
1756 /* prevent duplicates */
1757 if (find_rdev(mddev, rdev->bdev->bd_dev))
1758 return -EEXIST;
1759
Andre Nolldd8ac332009-03-31 14:33:13 +11001760 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1761 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1762 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001763 if (mddev->pers) {
1764 /* Cannot change size, so fail
1765 * If mddev->level <= 0, then we don't care
1766 * about aligning sizes (e.g. linear)
1767 */
1768 if (mddev->level > 0)
1769 return -ENOSPC;
1770 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001771 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
1774 /* Verify rdev->desc_nr is unique.
1775 * If it is -1, assign a free number, else
1776 * check number is not in use
1777 */
1778 if (rdev->desc_nr < 0) {
1779 int choice = 0;
1780 if (mddev->pers) choice = mddev->raid_disks;
1781 while (find_rdev_nr(mddev, choice))
1782 choice++;
1783 rdev->desc_nr = choice;
1784 } else {
1785 if (find_rdev_nr(mddev, rdev->desc_nr))
1786 return -EBUSY;
1787 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001788 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1789 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1790 mdname(mddev), mddev->max_disks);
1791 return -EBUSY;
1792 }
NeilBrown19133a42005-11-08 21:39:35 -08001793 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001794 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001795 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001798 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001799
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001800 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001801 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001802
Tejun Heo0762b8b2008-08-25 19:56:12 +09001803 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10001804 if (sysfs_create_link(&rdev->kobj, ko, "block"))
1805 /* failure here is OK */;
1806 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11001807
NeilBrown4b809912008-07-21 17:05:25 +10001808 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001809 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001810
1811 /* May as well allow recovery to be retried once */
1812 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001815
1816 fail:
1817 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1818 b, mdname(mddev));
1819 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
1821
NeilBrown177a99b2008-02-06 01:39:56 -08001822static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001823{
1824 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1825 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001826 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001827}
1828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1830{
1831 char b[BDEVNAME_SIZE];
1832 if (!rdev->mddev) {
1833 MD_BUG();
1834 return;
1835 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001836 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001837 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1839 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001840 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001841 sysfs_put(rdev->sysfs_state);
1842 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001843 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001844 * writing to 'remove' to "dev/state". We also need
1845 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001846 */
NeilBrown4b809912008-07-21 17:05:25 +10001847 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001848 INIT_WORK(&rdev->del_work, md_delayed_delete);
1849 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001850 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
1853/*
1854 * prevent the device from being mounted, repartitioned or
1855 * otherwise reused by a RAID array (or any other kernel
1856 * subsystem), by bd_claiming the device.
1857 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001858static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
1860 int err = 0;
1861 struct block_device *bdev;
1862 char b[BDEVNAME_SIZE];
1863
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001864 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 if (IS_ERR(bdev)) {
1866 printk(KERN_ERR "md: could not open %s.\n",
1867 __bdevname(dev, b));
1868 return PTR_ERR(bdev);
1869 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001870 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 if (err) {
1872 printk(KERN_ERR "md: could not bd_claim %s.\n",
1873 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001874 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 return err;
1876 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001877 if (!shared)
1878 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 rdev->bdev = bdev;
1880 return err;
1881}
1882
1883static void unlock_rdev(mdk_rdev_t *rdev)
1884{
1885 struct block_device *bdev = rdev->bdev;
1886 rdev->bdev = NULL;
1887 if (!bdev)
1888 MD_BUG();
1889 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001890 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891}
1892
1893void md_autodetect_dev(dev_t dev);
1894
1895static void export_rdev(mdk_rdev_t * rdev)
1896{
1897 char b[BDEVNAME_SIZE];
1898 printk(KERN_INFO "md: export_rdev(%s)\n",
1899 bdevname(rdev->bdev,b));
1900 if (rdev->mddev)
1901 MD_BUG();
1902 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001904 if (test_bit(AutoDetected, &rdev->flags))
1905 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906#endif
1907 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001908 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
1910
1911static void kick_rdev_from_array(mdk_rdev_t * rdev)
1912{
1913 unbind_rdev_from_array(rdev);
1914 export_rdev(rdev);
1915}
1916
1917static void export_array(mddev_t *mddev)
1918{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001919 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
NeilBrownd089c6a2008-02-06 01:39:59 -08001921 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if (!rdev->mddev) {
1923 MD_BUG();
1924 continue;
1925 }
1926 kick_rdev_from_array(rdev);
1927 }
1928 if (!list_empty(&mddev->disks))
1929 MD_BUG();
1930 mddev->raid_disks = 0;
1931 mddev->major_version = 0;
1932}
1933
1934static void print_desc(mdp_disk_t *desc)
1935{
1936 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1937 desc->major,desc->minor,desc->raid_disk,desc->state);
1938}
1939
Cheng Renquancd2ac932009-01-09 08:31:08 +11001940static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
1942 int i;
1943
1944 printk(KERN_INFO
1945 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1946 sb->major_version, sb->minor_version, sb->patch_version,
1947 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1948 sb->ctime);
1949 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1950 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1951 sb->md_minor, sb->layout, sb->chunk_size);
1952 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1953 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1954 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1955 sb->failed_disks, sb->spare_disks,
1956 sb->sb_csum, (unsigned long)sb->events_lo);
1957
1958 printk(KERN_INFO);
1959 for (i = 0; i < MD_SB_DISKS; i++) {
1960 mdp_disk_t *desc;
1961
1962 desc = sb->disks + i;
1963 if (desc->number || desc->major || desc->minor ||
1964 desc->raid_disk || (desc->state && (desc->state != 4))) {
1965 printk(" D %2d: ", i);
1966 print_desc(desc);
1967 }
1968 }
1969 printk(KERN_INFO "md: THIS: ");
1970 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972
Cheng Renquancd2ac932009-01-09 08:31:08 +11001973static void print_sb_1(struct mdp_superblock_1 *sb)
1974{
1975 __u8 *uuid;
1976
1977 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001978 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08001979 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001980 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001981 le32_to_cpu(sb->major_version),
1982 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08001983 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11001984 sb->set_name,
1985 (unsigned long long)le64_to_cpu(sb->ctime)
1986 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1987
1988 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001989 printk(KERN_INFO
1990 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11001991 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08001992 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001993 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1994 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001995 le32_to_cpu(sb->level),
1996 (unsigned long long)le64_to_cpu(sb->size),
1997 le32_to_cpu(sb->raid_disks),
1998 le32_to_cpu(sb->layout),
1999 le32_to_cpu(sb->chunksize),
2000 (unsigned long long)le64_to_cpu(sb->data_offset),
2001 (unsigned long long)le64_to_cpu(sb->data_size),
2002 (unsigned long long)le64_to_cpu(sb->super_offset),
2003 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2004 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002005 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002006 sb->devflags,
2007 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2008 (unsigned long long)le64_to_cpu(sb->events),
2009 (unsigned long long)le64_to_cpu(sb->resync_offset),
2010 le32_to_cpu(sb->sb_csum),
2011 le32_to_cpu(sb->max_dev)
2012 );
2013}
2014
2015static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
2017 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002018 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2019 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002020 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2021 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002023 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2024 switch (major_version) {
2025 case 0:
2026 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
2027 break;
2028 case 1:
2029 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
2030 break;
2031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 } else
2033 printk(KERN_INFO "md: no rdev superblock!\n");
2034}
2035
Adrian Bunk5e563412006-06-26 00:27:42 -07002036static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002038 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 mdk_rdev_t *rdev;
2040 mddev_t *mddev;
2041 char b[BDEVNAME_SIZE];
2042
2043 printk("\n");
2044 printk("md: **********************************\n");
2045 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2046 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002047 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
NeilBrown32a76272005-06-21 17:17:14 -07002049 if (mddev->bitmap)
2050 bitmap_print_sb(mddev->bitmap);
2051 else
2052 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002053 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 printk("<%s>", bdevname(rdev->bdev,b));
2055 printk("\n");
2056
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002057 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002058 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 }
2060 printk("md: **********************************\n");
2061 printk("\n");
2062}
2063
2064
NeilBrown42543762006-06-26 00:27:57 -07002065static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
NeilBrown42543762006-06-26 00:27:57 -07002067 /* Update each superblock (in-memory image), but
2068 * if we are allowed to, skip spares which already
2069 * have the right event counter, or have one earlier
2070 * (which would mean they aren't being marked as dirty
2071 * with the rest of the array)
2072 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 mdk_rdev_t *rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002074 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002075 if (rdev->sb_events == mddev->events ||
2076 (nospares &&
2077 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002078 rdev->sb_events+1 == mddev->events)) {
2079 /* Don't update this superblock */
2080 rdev->sb_loaded = 2;
2081 } else {
2082 super_types[mddev->major_version].
2083 sync_super(mddev, rdev);
2084 rdev->sb_loaded = 1;
2085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
2087}
2088
NeilBrown850b2b42006-10-03 01:15:46 -07002089static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002092 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002093 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002096 /* First make sure individual recovery_offsets are correct */
2097 list_for_each_entry(rdev, &mddev->disks, same_set) {
2098 if (rdev->raid_disk >= 0 &&
2099 mddev->delta_disks >= 0 &&
2100 !test_bit(In_sync, &rdev->flags) &&
2101 mddev->curr_resync_completed > rdev->recovery_offset)
2102 rdev->recovery_offset = mddev->curr_resync_completed;
2103
2104 }
2105 if (mddev->external || !mddev->persistent) {
2106 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
2107 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
2108 wake_up(&mddev->sb_wait);
2109 return;
2110 }
2111
NeilBrowna9701a32005-11-08 21:39:34 -08002112 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002113
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002114 mddev->utime = get_seconds();
2115
NeilBrown850b2b42006-10-03 01:15:46 -07002116 set_bit(MD_CHANGE_PENDING, &mddev->flags);
2117 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2118 force_change = 1;
2119 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2120 /* just a clean<-> dirty transition, possibly leave spares alone,
2121 * though if events isn't the right even/odd, we will have to do
2122 * spares after all
2123 */
2124 nospares = 1;
2125 if (force_change)
2126 nospares = 0;
2127 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002128 /* If the array is degraded, then skipping spares is both
2129 * dangerous and fairly pointless.
2130 * Dangerous because a device that was removed from the array
2131 * might have a event_count that still looks up-to-date,
2132 * so it can be re-added without a resync.
2133 * Pointless because if there are any spares to skip,
2134 * then a recovery will happen and soon that array won't
2135 * be degraded any more and the spare can go back to sleep then.
2136 */
NeilBrown850b2b42006-10-03 01:15:46 -07002137 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002138
NeilBrown06d91a52005-06-21 17:17:12 -07002139 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002140
2141 /* If this is just a dirty<->clean transition, and the array is clean
2142 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002143 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002144 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002145 && mddev->can_decrease_events
2146 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002147 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002148 mddev->can_decrease_events = 0;
2149 } else {
NeilBrown42543762006-06-26 00:27:57 -07002150 /* otherwise we have to go forward and ... */
2151 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002152 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155 if (!mddev->events) {
2156 /*
2157 * oops, this 64-bit counter should never wrap.
2158 * Either we are in around ~1 trillion A.C., assuming
2159 * 1 reboot per second, or we have a bug:
2160 */
2161 MD_BUG();
2162 mddev->events --;
2163 }
NeilBrowne6910632008-02-06 01:39:51 -08002164 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002165 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
2167 dprintk(KERN_INFO
2168 "md: updating %s RAID superblock on device (in sync %d)\n",
2169 mdname(mddev),mddev->in_sync);
2170
NeilBrown4ad13662007-07-17 04:06:13 -07002171 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002172 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 char b[BDEVNAME_SIZE];
2174 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002175 if (rdev->sb_loaded != 1)
2176 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002177 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 dprintk("(skipping faulty ");
2179
2180 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002181 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002182 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002183 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002184 rdev->sb_page);
2185 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2186 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002187 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002188 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 } else
2191 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002192 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 /* only need to write one superblock... */
2194 break;
2195 }
NeilBrowna9701a32005-11-08 21:39:34 -08002196 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002197 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002198
NeilBrowna9701a32005-11-08 21:39:34 -08002199 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002200 if (mddev->in_sync != sync_req ||
2201 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002202 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002203 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002204 goto repeat;
2205 }
NeilBrown850b2b42006-10-03 01:15:46 -07002206 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002207 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002208 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002209 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2210 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212}
2213
Andre Noll7f6ce762008-03-23 18:34:54 +01002214/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002215 * We want to accept with case. For this we use cmd_match.
2216 */
2217static int cmd_match(const char *cmd, const char *str)
2218{
2219 /* See if cmd, written into a sysfs file, matches
2220 * str. They must either be the same, or cmd can
2221 * have a trailing newline
2222 */
2223 while (*cmd && *str && *cmd == *str) {
2224 cmd++;
2225 str++;
2226 }
2227 if (*cmd == '\n')
2228 cmd++;
2229 if (*str || *cmd)
2230 return 0;
2231 return 1;
2232}
2233
NeilBrown86e6ffd2005-11-08 21:39:24 -08002234struct rdev_sysfs_entry {
2235 struct attribute attr;
2236 ssize_t (*show)(mdk_rdev_t *, char *);
2237 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2238};
2239
2240static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002241state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002242{
2243 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002244 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002245
NeilBrownb2d444d2005-11-08 21:39:31 -08002246 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002247 len+= sprintf(page+len, "%sfaulty",sep);
2248 sep = ",";
2249 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002250 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002251 len += sprintf(page+len, "%sin_sync",sep);
2252 sep = ",";
2253 }
NeilBrownf6556752006-06-26 00:28:01 -07002254 if (test_bit(WriteMostly, &rdev->flags)) {
2255 len += sprintf(page+len, "%swrite_mostly",sep);
2256 sep = ",";
2257 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002258 if (test_bit(Blocked, &rdev->flags)) {
2259 len += sprintf(page+len, "%sblocked", sep);
2260 sep = ",";
2261 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002262 if (!test_bit(Faulty, &rdev->flags) &&
2263 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002264 len += sprintf(page+len, "%sspare", sep);
2265 sep = ",";
2266 }
2267 return len+sprintf(page+len, "\n");
2268}
2269
NeilBrown45dc2de2006-06-26 00:27:58 -07002270static ssize_t
2271state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2272{
2273 /* can write
2274 * faulty - simulates and error
2275 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002276 * writemostly - sets write_mostly
2277 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002278 * blocked - sets the Blocked flag
2279 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002280 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002281 */
2282 int err = -EINVAL;
2283 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2284 md_error(rdev->mddev, rdev);
2285 err = 0;
2286 } else if (cmd_match(buf, "remove")) {
2287 if (rdev->raid_disk >= 0)
2288 err = -EBUSY;
2289 else {
2290 mddev_t *mddev = rdev->mddev;
2291 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002292 if (mddev->pers)
2293 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002294 md_new_event(mddev);
2295 err = 0;
2296 }
NeilBrownf6556752006-06-26 00:28:01 -07002297 } else if (cmd_match(buf, "writemostly")) {
2298 set_bit(WriteMostly, &rdev->flags);
2299 err = 0;
2300 } else if (cmd_match(buf, "-writemostly")) {
2301 clear_bit(WriteMostly, &rdev->flags);
2302 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002303 } else if (cmd_match(buf, "blocked")) {
2304 set_bit(Blocked, &rdev->flags);
2305 err = 0;
2306 } else if (cmd_match(buf, "-blocked")) {
2307 clear_bit(Blocked, &rdev->flags);
2308 wake_up(&rdev->blocked_wait);
2309 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2310 md_wakeup_thread(rdev->mddev->thread);
2311
2312 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002313 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2314 set_bit(In_sync, &rdev->flags);
2315 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002316 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002317 if (!err)
2318 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002319 return err ? err : len;
2320}
NeilBrown80ca3a42006-07-10 04:44:18 -07002321static struct rdev_sysfs_entry rdev_state =
2322__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002323
2324static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002325errors_show(mdk_rdev_t *rdev, char *page)
2326{
2327 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2328}
2329
2330static ssize_t
2331errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2332{
2333 char *e;
2334 unsigned long n = simple_strtoul(buf, &e, 10);
2335 if (*buf && (*e == 0 || *e == '\n')) {
2336 atomic_set(&rdev->corrected_errors, n);
2337 return len;
2338 }
2339 return -EINVAL;
2340}
2341static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002342__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002343
NeilBrown014236d2006-01-06 00:20:55 -08002344static ssize_t
2345slot_show(mdk_rdev_t *rdev, char *page)
2346{
2347 if (rdev->raid_disk < 0)
2348 return sprintf(page, "none\n");
2349 else
2350 return sprintf(page, "%d\n", rdev->raid_disk);
2351}
2352
2353static ssize_t
2354slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2355{
2356 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002357 int err;
2358 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002359 int slot = simple_strtoul(buf, &e, 10);
2360 if (strncmp(buf, "none", 4)==0)
2361 slot = -1;
2362 else if (e==buf || (*e && *e!= '\n'))
2363 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002364 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002365 /* Setting 'slot' on an active array requires also
2366 * updating the 'rd%d' link, and communicating
2367 * with the personality with ->hot_*_disk.
2368 * For now we only support removing
2369 * failed/spare devices. This normally happens automatically,
2370 * but not when the metadata is externally managed.
2371 */
NeilBrownc303da62008-02-06 01:39:51 -08002372 if (rdev->raid_disk == -1)
2373 return -EEXIST;
2374 /* personality does all needed checks */
2375 if (rdev->mddev->pers->hot_add_disk == NULL)
2376 return -EINVAL;
2377 err = rdev->mddev->pers->
2378 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2379 if (err)
2380 return err;
2381 sprintf(nm, "rd%d", rdev->raid_disk);
2382 sysfs_remove_link(&rdev->mddev->kobj, nm);
Maciej Trelab7103102010-04-14 16:58:16 +10002383 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002384 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2385 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002386 } else if (rdev->mddev->pers) {
2387 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002388 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002389 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002390 */
2391
2392 if (rdev->raid_disk != -1)
2393 return -EBUSY;
2394
2395 if (rdev->mddev->pers->hot_add_disk == NULL)
2396 return -EINVAL;
2397
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002398 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002399 if (rdev2->raid_disk == slot)
2400 return -EEXIST;
2401
2402 rdev->raid_disk = slot;
2403 if (test_bit(In_sync, &rdev->flags))
2404 rdev->saved_raid_disk = slot;
2405 else
2406 rdev->saved_raid_disk = -1;
2407 err = rdev->mddev->pers->
2408 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002409 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002410 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002411 return err;
Neil Brown52664732008-06-28 08:31:44 +10002412 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002413 sysfs_notify_dirent_safe(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002414 sprintf(nm, "rd%d", rdev->raid_disk);
2415 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002416 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002417 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002418 } else {
2419 if (slot >= rdev->mddev->raid_disks)
2420 return -ENOSPC;
2421 rdev->raid_disk = slot;
2422 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002423 clear_bit(Faulty, &rdev->flags);
2424 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002425 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002426 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002427 }
NeilBrown014236d2006-01-06 00:20:55 -08002428 return len;
2429}
2430
2431
2432static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002433__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002434
NeilBrown93c8cad2006-01-06 00:20:56 -08002435static ssize_t
2436offset_show(mdk_rdev_t *rdev, char *page)
2437{
Andrew Morton6961ece2006-01-06 00:20:59 -08002438 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002439}
2440
2441static ssize_t
2442offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2443{
2444 char *e;
2445 unsigned long long offset = simple_strtoull(buf, &e, 10);
2446 if (e==buf || (*e && *e != '\n'))
2447 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002448 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002449 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002450 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002451 /* Must set offset before size, so overlap checks
2452 * can be sane */
2453 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002454 rdev->data_offset = offset;
2455 return len;
2456}
2457
2458static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002459__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002460
NeilBrown83303b62006-01-06 00:21:06 -08002461static ssize_t
2462rdev_size_show(mdk_rdev_t *rdev, char *page)
2463{
Andre Nolldd8ac332009-03-31 14:33:13 +11002464 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002465}
2466
NeilBrownc5d79ad2008-02-06 01:39:54 -08002467static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2468{
2469 /* check if two start/length pairs overlap */
2470 if (s1+l1 <= s2)
2471 return 0;
2472 if (s2+l2 <= s1)
2473 return 0;
2474 return 1;
2475}
2476
Dan Williamsb522adc2009-03-31 15:00:31 +11002477static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2478{
2479 unsigned long long blocks;
2480 sector_t new;
2481
2482 if (strict_strtoull(buf, 10, &blocks) < 0)
2483 return -EINVAL;
2484
2485 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2486 return -EINVAL; /* sector conversion overflow */
2487
2488 new = blocks * 2;
2489 if (new != blocks * 2)
2490 return -EINVAL; /* unsigned long long to sector_t overflow */
2491
2492 *sectors = new;
2493 return 0;
2494}
2495
NeilBrown83303b62006-01-06 00:21:06 -08002496static ssize_t
2497rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2498{
NeilBrown27c529b2008-03-04 14:29:33 -08002499 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002500 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002501 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002502
Dan Williamsb522adc2009-03-31 15:00:31 +11002503 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002504 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002505 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002506 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002507 sectors = super_types[my_mddev->major_version].
2508 rdev_size_change(rdev, sectors);
2509 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002510 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002511 } else if (!sectors)
2512 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2513 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002514 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002515 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002516 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002517
Andre Nolldd8ac332009-03-31 14:33:13 +11002518 rdev->sectors = sectors;
2519 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002520 /* need to check that all other rdevs with the same ->bdev
2521 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002522 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002523 * we have to change it back, we will have the lock again.
2524 */
2525 mddev_t *mddev;
2526 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002527 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002528
NeilBrown27c529b2008-03-04 14:29:33 -08002529 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002530 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002531 mdk_rdev_t *rdev2;
2532
2533 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002534 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002535 if (test_bit(AllReserved, &rdev2->flags) ||
2536 (rdev->bdev == rdev2->bdev &&
2537 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002538 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002539 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002540 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002541 overlap = 1;
2542 break;
2543 }
2544 mddev_unlock(mddev);
2545 if (overlap) {
2546 mddev_put(mddev);
2547 break;
2548 }
2549 }
NeilBrown27c529b2008-03-04 14:29:33 -08002550 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002551 if (overlap) {
2552 /* Someone else could have slipped in a size
2553 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002554 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002555 * safe, and trust userspace not to race with
2556 * itself
2557 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002558 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002559 return -EBUSY;
2560 }
2561 }
NeilBrown83303b62006-01-06 00:21:06 -08002562 return len;
2563}
2564
2565static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002566__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002567
Dan Williams06e3c812009-12-12 21:17:12 -07002568
2569static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2570{
2571 unsigned long long recovery_start = rdev->recovery_offset;
2572
2573 if (test_bit(In_sync, &rdev->flags) ||
2574 recovery_start == MaxSector)
2575 return sprintf(page, "none\n");
2576
2577 return sprintf(page, "%llu\n", recovery_start);
2578}
2579
2580static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2581{
2582 unsigned long long recovery_start;
2583
2584 if (cmd_match(buf, "none"))
2585 recovery_start = MaxSector;
2586 else if (strict_strtoull(buf, 10, &recovery_start))
2587 return -EINVAL;
2588
2589 if (rdev->mddev->pers &&
2590 rdev->raid_disk >= 0)
2591 return -EBUSY;
2592
2593 rdev->recovery_offset = recovery_start;
2594 if (recovery_start == MaxSector)
2595 set_bit(In_sync, &rdev->flags);
2596 else
2597 clear_bit(In_sync, &rdev->flags);
2598 return len;
2599}
2600
2601static struct rdev_sysfs_entry rdev_recovery_start =
2602__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2603
NeilBrown86e6ffd2005-11-08 21:39:24 -08002604static struct attribute *rdev_default_attrs[] = {
2605 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002606 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002607 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002608 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002609 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002610 &rdev_recovery_start.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002611 NULL,
2612};
2613static ssize_t
2614rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2615{
2616 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2617 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002618 mddev_t *mddev = rdev->mddev;
2619 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002620
2621 if (!entry->show)
2622 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002623
2624 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2625 if (!rv) {
2626 if (rdev->mddev == NULL)
2627 rv = -EBUSY;
2628 else
2629 rv = entry->show(rdev, page);
2630 mddev_unlock(mddev);
2631 }
2632 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002633}
2634
2635static ssize_t
2636rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2637 const char *page, size_t length)
2638{
2639 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2640 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002641 ssize_t rv;
2642 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002643
2644 if (!entry->store)
2645 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002646 if (!capable(CAP_SYS_ADMIN))
2647 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002648 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002649 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002650 if (rdev->mddev == NULL)
2651 rv = -EBUSY;
2652 else
2653 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002654 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002655 }
2656 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002657}
2658
2659static void rdev_free(struct kobject *ko)
2660{
2661 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2662 kfree(rdev);
2663}
Emese Revfy52cf25d2010-01-19 02:58:23 +01002664static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002665 .show = rdev_attr_show,
2666 .store = rdev_attr_store,
2667};
2668static struct kobj_type rdev_ktype = {
2669 .release = rdev_free,
2670 .sysfs_ops = &rdev_sysfs_ops,
2671 .default_attrs = rdev_default_attrs,
2672};
2673
NeilBrowne8bb9a82010-06-01 19:37:26 +10002674void md_rdev_init(mdk_rdev_t *rdev)
2675{
2676 rdev->desc_nr = -1;
2677 rdev->saved_raid_disk = -1;
2678 rdev->raid_disk = -1;
2679 rdev->flags = 0;
2680 rdev->data_offset = 0;
2681 rdev->sb_events = 0;
2682 rdev->last_read_error.tv_sec = 0;
2683 rdev->last_read_error.tv_nsec = 0;
2684 atomic_set(&rdev->nr_pending, 0);
2685 atomic_set(&rdev->read_errors, 0);
2686 atomic_set(&rdev->corrected_errors, 0);
2687
2688 INIT_LIST_HEAD(&rdev->same_set);
2689 init_waitqueue_head(&rdev->blocked_wait);
2690}
2691EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692/*
2693 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2694 *
2695 * mark the device faulty if:
2696 *
2697 * - the device is nonexistent (zero size)
2698 * - the device has no valid superblock
2699 *
2700 * a faulty rdev _never_ has rdev->sb set.
2701 */
2702static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2703{
2704 char b[BDEVNAME_SIZE];
2705 int err;
2706 mdk_rdev_t *rdev;
2707 sector_t size;
2708
NeilBrown9ffae0c2006-01-06 00:20:32 -08002709 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 if (!rdev) {
2711 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2712 return ERR_PTR(-ENOMEM);
2713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
NeilBrowne8bb9a82010-06-01 19:37:26 +10002715 md_rdev_init(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 if ((err = alloc_disk_sb(rdev)))
2717 goto abort_free;
2718
NeilBrownc5d79ad2008-02-06 01:39:54 -08002719 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 if (err)
2721 goto abort_free;
2722
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002723 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002724
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2726 if (!size) {
2727 printk(KERN_WARNING
2728 "md: %s has zero or unknown size, marking faulty!\n",
2729 bdevname(rdev->bdev,b));
2730 err = -EINVAL;
2731 goto abort_free;
2732 }
2733
2734 if (super_format >= 0) {
2735 err = super_types[super_format].
2736 load_super(rdev, NULL, super_minor);
2737 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002738 printk(KERN_WARNING
2739 "md: %s does not have a valid v%d.%d "
2740 "superblock, not importing!\n",
2741 bdevname(rdev->bdev,b),
2742 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 goto abort_free;
2744 }
2745 if (err < 0) {
2746 printk(KERN_WARNING
2747 "md: could not read %s's sb, not importing!\n",
2748 bdevname(rdev->bdev,b));
2749 goto abort_free;
2750 }
2751 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002752
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 return rdev;
2754
2755abort_free:
2756 if (rdev->sb_page) {
2757 if (rdev->bdev)
2758 unlock_rdev(rdev);
2759 free_disk_sb(rdev);
2760 }
2761 kfree(rdev);
2762 return ERR_PTR(err);
2763}
2764
2765/*
2766 * Check a full RAID array for plausibility
2767 */
2768
2769
NeilBrowna757e642005-04-16 15:26:42 -07002770static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771{
2772 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002773 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 char b[BDEVNAME_SIZE];
2775
2776 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002777 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 switch (super_types[mddev->major_version].
2779 load_super(rdev, freshest, mddev->minor_version)) {
2780 case 1:
2781 freshest = rdev;
2782 break;
2783 case 0:
2784 break;
2785 default:
2786 printk( KERN_ERR \
2787 "md: fatal superblock inconsistency in %s"
2788 " -- removing from array\n",
2789 bdevname(rdev->bdev,b));
2790 kick_rdev_from_array(rdev);
2791 }
2792
2793
2794 super_types[mddev->major_version].
2795 validate_super(mddev, freshest);
2796
2797 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002798 rdev_for_each(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10002799 if (mddev->max_disks &&
2800 (rdev->desc_nr >= mddev->max_disks ||
2801 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002802 printk(KERN_WARNING
2803 "md: %s: %s: only %d devices permitted\n",
2804 mdname(mddev), bdevname(rdev->bdev, b),
2805 mddev->max_disks);
2806 kick_rdev_from_array(rdev);
2807 continue;
2808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 if (rdev != freshest)
2810 if (super_types[mddev->major_version].
2811 validate_super(mddev, rdev)) {
2812 printk(KERN_WARNING "md: kicking non-fresh %s"
2813 " from array!\n",
2814 bdevname(rdev->bdev,b));
2815 kick_rdev_from_array(rdev);
2816 continue;
2817 }
2818 if (mddev->level == LEVEL_MULTIPATH) {
2819 rdev->desc_nr = i++;
2820 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002821 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002822 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002823 rdev->raid_disk = -1;
2824 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 }
2826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827}
2828
NeilBrown72e02072009-12-14 12:49:55 +11002829/* Read a fixed-point number.
2830 * Numbers in sysfs attributes should be in "standard" units where
2831 * possible, so time should be in seconds.
2832 * However we internally use a a much smaller unit such as
2833 * milliseconds or jiffies.
2834 * This function takes a decimal number with a possible fractional
2835 * component, and produces an integer which is the result of
2836 * multiplying that number by 10^'scale'.
2837 * all without any floating-point arithmetic.
2838 */
2839int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2840{
2841 unsigned long result = 0;
2842 long decimals = -1;
2843 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2844 if (*cp == '.')
2845 decimals = 0;
2846 else if (decimals < scale) {
2847 unsigned int value;
2848 value = *cp - '0';
2849 result = result * 10 + value;
2850 if (decimals >= 0)
2851 decimals++;
2852 }
2853 cp++;
2854 }
2855 if (*cp == '\n')
2856 cp++;
2857 if (*cp)
2858 return -EINVAL;
2859 if (decimals < 0)
2860 decimals = 0;
2861 while (decimals < scale) {
2862 result *= 10;
2863 decimals ++;
2864 }
2865 *res = result;
2866 return 0;
2867}
2868
2869
NeilBrown19052c02008-08-05 15:54:13 +10002870static void md_safemode_timeout(unsigned long data);
2871
NeilBrowneae17012005-11-08 21:39:23 -08002872static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002873safe_delay_show(mddev_t *mddev, char *page)
2874{
2875 int msec = (mddev->safemode_delay*1000)/HZ;
2876 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2877}
2878static ssize_t
2879safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2880{
NeilBrown16f17b32006-06-26 00:27:37 -07002881 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07002882
NeilBrown72e02072009-12-14 12:49:55 +11002883 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002884 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07002885 if (msec == 0)
2886 mddev->safemode_delay = 0;
2887 else {
NeilBrown19052c02008-08-05 15:54:13 +10002888 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002889 mddev->safemode_delay = (msec*HZ)/1000;
2890 if (mddev->safemode_delay == 0)
2891 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002892 if (mddev->safemode_delay < old_delay)
2893 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002894 }
2895 return len;
2896}
2897static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002898__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002899
2900static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002901level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002902{
NeilBrown2604b702006-01-06 00:20:36 -08002903 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002904 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002905 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002906 else if (mddev->clevel[0])
2907 return sprintf(page, "%s\n", mddev->clevel);
2908 else if (mddev->level != LEVEL_NONE)
2909 return sprintf(page, "%d\n", mddev->level);
2910 else
2911 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002912}
2913
NeilBrownd9d166c2006-01-06 00:20:51 -08002914static ssize_t
2915level_store(mddev_t *mddev, const char *buf, size_t len)
2916{
Dan Williamsf2859af2010-05-02 10:04:16 -07002917 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002918 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002919 struct mdk_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07002920 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11002921 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002922 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002923
2924 if (mddev->pers == NULL) {
2925 if (len == 0)
2926 return 0;
2927 if (len >= sizeof(mddev->clevel))
2928 return -ENOSPC;
2929 strncpy(mddev->clevel, buf, len);
2930 if (mddev->clevel[len-1] == '\n')
2931 len--;
2932 mddev->clevel[len] = 0;
2933 mddev->level = LEVEL_NONE;
2934 return rv;
2935 }
2936
2937 /* request to change the personality. Need to ensure:
2938 * - array is not engaged in resync/recovery/reshape
2939 * - old personality can be suspended
2940 * - new personality will access other array.
2941 */
2942
NeilBrownbb4f1e92010-08-08 21:18:03 +10002943 if (mddev->sync_thread ||
2944 mddev->reshape_position != MaxSector ||
2945 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08002946 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002947
2948 if (!mddev->pers->quiesce) {
2949 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2950 mdname(mddev), mddev->pers->name);
2951 return -EINVAL;
2952 }
2953
2954 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07002955 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11002956 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07002957 strncpy(clevel, buf, len);
2958 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002959 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07002960 clevel[len] = 0;
2961 if (strict_strtol(clevel, 10, &level))
2962 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11002963
Dan Williamsf2859af2010-05-02 10:04:16 -07002964 if (request_module("md-%s", clevel) != 0)
2965 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11002966 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07002967 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11002968 if (!pers || !try_module_get(pers->owner)) {
2969 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07002970 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11002971 return -EINVAL;
2972 }
2973 spin_unlock(&pers_lock);
2974
2975 if (pers == mddev->pers) {
2976 /* Nothing to do! */
2977 module_put(pers->owner);
2978 return rv;
2979 }
2980 if (!pers->takeover) {
2981 module_put(pers->owner);
2982 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07002983 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11002984 return -EINVAL;
2985 }
2986
NeilBrowne93f68a2010-06-15 09:36:03 +01002987 list_for_each_entry(rdev, &mddev->disks, same_set)
2988 rdev->new_raid_disk = rdev->raid_disk;
2989
NeilBrown245f46c2009-03-31 14:39:39 +11002990 /* ->takeover must set new_* and/or delta_disks
2991 * if it succeeds, and may set them when it fails.
2992 */
2993 priv = pers->takeover(mddev);
2994 if (IS_ERR(priv)) {
2995 mddev->new_level = mddev->level;
2996 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002997 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002998 mddev->raid_disks -= mddev->delta_disks;
2999 mddev->delta_disks = 0;
3000 module_put(pers->owner);
3001 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003002 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003003 return PTR_ERR(priv);
3004 }
3005
3006 /* Looks like we have a winner */
3007 mddev_suspend(mddev);
3008 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003009
3010 if (mddev->pers->sync_request == NULL &&
3011 pers->sync_request != NULL) {
3012 /* need to add the md_redundancy_group */
3013 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3014 printk(KERN_WARNING
3015 "md: cannot register extra attributes for %s\n",
3016 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003017 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003018 }
3019 if (mddev->pers->sync_request != NULL &&
3020 pers->sync_request == NULL) {
3021 /* need to remove the md_redundancy_group */
3022 if (mddev->to_remove == NULL)
3023 mddev->to_remove = &md_redundancy_group;
3024 }
3025
Trela Maciej54071b32010-03-08 16:02:42 +11003026 if (mddev->pers->sync_request == NULL &&
3027 mddev->external) {
3028 /* We are converting from a no-redundancy array
3029 * to a redundancy array and metadata is managed
3030 * externally so we need to be sure that writes
3031 * won't block due to a need to transition
3032 * clean->dirty
3033 * until external management is started.
3034 */
3035 mddev->in_sync = 0;
3036 mddev->safemode_delay = 0;
3037 mddev->safemode = 0;
3038 }
3039
NeilBrowne93f68a2010-06-15 09:36:03 +01003040 list_for_each_entry(rdev, &mddev->disks, same_set) {
3041 char nm[20];
3042 if (rdev->raid_disk < 0)
3043 continue;
3044 if (rdev->new_raid_disk > mddev->raid_disks)
3045 rdev->new_raid_disk = -1;
3046 if (rdev->new_raid_disk == rdev->raid_disk)
3047 continue;
3048 sprintf(nm, "rd%d", rdev->raid_disk);
3049 sysfs_remove_link(&mddev->kobj, nm);
3050 }
3051 list_for_each_entry(rdev, &mddev->disks, same_set) {
3052 if (rdev->raid_disk < 0)
3053 continue;
3054 if (rdev->new_raid_disk == rdev->raid_disk)
3055 continue;
3056 rdev->raid_disk = rdev->new_raid_disk;
3057 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003058 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003059 else {
3060 char nm[20];
3061 sprintf(nm, "rd%d", rdev->raid_disk);
3062 if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3063 printk("md: cannot register %s for %s after level change\n",
3064 nm, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003065 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003066 }
3067
3068 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003069 mddev->pers = pers;
3070 mddev->private = priv;
3071 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3072 mddev->level = mddev->new_level;
3073 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003074 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003075 mddev->delta_disks = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003076 if (mddev->pers->sync_request == NULL) {
3077 /* this is now an array without redundancy, so
3078 * it must always be in_sync
3079 */
3080 mddev->in_sync = 1;
3081 del_timer_sync(&mddev->safemode_timer);
3082 }
NeilBrown245f46c2009-03-31 14:39:39 +11003083 pers->run(mddev);
3084 mddev_resume(mddev);
3085 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3086 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3087 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003088 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003089 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003090 return rv;
3091}
3092
3093static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003094__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003095
NeilBrownd4dbd022006-06-26 00:27:59 -07003096
3097static ssize_t
3098layout_show(mddev_t *mddev, char *page)
3099{
3100 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003101 if (mddev->reshape_position != MaxSector &&
3102 mddev->layout != mddev->new_layout)
3103 return sprintf(page, "%d (%d)\n",
3104 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003105 return sprintf(page, "%d\n", mddev->layout);
3106}
3107
3108static ssize_t
3109layout_store(mddev_t *mddev, const char *buf, size_t len)
3110{
3111 char *e;
3112 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003113
3114 if (!*buf || (*e && *e != '\n'))
3115 return -EINVAL;
3116
NeilBrownb3546032009-03-31 14:56:41 +11003117 if (mddev->pers) {
3118 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003119 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003120 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003121 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003122 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003123 if (err) {
3124 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003125 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003126 }
NeilBrownb3546032009-03-31 14:56:41 +11003127 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003128 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003129 if (mddev->reshape_position == MaxSector)
3130 mddev->layout = n;
3131 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003132 return len;
3133}
3134static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003135__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003136
3137
NeilBrowneae17012005-11-08 21:39:23 -08003138static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003139raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003140{
NeilBrownbb636542005-11-08 21:39:45 -08003141 if (mddev->raid_disks == 0)
3142 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003143 if (mddev->reshape_position != MaxSector &&
3144 mddev->delta_disks != 0)
3145 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3146 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003147 return sprintf(page, "%d\n", mddev->raid_disks);
3148}
3149
NeilBrownda943b992006-01-06 00:20:54 -08003150static int update_raid_disks(mddev_t *mddev, int raid_disks);
3151
3152static ssize_t
3153raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3154{
NeilBrownda943b992006-01-06 00:20:54 -08003155 char *e;
3156 int rv = 0;
3157 unsigned long n = simple_strtoul(buf, &e, 10);
3158
3159 if (!*buf || (*e && *e != '\n'))
3160 return -EINVAL;
3161
3162 if (mddev->pers)
3163 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003164 else if (mddev->reshape_position != MaxSector) {
3165 int olddisks = mddev->raid_disks - mddev->delta_disks;
3166 mddev->delta_disks = n - olddisks;
3167 mddev->raid_disks = n;
3168 } else
NeilBrownda943b992006-01-06 00:20:54 -08003169 mddev->raid_disks = n;
3170 return rv ? rv : len;
3171}
3172static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003173__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003174
NeilBrown24dd4692005-11-08 21:39:26 -08003175static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003176chunk_size_show(mddev_t *mddev, char *page)
3177{
NeilBrown08a02ec2007-05-09 02:35:38 -07003178 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003179 mddev->chunk_sectors != mddev->new_chunk_sectors)
3180 return sprintf(page, "%d (%d)\n",
3181 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003182 mddev->chunk_sectors << 9);
3183 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003184}
3185
3186static ssize_t
3187chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3188{
NeilBrown3b343802006-01-06 00:20:47 -08003189 char *e;
3190 unsigned long n = simple_strtoul(buf, &e, 10);
3191
NeilBrown3b343802006-01-06 00:20:47 -08003192 if (!*buf || (*e && *e != '\n'))
3193 return -EINVAL;
3194
NeilBrownb3546032009-03-31 14:56:41 +11003195 if (mddev->pers) {
3196 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003197 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003198 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003199 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003200 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003201 if (err) {
3202 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003203 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003204 }
NeilBrownb3546032009-03-31 14:56:41 +11003205 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003206 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003207 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003208 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003209 }
NeilBrown3b343802006-01-06 00:20:47 -08003210 return len;
3211}
3212static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003213__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003214
NeilBrowna94213b2006-06-26 00:28:00 -07003215static ssize_t
3216resync_start_show(mddev_t *mddev, char *page)
3217{
NeilBrownd1a7c502009-03-31 15:24:32 +11003218 if (mddev->recovery_cp == MaxSector)
3219 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003220 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3221}
3222
3223static ssize_t
3224resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3225{
NeilBrowna94213b2006-06-26 00:28:00 -07003226 char *e;
3227 unsigned long long n = simple_strtoull(buf, &e, 10);
3228
3229 if (mddev->pers)
3230 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003231 if (cmd_match(buf, "none"))
3232 n = MaxSector;
3233 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003234 return -EINVAL;
3235
3236 mddev->recovery_cp = n;
3237 return len;
3238}
3239static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003240__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003241
NeilBrown9e653b62006-06-26 00:27:58 -07003242/*
3243 * The array state can be:
3244 *
3245 * clear
3246 * No devices, no size, no level
3247 * Equivalent to STOP_ARRAY ioctl
3248 * inactive
3249 * May have some settings, but array is not active
3250 * all IO results in error
3251 * When written, doesn't tear down array, but just stops it
3252 * suspended (not supported yet)
3253 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003254 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003255 * readonly
3256 * no resync can happen. no superblocks get written.
3257 * write requests fail
3258 * read-auto
3259 * like readonly, but behaves like 'clean' on a write request.
3260 *
3261 * clean - no pending writes, but otherwise active.
3262 * When written to inactive array, starts without resync
3263 * If a write request arrives then
3264 * if metadata is known, mark 'dirty' and switch to 'active'.
3265 * if not known, block and switch to write-pending
3266 * If written to an active array that has pending writes, then fails.
3267 * active
3268 * fully active: IO and resync can be happening.
3269 * When written to inactive array, starts with resync
3270 *
3271 * write-pending
3272 * clean, but writes are blocked waiting for 'active' to be written.
3273 *
3274 * active-idle
3275 * like active, but no writes have been seen for a while (100msec).
3276 *
3277 */
3278enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3279 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003280static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003281 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3282 "write-pending", "active-idle", NULL };
3283
3284static int match_word(const char *word, char **list)
3285{
3286 int n;
3287 for (n=0; list[n]; n++)
3288 if (cmd_match(word, list[n]))
3289 break;
3290 return n;
3291}
3292
3293static ssize_t
3294array_state_show(mddev_t *mddev, char *page)
3295{
3296 enum array_state st = inactive;
3297
3298 if (mddev->pers)
3299 switch(mddev->ro) {
3300 case 1:
3301 st = readonly;
3302 break;
3303 case 2:
3304 st = read_auto;
3305 break;
3306 case 0:
3307 if (mddev->in_sync)
3308 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08003309 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3310 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003311 else if (mddev->safemode)
3312 st = active_idle;
3313 else
3314 st = active;
3315 }
3316 else {
3317 if (list_empty(&mddev->disks) &&
3318 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003319 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003320 st = clear;
3321 else
3322 st = inactive;
3323 }
3324 return sprintf(page, "%s\n", array_states[st]);
3325}
3326
Neil Browndf5b20c2008-07-11 22:02:22 +10003327static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrowna4bd82d2010-03-29 13:23:10 +11003328static int md_set_readonly(mddev_t * mddev, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003329static int do_md_run(mddev_t * mddev);
3330static int restart_array(mddev_t *mddev);
3331
3332static ssize_t
3333array_state_store(mddev_t *mddev, const char *buf, size_t len)
3334{
3335 int err = -EINVAL;
3336 enum array_state st = match_word(buf, array_states);
3337 switch(st) {
3338 case bad_word:
3339 break;
3340 case clear:
3341 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003342 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003343 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003344 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003345 break;
3346 case inactive:
3347 /* stopping an active array */
3348 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003349 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003350 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003351 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003352 } else
3353 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003354 break;
3355 case suspended:
3356 break; /* not supported yet */
3357 case readonly:
3358 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003359 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003360 else {
3361 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003362 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003363 err = do_md_run(mddev);
3364 }
3365 break;
3366 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003367 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003368 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003369 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003370 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003371 err = restart_array(mddev);
3372 if (err == 0) {
3373 mddev->ro = 2;
3374 set_disk_ro(mddev->gendisk, 0);
3375 }
NeilBrown9e653b62006-06-26 00:27:58 -07003376 } else {
3377 mddev->ro = 2;
3378 err = do_md_run(mddev);
3379 }
3380 break;
3381 case clean:
3382 if (mddev->pers) {
3383 restart_array(mddev);
3384 spin_lock_irq(&mddev->write_lock);
3385 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003386 if (mddev->in_sync == 0) {
3387 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003388 if (mddev->safemode == 1)
3389 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003390 if (mddev->persistent)
3391 set_bit(MD_CHANGE_CLEAN,
3392 &mddev->flags);
3393 }
3394 err = 0;
3395 } else
3396 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003397 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003398 } else
3399 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003400 break;
3401 case active:
3402 if (mddev->pers) {
3403 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003404 if (mddev->external)
3405 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003406 wake_up(&mddev->sb_wait);
3407 err = 0;
3408 } else {
3409 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003410 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003411 err = do_md_run(mddev);
3412 }
3413 break;
3414 case write_pending:
3415 case active_idle:
3416 /* these cannot be set */
3417 break;
3418 }
3419 if (err)
3420 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003421 else {
NeilBrown00bcb4a2010-06-01 19:37:23 +10003422 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003423 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003424 }
NeilBrown9e653b62006-06-26 00:27:58 -07003425}
NeilBrown80ca3a42006-07-10 04:44:18 -07003426static struct md_sysfs_entry md_array_state =
3427__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003428
NeilBrown6d7ff732006-01-06 00:21:16 -08003429static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003430max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3431 return sprintf(page, "%d\n",
3432 atomic_read(&mddev->max_corr_read_errors));
3433}
3434
3435static ssize_t
3436max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3437{
3438 char *e;
3439 unsigned long n = simple_strtoul(buf, &e, 10);
3440
3441 if (*buf && (*e == 0 || *e == '\n')) {
3442 atomic_set(&mddev->max_corr_read_errors, n);
3443 return len;
3444 }
3445 return -EINVAL;
3446}
3447
3448static struct md_sysfs_entry max_corr_read_errors =
3449__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3450 max_corrected_read_errors_store);
3451
3452static ssize_t
NeilBrown6d7ff732006-01-06 00:21:16 -08003453null_show(mddev_t *mddev, char *page)
3454{
3455 return -EINVAL;
3456}
3457
3458static ssize_t
3459new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3460{
3461 /* buf must be %d:%d\n? giving major and minor numbers */
3462 /* The new device is added to the array.
3463 * If the array has a persistent superblock, we read the
3464 * superblock to initialise info and check validity.
3465 * Otherwise, only checking done is that in bind_rdev_to_array,
3466 * which mainly checks size.
3467 */
3468 char *e;
3469 int major = simple_strtoul(buf, &e, 10);
3470 int minor;
3471 dev_t dev;
3472 mdk_rdev_t *rdev;
3473 int err;
3474
3475 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3476 return -EINVAL;
3477 minor = simple_strtoul(e+1, &e, 10);
3478 if (*e && *e != '\n')
3479 return -EINVAL;
3480 dev = MKDEV(major, minor);
3481 if (major != MAJOR(dev) ||
3482 minor != MINOR(dev))
3483 return -EOVERFLOW;
3484
3485
3486 if (mddev->persistent) {
3487 rdev = md_import_device(dev, mddev->major_version,
3488 mddev->minor_version);
3489 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3490 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3491 mdk_rdev_t, same_set);
3492 err = super_types[mddev->major_version]
3493 .load_super(rdev, rdev0, mddev->minor_version);
3494 if (err < 0)
3495 goto out;
3496 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003497 } else if (mddev->external)
3498 rdev = md_import_device(dev, -2, -1);
3499 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003500 rdev = md_import_device(dev, -1, -1);
3501
3502 if (IS_ERR(rdev))
3503 return PTR_ERR(rdev);
3504 err = bind_rdev_to_array(rdev, mddev);
3505 out:
3506 if (err)
3507 export_rdev(rdev);
3508 return err ? err : len;
3509}
3510
3511static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003512__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003513
3514static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003515bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3516{
3517 char *end;
3518 unsigned long chunk, end_chunk;
3519
3520 if (!mddev->bitmap)
3521 goto out;
3522 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3523 while (*buf) {
3524 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3525 if (buf == end) break;
3526 if (*end == '-') { /* range */
3527 buf = end + 1;
3528 end_chunk = simple_strtoul(buf, &end, 0);
3529 if (buf == end) break;
3530 }
3531 if (*end && !isspace(*end)) break;
3532 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003533 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003534 }
3535 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3536out:
3537 return len;
3538}
3539
3540static struct md_sysfs_entry md_bitmap =
3541__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3542
3543static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003544size_show(mddev_t *mddev, char *page)
3545{
Andre Noll58c0fed2009-03-31 14:33:13 +11003546 return sprintf(page, "%llu\n",
3547 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003548}
3549
Andre Nolld71f9f82008-07-11 22:02:22 +10003550static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003551
3552static ssize_t
3553size_store(mddev_t *mddev, const char *buf, size_t len)
3554{
3555 /* If array is inactive, we can reduce the component size, but
3556 * not increase it (except from 0).
3557 * If array is active, we can try an on-line resize
3558 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003559 sector_t sectors;
3560 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003561
Andre Noll58c0fed2009-03-31 14:33:13 +11003562 if (err < 0)
3563 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003564 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003565 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003566 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003567 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003568 if (mddev->dev_sectors == 0 ||
3569 mddev->dev_sectors > sectors)
3570 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003571 else
3572 err = -ENOSPC;
3573 }
3574 return err ? err : len;
3575}
3576
3577static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003578__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003579
NeilBrown8bb93aa2006-01-06 00:20:50 -08003580
3581/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003582 * This is one of
3583 * 'none' for arrays with no metadata (good luck...)
3584 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003585 * or N.M for internally known formats
3586 */
3587static ssize_t
3588metadata_show(mddev_t *mddev, char *page)
3589{
3590 if (mddev->persistent)
3591 return sprintf(page, "%d.%d\n",
3592 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003593 else if (mddev->external)
3594 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003595 else
3596 return sprintf(page, "none\n");
3597}
3598
3599static ssize_t
3600metadata_store(mddev_t *mddev, const char *buf, size_t len)
3601{
3602 int major, minor;
3603 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003604 /* Changing the details of 'external' metadata is
3605 * always permitted. Otherwise there must be
3606 * no devices attached to the array.
3607 */
3608 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3609 ;
3610 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003611 return -EBUSY;
3612
3613 if (cmd_match(buf, "none")) {
3614 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003615 mddev->external = 0;
3616 mddev->major_version = 0;
3617 mddev->minor_version = 90;
3618 return len;
3619 }
3620 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003621 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003622 if (namelen >= sizeof(mddev->metadata_type))
3623 namelen = sizeof(mddev->metadata_type)-1;
3624 strncpy(mddev->metadata_type, buf+9, namelen);
3625 mddev->metadata_type[namelen] = 0;
3626 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3627 mddev->metadata_type[--namelen] = 0;
3628 mddev->persistent = 0;
3629 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003630 mddev->major_version = 0;
3631 mddev->minor_version = 90;
3632 return len;
3633 }
3634 major = simple_strtoul(buf, &e, 10);
3635 if (e==buf || *e != '.')
3636 return -EINVAL;
3637 buf = e+1;
3638 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003639 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003640 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003641 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003642 return -ENOENT;
3643 mddev->major_version = major;
3644 mddev->minor_version = minor;
3645 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003646 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003647 return len;
3648}
3649
3650static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003651__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003652
NeilBrowna35b0d62006-01-06 00:20:49 -08003653static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003654action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003655{
NeilBrown7eec3142005-11-08 21:39:44 -08003656 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003657 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3658 type = "frozen";
3659 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003660 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003661 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3662 type = "reshape";
3663 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003664 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3665 type = "resync";
3666 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3667 type = "check";
3668 else
3669 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003670 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003671 type = "recover";
3672 }
3673 return sprintf(page, "%s\n", type);
3674}
3675
3676static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003677action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003678{
NeilBrown7eec3142005-11-08 21:39:44 -08003679 if (!mddev->pers || !mddev->pers->sync_request)
3680 return -EINVAL;
3681
NeilBrownb6a9ce62009-05-26 09:41:17 +10003682 if (cmd_match(page, "frozen"))
3683 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3684 else
3685 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3686
3687 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003688 if (mddev->sync_thread) {
3689 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3690 md_unregister_thread(mddev->sync_thread);
3691 mddev->sync_thread = NULL;
3692 mddev->recovery = 0;
3693 }
NeilBrown03c902e2006-01-06 00:20:46 -08003694 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3695 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003696 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003697 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003698 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003699 else if (cmd_match(page, "recover")) {
3700 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3701 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3702 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003703 int err;
3704 if (mddev->pers->start_reshape == NULL)
3705 return -EINVAL;
3706 err = mddev->pers->start_reshape(mddev);
3707 if (err)
3708 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003709 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003710 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003711 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003712 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003713 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003714 return -EINVAL;
3715 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3716 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003717 }
NeilBrown03c902e2006-01-06 00:20:46 -08003718 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003719 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10003720 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003721 return len;
3722}
3723
NeilBrown9d888832005-11-08 21:39:26 -08003724static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003725mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003726{
3727 return sprintf(page, "%llu\n",
3728 (unsigned long long) mddev->resync_mismatches);
3729}
3730
NeilBrown80ca3a42006-07-10 04:44:18 -07003731static struct md_sysfs_entry md_scan_mode =
3732__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003733
NeilBrown96de1e62005-11-08 21:39:39 -08003734
NeilBrown80ca3a42006-07-10 04:44:18 -07003735static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003736
NeilBrown88202a02006-01-06 00:21:36 -08003737static ssize_t
3738sync_min_show(mddev_t *mddev, char *page)
3739{
3740 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3741 mddev->sync_speed_min ? "local": "system");
3742}
3743
3744static ssize_t
3745sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3746{
3747 int min;
3748 char *e;
3749 if (strncmp(buf, "system", 6)==0) {
3750 mddev->sync_speed_min = 0;
3751 return len;
3752 }
3753 min = simple_strtoul(buf, &e, 10);
3754 if (buf == e || (*e && *e != '\n') || min <= 0)
3755 return -EINVAL;
3756 mddev->sync_speed_min = min;
3757 return len;
3758}
3759
3760static struct md_sysfs_entry md_sync_min =
3761__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3762
3763static ssize_t
3764sync_max_show(mddev_t *mddev, char *page)
3765{
3766 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3767 mddev->sync_speed_max ? "local": "system");
3768}
3769
3770static ssize_t
3771sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3772{
3773 int max;
3774 char *e;
3775 if (strncmp(buf, "system", 6)==0) {
3776 mddev->sync_speed_max = 0;
3777 return len;
3778 }
3779 max = simple_strtoul(buf, &e, 10);
3780 if (buf == e || (*e && *e != '\n') || max <= 0)
3781 return -EINVAL;
3782 mddev->sync_speed_max = max;
3783 return len;
3784}
3785
3786static struct md_sysfs_entry md_sync_max =
3787__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3788
Iustin Popd7f3d292007-10-16 23:30:54 -07003789static ssize_t
3790degraded_show(mddev_t *mddev, char *page)
3791{
3792 return sprintf(page, "%d\n", mddev->degraded);
3793}
3794static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003795
3796static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003797sync_force_parallel_show(mddev_t *mddev, char *page)
3798{
3799 return sprintf(page, "%d\n", mddev->parallel_resync);
3800}
3801
3802static ssize_t
3803sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3804{
3805 long n;
3806
3807 if (strict_strtol(buf, 10, &n))
3808 return -EINVAL;
3809
3810 if (n != 0 && n != 1)
3811 return -EINVAL;
3812
3813 mddev->parallel_resync = n;
3814
3815 if (mddev->sync_thread)
3816 wake_up(&resync_wait);
3817
3818 return len;
3819}
3820
3821/* force parallel resync, even with shared block devices */
3822static struct md_sysfs_entry md_sync_force_parallel =
3823__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3824 sync_force_parallel_show, sync_force_parallel_store);
3825
3826static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003827sync_speed_show(mddev_t *mddev, char *page)
3828{
3829 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003830 if (mddev->curr_resync == 0)
3831 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003832 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3833 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003834 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003835 db = resync - mddev->resync_mark_cnt;
3836 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003837}
3838
NeilBrown80ca3a42006-07-10 04:44:18 -07003839static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003840
3841static ssize_t
3842sync_completed_show(mddev_t *mddev, char *page)
3843{
Andre Noll58c0fed2009-03-31 14:33:13 +11003844 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003845
NeilBrownacb180b2009-04-14 16:28:34 +10003846 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3847 return sprintf(page, "none\n");
3848
NeilBrown88202a02006-01-06 00:21:36 -08003849 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003850 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003851 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003852 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003853
NeilBrownacb180b2009-04-14 16:28:34 +10003854 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003855 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003856}
3857
NeilBrown80ca3a42006-07-10 04:44:18 -07003858static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003859
NeilBrowne464eaf2006-03-27 01:18:14 -08003860static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003861min_sync_show(mddev_t *mddev, char *page)
3862{
3863 return sprintf(page, "%llu\n",
3864 (unsigned long long)mddev->resync_min);
3865}
3866static ssize_t
3867min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3868{
3869 unsigned long long min;
3870 if (strict_strtoull(buf, 10, &min))
3871 return -EINVAL;
3872 if (min > mddev->resync_max)
3873 return -EINVAL;
3874 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3875 return -EBUSY;
3876
3877 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003878 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003879 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003880 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003881 return -EINVAL;
3882 }
3883 mddev->resync_min = min;
3884
3885 return len;
3886}
3887
3888static struct md_sysfs_entry md_min_sync =
3889__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3890
3891static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003892max_sync_show(mddev_t *mddev, char *page)
3893{
3894 if (mddev->resync_max == MaxSector)
3895 return sprintf(page, "max\n");
3896 else
3897 return sprintf(page, "%llu\n",
3898 (unsigned long long)mddev->resync_max);
3899}
3900static ssize_t
3901max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3902{
3903 if (strncmp(buf, "max", 3) == 0)
3904 mddev->resync_max = MaxSector;
3905 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003906 unsigned long long max;
3907 if (strict_strtoull(buf, 10, &max))
3908 return -EINVAL;
3909 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003910 return -EINVAL;
3911 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003912 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003913 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3914 return -EBUSY;
3915
3916 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003917 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003918 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003919 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003920 return -EINVAL;
3921 }
3922 mddev->resync_max = max;
3923 }
3924 wake_up(&mddev->recovery_wait);
3925 return len;
3926}
3927
3928static struct md_sysfs_entry md_max_sync =
3929__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3930
3931static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003932suspend_lo_show(mddev_t *mddev, char *page)
3933{
3934 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3935}
3936
3937static ssize_t
3938suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3939{
3940 char *e;
3941 unsigned long long new = simple_strtoull(buf, &e, 10);
3942
NeilBrownb8d966e2009-07-01 11:14:04 +10003943 if (mddev->pers == NULL ||
3944 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003945 return -EINVAL;
3946 if (buf == e || (*e && *e != '\n'))
3947 return -EINVAL;
3948 if (new >= mddev->suspend_hi ||
3949 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3950 mddev->suspend_lo = new;
3951 mddev->pers->quiesce(mddev, 2);
3952 return len;
3953 } else
3954 return -EINVAL;
3955}
3956static struct md_sysfs_entry md_suspend_lo =
3957__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3958
3959
3960static ssize_t
3961suspend_hi_show(mddev_t *mddev, char *page)
3962{
3963 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3964}
3965
3966static ssize_t
3967suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3968{
3969 char *e;
3970 unsigned long long new = simple_strtoull(buf, &e, 10);
3971
NeilBrownb8d966e2009-07-01 11:14:04 +10003972 if (mddev->pers == NULL ||
3973 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003974 return -EINVAL;
3975 if (buf == e || (*e && *e != '\n'))
3976 return -EINVAL;
3977 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3978 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3979 mddev->suspend_hi = new;
3980 mddev->pers->quiesce(mddev, 1);
3981 mddev->pers->quiesce(mddev, 0);
3982 return len;
3983 } else
3984 return -EINVAL;
3985}
3986static struct md_sysfs_entry md_suspend_hi =
3987__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3988
NeilBrown08a02ec2007-05-09 02:35:38 -07003989static ssize_t
3990reshape_position_show(mddev_t *mddev, char *page)
3991{
3992 if (mddev->reshape_position != MaxSector)
3993 return sprintf(page, "%llu\n",
3994 (unsigned long long)mddev->reshape_position);
3995 strcpy(page, "none\n");
3996 return 5;
3997}
3998
3999static ssize_t
4000reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
4001{
4002 char *e;
4003 unsigned long long new = simple_strtoull(buf, &e, 10);
4004 if (mddev->pers)
4005 return -EBUSY;
4006 if (buf == e || (*e && *e != '\n'))
4007 return -EINVAL;
4008 mddev->reshape_position = new;
4009 mddev->delta_disks = 0;
4010 mddev->new_level = mddev->level;
4011 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004012 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004013 return len;
4014}
4015
4016static struct md_sysfs_entry md_reshape_position =
4017__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4018 reshape_position_store);
4019
Dan Williamsb522adc2009-03-31 15:00:31 +11004020static ssize_t
4021array_size_show(mddev_t *mddev, char *page)
4022{
4023 if (mddev->external_size)
4024 return sprintf(page, "%llu\n",
4025 (unsigned long long)mddev->array_sectors/2);
4026 else
4027 return sprintf(page, "default\n");
4028}
4029
4030static ssize_t
4031array_size_store(mddev_t *mddev, const char *buf, size_t len)
4032{
4033 sector_t sectors;
4034
4035 if (strncmp(buf, "default", 7) == 0) {
4036 if (mddev->pers)
4037 sectors = mddev->pers->size(mddev, 0, 0);
4038 else
4039 sectors = mddev->array_sectors;
4040
4041 mddev->external_size = 0;
4042 } else {
4043 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4044 return -EINVAL;
4045 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004046 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004047
4048 mddev->external_size = 1;
4049 }
4050
4051 mddev->array_sectors = sectors;
4052 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004053 if (mddev->pers)
4054 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11004055
4056 return len;
4057}
4058
4059static struct md_sysfs_entry md_array_size =
4060__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4061 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004062
NeilBrowneae17012005-11-08 21:39:23 -08004063static struct attribute *md_default_attrs[] = {
4064 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004065 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004066 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004067 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004068 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004069 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004070 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004071 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004072 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004073 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004074 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004075 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004076 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004077 NULL,
4078};
4079
4080static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004081 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004082 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004083 &md_sync_min.attr,
4084 &md_sync_max.attr,
4085 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004086 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004087 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004088 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004089 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004090 &md_suspend_lo.attr,
4091 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004092 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004093 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004094 NULL,
4095};
NeilBrown411036f2005-11-08 21:39:40 -08004096static struct attribute_group md_redundancy_group = {
4097 .name = NULL,
4098 .attrs = md_redundancy_attrs,
4099};
4100
NeilBrowneae17012005-11-08 21:39:23 -08004101
4102static ssize_t
4103md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4104{
4105 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4106 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004107 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004108
4109 if (!entry->show)
4110 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004111 rv = mddev_lock(mddev);
4112 if (!rv) {
4113 rv = entry->show(mddev, page);
4114 mddev_unlock(mddev);
4115 }
NeilBrown96de1e62005-11-08 21:39:39 -08004116 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004117}
4118
4119static ssize_t
4120md_attr_store(struct kobject *kobj, struct attribute *attr,
4121 const char *page, size_t length)
4122{
4123 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4124 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004125 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004126
4127 if (!entry->store)
4128 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004129 if (!capable(CAP_SYS_ADMIN))
4130 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004131 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004132 if (mddev->hold_active == UNTIL_IOCTL)
4133 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004134 if (!rv) {
4135 rv = entry->store(mddev, page, length);
4136 mddev_unlock(mddev);
4137 }
NeilBrown96de1e62005-11-08 21:39:39 -08004138 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004139}
4140
4141static void md_free(struct kobject *ko)
4142{
4143 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004144
4145 if (mddev->sysfs_state)
4146 sysfs_put(mddev->sysfs_state);
4147
4148 if (mddev->gendisk) {
4149 del_gendisk(mddev->gendisk);
4150 put_disk(mddev->gendisk);
4151 }
4152 if (mddev->queue)
4153 blk_cleanup_queue(mddev->queue);
4154
NeilBrowneae17012005-11-08 21:39:23 -08004155 kfree(mddev);
4156}
4157
Emese Revfy52cf25d2010-01-19 02:58:23 +01004158static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004159 .show = md_attr_show,
4160 .store = md_attr_store,
4161};
4162static struct kobj_type md_ktype = {
4163 .release = md_free,
4164 .sysfs_ops = &md_sysfs_ops,
4165 .default_attrs = md_default_attrs,
4166};
4167
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168int mdp_major = 0;
4169
Dan Williams5fd3a172009-03-04 00:57:25 -07004170static void mddev_delayed_delete(struct work_struct *ws)
4171{
4172 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4173
NeilBrown43a70502009-12-14 12:49:55 +11004174 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004175 kobject_del(&mddev->kobj);
4176 kobject_put(&mddev->kobj);
4177}
4178
NeilBrownefeb53c2009-01-09 08:31:10 +11004179static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004181 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 mddev_t *mddev = mddev_find(dev);
4183 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004184 int partitioned;
4185 int shift;
4186 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004187 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
4189 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004190 return -ENODEV;
4191
4192 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4193 shift = partitioned ? MdpMinorShift : 0;
4194 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
NeilBrownd3374822009-01-09 08:31:10 +11004196 /* wait for any previous instance if this device
4197 * to be completed removed (mddev_delayed_delete).
4198 */
4199 flush_scheduled_work();
4200
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004201 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004202 error = -EEXIST;
4203 if (mddev->gendisk)
4204 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004205
4206 if (name) {
4207 /* Need to ensure that 'name' is not a duplicate.
4208 */
4209 mddev_t *mddev2;
4210 spin_lock(&all_mddevs_lock);
4211
4212 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4213 if (mddev2->gendisk &&
4214 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4215 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004216 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004217 }
4218 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 }
NeilBrown8b765392009-01-09 08:31:08 +11004220
NeilBrown0909dc42009-07-01 12:27:21 +10004221 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004222 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004223 if (!mddev->queue)
4224 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004225 mddev->queue->queuedata = mddev;
4226
NeilBrown8b765392009-01-09 08:31:08 +11004227 /* Can be unlocked because the queue is new: no concurrency */
4228 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4229
NeilBrown409c57f2009-03-31 14:39:39 +11004230 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004231
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 disk = alloc_disk(1 << shift);
4233 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004234 blk_cleanup_queue(mddev->queue);
4235 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004236 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004238 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004240 if (name)
4241 strcpy(disk->disk_name, name);
4242 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004244 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 disk->fops = &md_fops;
4247 disk->private_data = mddev;
4248 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004249 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004250 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004251 * remove it now.
4252 */
4253 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 add_disk(disk);
4255 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004256 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4257 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004258 if (error) {
4259 /* This isn't possible, but as kobject_init_and_add is marked
4260 * __must_check, we must do something with the result
4261 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004262 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4263 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004264 error = 0;
4265 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004266 if (mddev->kobj.sd &&
4267 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004268 printk(KERN_DEBUG "pointless warning\n");
NeilBrown0909dc42009-07-01 12:27:21 +10004269 abort:
4270 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004271 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004272 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004273 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004274 }
NeilBrownd3374822009-01-09 08:31:10 +11004275 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004276 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004277}
4278
4279static struct kobject *md_probe(dev_t dev, int *part, void *data)
4280{
4281 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 return NULL;
4283}
4284
NeilBrownefeb53c2009-01-09 08:31:10 +11004285static int add_named_array(const char *val, struct kernel_param *kp)
4286{
4287 /* val must be "md_*" where * is not all digits.
4288 * We allocate an array with a large free minor number, and
4289 * set the name to val. val must not already be an active name.
4290 */
4291 int len = strlen(val);
4292 char buf[DISK_NAME_LEN];
4293
4294 while (len && val[len-1] == '\n')
4295 len--;
4296 if (len >= DISK_NAME_LEN)
4297 return -E2BIG;
4298 strlcpy(buf, val, len+1);
4299 if (strncmp(buf, "md_", 3) != 0)
4300 return -EINVAL;
4301 return md_alloc(0, buf);
4302}
4303
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304static void md_safemode_timeout(unsigned long data)
4305{
4306 mddev_t *mddev = (mddev_t *) data;
4307
Neil Brown0fd62b82008-06-28 08:31:36 +10004308 if (!atomic_read(&mddev->writes_pending)) {
4309 mddev->safemode = 1;
4310 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004311 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 md_wakeup_thread(mddev->thread);
4314}
4315
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004316static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317
NeilBrown390ee602010-06-01 19:37:27 +10004318int md_run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319{
NeilBrown2604b702006-01-06 00:20:36 -08004320 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 mdk_rdev_t *rdev;
NeilBrown2604b702006-01-06 00:20:36 -08004322 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323
NeilBrowna757e642005-04-16 15:26:42 -07004324 if (list_empty(&mddev->disks))
4325 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
4328 if (mddev->pers)
4329 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004330 /* Cannot run until previous stop completes properly */
4331 if (mddev->sysfs_active)
4332 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004333
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 /*
4335 * Analyze all RAID superblock(s)
4336 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004337 if (!mddev->raid_disks) {
4338 if (!mddev->persistent)
4339 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004340 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
NeilBrownd9d166c2006-01-06 00:20:51 -08004343 if (mddev->level != LEVEL_NONE)
4344 request_module("md-level-%d", mddev->level);
4345 else if (mddev->clevel[0])
4346 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
4348 /*
4349 * Drop all container device buffers, from now on
4350 * the only valid external interface is through the md
4351 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004353 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004354 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 continue;
4356 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004357 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004358
4359 /* perform some consistency tests on the device.
4360 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004361 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004362 */
Andre Noll0f420352008-07-11 22:02:23 +10004363 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004364 if (mddev->dev_sectors &&
4365 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004366 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004367 printk("md: %s: data overlaps metadata\n",
4368 mdname(mddev));
4369 return -EINVAL;
4370 }
4371 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004372 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004373 > rdev->data_offset) {
4374 printk("md: %s: metadata overlaps data\n",
4375 mdname(mddev));
4376 return -EINVAL;
4377 }
4378 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004379 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 }
4381
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004383 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004384 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004386 if (mddev->level != LEVEL_NONE)
4387 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4388 mddev->level);
4389 else
4390 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4391 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 return -EINVAL;
4393 }
NeilBrown2604b702006-01-06 00:20:36 -08004394 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004396 if (mddev->level != pers->level) {
4397 mddev->level = pers->level;
4398 mddev->new_level = pers->level;
4399 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004400 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401
NeilBrownf6705572006-03-27 01:18:11 -08004402 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004403 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004404 /* This personality cannot handle reshaping... */
4405 mddev->pers = NULL;
4406 module_put(pers->owner);
4407 return -EINVAL;
4408 }
4409
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004410 if (pers->sync_request) {
4411 /* Warn if this is a potentially silly
4412 * configuration.
4413 */
4414 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4415 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004416 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004417
4418 list_for_each_entry(rdev, &mddev->disks, same_set)
4419 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004420 if (rdev < rdev2 &&
4421 rdev->bdev->bd_contains ==
4422 rdev2->bdev->bd_contains) {
4423 printk(KERN_WARNING
4424 "%s: WARNING: %s appears to be"
4425 " on the same physical disk as"
4426 " %s.\n",
4427 mdname(mddev),
4428 bdevname(rdev->bdev,b),
4429 bdevname(rdev2->bdev,b2));
4430 warned = 1;
4431 }
4432 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004433
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004434 if (warned)
4435 printk(KERN_WARNING
4436 "True protection against single-disk"
4437 " failure might be compromised.\n");
4438 }
4439
NeilBrown657390d2005-08-26 18:34:16 -07004440 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004441 /* may be over-ridden by personality */
4442 mddev->resync_max_sectors = mddev->dev_sectors;
4443
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004444 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445
NeilBrown0f9552b2009-12-30 12:08:50 +11004446 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004447 mddev->ro = 2; /* read-only, but switch on first write */
4448
NeilBrownb15c2e52006-01-06 00:20:16 -08004449 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004450 if (err)
4451 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004452 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4453 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4454 " but 'external_size' not in effect?\n", __func__);
4455 printk(KERN_ERR
4456 "md: invalid array_size %llu > default size %llu\n",
4457 (unsigned long long)mddev->array_sectors / 2,
4458 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4459 err = -EINVAL;
4460 mddev->pers->stop(mddev);
4461 }
4462 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004463 err = bitmap_create(mddev);
4464 if (err) {
4465 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4466 mdname(mddev), err);
4467 mddev->pers->stop(mddev);
4468 }
4469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 module_put(mddev->pers->owner);
4472 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004473 bitmap_destroy(mddev);
4474 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004476 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004477 if (mddev->kobj.sd &&
4478 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004479 printk(KERN_WARNING
4480 "md: cannot register extra attributes for %s\n",
4481 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004482 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004483 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004484 mddev->ro = 0;
4485
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004487 atomic_set(&mddev->max_corr_read_errors,
4488 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 mddev->safemode = 0;
4490 mddev->safemode_timer.function = md_safemode_timeout;
4491 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004492 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004494
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004495 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004496 if (rdev->raid_disk >= 0) {
4497 char nm[20];
4498 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004499 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004500 /* failure here is OK */;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
4503 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4504
NeilBrown850b2b42006-10-03 01:15:46 -07004505 if (mddev->flags)
4506 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507
NeilBrown0b8c9de2006-07-10 04:44:15 -07004508 md_wakeup_thread(mddev->thread);
4509 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004510
NeilBrownd7603b72006-01-06 00:20:30 -08004511 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004512 sysfs_notify_dirent_safe(mddev->sysfs_state);
4513 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004514 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 return 0;
4516}
NeilBrown390ee602010-06-01 19:37:27 +10004517EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518
NeilBrownfe60b012010-03-29 11:10:42 +11004519static int do_md_run(mddev_t *mddev)
4520{
4521 int err;
4522
4523 err = md_run(mddev);
4524 if (err)
4525 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10004526 err = bitmap_load(mddev);
4527 if (err) {
4528 bitmap_destroy(mddev);
4529 goto out;
4530 }
NeilBrownfe60b012010-03-29 11:10:42 +11004531 set_capacity(mddev->gendisk, mddev->array_sectors);
4532 revalidate_disk(mddev->gendisk);
4533 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4534out:
4535 return err;
4536}
4537
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538static int restart_array(mddev_t *mddev)
4539{
4540 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541
Andre Noll80fab1d2008-07-11 22:02:21 +10004542 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004544 return -ENXIO;
4545 if (!mddev->pers)
4546 return -EINVAL;
4547 if (!mddev->ro)
4548 return -EBUSY;
4549 mddev->safemode = 0;
4550 mddev->ro = 0;
4551 set_disk_ro(disk, 0);
4552 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4553 mdname(mddev));
4554 /* Kick recovery or resync if necessary */
4555 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4556 md_wakeup_thread(mddev->thread);
4557 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004558 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004559 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560}
4561
NeilBrownacc55e22006-06-26 00:27:47 -07004562/* similar to deny_write_access, but accounts for our holding a reference
4563 * to the file ourselves */
4564static int deny_bitmap_write_access(struct file * file)
4565{
4566 struct inode *inode = file->f_mapping->host;
4567
4568 spin_lock(&inode->i_lock);
4569 if (atomic_read(&inode->i_writecount) > 1) {
4570 spin_unlock(&inode->i_lock);
4571 return -ETXTBSY;
4572 }
4573 atomic_set(&inode->i_writecount, -1);
4574 spin_unlock(&inode->i_lock);
4575
4576 return 0;
4577}
4578
NeilBrown43a70502009-12-14 12:49:55 +11004579void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004580{
4581 struct inode *inode = file->f_mapping->host;
4582
4583 spin_lock(&inode->i_lock);
4584 atomic_set(&inode->i_writecount, 1);
4585 spin_unlock(&inode->i_lock);
4586}
4587
NeilBrown6177b472010-03-29 11:37:13 +11004588static void md_clean(mddev_t *mddev)
4589{
4590 mddev->array_sectors = 0;
4591 mddev->external_size = 0;
4592 mddev->dev_sectors = 0;
4593 mddev->raid_disks = 0;
4594 mddev->recovery_cp = 0;
4595 mddev->resync_min = 0;
4596 mddev->resync_max = MaxSector;
4597 mddev->reshape_position = MaxSector;
4598 mddev->external = 0;
4599 mddev->persistent = 0;
4600 mddev->level = LEVEL_NONE;
4601 mddev->clevel[0] = 0;
4602 mddev->flags = 0;
4603 mddev->ro = 0;
4604 mddev->metadata_type[0] = 0;
4605 mddev->chunk_sectors = 0;
4606 mddev->ctime = mddev->utime = 0;
4607 mddev->layout = 0;
4608 mddev->max_disks = 0;
4609 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10004610 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004611 mddev->delta_disks = 0;
4612 mddev->new_level = LEVEL_NONE;
4613 mddev->new_layout = 0;
4614 mddev->new_chunk_sectors = 0;
4615 mddev->curr_resync = 0;
4616 mddev->resync_mismatches = 0;
4617 mddev->suspend_lo = mddev->suspend_hi = 0;
4618 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4619 mddev->recovery = 0;
4620 mddev->in_sync = 0;
4621 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004622 mddev->safemode = 0;
4623 mddev->bitmap_info.offset = 0;
4624 mddev->bitmap_info.default_offset = 0;
4625 mddev->bitmap_info.chunksize = 0;
4626 mddev->bitmap_info.daemon_sleep = 0;
4627 mddev->bitmap_info.max_write_behind = 0;
NeilBrown252ac522010-06-01 19:37:29 +10004628 mddev->plug = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004629}
4630
NeilBrown390ee602010-06-01 19:37:27 +10004631void md_stop_writes(mddev_t *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11004632{
4633 if (mddev->sync_thread) {
4634 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4635 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4636 md_unregister_thread(mddev->sync_thread);
4637 mddev->sync_thread = NULL;
4638 }
4639
4640 del_timer_sync(&mddev->safemode_timer);
4641
4642 bitmap_flush(mddev);
4643 md_super_wait(mddev);
4644
4645 if (!mddev->in_sync || mddev->flags) {
4646 /* mark array as shutdown cleanly */
4647 mddev->in_sync = 1;
4648 md_update_sb(mddev, 1);
4649 }
4650}
NeilBrown390ee602010-06-01 19:37:27 +10004651EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11004652
NeilBrown390ee602010-06-01 19:37:27 +10004653void md_stop(mddev_t *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11004654{
4655 mddev->pers->stop(mddev);
4656 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4657 mddev->to_remove = &md_redundancy_group;
4658 module_put(mddev->pers->owner);
4659 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11004660 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11004661}
NeilBrown390ee602010-06-01 19:37:27 +10004662EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11004663
NeilBrowna4bd82d2010-03-29 13:23:10 +11004664static int md_set_readonly(mddev_t *mddev, int is_open)
4665{
4666 int err = 0;
4667 mutex_lock(&mddev->open_mutex);
4668 if (atomic_read(&mddev->openers) > is_open) {
4669 printk("md: %s still in use.\n",mdname(mddev));
4670 err = -EBUSY;
4671 goto out;
4672 }
4673 if (mddev->pers) {
4674 md_stop_writes(mddev);
4675
4676 err = -ENXIO;
4677 if (mddev->ro==1)
4678 goto out;
4679 mddev->ro = 1;
4680 set_disk_ro(mddev->gendisk, 1);
4681 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004682 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004683 err = 0;
4684 }
4685out:
4686 mutex_unlock(&mddev->open_mutex);
4687 return err;
4688}
4689
NeilBrown9e653b62006-06-26 00:27:58 -07004690/* mode:
4691 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07004692 * 2 - stop but do not disassemble array
4693 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004694static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004697 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698
NeilBrownc8c00a62009-08-10 12:50:52 +10004699 mutex_lock(&mddev->open_mutex);
NeilBrownbb4f1e92010-08-08 21:18:03 +10004700 if (atomic_read(&mddev->openers) > is_open ||
4701 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004702 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10004703 mutex_unlock(&mddev->open_mutex);
4704 return -EBUSY;
4705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706
NeilBrown6e17b022010-08-07 21:41:19 +10004707 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11004708 if (mddev->ro)
4709 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004710
NeilBrown390ee602010-06-01 19:37:27 +10004711 md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004712 md_stop(mddev);
4713 mddev->queue->merge_bvec_fn = NULL;
4714 mddev->queue->unplug_fn = NULL;
4715 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004716
NeilBrowna4bd82d2010-03-29 13:23:10 +11004717 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10004718 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004719
NeilBrowna4bd82d2010-03-29 13:23:10 +11004720 list_for_each_entry(rdev, &mddev->disks, same_set)
4721 if (rdev->raid_disk >= 0) {
4722 char nm[20];
4723 sprintf(nm, "rd%d", rdev->raid_disk);
4724 sysfs_remove_link(&mddev->kobj, nm);
4725 }
NeilBrownc4647292009-05-07 12:51:06 +10004726
NeilBrowna4bd82d2010-03-29 13:23:10 +11004727 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10004728 mutex_unlock(&mddev->open_mutex);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004729 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08004730
NeilBrowna4bd82d2010-03-29 13:23:10 +11004731 if (mddev->ro)
4732 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10004733 } else
4734 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 /*
4736 * Free resources if final stop
4737 */
NeilBrown9e653b62006-06-26 00:27:58 -07004738 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4740
NeilBrown978f9462006-02-02 14:28:05 -08004741 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004742 if (mddev->bitmap_info.file) {
4743 restore_bitmap_write_access(mddev->bitmap_info.file);
4744 fput(mddev->bitmap_info.file);
4745 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004746 }
NeilBrownc3d97142009-12-14 12:49:52 +11004747 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004748
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 export_array(mddev);
4750
NeilBrown6177b472010-03-29 11:37:13 +11004751 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11004752 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004753 if (mddev->hold_active == UNTIL_STOP)
4754 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11004755 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004756 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004757 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004758 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10004759 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760}
4761
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004762#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763static void autorun_array(mddev_t *mddev)
4764{
4765 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 int err;
4767
NeilBrowna757e642005-04-16 15:26:42 -07004768 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
4771 printk(KERN_INFO "md: running: ");
4772
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004773 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 char b[BDEVNAME_SIZE];
4775 printk("<%s>", bdevname(rdev->bdev,b));
4776 }
4777 printk("\n");
4778
NeilBrownd710e132008-10-13 11:55:12 +11004779 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 if (err) {
4781 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004782 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 }
4784}
4785
4786/*
4787 * lets try to run arrays based on all disks that have arrived
4788 * until now. (those are in pending_raid_disks)
4789 *
4790 * the method: pick the first pending disk, collect all disks with
4791 * the same UUID, remove all from the pending list and put them into
4792 * the 'same_array' list. Then order this list based on superblock
4793 * update time (freshest comes first), kick out 'old' disks and
4794 * compare superblocks. If everything's fine then run it.
4795 *
4796 * If "unit" is allocated, then bump its reference count
4797 */
4798static void autorun_devices(int part)
4799{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004800 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 mddev_t *mddev;
4802 char b[BDEVNAME_SIZE];
4803
4804 printk(KERN_INFO "md: autorun ...\n");
4805 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004806 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004808 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 rdev0 = list_entry(pending_raid_disks.next,
4810 mdk_rdev_t, same_set);
4811
4812 printk(KERN_INFO "md: considering %s ...\n",
4813 bdevname(rdev0->bdev,b));
4814 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004815 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 if (super_90_load(rdev, rdev0, 0) >= 0) {
4817 printk(KERN_INFO "md: adding %s ...\n",
4818 bdevname(rdev->bdev,b));
4819 list_move(&rdev->same_set, &candidates);
4820 }
4821 /*
4822 * now we have a set of devices, with all of them having
4823 * mostly sane superblocks. It's time to allocate the
4824 * mddev.
4825 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004826 if (part) {
4827 dev = MKDEV(mdp_major,
4828 rdev0->preferred_minor << MdpMinorShift);
4829 unit = MINOR(dev) >> MdpMinorShift;
4830 } else {
4831 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4832 unit = MINOR(dev);
4833 }
4834 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4836 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4837 break;
4838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839
4840 md_probe(dev, NULL, NULL);
4841 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004842 if (!mddev || !mddev->gendisk) {
4843 if (mddev)
4844 mddev_put(mddev);
4845 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 "md: cannot allocate memory for md drive.\n");
4847 break;
4848 }
4849 if (mddev_lock(mddev))
4850 printk(KERN_WARNING "md: %s locked, cannot run\n",
4851 mdname(mddev));
4852 else if (mddev->raid_disks || mddev->major_version
4853 || !list_empty(&mddev->disks)) {
4854 printk(KERN_WARNING
4855 "md: %s already running, cannot run %s\n",
4856 mdname(mddev), bdevname(rdev0->bdev,b));
4857 mddev_unlock(mddev);
4858 } else {
4859 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004860 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004861 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 list_del_init(&rdev->same_set);
4863 if (bind_rdev_to_array(rdev, mddev))
4864 export_rdev(rdev);
4865 }
4866 autorun_array(mddev);
4867 mddev_unlock(mddev);
4868 }
4869 /* on success, candidates will be empty, on error
4870 * it won't...
4871 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004872 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004873 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 mddev_put(mddev);
4877 }
4878 printk(KERN_INFO "md: ... autorun DONE.\n");
4879}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004880#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882static int get_version(void __user * arg)
4883{
4884 mdu_version_t ver;
4885
4886 ver.major = MD_MAJOR_VERSION;
4887 ver.minor = MD_MINOR_VERSION;
4888 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4889
4890 if (copy_to_user(arg, &ver, sizeof(ver)))
4891 return -EFAULT;
4892
4893 return 0;
4894}
4895
4896static int get_array_info(mddev_t * mddev, void __user * arg)
4897{
4898 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004899 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901
NeilBrowna9f326e2009-09-23 18:06:41 +10004902 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004903 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004905 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 failed++;
4907 else {
4908 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004909 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004910 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 else
4912 spare++;
4913 }
4914 }
4915
4916 info.major_version = mddev->major_version;
4917 info.minor_version = mddev->minor_version;
4918 info.patch_version = MD_PATCHLEVEL_VERSION;
4919 info.ctime = mddev->ctime;
4920 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004921 info.size = mddev->dev_sectors / 2;
4922 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004923 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 info.nr_disks = nr;
4925 info.raid_disks = mddev->raid_disks;
4926 info.md_minor = mddev->md_minor;
4927 info.not_persistent= !mddev->persistent;
4928
4929 info.utime = mddev->utime;
4930 info.state = 0;
4931 if (mddev->in_sync)
4932 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11004933 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07004934 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004935 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 info.working_disks = working;
4937 info.failed_disks = failed;
4938 info.spare_disks = spare;
4939
4940 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004941 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942
4943 if (copy_to_user(arg, &info, sizeof(info)))
4944 return -EFAULT;
4945
4946 return 0;
4947}
4948
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004949static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004950{
4951 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4952 char *ptr, *buf = NULL;
4953 int err = -ENOMEM;
4954
Dan Williamsb5470dc2008-06-27 21:44:04 -07004955 if (md_allow_write(mddev))
4956 file = kmalloc(sizeof(*file), GFP_NOIO);
4957 else
4958 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004959
NeilBrown32a76272005-06-21 17:17:14 -07004960 if (!file)
4961 goto out;
4962
4963 /* bitmap disabled, zero the first byte and copy out */
4964 if (!mddev->bitmap || !mddev->bitmap->file) {
4965 file->pathname[0] = '\0';
4966 goto copy_out;
4967 }
4968
4969 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4970 if (!buf)
4971 goto out;
4972
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004973 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4974 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004975 goto out;
4976
4977 strcpy(file->pathname, ptr);
4978
4979copy_out:
4980 err = 0;
4981 if (copy_to_user(arg, file, sizeof(*file)))
4982 err = -EFAULT;
4983out:
4984 kfree(buf);
4985 kfree(file);
4986 return err;
4987}
4988
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989static int get_disk_info(mddev_t * mddev, void __user * arg)
4990{
4991 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 mdk_rdev_t *rdev;
4993
4994 if (copy_from_user(&info, arg, sizeof(info)))
4995 return -EFAULT;
4996
Andre Noll26ef3792008-07-11 22:02:21 +10004997 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 if (rdev) {
4999 info.major = MAJOR(rdev->bdev->bd_dev);
5000 info.minor = MINOR(rdev->bdev->bd_dev);
5001 info.raid_disk = rdev->raid_disk;
5002 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005003 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005005 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 info.state |= (1<<MD_DISK_ACTIVE);
5007 info.state |= (1<<MD_DISK_SYNC);
5008 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005009 if (test_bit(WriteMostly, &rdev->flags))
5010 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 } else {
5012 info.major = info.minor = 0;
5013 info.raid_disk = -1;
5014 info.state = (1<<MD_DISK_REMOVED);
5015 }
5016
5017 if (copy_to_user(arg, &info, sizeof(info)))
5018 return -EFAULT;
5019
5020 return 0;
5021}
5022
5023static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5024{
5025 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5026 mdk_rdev_t *rdev;
5027 dev_t dev = MKDEV(info->major,info->minor);
5028
5029 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5030 return -EOVERFLOW;
5031
5032 if (!mddev->raid_disks) {
5033 int err;
5034 /* expecting a device which has a superblock */
5035 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5036 if (IS_ERR(rdev)) {
5037 printk(KERN_WARNING
5038 "md: md_import_device returned %ld\n",
5039 PTR_ERR(rdev));
5040 return PTR_ERR(rdev);
5041 }
5042 if (!list_empty(&mddev->disks)) {
5043 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5044 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005045 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 .load_super(rdev, rdev0, mddev->minor_version);
5047 if (err < 0) {
5048 printk(KERN_WARNING
5049 "md: %s has different UUID to %s\n",
5050 bdevname(rdev->bdev,b),
5051 bdevname(rdev0->bdev,b2));
5052 export_rdev(rdev);
5053 return -EINVAL;
5054 }
5055 }
5056 err = bind_rdev_to_array(rdev, mddev);
5057 if (err)
5058 export_rdev(rdev);
5059 return err;
5060 }
5061
5062 /*
5063 * add_new_disk can be used once the array is assembled
5064 * to add "hot spares". They must already have a superblock
5065 * written
5066 */
5067 if (mddev->pers) {
5068 int err;
5069 if (!mddev->pers->hot_add_disk) {
5070 printk(KERN_WARNING
5071 "%s: personality does not support diskops!\n",
5072 mdname(mddev));
5073 return -EINVAL;
5074 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005075 if (mddev->persistent)
5076 rdev = md_import_device(dev, mddev->major_version,
5077 mddev->minor_version);
5078 else
5079 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 if (IS_ERR(rdev)) {
5081 printk(KERN_WARNING
5082 "md: md_import_device returned %ld\n",
5083 PTR_ERR(rdev));
5084 return PTR_ERR(rdev);
5085 }
NeilBrown41158c72005-06-21 17:17:25 -07005086 /* set save_raid_disk if appropriate */
5087 if (!mddev->persistent) {
5088 if (info->state & (1<<MD_DISK_SYNC) &&
5089 info->raid_disk < mddev->raid_disks)
5090 rdev->raid_disk = info->raid_disk;
5091 else
5092 rdev->raid_disk = -1;
5093 } else
5094 super_types[mddev->major_version].
5095 validate_super(mddev, rdev);
5096 rdev->saved_raid_disk = rdev->raid_disk;
5097
NeilBrownb2d444d2005-11-08 21:39:31 -08005098 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005099 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5100 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005101 else
5102 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005103
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 rdev->raid_disk = -1;
5105 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005106 if (!err && !mddev->pers->hot_remove_disk) {
5107 /* If there is hot_add_disk but no hot_remove_disk
5108 * then added disks for geometry changes,
5109 * and should be added immediately.
5110 */
5111 super_types[mddev->major_version].
5112 validate_super(mddev, rdev);
5113 err = mddev->pers->hot_add_disk(mddev, rdev);
5114 if (err)
5115 unbind_rdev_from_array(rdev);
5116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 if (err)
5118 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005119 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005120 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005121
NeilBrown17571282006-12-10 02:20:52 -08005122 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005123 if (mddev->degraded)
5124 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005125 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07005126 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 return err;
5128 }
5129
5130 /* otherwise, add_new_disk is only allowed
5131 * for major_version==0 superblocks
5132 */
5133 if (mddev->major_version != 0) {
5134 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5135 mdname(mddev));
5136 return -EINVAL;
5137 }
5138
5139 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5140 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005141 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 if (IS_ERR(rdev)) {
5143 printk(KERN_WARNING
5144 "md: error, md_import_device() returned %ld\n",
5145 PTR_ERR(rdev));
5146 return PTR_ERR(rdev);
5147 }
5148 rdev->desc_nr = info->number;
5149 if (info->raid_disk < mddev->raid_disks)
5150 rdev->raid_disk = info->raid_disk;
5151 else
5152 rdev->raid_disk = -1;
5153
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005155 if (info->state & (1<<MD_DISK_SYNC))
5156 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005158 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5159 set_bit(WriteMostly, &rdev->flags);
5160
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 if (!mddev->persistent) {
5162 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10005163 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 } else
Andre Noll0f420352008-07-11 22:02:23 +10005165 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10005166 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167
NeilBrown2bf071b2006-01-06 00:20:55 -08005168 err = bind_rdev_to_array(rdev, mddev);
5169 if (err) {
5170 export_rdev(rdev);
5171 return err;
5172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 }
5174
5175 return 0;
5176}
5177
5178static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5179{
5180 char b[BDEVNAME_SIZE];
5181 mdk_rdev_t *rdev;
5182
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 rdev = find_rdev(mddev, dev);
5184 if (!rdev)
5185 return -ENXIO;
5186
5187 if (rdev->raid_disk >= 0)
5188 goto busy;
5189
5190 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005191 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005192 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193
5194 return 0;
5195busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005196 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 bdevname(rdev->bdev,b), mdname(mddev));
5198 return -EBUSY;
5199}
5200
5201static int hot_add_disk(mddev_t * mddev, dev_t dev)
5202{
5203 char b[BDEVNAME_SIZE];
5204 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 mdk_rdev_t *rdev;
5206
5207 if (!mddev->pers)
5208 return -ENODEV;
5209
5210 if (mddev->major_version != 0) {
5211 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5212 " version-0 superblocks.\n",
5213 mdname(mddev));
5214 return -EINVAL;
5215 }
5216 if (!mddev->pers->hot_add_disk) {
5217 printk(KERN_WARNING
5218 "%s: personality does not support diskops!\n",
5219 mdname(mddev));
5220 return -EINVAL;
5221 }
5222
NeilBrownd710e132008-10-13 11:55:12 +11005223 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 if (IS_ERR(rdev)) {
5225 printk(KERN_WARNING
5226 "md: error, md_import_device() returned %ld\n",
5227 PTR_ERR(rdev));
5228 return -EINVAL;
5229 }
5230
5231 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10005232 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 else
Andre Noll0f420352008-07-11 22:02:23 +10005234 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235
NeilBrown8190e752009-06-18 08:48:58 +10005236 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237
NeilBrownb2d444d2005-11-08 21:39:31 -08005238 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 printk(KERN_WARNING
5240 "md: can not hot-add faulty %s disk to %s!\n",
5241 bdevname(rdev->bdev,b), mdname(mddev));
5242 err = -EINVAL;
5243 goto abort_export;
5244 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005245 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005247 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005248 err = bind_rdev_to_array(rdev, mddev);
5249 if (err)
5250 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251
5252 /*
5253 * The rest should better be atomic, we can have disk failures
5254 * noticed in interrupt contexts ...
5255 */
5256
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 rdev->raid_disk = -1;
5258
NeilBrown850b2b42006-10-03 01:15:46 -07005259 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260
5261 /*
5262 * Kick recovery, maybe this spare has to be added to the
5263 * array immediately.
5264 */
5265 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5266 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005267 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 return 0;
5269
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270abort_export:
5271 export_rdev(rdev);
5272 return err;
5273}
5274
NeilBrown32a76272005-06-21 17:17:14 -07005275static int set_bitmap_file(mddev_t *mddev, int fd)
5276{
5277 int err;
5278
NeilBrown36fa3062005-09-09 16:23:45 -07005279 if (mddev->pers) {
5280 if (!mddev->pers->quiesce)
5281 return -EBUSY;
5282 if (mddev->recovery || mddev->sync_thread)
5283 return -EBUSY;
5284 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005285 }
5286
NeilBrown36fa3062005-09-09 16:23:45 -07005287
5288 if (fd >= 0) {
5289 if (mddev->bitmap)
5290 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005291 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005292
NeilBrownc3d97142009-12-14 12:49:52 +11005293 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005294 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5295 mdname(mddev));
5296 return -EBADF;
5297 }
5298
NeilBrownc3d97142009-12-14 12:49:52 +11005299 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005300 if (err) {
5301 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5302 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005303 fput(mddev->bitmap_info.file);
5304 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005305 return err;
5306 }
NeilBrownc3d97142009-12-14 12:49:52 +11005307 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005308 } else if (mddev->bitmap == NULL)
5309 return -ENOENT; /* cannot remove what isn't there */
5310 err = 0;
5311 if (mddev->pers) {
5312 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005313 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005314 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005315 if (!err)
5316 err = bitmap_load(mddev);
5317 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005318 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005319 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005320 fd = -1; /* make sure to put the file */
5321 }
NeilBrown36fa3062005-09-09 16:23:45 -07005322 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005323 }
5324 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005325 if (mddev->bitmap_info.file) {
5326 restore_bitmap_write_access(mddev->bitmap_info.file);
5327 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005328 }
NeilBrownc3d97142009-12-14 12:49:52 +11005329 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005330 }
5331
NeilBrown32a76272005-06-21 17:17:14 -07005332 return err;
5333}
5334
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335/*
5336 * set_array_info is used two different ways
5337 * The original usage is when creating a new array.
5338 * In this usage, raid_disks is > 0 and it together with
5339 * level, size, not_persistent,layout,chunksize determine the
5340 * shape of the array.
5341 * This will always create an array with a type-0.90.0 superblock.
5342 * The newer usage is when assembling an array.
5343 * In this case raid_disks will be 0, and the major_version field is
5344 * use to determine which style super-blocks are to be found on the devices.
5345 * The minor and patch _version numbers are also kept incase the
5346 * super_block handler wishes to interpret them.
5347 */
5348static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5349{
5350
5351 if (info->raid_disks == 0) {
5352 /* just setting version number for superblock loading */
5353 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005354 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 super_types[info->major_version].name == NULL) {
5356 /* maybe try to auto-load a module? */
5357 printk(KERN_INFO
5358 "md: superblock version %d not known\n",
5359 info->major_version);
5360 return -EINVAL;
5361 }
5362 mddev->major_version = info->major_version;
5363 mddev->minor_version = info->minor_version;
5364 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005365 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005366 /* ensure mddev_put doesn't delete this now that there
5367 * is some minimal configuration.
5368 */
5369 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 return 0;
5371 }
5372 mddev->major_version = MD_MAJOR_VERSION;
5373 mddev->minor_version = MD_MINOR_VERSION;
5374 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5375 mddev->ctime = get_seconds();
5376
5377 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005378 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005379 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 mddev->raid_disks = info->raid_disks;
5381 /* don't set md_minor, it is determined by which /dev/md* was
5382 * openned
5383 */
5384 if (info->state & (1<<MD_SB_CLEAN))
5385 mddev->recovery_cp = MaxSector;
5386 else
5387 mddev->recovery_cp = 0;
5388 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005389 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390
5391 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005392 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393
5394 mddev->max_disks = MD_SB_DISKS;
5395
NeilBrowne6910632008-02-06 01:39:51 -08005396 if (mddev->persistent)
5397 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005398 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399
NeilBrownc3d97142009-12-14 12:49:52 +11005400 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5401 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005402
NeilBrownf6705572006-03-27 01:18:11 -08005403 mddev->reshape_position = MaxSector;
5404
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 /*
5406 * Generate a 128 bit UUID
5407 */
5408 get_random_bytes(mddev->uuid, 16);
5409
NeilBrownf6705572006-03-27 01:18:11 -08005410 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005411 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005412 mddev->new_layout = mddev->layout;
5413 mddev->delta_disks = 0;
5414
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 return 0;
5416}
5417
Dan Williams1f403622009-03-31 14:59:03 +11005418void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5419{
Dan Williamsb522adc2009-03-31 15:00:31 +11005420 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5421
5422 if (mddev->external_size)
5423 return;
5424
Dan Williams1f403622009-03-31 14:59:03 +11005425 mddev->array_sectors = array_sectors;
5426}
5427EXPORT_SYMBOL(md_set_array_sectors);
5428
Andre Nolld71f9f82008-07-11 22:02:22 +10005429static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005430{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005431 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005432 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005433 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005434
5435 if (mddev->pers->resize == NULL)
5436 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005437 /* The "num_sectors" is the number of sectors of each device that
5438 * is used. This can only make sense for arrays with redundancy.
5439 * linear and raid0 always use whatever space is available. We can only
5440 * consider changing this number if no resync or reconstruction is
5441 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005442 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005443 * of each device. If num_sectors is zero, we find the largest size
5444 * that fits.
5445
NeilBrowna35b0d62006-01-06 00:20:49 -08005446 */
5447 if (mddev->sync_thread)
5448 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005449 if (mddev->bitmap)
5450 /* Sorry, cannot grow a bitmap yet, just remove it,
5451 * grow, and re-add.
5452 */
5453 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005454 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005455 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005456
Andre Nolld71f9f82008-07-11 22:02:22 +10005457 if (fit && (num_sectors == 0 || num_sectors > avail))
5458 num_sectors = avail;
5459 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005460 return -ENOSPC;
5461 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005462 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005463 if (!rv)
5464 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005465 return rv;
5466}
5467
NeilBrownda943b992006-01-06 00:20:54 -08005468static int update_raid_disks(mddev_t *mddev, int raid_disks)
5469{
5470 int rv;
5471 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005472 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005473 return -EINVAL;
5474 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005475 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08005476 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005477 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005478 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005479 mddev->delta_disks = raid_disks - mddev->raid_disks;
5480
5481 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005482 return rv;
5483}
5484
5485
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486/*
5487 * update_array_info is used to change the configuration of an
5488 * on-line array.
5489 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5490 * fields in the info are checked against the array.
5491 * Any differences that cannot be handled will cause an error.
5492 * Normally, only one change can be managed at a time.
5493 */
5494static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5495{
5496 int rv = 0;
5497 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005498 int state = 0;
5499
5500 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005501 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005502 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503
5504 if (mddev->major_version != info->major_version ||
5505 mddev->minor_version != info->minor_version ||
5506/* mddev->patch_version != info->patch_version || */
5507 mddev->ctime != info->ctime ||
5508 mddev->level != info->level ||
5509/* mddev->layout != info->layout || */
5510 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005511 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005512 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5513 ((state^info->state) & 0xfffffe00)
5514 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 return -EINVAL;
5516 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005517 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5518 cnt++;
5519 if (mddev->raid_disks != info->raid_disks)
5520 cnt++;
5521 if (mddev->layout != info->layout)
5522 cnt++;
5523 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5524 cnt++;
5525 if (cnt == 0)
5526 return 0;
5527 if (cnt > 1)
5528 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
5530 if (mddev->layout != info->layout) {
5531 /* Change layout
5532 * we don't need to do anything at the md level, the
5533 * personality will take care of it all.
5534 */
NeilBrown50ac1682009-06-18 08:47:55 +10005535 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005537 else {
5538 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005539 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005540 if (rv)
5541 mddev->new_layout = mddev->layout;
5542 return rv;
5543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005545 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005546 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547
NeilBrownda943b992006-01-06 00:20:54 -08005548 if (mddev->raid_disks != info->raid_disks)
5549 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550
NeilBrown36fa3062005-09-09 16:23:45 -07005551 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5552 if (mddev->pers->quiesce == NULL)
5553 return -EINVAL;
5554 if (mddev->recovery || mddev->sync_thread)
5555 return -EBUSY;
5556 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5557 /* add the bitmap */
5558 if (mddev->bitmap)
5559 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005560 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005561 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005562 mddev->bitmap_info.offset =
5563 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005564 mddev->pers->quiesce(mddev, 1);
5565 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005566 if (!rv)
5567 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07005568 if (rv)
5569 bitmap_destroy(mddev);
5570 mddev->pers->quiesce(mddev, 0);
5571 } else {
5572 /* remove the bitmap */
5573 if (!mddev->bitmap)
5574 return -ENOENT;
5575 if (mddev->bitmap->file)
5576 return -EINVAL;
5577 mddev->pers->quiesce(mddev, 1);
5578 bitmap_destroy(mddev);
5579 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005580 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005581 }
5582 }
NeilBrown850b2b42006-10-03 01:15:46 -07005583 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 return rv;
5585}
5586
5587static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5588{
5589 mdk_rdev_t *rdev;
5590
5591 if (mddev->pers == NULL)
5592 return -ENODEV;
5593
5594 rdev = find_rdev(mddev, dev);
5595 if (!rdev)
5596 return -ENODEV;
5597
5598 md_error(mddev, rdev);
5599 return 0;
5600}
5601
Andre Noll2f9618c2008-04-25 18:57:58 +02005602/*
5603 * We have a problem here : there is no easy way to give a CHS
5604 * virtual geometry. We currently pretend that we have a 2 heads
5605 * 4 sectors (with a BIG number of cylinders...). This drives
5606 * dosfs just mad... ;-)
5607 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005608static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5609{
5610 mddev_t *mddev = bdev->bd_disk->private_data;
5611
5612 geo->heads = 2;
5613 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11005614 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005615 return 0;
5616}
5617
Al Viroa39907f2008-03-02 10:31:15 -05005618static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 unsigned int cmd, unsigned long arg)
5620{
5621 int err = 0;
5622 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10005624 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625
5626 if (!capable(CAP_SYS_ADMIN))
5627 return -EACCES;
5628
5629 /*
5630 * Commands dealing with the RAID driver but not any
5631 * particular array:
5632 */
5633 switch (cmd)
5634 {
5635 case RAID_VERSION:
5636 err = get_version(argp);
5637 goto done;
5638
5639 case PRINT_RAID_DEBUG:
5640 err = 0;
5641 md_print_devices();
5642 goto done;
5643
5644#ifndef MODULE
5645 case RAID_AUTORUN:
5646 err = 0;
5647 autostart_arrays(arg);
5648 goto done;
5649#endif
5650 default:;
5651 }
5652
5653 /*
5654 * Commands creating/starting a new array:
5655 */
5656
Al Viroa39907f2008-03-02 10:31:15 -05005657 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658
5659 if (!mddev) {
5660 BUG();
5661 goto abort;
5662 }
5663
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 err = mddev_lock(mddev);
5665 if (err) {
5666 printk(KERN_INFO
5667 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5668 err, cmd);
5669 goto abort;
5670 }
5671
5672 switch (cmd)
5673 {
5674 case SET_ARRAY_INFO:
5675 {
5676 mdu_array_info_t info;
5677 if (!arg)
5678 memset(&info, 0, sizeof(info));
5679 else if (copy_from_user(&info, argp, sizeof(info))) {
5680 err = -EFAULT;
5681 goto abort_unlock;
5682 }
5683 if (mddev->pers) {
5684 err = update_array_info(mddev, &info);
5685 if (err) {
5686 printk(KERN_WARNING "md: couldn't update"
5687 " array info. %d\n", err);
5688 goto abort_unlock;
5689 }
5690 goto done_unlock;
5691 }
5692 if (!list_empty(&mddev->disks)) {
5693 printk(KERN_WARNING
5694 "md: array %s already has disks!\n",
5695 mdname(mddev));
5696 err = -EBUSY;
5697 goto abort_unlock;
5698 }
5699 if (mddev->raid_disks) {
5700 printk(KERN_WARNING
5701 "md: array %s already initialised!\n",
5702 mdname(mddev));
5703 err = -EBUSY;
5704 goto abort_unlock;
5705 }
5706 err = set_array_info(mddev, &info);
5707 if (err) {
5708 printk(KERN_WARNING "md: couldn't set"
5709 " array info. %d\n", err);
5710 goto abort_unlock;
5711 }
5712 }
5713 goto done_unlock;
5714
5715 default:;
5716 }
5717
5718 /*
5719 * Commands querying/configuring an existing array:
5720 */
NeilBrown32a76272005-06-21 17:17:14 -07005721 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005722 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005723 if ((!mddev->raid_disks && !mddev->external)
5724 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5725 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5726 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 err = -ENODEV;
5728 goto abort_unlock;
5729 }
5730
5731 /*
5732 * Commands even a read-only array can execute:
5733 */
5734 switch (cmd)
5735 {
5736 case GET_ARRAY_INFO:
5737 err = get_array_info(mddev, argp);
5738 goto done_unlock;
5739
NeilBrown32a76272005-06-21 17:17:14 -07005740 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005741 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005742 goto done_unlock;
5743
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 case GET_DISK_INFO:
5745 err = get_disk_info(mddev, argp);
5746 goto done_unlock;
5747
5748 case RESTART_ARRAY_RW:
5749 err = restart_array(mddev);
5750 goto done_unlock;
5751
5752 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005753 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 goto done_unlock;
5755
5756 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11005757 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 goto done_unlock;
5759
Dan Williamse2218352010-05-12 08:25:37 +10005760 case BLKROSET:
5761 if (get_user(ro, (int __user *)(arg))) {
5762 err = -EFAULT;
5763 goto done_unlock;
5764 }
5765 err = -EINVAL;
5766
5767 /* if the bdev is going readonly the value of mddev->ro
5768 * does not matter, no writes are coming
5769 */
5770 if (ro)
5771 goto done_unlock;
5772
5773 /* are we are already prepared for writes? */
5774 if (mddev->ro != 1)
5775 goto done_unlock;
5776
5777 /* transitioning to readauto need only happen for
5778 * arrays that call md_write_start
5779 */
5780 if (mddev->pers) {
5781 err = restart_array(mddev);
5782 if (err == 0) {
5783 mddev->ro = 2;
5784 set_disk_ro(mddev->gendisk, 0);
5785 }
5786 }
5787 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 }
5789
5790 /*
5791 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005792 * superblock, so we do not allow them on read-only arrays.
5793 * However non-MD ioctls (e.g. get-size) will still come through
5794 * here and hit the 'default' below, so only disallow
5795 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005797 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005798 if (mddev->ro == 2) {
5799 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005800 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005801 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5802 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005803 } else {
5804 err = -EROFS;
5805 goto abort_unlock;
5806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 }
5808
5809 switch (cmd)
5810 {
5811 case ADD_NEW_DISK:
5812 {
5813 mdu_disk_info_t info;
5814 if (copy_from_user(&info, argp, sizeof(info)))
5815 err = -EFAULT;
5816 else
5817 err = add_new_disk(mddev, &info);
5818 goto done_unlock;
5819 }
5820
5821 case HOT_REMOVE_DISK:
5822 err = hot_remove_disk(mddev, new_decode_dev(arg));
5823 goto done_unlock;
5824
5825 case HOT_ADD_DISK:
5826 err = hot_add_disk(mddev, new_decode_dev(arg));
5827 goto done_unlock;
5828
5829 case SET_DISK_FAULTY:
5830 err = set_disk_faulty(mddev, new_decode_dev(arg));
5831 goto done_unlock;
5832
5833 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005834 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 goto done_unlock;
5836
NeilBrown32a76272005-06-21 17:17:14 -07005837 case SET_BITMAP_FILE:
5838 err = set_bitmap_file(mddev, (int)arg);
5839 goto done_unlock;
5840
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 err = -EINVAL;
5843 goto abort_unlock;
5844 }
5845
5846done_unlock:
5847abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005848 if (mddev->hold_active == UNTIL_IOCTL &&
5849 err != -EINVAL)
5850 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 mddev_unlock(mddev);
5852
5853 return err;
5854done:
5855 if (err)
5856 MD_BUG();
5857abort:
5858 return err;
5859}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005860#ifdef CONFIG_COMPAT
5861static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5862 unsigned int cmd, unsigned long arg)
5863{
5864 switch (cmd) {
5865 case HOT_REMOVE_DISK:
5866 case HOT_ADD_DISK:
5867 case SET_DISK_FAULTY:
5868 case SET_BITMAP_FILE:
5869 /* These take in integer arg, do not convert */
5870 break;
5871 default:
5872 arg = (unsigned long)compat_ptr(arg);
5873 break;
5874 }
5875
5876 return md_ioctl(bdev, mode, cmd, arg);
5877}
5878#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879
Al Viroa39907f2008-03-02 10:31:15 -05005880static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881{
5882 /*
5883 * Succeed if we can lock the mddev, which confirms that
5884 * it isn't being stopped right now.
5885 */
NeilBrownd3374822009-01-09 08:31:10 +11005886 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887 int err;
5888
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02005889 lock_kernel();
NeilBrownd3374822009-01-09 08:31:10 +11005890 if (mddev->gendisk != bdev->bd_disk) {
5891 /* we are racing with mddev_put which is discarding this
5892 * bd_disk.
5893 */
5894 mddev_put(mddev);
5895 /* Wait until bdev->bd_disk is definitely gone */
5896 flush_scheduled_work();
5897 /* Then retry the open from the top */
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02005898 unlock_kernel();
NeilBrownd3374822009-01-09 08:31:10 +11005899 return -ERESTARTSYS;
5900 }
5901 BUG_ON(mddev != bdev->bd_disk->private_data);
5902
NeilBrownc8c00a62009-08-10 12:50:52 +10005903 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 goto out;
5905
5906 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005907 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005908 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909
NeilBrownf3b99be2010-06-24 13:31:03 +10005910 check_disk_size_change(mddev->gendisk, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 out:
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02005912 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 return err;
5914}
5915
Al Viroa39907f2008-03-02 10:31:15 -05005916static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917{
Al Viroa39907f2008-03-02 10:31:15 -05005918 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005920 BUG_ON(!mddev);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02005921 lock_kernel();
NeilBrownf2ea68c2008-07-21 17:05:25 +10005922 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 mddev_put(mddev);
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02005924 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925
5926 return 0;
5927}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005928static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929{
5930 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005931 .open = md_open,
5932 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005933 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005934#ifdef CONFIG_COMPAT
5935 .compat_ioctl = md_compat_ioctl,
5936#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005937 .getgeo = md_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938};
5939
Adrian Bunk75c96f82005-05-05 16:16:09 -07005940static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941{
5942 mdk_thread_t *thread = arg;
5943
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 /*
5945 * md_thread is a 'system-thread', it's priority should be very
5946 * high. We avoid resource deadlocks individually in each
5947 * raid personality. (RAID5 does preallocation) We also use RR and
5948 * the very same RT priority as kswapd, thus we will never get
5949 * into a priority inversion deadlock.
5950 *
5951 * we definitely have to have equal or higher priority than
5952 * bdflush, otherwise bdflush will deadlock if there are too
5953 * many dirty RAID5 blocks.
5954 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955
NeilBrown6985c432005-10-19 21:23:47 -07005956 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005957 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958
NeilBrown93588e22005-11-15 00:09:12 -08005959 /* We need to wait INTERRUPTIBLE so that
5960 * we don't add to the load-average.
5961 * That means we need to be sure no signals are
5962 * pending
5963 */
5964 if (signal_pending(current))
5965 flush_signals(current);
5966
5967 wait_event_interruptible_timeout
5968 (thread->wqueue,
5969 test_bit(THREAD_WAKEUP, &thread->flags)
5970 || kthread_should_stop(),
5971 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972
5973 clear_bit(THREAD_WAKEUP, &thread->flags);
5974
NeilBrown787453c2005-11-08 21:39:43 -08005975 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005977
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 return 0;
5979}
5980
5981void md_wakeup_thread(mdk_thread_t *thread)
5982{
5983 if (thread) {
5984 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5985 set_bit(THREAD_WAKEUP, &thread->flags);
5986 wake_up(&thread->wqueue);
5987 }
5988}
5989
5990mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5991 const char *name)
5992{
5993 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994
NeilBrown9ffae0c2006-01-06 00:20:32 -08005995 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 if (!thread)
5997 return NULL;
5998
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 init_waitqueue_head(&thread->wqueue);
6000
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 thread->run = run;
6002 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006003 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006004 thread->tsk = kthread_run(md_thread, thread,
6005 "%s_%s",
6006 mdname(thread->mddev),
6007 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006008 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 kfree(thread);
6010 return NULL;
6011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 return thread;
6013}
6014
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015void md_unregister_thread(mdk_thread_t *thread)
6016{
NeilBrowne0cf8f02009-03-31 14:39:39 +11006017 if (!thread)
6018 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07006019 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07006020
6021 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 kfree(thread);
6023}
6024
6025void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6026{
6027 if (!mddev) {
6028 MD_BUG();
6029 return;
6030 }
6031
NeilBrownb2d444d2005-11-08 21:39:31 -08006032 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006034
6035 if (mddev->external)
6036 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07006037/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6039 mdname(mddev),
6040 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6041 __builtin_return_address(0),__builtin_return_address(1),
6042 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07006043*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07006044 if (!mddev->pers)
6045 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 if (!mddev->pers->error_handler)
6047 return;
6048 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006049 if (mddev->degraded)
6050 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006051 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6053 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6054 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006055 if (mddev->event_work.func)
6056 schedule_work(&mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006057 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058}
6059
6060/* seq_file implementation /proc/mdstat */
6061
6062static void status_unused(struct seq_file *seq)
6063{
6064 int i = 0;
6065 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066
6067 seq_printf(seq, "unused devices: ");
6068
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006069 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 char b[BDEVNAME_SIZE];
6071 i++;
6072 seq_printf(seq, "%s ",
6073 bdevname(rdev->bdev,b));
6074 }
6075 if (!i)
6076 seq_printf(seq, "<none>");
6077
6078 seq_printf(seq, "\n");
6079}
6080
6081
6082static void status_resync(struct seq_file *seq, mddev_t * mddev)
6083{
NeilBrowndd71cf62009-05-07 12:49:35 +10006084 sector_t max_sectors, resync, res;
6085 unsigned long dt, db;
6086 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006087 int scale;
6088 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089
NeilBrowndd71cf62009-05-07 12:49:35 +10006090 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091
6092 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006093 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006095 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096
6097 /*
6098 * Should not happen.
6099 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006100 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 MD_BUG();
6102 return;
6103 }
NeilBrown4588b422006-03-27 01:18:04 -08006104 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006105 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006106 * u32, as those are the requirements for sector_div.
6107 * Thus 'scale' must be at least 10
6108 */
6109 scale = 10;
6110 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006111 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006112 scale++;
6113 }
6114 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006115 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006116
6117 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 {
NeilBrown4588b422006-03-27 01:18:04 -08006119 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 seq_printf(seq, "[");
6121 for (i = 0; i < x; i++)
6122 seq_printf(seq, "=");
6123 seq_printf(seq, ">");
6124 for (i = 0; i < y; i++)
6125 seq_printf(seq, ".");
6126 seq_printf(seq, "] ");
6127 }
NeilBrown4588b422006-03-27 01:18:04 -08006128 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006129 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6130 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006131 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6132 "check" :
6133 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6134 "resync" : "recovery"))),
6135 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006136 (unsigned long long) resync/2,
6137 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138
6139 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 * dt: time from mark until now
6141 * db: blocks written from mark until now
6142 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006143 *
6144 * rt is a sector_t, so could be 32bit or 64bit.
6145 * So we divide before multiply in case it is 32bit and close
6146 * to the limit.
6147 * We scale the divisor (db) by 32 to avoid loosing precision
6148 * near the end of resync when the number of remaining sectors
6149 * is close to 'db'.
6150 * We then divide rt by 32 after multiplying by db to compensate.
6151 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152 */
6153 dt = ((jiffies - mddev->resync_mark) / HZ);
6154 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006155 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6156 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157
NeilBrowndd71cf62009-05-07 12:49:35 +10006158 rt = max_sectors - resync; /* number of remaining sectors */
6159 sector_div(rt, db/32+1);
6160 rt *= dt;
6161 rt >>= 5;
6162
6163 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6164 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165
NeilBrownff4e8d92006-07-10 04:44:16 -07006166 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167}
6168
6169static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6170{
6171 struct list_head *tmp;
6172 loff_t l = *pos;
6173 mddev_t *mddev;
6174
6175 if (l >= 0x10000)
6176 return NULL;
6177 if (!l--)
6178 /* header */
6179 return (void*)1;
6180
6181 spin_lock(&all_mddevs_lock);
6182 list_for_each(tmp,&all_mddevs)
6183 if (!l--) {
6184 mddev = list_entry(tmp, mddev_t, all_mddevs);
6185 mddev_get(mddev);
6186 spin_unlock(&all_mddevs_lock);
6187 return mddev;
6188 }
6189 spin_unlock(&all_mddevs_lock);
6190 if (!l--)
6191 return (void*)2;/* tail */
6192 return NULL;
6193}
6194
6195static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6196{
6197 struct list_head *tmp;
6198 mddev_t *next_mddev, *mddev = v;
6199
6200 ++*pos;
6201 if (v == (void*)2)
6202 return NULL;
6203
6204 spin_lock(&all_mddevs_lock);
6205 if (v == (void*)1)
6206 tmp = all_mddevs.next;
6207 else
6208 tmp = mddev->all_mddevs.next;
6209 if (tmp != &all_mddevs)
6210 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6211 else {
6212 next_mddev = (void*)2;
6213 *pos = 0x10000;
6214 }
6215 spin_unlock(&all_mddevs_lock);
6216
6217 if (v != (void*)1)
6218 mddev_put(mddev);
6219 return next_mddev;
6220
6221}
6222
6223static void md_seq_stop(struct seq_file *seq, void *v)
6224{
6225 mddev_t *mddev = v;
6226
6227 if (mddev && v != (void*)1 && v != (void*)2)
6228 mddev_put(mddev);
6229}
6230
NeilBrownd7603b72006-01-06 00:20:30 -08006231struct mdstat_info {
6232 int event;
6233};
6234
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235static int md_seq_show(struct seq_file *seq, void *v)
6236{
6237 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006238 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006240 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006241 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242
6243 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006244 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245 seq_printf(seq, "Personalities : ");
6246 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006247 list_for_each_entry(pers, &pers_list, list)
6248 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249
6250 spin_unlock(&pers_lock);
6251 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006252 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253 return 0;
6254 }
6255 if (v == (void*)2) {
6256 status_unused(seq);
6257 return 0;
6258 }
6259
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006260 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006262
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6264 seq_printf(seq, "%s : %sactive", mdname(mddev),
6265 mddev->pers ? "" : "in");
6266 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006267 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006269 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006270 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 seq_printf(seq, " %s", mddev->pers->name);
6272 }
6273
Andre Nolldd8ac332009-03-31 14:33:13 +11006274 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006275 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276 char b[BDEVNAME_SIZE];
6277 seq_printf(seq, " %s[%d]",
6278 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006279 if (test_bit(WriteMostly, &rdev->flags))
6280 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006281 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282 seq_printf(seq, "(F)");
6283 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006284 } else if (rdev->raid_disk < 0)
6285 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006286 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287 }
6288
6289 if (!list_empty(&mddev->disks)) {
6290 if (mddev->pers)
6291 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006292 (unsigned long long)
6293 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294 else
6295 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006296 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006298 if (mddev->persistent) {
6299 if (mddev->major_version != 0 ||
6300 mddev->minor_version != 90) {
6301 seq_printf(seq," super %d.%d",
6302 mddev->major_version,
6303 mddev->minor_version);
6304 }
NeilBrowne6910632008-02-06 01:39:51 -08006305 } else if (mddev->external)
6306 seq_printf(seq, " super external:%s",
6307 mddev->metadata_type);
6308 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006309 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310
6311 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006312 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006314 if (mddev->pers->sync_request) {
6315 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006316 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006317 seq_printf(seq, "\n ");
6318 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6319 seq_printf(seq, "\tresync=DELAYED\n ");
6320 else if (mddev->recovery_cp < MaxSector)
6321 seq_printf(seq, "\tresync=PENDING\n ");
6322 }
NeilBrown32a76272005-06-21 17:17:14 -07006323 } else
6324 seq_printf(seq, "\n ");
6325
6326 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006327 unsigned long chunk_kb;
6328 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006329 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006330 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006331 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6332 "%lu%s chunk",
6333 bitmap->pages - bitmap->missing_pages,
6334 bitmap->pages,
6335 (bitmap->pages - bitmap->missing_pages)
6336 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006337 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006338 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006339 if (bitmap->file) {
6340 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006341 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006342 }
NeilBrown78d742d2005-06-21 17:17:15 -07006343
NeilBrown32a76272005-06-21 17:17:14 -07006344 seq_printf(seq, "\n");
6345 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346 }
6347
6348 seq_printf(seq, "\n");
6349 }
6350 mddev_unlock(mddev);
6351
6352 return 0;
6353}
6354
Jan Engelhardt110518b2009-05-07 12:49:37 +10006355static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356 .start = md_seq_start,
6357 .next = md_seq_next,
6358 .stop = md_seq_stop,
6359 .show = md_seq_show,
6360};
6361
6362static int md_seq_open(struct inode *inode, struct file *file)
6363{
6364 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006365 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6366 if (mi == NULL)
6367 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368
6369 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006370 if (error)
6371 kfree(mi);
6372 else {
6373 struct seq_file *p = file->private_data;
6374 p->private = mi;
6375 mi->event = atomic_read(&md_event_count);
6376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 return error;
6378}
6379
NeilBrownd7603b72006-01-06 00:20:30 -08006380static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6381{
6382 struct seq_file *m = filp->private_data;
6383 struct mdstat_info *mi = m->private;
6384 int mask;
6385
6386 poll_wait(filp, &md_event_waiters, wait);
6387
6388 /* always allow read */
6389 mask = POLLIN | POLLRDNORM;
6390
6391 if (mi->event != atomic_read(&md_event_count))
6392 mask |= POLLERR | POLLPRI;
6393 return mask;
6394}
6395
Arjan van de Venfa027c22007-02-12 00:55:33 -08006396static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006397 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 .open = md_seq_open,
6399 .read = seq_read,
6400 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006401 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006402 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403};
6404
NeilBrown2604b702006-01-06 00:20:36 -08006405int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006408 list_add_tail(&p->list, &pers_list);
6409 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 spin_unlock(&pers_lock);
6411 return 0;
6412}
6413
NeilBrown2604b702006-01-06 00:20:36 -08006414int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415{
NeilBrown2604b702006-01-06 00:20:36 -08006416 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006418 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 spin_unlock(&pers_lock);
6420 return 0;
6421}
6422
NeilBrowneea1bf32009-03-31 14:27:02 +11006423static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424{
6425 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006426 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006427 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428
6429 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006430 rcu_read_lock();
6431 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006433 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6434 (int)part_stat_read(&disk->part0, sectors[1]) -
6435 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006436 /* sync IO will cause sync_io to increase before the disk_stats
6437 * as sync_io is counted when a request starts, and
6438 * disk_stats is counted when it completes.
6439 * So resync activity will cause curr_events to be smaller than
6440 * when there was no such activity.
6441 * non-sync IO will cause disk_stat to increase without
6442 * increasing sync_io so curr_events will (eventually)
6443 * be larger than it was before. Once it becomes
6444 * substantially larger, the test below will cause
6445 * the array to appear non-idle, and resync will slow
6446 * down.
6447 * If there is a lot of outstanding resync activity when
6448 * we set last_event to curr_events, then all that activity
6449 * completing might cause the array to appear non-idle
6450 * and resync will be slowed down even though there might
6451 * not have been non-resync activity. This will only
6452 * happen once though. 'last_events' will soon reflect
6453 * the state where there is little or no outstanding
6454 * resync requests, and further resync activity will
6455 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006456 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006458 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459 rdev->last_events = curr_events;
6460 idle = 0;
6461 }
6462 }
NeilBrown4b809912008-07-21 17:05:25 +10006463 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464 return idle;
6465}
6466
6467void md_done_sync(mddev_t *mddev, int blocks, int ok)
6468{
6469 /* another "blocks" (512byte) blocks have been synced */
6470 atomic_sub(blocks, &mddev->recovery_active);
6471 wake_up(&mddev->recovery_wait);
6472 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006473 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474 md_wakeup_thread(mddev->thread);
6475 // stop recovery, signal do_sync ....
6476 }
6477}
6478
6479
NeilBrown06d91a52005-06-21 17:17:12 -07006480/* md_write_start(mddev, bi)
6481 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006482 * in superblock) before writing, schedule a superblock update
6483 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006484 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006485void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486{
Neil Brown0fd62b82008-06-28 08:31:36 +10006487 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006488 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006489 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006490
NeilBrownf91de922005-11-08 21:39:36 -08006491 BUG_ON(mddev->ro == 1);
6492 if (mddev->ro == 2) {
6493 /* need to switch to read/write */
6494 mddev->ro = 0;
6495 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6496 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006497 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006498 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006499 }
NeilBrown06d91a52005-06-21 17:17:12 -07006500 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006501 if (mddev->safemode == 1)
6502 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006503 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006504 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006505 if (mddev->in_sync) {
6506 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006507 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006508 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006509 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006510 }
NeilBrowna9701a32005-11-08 21:39:34 -08006511 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006512 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006513 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10006514 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006515 wait_event(mddev->sb_wait,
6516 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6517 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518}
6519
6520void md_write_end(mddev_t *mddev)
6521{
6522 if (atomic_dec_and_test(&mddev->writes_pending)) {
6523 if (mddev->safemode == 2)
6524 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006525 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6527 }
6528}
6529
NeilBrown2a2275d2007-01-26 00:57:11 -08006530/* md_allow_write(mddev)
6531 * Calling this ensures that the array is marked 'active' so that writes
6532 * may proceed without blocking. It is important to call this before
6533 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6534 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006535 *
6536 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6537 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006538 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006539int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006540{
6541 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006542 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006543 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006544 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006545 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006546 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006547
6548 spin_lock_irq(&mddev->write_lock);
6549 if (mddev->in_sync) {
6550 mddev->in_sync = 0;
6551 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6552 if (mddev->safemode_delay &&
6553 mddev->safemode == 0)
6554 mddev->safemode = 1;
6555 spin_unlock_irq(&mddev->write_lock);
6556 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006557 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006558 } else
6559 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006560
6561 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6562 return -EAGAIN;
6563 else
6564 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006565}
6566EXPORT_SYMBOL_GPL(md_allow_write);
6567
NeilBrownb63d7c22010-06-01 19:37:33 +10006568void md_unplug(mddev_t *mddev)
NeilBrown252ac522010-06-01 19:37:29 +10006569{
6570 if (mddev->queue)
6571 blk_unplug(mddev->queue);
6572 if (mddev->plug)
6573 mddev->plug->unplug_fn(mddev->plug);
6574}
6575
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576#define SYNC_MARKS 10
6577#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006578void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579{
6580 mddev_t *mddev2;
6581 unsigned int currspeed = 0,
6582 window;
NeilBrown57afd892005-06-21 17:17:13 -07006583 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584 unsigned long mark[SYNC_MARKS];
6585 sector_t mark_cnt[SYNC_MARKS];
6586 int last_mark,m;
6587 struct list_head *tmp;
6588 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006589 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006590 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006591 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592
6593 /* just incase thread restarts... */
6594 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6595 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006596 if (mddev->ro) /* never try to sync a read-only array */
6597 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598
NeilBrown61df9d92006-10-03 01:15:57 -07006599 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6600 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6601 desc = "data-check";
6602 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6603 desc = "requested-resync";
6604 else
6605 desc = "resync";
6606 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6607 desc = "reshape";
6608 else
6609 desc = "recovery";
6610
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611 /* we overload curr_resync somewhat here.
6612 * 0 == not engaged in resync at all
6613 * 2 == checking that there is no conflict with another sync
6614 * 1 == like 2, but have yielded to allow conflicting resync to
6615 * commense
6616 * other == active in resync - this many blocks
6617 *
6618 * Before starting a resync we must have set curr_resync to
6619 * 2, and then checked that every "conflicting" array has curr_resync
6620 * less than ours. When we find one that is the same or higher
6621 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6622 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6623 * This will mean we have to start checking from the beginning again.
6624 *
6625 */
6626
6627 do {
6628 mddev->curr_resync = 2;
6629
6630 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006631 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006632 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006633
6634 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006635 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006636 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637 if (mddev2 == mddev)
6638 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006639 if (!mddev->parallel_resync
6640 && mddev2->curr_resync
6641 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642 DEFINE_WAIT(wq);
6643 if (mddev < mddev2 && mddev->curr_resync == 2) {
6644 /* arbitrarily yield */
6645 mddev->curr_resync = 1;
6646 wake_up(&resync_wait);
6647 }
6648 if (mddev > mddev2 && mddev->curr_resync == 1)
6649 /* no need to wait here, we can wait the next
6650 * time 'round when curr_resync == 2
6651 */
6652 continue;
NeilBrown97441972008-09-19 11:49:54 +10006653 /* We need to wait 'interruptible' so as not to
6654 * contribute to the load average, and not to
6655 * be caught by 'softlockup'
6656 */
6657 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006658 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006659 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006660 printk(KERN_INFO "md: delaying %s of %s"
6661 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006663 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006665 if (signal_pending(current))
6666 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667 schedule();
6668 finish_wait(&resync_wait, &wq);
6669 goto try_again;
6670 }
6671 finish_wait(&resync_wait, &wq);
6672 }
6673 }
6674 } while (mddev->curr_resync < 2);
6675
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006676 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006677 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006679 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680 */
6681 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006682 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006683 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006684 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6685 j = mddev->resync_min;
6686 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006687 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006688
NeilBrownccfcc3c2006-03-27 01:18:09 -08006689 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006690 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006691 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006692 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006693 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006694 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006695 rcu_read_lock();
6696 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006697 if (rdev->raid_disk >= 0 &&
6698 !test_bit(Faulty, &rdev->flags) &&
6699 !test_bit(In_sync, &rdev->flags) &&
6700 rdev->recovery_offset < j)
6701 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006702 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006704
NeilBrown61df9d92006-10-03 01:15:57 -07006705 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6706 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6707 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006708 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006709 "(but not more than %d KB/sec) for %s.\n",
6710 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711
NeilBrowneea1bf32009-03-31 14:27:02 +11006712 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006713
NeilBrown57afd892005-06-21 17:17:13 -07006714 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006715 for (m = 0; m < SYNC_MARKS; m++) {
6716 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006717 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006718 }
6719 last_mark = 0;
6720 mddev->resync_mark = mark[last_mark];
6721 mddev->resync_mark_cnt = mark_cnt[last_mark];
6722
6723 /*
6724 * Tune reconstruction:
6725 */
6726 window = 32*(PAGE_SIZE/512);
6727 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6728 window/2,(unsigned long long) max_sectors/2);
6729
6730 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731 last_check = 0;
6732
6733 if (j>2) {
6734 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006735 "md: resuming %s of %s from checkpoint.\n",
6736 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737 mddev->curr_resync = j;
6738 }
NeilBrownefa59332009-12-14 12:49:49 +11006739 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740
6741 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006742 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743
NeilBrown57afd892005-06-21 17:17:13 -07006744 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006745
NeilBrown7a91ee12009-05-26 12:57:21 +10006746 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6747 ((mddev->curr_resync > mddev->curr_resync_completed &&
6748 (mddev->curr_resync - mddev->curr_resync_completed)
6749 > (max_sectors >> 4)) ||
6750 (j - mddev->curr_resync_completed)*2
6751 >= mddev->resync_max - mddev->curr_resync_completed
6752 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006753 /* time to update curr_resync_completed */
NeilBrown252ac522010-06-01 19:37:29 +10006754 md_unplug(mddev);
NeilBrown97e4f422009-03-31 14:33:13 +11006755 wait_event(mddev->recovery_wait,
6756 atomic_read(&mddev->recovery_active) == 0);
6757 mddev->curr_resync_completed =
6758 mddev->curr_resync;
NeilBrown676e42d2010-06-01 19:37:26 +10006759 if (mddev->persistent)
6760 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006761 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006762 }
NeilBrownacb180b2009-04-14 16:28:34 +10006763
NeilBrowne62e58a2009-07-01 13:15:35 +10006764 while (j >= mddev->resync_max && !kthread_should_stop()) {
6765 /* As this condition is controlled by user-space,
6766 * we can block indefinitely, so use '_interruptible'
6767 * to avoid triggering warnings.
6768 */
6769 flush_signals(current); /* just in case */
6770 wait_event_interruptible(mddev->recovery_wait,
6771 mddev->resync_max > j
6772 || kthread_should_stop());
6773 }
NeilBrownacb180b2009-04-14 16:28:34 +10006774
6775 if (kthread_should_stop())
6776 goto interrupted;
6777
NeilBrown57afd892005-06-21 17:17:13 -07006778 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006779 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006780 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006781 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 goto out;
6783 }
NeilBrown57afd892005-06-21 17:17:13 -07006784
6785 if (!skipped) { /* actual IO requested */
6786 io_sectors += sectors;
6787 atomic_add(sectors, &mddev->recovery_active);
6788 }
6789
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790 j += sectors;
6791 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006792 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006793 if (last_check == 0)
6794 /* this is the earliers that rebuilt will be
6795 * visible in /proc/mdstat
6796 */
6797 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006798
6799 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800 continue;
6801
NeilBrown57afd892005-06-21 17:17:13 -07006802 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803
NeilBrowndfc70642008-05-23 13:04:39 -07006804 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 break;
6806
6807 repeat:
6808 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6809 /* step marks */
6810 int next = (last_mark+1) % SYNC_MARKS;
6811
6812 mddev->resync_mark = mark[next];
6813 mddev->resync_mark_cnt = mark_cnt[next];
6814 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006815 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816 last_mark = next;
6817 }
6818
6819
NeilBrownc6207272008-02-06 01:39:52 -08006820 if (kthread_should_stop())
6821 goto interrupted;
6822
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823
6824 /*
6825 * this loop exits only if either when we are slower than
6826 * the 'hard' speed limit, or the system was IO-idle for
6827 * a jiffy.
6828 * the system might be non-idle CPU-wise, but we only care
6829 * about not overloading the IO subsystem. (things like an
6830 * e2fsck being done on the RAID array should execute fast)
6831 */
NeilBrown252ac522010-06-01 19:37:29 +10006832 md_unplug(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833 cond_resched();
6834
NeilBrown57afd892005-06-21 17:17:13 -07006835 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6836 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837
NeilBrown88202a02006-01-06 00:21:36 -08006838 if (currspeed > speed_min(mddev)) {
6839 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006840 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006841 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842 goto repeat;
6843 }
6844 }
6845 }
NeilBrown61df9d92006-10-03 01:15:57 -07006846 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847 /*
6848 * this also signals 'finished resyncing' to md_stop
6849 */
6850 out:
NeilBrown252ac522010-06-01 19:37:29 +10006851 md_unplug(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852
6853 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6854
6855 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006856 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857
NeilBrowndfc70642008-05-23 13:04:39 -07006858 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006859 mddev->curr_resync > 2) {
6860 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6861 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6862 if (mddev->curr_resync >= mddev->recovery_cp) {
6863 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006864 "md: checkpointing %s of %s.\n",
6865 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006866 mddev->recovery_cp = mddev->curr_resync;
6867 }
6868 } else
6869 mddev->recovery_cp = MaxSector;
6870 } else {
6871 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6872 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006873 rcu_read_lock();
6874 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006875 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10006876 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006877 !test_bit(Faulty, &rdev->flags) &&
6878 !test_bit(In_sync, &rdev->flags) &&
6879 rdev->recovery_offset < mddev->curr_resync)
6880 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07006881 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883 }
NeilBrown17571282006-12-10 02:20:52 -08006884 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006887 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6888 /* We completed so min/max setting can be forgotten if used. */
6889 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6890 mddev->resync_min = 0;
6891 mddev->resync_max = MaxSector;
6892 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6893 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006894 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006895 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6896 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006897 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006898 wake_up(&resync_wait);
6899 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6900 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006901 return;
6902
6903 interrupted:
6904 /*
6905 * got a signal, exit.
6906 */
6907 printk(KERN_INFO
6908 "md: md_do_sync() got signal ... exiting\n");
6909 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6910 goto out;
6911
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912}
NeilBrown29269552006-03-27 01:18:10 -08006913EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914
6915
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006916static int remove_and_add_spares(mddev_t *mddev)
6917{
6918 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006919 int spares = 0;
6920
NeilBrown97e4f422009-03-31 14:33:13 +11006921 mddev->curr_resync_completed = 0;
6922
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006923 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006924 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006925 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006926 (test_bit(Faulty, &rdev->flags) ||
6927 ! test_bit(In_sync, &rdev->flags)) &&
6928 atomic_read(&rdev->nr_pending)==0) {
6929 if (mddev->pers->hot_remove_disk(
6930 mddev, rdev->raid_disk)==0) {
6931 char nm[20];
6932 sprintf(nm,"rd%d", rdev->raid_disk);
6933 sysfs_remove_link(&mddev->kobj, nm);
6934 rdev->raid_disk = -1;
6935 }
6936 }
6937
NeilBrown4044ba52009-01-09 08:31:11 +11006938 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006939 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006940 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006941 !test_bit(In_sync, &rdev->flags) &&
6942 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006943 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006944 if (rdev->raid_disk < 0
6945 && !test_bit(Faulty, &rdev->flags)) {
6946 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006947 if (mddev->pers->
6948 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006949 char nm[20];
6950 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006951 if (sysfs_create_link(&mddev->kobj,
6952 &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10006953 /* failure here is OK */;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006954 spares++;
6955 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11006956 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006957 } else
6958 break;
6959 }
NeilBrowndfc70642008-05-23 13:04:39 -07006960 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006961 }
6962 return spares;
6963}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964/*
6965 * This routine is regularly called by all per-raid-array threads to
6966 * deal with generic issues like resync and super-block update.
6967 * Raid personalities that don't have a thread (linear/raid0) do not
6968 * need this as they never do any recovery or update the superblock.
6969 *
6970 * It does not do any resync itself, but rather "forks" off other threads
6971 * to do that as needed.
6972 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6973 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006974 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975 * and wakeups up this thread which will reap the thread and finish up.
6976 * This thread also removes any faulty devices (with nr_pending == 0).
6977 *
6978 * The overall approach is:
6979 * 1/ if the superblock needs updating, update it.
6980 * 2/ If a recovery thread is running, don't do anything else.
6981 * 3/ If recovery has finished, clean up, possibly marking spares active.
6982 * 4/ If there are any faulty devices, remove them.
6983 * 5/ If array is degraded, try to add spares devices
6984 * 6/ If array has spares or is not in-sync, start a resync thread.
6985 */
6986void md_check_recovery(mddev_t *mddev)
6987{
6988 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989
6990
NeilBrown5f404022005-06-21 17:17:16 -07006991 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11006992 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993
6994 if (mddev->ro)
6995 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006996
6997 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006998 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006999 printk(KERN_INFO "md: %s in immediate safe mode\n",
7000 mdname(mddev));
7001 mddev->safemode = 2;
7002 }
7003 flush_signals(current);
7004 }
7005
NeilBrownc89a8ee2008-08-05 15:54:13 +10007006 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7007 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08007009 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007011 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007012 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007013 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7014 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015 ))
7016 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007017
NeilBrowndf5b89b2006-03-27 01:18:20 -08007018 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007019 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007020
NeilBrownc89a8ee2008-08-05 15:54:13 +10007021 if (mddev->ro) {
7022 /* Only thing we do on a ro array is remove
7023 * failed devices.
7024 */
7025 remove_and_add_spares(mddev);
7026 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7027 goto unlock;
7028 }
7029
NeilBrown31a59e32008-04-30 00:52:30 -07007030 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007031 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007032 spin_lock_irq(&mddev->write_lock);
7033 if (mddev->safemode &&
7034 !atomic_read(&mddev->writes_pending) &&
7035 !mddev->in_sync &&
7036 mddev->recovery_cp == MaxSector) {
7037 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007038 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07007039 if (mddev->persistent)
7040 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7041 }
7042 if (mddev->safemode == 1)
7043 mddev->safemode = 0;
7044 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007045 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007046 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007047 }
NeilBrownfca4d842005-06-21 17:17:11 -07007048
NeilBrown850b2b42006-10-03 01:15:46 -07007049 if (mddev->flags)
7050 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007051
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7053 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7054 /* resync/recovery still happening */
7055 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7056 goto unlock;
7057 }
7058 if (mddev->sync_thread) {
7059 /* resync has finished, collect result */
7060 md_unregister_thread(mddev->sync_thread);
7061 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07007062 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7063 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064 /* success...*/
7065 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10007066 if (mddev->pers->spare_active(mddev))
7067 sysfs_notify(&mddev->kobj, NULL,
7068 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069 }
NeilBrowncea9c222009-03-31 15:15:05 +11007070 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7071 mddev->pers->finish_reshape)
7072 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07007073 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07007074
7075 /* if array is no-longer degraded, then any saved_raid_disk
7076 * information must be scrapped
7077 */
7078 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007079 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07007080 rdev->saved_raid_disk = -1;
7081
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082 mddev->recovery = 0;
7083 /* flag recovery needed just to double check */
7084 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007085 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007086 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087 goto unlock;
7088 }
Neil Brown72a23c22008-06-28 08:31:41 +10007089 /* Set RUNNING before clearing NEEDED to avoid
7090 * any transients in the value of "sync_action".
7091 */
7092 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7093 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007094 /* Clear some bits that don't mean anything, but
7095 * might be left set
7096 */
NeilBrown24dd4692005-11-08 21:39:26 -08007097 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7098 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007100 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7101 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102 /* no recovery is running.
7103 * remove any failed drives, then
7104 * add spares if possible.
7105 * Spare are also removed and re-added, to allow
7106 * the personality to fail the re-add.
7107 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007109 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007110 if (mddev->pers->check_reshape == NULL ||
7111 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007112 /* Cannot proceed */
7113 goto unlock;
7114 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007115 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007116 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007117 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7118 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007119 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007120 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007121 } else if (mddev->recovery_cp < MaxSector) {
7122 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007123 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007124 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7125 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007127
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007129 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7130 /* We are adding a device or devices to an array
7131 * which has the bitmap stored on all devices.
7132 * So make sure all bitmap pages get written
7133 */
7134 bitmap_write_all(mddev->bitmap);
7135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 mddev->sync_thread = md_register_thread(md_do_sync,
7137 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007138 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139 if (!mddev->sync_thread) {
7140 printk(KERN_ERR "%s: could not start resync"
7141 " thread...\n",
7142 mdname(mddev));
7143 /* leave the spares where they are, it shouldn't hurt */
7144 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08007145 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146 md_wakeup_thread(mddev->sync_thread);
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 }
7150 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007151 if (!mddev->sync_thread) {
7152 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7153 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7154 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007155 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007156 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 mddev_unlock(mddev);
7159 }
7160}
7161
Dan Williams6bfe0b42008-04-30 00:52:32 -07007162void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7163{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007164 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007165 wait_event_timeout(rdev->blocked_wait,
7166 !test_bit(Blocked, &rdev->flags),
7167 msecs_to_jiffies(5000));
7168 rdev_dec_pending(rdev, mddev);
7169}
7170EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7171
Adrian Bunk75c96f82005-05-05 16:16:09 -07007172static int md_notify_reboot(struct notifier_block *this,
7173 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174{
7175 struct list_head *tmp;
7176 mddev_t *mddev;
7177
7178 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7179
7180 printk(KERN_INFO "md: stopping all md devices.\n");
7181
NeilBrown29ac4aa2008-02-06 01:39:58 -08007182 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007183 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007184 /* Force a switch to readonly even array
7185 * appears to still be in use. Hence
7186 * the '100'.
7187 */
NeilBrowna4bd82d2010-03-29 13:23:10 +11007188 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007189 mddev_unlock(mddev);
7190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191 /*
7192 * certain more exotic SCSI devices are known to be
7193 * volatile wrt too early system reboots. While the
7194 * right place to handle this issue is the given
7195 * driver, we do want to have a safe RAID driver ...
7196 */
7197 mdelay(1000*1);
7198 }
7199 return NOTIFY_DONE;
7200}
7201
Adrian Bunk75c96f82005-05-05 16:16:09 -07007202static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203 .notifier_call = md_notify_reboot,
7204 .next = NULL,
7205 .priority = INT_MAX, /* before any real devices */
7206};
7207
7208static void md_geninit(void)
7209{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7211
Denis V. Lunevc7705f32008-04-29 01:02:35 -07007212 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213}
7214
Adrian Bunk75c96f82005-05-05 16:16:09 -07007215static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007217 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218 return -1;
7219 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007220 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221 return -1;
7222 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007223 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007224 md_probe, NULL, NULL);
7225 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226 md_probe, NULL, NULL);
7227
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007229 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230
7231 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007232 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233}
7234
7235
7236#ifndef MODULE
7237
7238/*
7239 * Searches all registered partitions for autorun RAID arrays
7240 * at boot time.
7241 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007242
7243static LIST_HEAD(all_detected_devices);
7244struct detected_devices_node {
7245 struct list_head list;
7246 dev_t dev;
7247};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248
7249void md_autodetect_dev(dev_t dev)
7250{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007251 struct detected_devices_node *node_detected_dev;
7252
7253 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7254 if (node_detected_dev) {
7255 node_detected_dev->dev = dev;
7256 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7257 } else {
7258 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7259 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007261}
7262
7263
7264static void autostart_arrays(int part)
7265{
7266 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007267 struct detected_devices_node *node_detected_dev;
7268 dev_t dev;
7269 int i_scanned, i_passed;
7270
7271 i_scanned = 0;
7272 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273
7274 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7275
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007276 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7277 i_scanned++;
7278 node_detected_dev = list_entry(all_detected_devices.next,
7279 struct detected_devices_node, list);
7280 list_del(&node_detected_dev->list);
7281 dev = node_detected_dev->dev;
7282 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007283 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284 if (IS_ERR(rdev))
7285 continue;
7286
NeilBrownb2d444d2005-11-08 21:39:31 -08007287 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288 MD_BUG();
7289 continue;
7290 }
NeilBrownd0fae182008-03-04 14:29:31 -08007291 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007293 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007295
7296 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7297 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007298
7299 autorun_devices(part);
7300}
7301
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007302#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007303
7304static __exit void md_exit(void)
7305{
7306 mddev_t *mddev;
7307 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007308
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007309 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007310 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007311
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007312 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007313 unregister_blkdev(mdp_major, "mdp");
7314 unregister_reboot_notifier(&md_notifier);
7315 unregister_sysctl_table(raid_table_header);
7316 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007317 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007319 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007320 }
7321}
7322
Dan Williams685784a2007-07-09 11:56:42 -07007323subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324module_exit(md_exit)
7325
NeilBrownf91de922005-11-08 21:39:36 -08007326static int get_ro(char *buffer, struct kernel_param *kp)
7327{
7328 return sprintf(buffer, "%d", start_readonly);
7329}
7330static int set_ro(const char *val, struct kernel_param *kp)
7331{
7332 char *e;
7333 int num = simple_strtoul(val, &e, 10);
7334 if (*val && (*e == '\0' || *e == '\n')) {
7335 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007336 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007337 }
7338 return -EINVAL;
7339}
7340
NeilBrown80ca3a42006-07-10 04:44:18 -07007341module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7342module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007343
NeilBrownefeb53c2009-01-09 08:31:10 +11007344module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007345
Linus Torvalds1da177e2005-04-16 15:20:36 -07007346EXPORT_SYMBOL(register_md_personality);
7347EXPORT_SYMBOL(unregister_md_personality);
7348EXPORT_SYMBOL(md_error);
7349EXPORT_SYMBOL(md_done_sync);
7350EXPORT_SYMBOL(md_write_start);
7351EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352EXPORT_SYMBOL(md_register_thread);
7353EXPORT_SYMBOL(md_unregister_thread);
7354EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355EXPORT_SYMBOL(md_check_recovery);
7356MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007357MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007358MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007359MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);