blob: 3149bf33f1c51cbc64c48194db0ee2dd4cbde933 [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 Bergmann2a48fc02010-06-02 14:28:52 +020039#include <linux/mutex.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
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110061static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#endif
63
NeilBrown2604b702006-01-06 00:20:36 -080064static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static DEFINE_SPINLOCK(pers_lock);
66
Adrian Bunk5e563412006-06-26 00:27:42 -070067static void md_print_devices(void);
68
Bernd Schubert90b08712008-05-23 13:04:38 -070069static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020070static struct workqueue_struct *md_wq;
71static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070072
Adrian Bunk5e563412006-06-26 00:27:42 -070073#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/*
Robert Becker1e509152009-12-14 12:49:58 +110076 * Default number of read corrections we'll attempt on an rdev
77 * before ejecting it from the array. We divide the read error
78 * count by 2 for every hour elapsed between read errors.
79 */
80#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
81/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
83 * is 1000 KB/sec, so the extra system load does not show up that much.
84 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070085 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * subsystem is idle. There is also an 'absolute maximum' reconstruction
87 * speed limit - in case reconstruction slows down your system despite
88 * idle IO detection.
89 *
90 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080091 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
93
94static int sysctl_speed_limit_min = 1000;
95static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080096static inline int speed_min(mddev_t *mddev)
97{
98 return mddev->sync_speed_min ?
99 mddev->sync_speed_min : sysctl_speed_limit_min;
100}
101
102static inline int speed_max(mddev_t *mddev)
103{
104 return mddev->sync_speed_max ?
105 mddev->sync_speed_max : sysctl_speed_limit_max;
106}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108static struct ctl_table_header *raid_table_header;
109
110static ctl_table raid_table[] = {
111 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 .procname = "speed_limit_min",
113 .data = &sysctl_speed_limit_min,
114 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700115 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800116 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 },
118 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .procname = "speed_limit_max",
120 .data = &sysctl_speed_limit_max,
121 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700122 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800123 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800125 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
128static ctl_table raid_dir_table[] = {
129 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 .procname = "raid",
131 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700132 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .child = raid_table,
134 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800135 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
138static ctl_table raid_root_table[] = {
139 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 .procname = "dev",
141 .maxlen = 0,
142 .mode = 0555,
143 .child = raid_dir_table,
144 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800145 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700148static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
NeilBrownf91de922005-11-08 21:39:36 -0800150static int start_readonly;
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/*
NeilBrownd7603b72006-01-06 00:20:30 -0800153 * We have a system wide 'event count' that is incremented
154 * on any 'interesting' event, and readers of /proc/mdstat
155 * can use 'poll' or 'select' to find out when the event
156 * count increases.
157 *
158 * Events are:
159 * start array, stop array, error, add device, remove device,
160 * start build, activate spare
161 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800162static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800163static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800164void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800165{
166 atomic_inc(&md_event_count);
167 wake_up(&md_event_waiters);
168}
NeilBrown29269552006-03-27 01:18:10 -0800169EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800170
NeilBrownc331eb02006-05-30 21:27:13 -0700171/* Alternate version that can be called from interrupts
172 * when calling sysfs_notify isn't needed.
173 */
Adrian Bunk05381952006-06-26 00:28:01 -0700174static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700175{
176 atomic_inc(&md_event_count);
177 wake_up(&md_event_waiters);
178}
179
NeilBrownd7603b72006-01-06 00:20:30 -0800180/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * Enables to iterate over all existing md arrays
182 * all_mddevs_lock protects this list.
183 */
184static LIST_HEAD(all_mddevs);
185static DEFINE_SPINLOCK(all_mddevs_lock);
186
187
188/*
189 * iterates through all used mddevs in the system.
190 * We take care to grab the all_mddevs_lock whenever navigating
191 * the list, and to always hold a refcount when unlocked.
192 * Any code which breaks out of this loop while own
193 * a reference to the current mddev and must mddev_put it.
194 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800195#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 \
197 for (({ spin_lock(&all_mddevs_lock); \
198 tmp = all_mddevs.next; \
199 mddev = NULL;}); \
200 ({ if (tmp != &all_mddevs) \
201 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
202 spin_unlock(&all_mddevs_lock); \
203 if (mddev) mddev_put(mddev); \
204 mddev = list_entry(tmp, mddev_t, all_mddevs); \
205 tmp != &all_mddevs;}); \
206 ({ spin_lock(&all_mddevs_lock); \
207 tmp = tmp->next;}) \
208 )
209
210
NeilBrown409c57f2009-03-31 14:39:39 +1100211/* Rather than calling directly into the personality make_request function,
212 * IO requests come here first so that we can check if the device is
213 * being suspended pending a reconfiguration.
214 * We hold a refcount over the call to ->make_request. By the time that
215 * call has finished, the bio has been linked into some internal structure
216 * and so is visible to ->quiesce(), so we don't need the refcount any more.
217 */
218static int md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
NeilBrown49077322010-03-25 16:20:56 +1100220 const int rw = bio_data_dir(bio);
NeilBrown409c57f2009-03-31 14:39:39 +1100221 mddev_t *mddev = q->queuedata;
222 int rv;
NeilBrown49077322010-03-25 16:20:56 +1100223 int cpu;
224
NeilBrown409c57f2009-03-31 14:39:39 +1100225 if (mddev == NULL || mddev->pers == NULL) {
226 bio_io_error(bio);
227 return 0;
228 }
229 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200230 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100231 DEFINE_WAIT(__wait);
232 for (;;) {
233 prepare_to_wait(&mddev->sb_wait, &__wait,
234 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200235 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100236 break;
237 rcu_read_unlock();
238 schedule();
239 rcu_read_lock();
240 }
241 finish_wait(&mddev->sb_wait, &__wait);
242 }
243 atomic_inc(&mddev->active_io);
244 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100245
NeilBrown21a52c62010-04-01 15:02:13 +1100246 rv = mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100247
248 cpu = part_stat_lock();
249 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
250 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
251 bio_sectors(bio));
252 part_stat_unlock();
253
NeilBrown409c57f2009-03-31 14:39:39 +1100254 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
255 wake_up(&mddev->sb_wait);
256
257 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
NeilBrown9e35b992010-04-06 14:23:02 +1000260/* mddev_suspend makes sure no new requests are submitted
261 * to the device, and that any requests that have been submitted
262 * are completely handled.
263 * Once ->stop is called and completes, the module will be completely
264 * unused.
265 */
NeilBrown390ee602010-06-01 19:37:27 +1000266void mddev_suspend(mddev_t *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100267{
268 BUG_ON(mddev->suspended);
269 mddev->suspended = 1;
270 synchronize_rcu();
271 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
272 mddev->pers->quiesce(mddev, 1);
NeilBrown409c57f2009-03-31 14:39:39 +1100273}
NeilBrown390ee602010-06-01 19:37:27 +1000274EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100275
NeilBrown390ee602010-06-01 19:37:27 +1000276void mddev_resume(mddev_t *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100277{
278 mddev->suspended = 0;
279 wake_up(&mddev->sb_wait);
280 mddev->pers->quiesce(mddev, 0);
281}
NeilBrown390ee602010-06-01 19:37:27 +1000282EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100283
NeilBrown3fa841d2009-09-23 18:10:29 +1000284int mddev_congested(mddev_t *mddev, int bits)
285{
286 return mddev->suspended;
287}
288EXPORT_SYMBOL(mddev_congested);
289
NeilBrowna2826aa2009-12-14 12:49:49 +1100290/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200291 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100292 */
293
Tejun Heoe9c74692010-09-03 11:56:18 +0200294static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100295{
296 mdk_rdev_t *rdev = bio->bi_private;
297 mddev_t *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100298
299 rdev_dec_pending(rdev, mddev);
300
301 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200302 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200303 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100304 }
305 bio_put(bio);
306}
307
Tejun Heoe9c74692010-09-03 11:56:18 +0200308static void submit_flushes(mddev_t *mddev)
NeilBrowna2826aa2009-12-14 12:49:49 +1100309{
310 mdk_rdev_t *rdev;
311
312 rcu_read_lock();
313 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
314 if (rdev->raid_disk >= 0 &&
315 !test_bit(Faulty, &rdev->flags)) {
316 /* Take two references, one is dropped
317 * when request finishes, one after
318 * we reclaim rcu_read_lock
319 */
320 struct bio *bi;
321 atomic_inc(&rdev->nr_pending);
322 atomic_inc(&rdev->nr_pending);
323 rcu_read_unlock();
324 bi = bio_alloc(GFP_KERNEL, 0);
Tejun Heoe9c74692010-09-03 11:56:18 +0200325 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100326 bi->bi_private = rdev;
327 bi->bi_bdev = rdev->bdev;
328 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200329 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100330 rcu_read_lock();
331 rdev_dec_pending(rdev, mddev);
332 }
333 rcu_read_unlock();
334}
335
Tejun Heoe9c74692010-09-03 11:56:18 +0200336static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100337{
Tejun Heoe9c74692010-09-03 11:56:18 +0200338 mddev_t *mddev = container_of(ws, mddev_t, flush_work);
339 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100340
341 atomic_set(&mddev->flush_pending, 1);
342
Tejun Heoe9c74692010-09-03 11:56:18 +0200343 if (bio->bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100344 /* an empty barrier - all done */
345 bio_endio(bio, 0);
346 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200347 bio->bi_rw &= ~REQ_FLUSH;
NeilBrown21a52c62010-04-01 15:02:13 +1100348 if (mddev->pers->make_request(mddev, bio))
NeilBrowna2826aa2009-12-14 12:49:49 +1100349 generic_make_request(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100350 }
351 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200352 mddev->flush_bio = NULL;
NeilBrowna2826aa2009-12-14 12:49:49 +1100353 wake_up(&mddev->sb_wait);
354 }
355}
356
Tejun Heoe9c74692010-09-03 11:56:18 +0200357void md_flush_request(mddev_t *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100358{
359 spin_lock_irq(&mddev->write_lock);
360 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200361 !mddev->flush_bio,
NeilBrowna2826aa2009-12-14 12:49:49 +1100362 mddev->write_lock, /*nothing*/);
Tejun Heoe9c74692010-09-03 11:56:18 +0200363 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100364 spin_unlock_irq(&mddev->write_lock);
365
366 atomic_set(&mddev->flush_pending, 1);
Tejun Heoe9c74692010-09-03 11:56:18 +0200367 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
NeilBrowna2826aa2009-12-14 12:49:49 +1100368
Tejun Heoe9c74692010-09-03 11:56:18 +0200369 submit_flushes(mddev);
NeilBrowna2826aa2009-12-14 12:49:49 +1100370
371 if (atomic_dec_and_test(&mddev->flush_pending))
Tejun Heoe804ac72010-10-15 15:36:08 +0200372 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100373}
Tejun Heoe9c74692010-09-03 11:56:18 +0200374EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100375
NeilBrown2ac87402010-06-01 19:37:29 +1000376/* Support for plugging.
377 * This mirrors the plugging support in request_queue, but does not
378 * require having a whole queue
379 */
380static void plugger_work(struct work_struct *work)
381{
382 struct plug_handle *plug =
383 container_of(work, struct plug_handle, unplug_work);
384 plug->unplug_fn(plug);
385}
386static void plugger_timeout(unsigned long data)
387{
388 struct plug_handle *plug = (void *)data;
389 kblockd_schedule_work(NULL, &plug->unplug_work);
390}
391void plugger_init(struct plug_handle *plug,
392 void (*unplug_fn)(struct plug_handle *))
393{
394 plug->unplug_flag = 0;
395 plug->unplug_fn = unplug_fn;
396 init_timer(&plug->unplug_timer);
397 plug->unplug_timer.function = plugger_timeout;
398 plug->unplug_timer.data = (unsigned long)plug;
399 INIT_WORK(&plug->unplug_work, plugger_work);
400}
401EXPORT_SYMBOL_GPL(plugger_init);
402
403void plugger_set_plug(struct plug_handle *plug)
404{
405 if (!test_and_set_bit(PLUGGED_FLAG, &plug->unplug_flag))
406 mod_timer(&plug->unplug_timer, jiffies + msecs_to_jiffies(3)+1);
407}
408EXPORT_SYMBOL_GPL(plugger_set_plug);
409
410int plugger_remove_plug(struct plug_handle *plug)
411{
412 if (test_and_clear_bit(PLUGGED_FLAG, &plug->unplug_flag)) {
413 del_timer(&plug->unplug_timer);
414 return 1;
415 } else
416 return 0;
417}
418EXPORT_SYMBOL_GPL(plugger_remove_plug);
419
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421static inline mddev_t *mddev_get(mddev_t *mddev)
422{
423 atomic_inc(&mddev->active);
424 return mddev;
425}
426
Dan Williams5fd3a172009-03-04 00:57:25 -0700427static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429static void mddev_put(mddev_t *mddev)
430{
431 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
432 return;
NeilBrownd3374822009-01-09 08:31:10 +1100433 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100434 mddev->ctime == 0 && !mddev->hold_active) {
435 /* Array is not configured at all, and not held active,
436 * so destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 list_del(&mddev->all_mddevs);
NeilBrownd3374822009-01-09 08:31:10 +1100438 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200439 /* We did a probe so need to clean up. Call
440 * queue_work inside the spinlock so that
441 * flush_workqueue() after mddev_find will
442 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100443 */
444 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200445 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100446 } 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
NeilBrown2b193362010-10-27 15:16:40 +1100724int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size,
725 struct page *page, int rw)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
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
NeilBrown2b193362010-10-27 15:16:40 +1100733 bio->bi_bdev = rdev->bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 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
NeilBrown2b193362010-10-27 15:16:40 +1100759 if (!sync_page_io(rdev, 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;
NeilBrownddcf3522010-09-08 16:48:17 +10001581 } else
1582 max_dev = le32_to_cpu(sb->max_dev);
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 for (i=0; i<max_dev;i++)
1585 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1586
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001587 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001589 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001591 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001593 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001594 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 else
1596 sb->dev_roles[i] = cpu_to_le16(0xffff);
1597 }
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 sb->sb_csum = calc_sb_1_csum(sb);
1600}
1601
Chris Webb0cd17fe2008-06-28 08:31:46 +10001602static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001603super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001604{
1605 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001606 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001607 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001608 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001609 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001610 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001611 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1612 max_sectors -= rdev->data_offset;
1613 if (!num_sectors || num_sectors > max_sectors)
1614 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001615 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001616 /* minor version 0 with bitmap we can't move */
1617 return 0;
1618 } else {
1619 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001620 sector_t sb_start;
1621 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1622 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001623 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001624 if (!num_sectors || num_sectors > max_sectors)
1625 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001626 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001627 }
1628 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001629 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001630 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001631 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001632 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001633 rdev->sb_page);
1634 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001635 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001636}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Adrian Bunk75c96f82005-05-05 16:16:09 -07001638static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 [0] = {
1640 .name = "0.90.0",
1641 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001642 .load_super = super_90_load,
1643 .validate_super = super_90_validate,
1644 .sync_super = super_90_sync,
1645 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 },
1647 [1] = {
1648 .name = "md-1",
1649 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001650 .load_super = super_1_load,
1651 .validate_super = super_1_validate,
1652 .sync_super = super_1_sync,
1653 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 },
1655};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1658{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001659 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
NeilBrown4b809912008-07-21 17:05:25 +10001661 rcu_read_lock();
1662 rdev_for_each_rcu(rdev, mddev1)
1663 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001664 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001665 rdev2->bdev->bd_contains) {
1666 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001667 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001668 }
1669 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return 0;
1671}
1672
1673static LIST_HEAD(pending_raid_disks);
1674
Andre Nollac5e7112009-08-03 10:59:47 +10001675/*
1676 * Try to register data integrity profile for an mddev
1677 *
1678 * This is called when an array is started and after a disk has been kicked
1679 * from the array. It only succeeds if all working and active component devices
1680 * are integrity capable with matching profiles.
1681 */
1682int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001683{
Andre Nollac5e7112009-08-03 10:59:47 +10001684 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001685
Andre Nollac5e7112009-08-03 10:59:47 +10001686 if (list_empty(&mddev->disks))
1687 return 0; /* nothing to do */
1688 if (blk_get_integrity(mddev->gendisk))
1689 return 0; /* already registered */
1690 list_for_each_entry(rdev, &mddev->disks, same_set) {
1691 /* skip spares and non-functional disks */
1692 if (test_bit(Faulty, &rdev->flags))
1693 continue;
1694 if (rdev->raid_disk < 0)
1695 continue;
1696 /*
1697 * If at least one rdev is not integrity capable, we can not
1698 * enable data integrity for the md device.
1699 */
1700 if (!bdev_get_integrity(rdev->bdev))
1701 return -EINVAL;
1702 if (!reference) {
1703 /* Use the first rdev as the reference */
1704 reference = rdev;
1705 continue;
1706 }
1707 /* does this rdev's profile match the reference profile? */
1708 if (blk_integrity_compare(reference->bdev->bd_disk,
1709 rdev->bdev->bd_disk) < 0)
1710 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001711 }
Andre Nollac5e7112009-08-03 10:59:47 +10001712 /*
1713 * All component devices are integrity capable and have matching
1714 * profiles, register the common profile for the md device.
1715 */
1716 if (blk_integrity_register(mddev->gendisk,
1717 bdev_get_integrity(reference->bdev)) != 0) {
1718 printk(KERN_ERR "md: failed to register integrity for %s\n",
1719 mdname(mddev));
1720 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001721 }
Andre Nollac5e7112009-08-03 10:59:47 +10001722 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1723 mdname(mddev));
1724 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001725}
Andre Nollac5e7112009-08-03 10:59:47 +10001726EXPORT_SYMBOL(md_integrity_register);
1727
1728/* Disable data integrity if non-capable/non-matching disk is being added */
1729void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1730{
1731 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1732 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1733
1734 if (!bi_mddev) /* nothing to do */
1735 return;
1736 if (rdev->raid_disk < 0) /* skip spares */
1737 return;
1738 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1739 rdev->bdev->bd_disk) >= 0)
1740 return;
1741 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1742 blk_integrity_unregister(mddev->gendisk);
1743}
1744EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1747{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001748 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001749 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001750 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001751 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 if (rdev->mddev) {
1754 MD_BUG();
1755 return -EINVAL;
1756 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001757
1758 /* prevent duplicates */
1759 if (find_rdev(mddev, rdev->bdev->bd_dev))
1760 return -EEXIST;
1761
Andre Nolldd8ac332009-03-31 14:33:13 +11001762 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1763 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1764 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001765 if (mddev->pers) {
1766 /* Cannot change size, so fail
1767 * If mddev->level <= 0, then we don't care
1768 * about aligning sizes (e.g. linear)
1769 */
1770 if (mddev->level > 0)
1771 return -ENOSPC;
1772 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001773 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
1776 /* Verify rdev->desc_nr is unique.
1777 * If it is -1, assign a free number, else
1778 * check number is not in use
1779 */
1780 if (rdev->desc_nr < 0) {
1781 int choice = 0;
1782 if (mddev->pers) choice = mddev->raid_disks;
1783 while (find_rdev_nr(mddev, choice))
1784 choice++;
1785 rdev->desc_nr = choice;
1786 } else {
1787 if (find_rdev_nr(mddev, rdev->desc_nr))
1788 return -EBUSY;
1789 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001790 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1791 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1792 mdname(mddev), mddev->max_disks);
1793 return -EBUSY;
1794 }
NeilBrown19133a42005-11-08 21:39:35 -08001795 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001796 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001797 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001800 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001801
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001802 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001803 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001804
Tejun Heo0762b8b2008-08-25 19:56:12 +09001805 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10001806 if (sysfs_create_link(&rdev->kobj, ko, "block"))
1807 /* failure here is OK */;
1808 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11001809
NeilBrown4b809912008-07-21 17:05:25 +10001810 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001811 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001812
1813 /* May as well allow recovery to be retried once */
1814 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001817
1818 fail:
1819 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1820 b, mdname(mddev));
1821 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
1823
NeilBrown177a99b2008-02-06 01:39:56 -08001824static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001825{
1826 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1827 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001828 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001829}
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1832{
1833 char b[BDEVNAME_SIZE];
1834 if (!rdev->mddev) {
1835 MD_BUG();
1836 return;
1837 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001838 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001839 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1841 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001842 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001843 sysfs_put(rdev->sysfs_state);
1844 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001845 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001846 * writing to 'remove' to "dev/state". We also need
1847 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001848 */
NeilBrown4b809912008-07-21 17:05:25 +10001849 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001850 INIT_WORK(&rdev->del_work, md_delayed_delete);
1851 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02001852 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853}
1854
1855/*
1856 * prevent the device from being mounted, repartitioned or
1857 * otherwise reused by a RAID array (or any other kernel
1858 * subsystem), by bd_claiming the device.
1859 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001860static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
1862 int err = 0;
1863 struct block_device *bdev;
1864 char b[BDEVNAME_SIZE];
1865
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001866 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 if (IS_ERR(bdev)) {
1868 printk(KERN_ERR "md: could not open %s.\n",
1869 __bdevname(dev, b));
1870 return PTR_ERR(bdev);
1871 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001872 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 if (err) {
1874 printk(KERN_ERR "md: could not bd_claim %s.\n",
1875 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001876 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 return err;
1878 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001879 if (!shared)
1880 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 rdev->bdev = bdev;
1882 return err;
1883}
1884
1885static void unlock_rdev(mdk_rdev_t *rdev)
1886{
1887 struct block_device *bdev = rdev->bdev;
1888 rdev->bdev = NULL;
1889 if (!bdev)
1890 MD_BUG();
1891 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001892 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
1895void md_autodetect_dev(dev_t dev);
1896
1897static void export_rdev(mdk_rdev_t * rdev)
1898{
1899 char b[BDEVNAME_SIZE];
1900 printk(KERN_INFO "md: export_rdev(%s)\n",
1901 bdevname(rdev->bdev,b));
1902 if (rdev->mddev)
1903 MD_BUG();
1904 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001906 if (test_bit(AutoDetected, &rdev->flags))
1907 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908#endif
1909 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001910 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911}
1912
1913static void kick_rdev_from_array(mdk_rdev_t * rdev)
1914{
1915 unbind_rdev_from_array(rdev);
1916 export_rdev(rdev);
1917}
1918
1919static void export_array(mddev_t *mddev)
1920{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001921 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
NeilBrownd089c6a2008-02-06 01:39:59 -08001923 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 if (!rdev->mddev) {
1925 MD_BUG();
1926 continue;
1927 }
1928 kick_rdev_from_array(rdev);
1929 }
1930 if (!list_empty(&mddev->disks))
1931 MD_BUG();
1932 mddev->raid_disks = 0;
1933 mddev->major_version = 0;
1934}
1935
1936static void print_desc(mdp_disk_t *desc)
1937{
1938 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1939 desc->major,desc->minor,desc->raid_disk,desc->state);
1940}
1941
Cheng Renquancd2ac932009-01-09 08:31:08 +11001942static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
1944 int i;
1945
1946 printk(KERN_INFO
1947 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1948 sb->major_version, sb->minor_version, sb->patch_version,
1949 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1950 sb->ctime);
1951 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1952 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1953 sb->md_minor, sb->layout, sb->chunk_size);
1954 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1955 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1956 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1957 sb->failed_disks, sb->spare_disks,
1958 sb->sb_csum, (unsigned long)sb->events_lo);
1959
1960 printk(KERN_INFO);
1961 for (i = 0; i < MD_SB_DISKS; i++) {
1962 mdp_disk_t *desc;
1963
1964 desc = sb->disks + i;
1965 if (desc->number || desc->major || desc->minor ||
1966 desc->raid_disk || (desc->state && (desc->state != 4))) {
1967 printk(" D %2d: ", i);
1968 print_desc(desc);
1969 }
1970 }
1971 printk(KERN_INFO "md: THIS: ");
1972 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
Cheng Renquancd2ac932009-01-09 08:31:08 +11001975static void print_sb_1(struct mdp_superblock_1 *sb)
1976{
1977 __u8 *uuid;
1978
1979 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001980 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08001981 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001982 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001983 le32_to_cpu(sb->major_version),
1984 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08001985 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11001986 sb->set_name,
1987 (unsigned long long)le64_to_cpu(sb->ctime)
1988 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1989
1990 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001991 printk(KERN_INFO
1992 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11001993 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08001994 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001995 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1996 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001997 le32_to_cpu(sb->level),
1998 (unsigned long long)le64_to_cpu(sb->size),
1999 le32_to_cpu(sb->raid_disks),
2000 le32_to_cpu(sb->layout),
2001 le32_to_cpu(sb->chunksize),
2002 (unsigned long long)le64_to_cpu(sb->data_offset),
2003 (unsigned long long)le64_to_cpu(sb->data_size),
2004 (unsigned long long)le64_to_cpu(sb->super_offset),
2005 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2006 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002007 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002008 sb->devflags,
2009 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2010 (unsigned long long)le64_to_cpu(sb->events),
2011 (unsigned long long)le64_to_cpu(sb->resync_offset),
2012 le32_to_cpu(sb->sb_csum),
2013 le32_to_cpu(sb->max_dev)
2014 );
2015}
2016
2017static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
2019 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002020 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2021 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002022 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2023 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002025 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2026 switch (major_version) {
2027 case 0:
2028 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
2029 break;
2030 case 1:
2031 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
2032 break;
2033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 } else
2035 printk(KERN_INFO "md: no rdev superblock!\n");
2036}
2037
Adrian Bunk5e563412006-06-26 00:27:42 -07002038static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002040 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 mdk_rdev_t *rdev;
2042 mddev_t *mddev;
2043 char b[BDEVNAME_SIZE];
2044
2045 printk("\n");
2046 printk("md: **********************************\n");
2047 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2048 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002049 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
NeilBrown32a76272005-06-21 17:17:14 -07002051 if (mddev->bitmap)
2052 bitmap_print_sb(mddev->bitmap);
2053 else
2054 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002055 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 printk("<%s>", bdevname(rdev->bdev,b));
2057 printk("\n");
2058
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002059 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002060 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
2062 printk("md: **********************************\n");
2063 printk("\n");
2064}
2065
2066
NeilBrown42543762006-06-26 00:27:57 -07002067static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
NeilBrown42543762006-06-26 00:27:57 -07002069 /* Update each superblock (in-memory image), but
2070 * if we are allowed to, skip spares which already
2071 * have the right event counter, or have one earlier
2072 * (which would mean they aren't being marked as dirty
2073 * with the rest of the array)
2074 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 mdk_rdev_t *rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002076 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002077 if (rdev->sb_events == mddev->events ||
2078 (nospares &&
2079 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002080 rdev->sb_events+1 == mddev->events)) {
2081 /* Don't update this superblock */
2082 rdev->sb_loaded = 2;
2083 } else {
2084 super_types[mddev->major_version].
2085 sync_super(mddev, rdev);
2086 rdev->sb_loaded = 1;
2087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
2089}
2090
NeilBrown850b2b42006-10-03 01:15:46 -07002091static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002094 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002095 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002098 /* First make sure individual recovery_offsets are correct */
2099 list_for_each_entry(rdev, &mddev->disks, same_set) {
2100 if (rdev->raid_disk >= 0 &&
2101 mddev->delta_disks >= 0 &&
2102 !test_bit(In_sync, &rdev->flags) &&
2103 mddev->curr_resync_completed > rdev->recovery_offset)
2104 rdev->recovery_offset = mddev->curr_resync_completed;
2105
2106 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002107 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002108 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002109 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownd97a41d2010-10-28 17:30:20 +11002110 if (!mddev->external)
2111 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002112 wake_up(&mddev->sb_wait);
2113 return;
2114 }
2115
NeilBrowna9701a32005-11-08 21:39:34 -08002116 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002117
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002118 mddev->utime = get_seconds();
2119
NeilBrown850b2b42006-10-03 01:15:46 -07002120 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2121 force_change = 1;
2122 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2123 /* just a clean<-> dirty transition, possibly leave spares alone,
2124 * though if events isn't the right even/odd, we will have to do
2125 * spares after all
2126 */
2127 nospares = 1;
2128 if (force_change)
2129 nospares = 0;
2130 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002131 /* If the array is degraded, then skipping spares is both
2132 * dangerous and fairly pointless.
2133 * Dangerous because a device that was removed from the array
2134 * might have a event_count that still looks up-to-date,
2135 * so it can be re-added without a resync.
2136 * Pointless because if there are any spares to skip,
2137 * then a recovery will happen and soon that array won't
2138 * be degraded any more and the spare can go back to sleep then.
2139 */
NeilBrown850b2b42006-10-03 01:15:46 -07002140 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002141
NeilBrown06d91a52005-06-21 17:17:12 -07002142 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002143
2144 /* If this is just a dirty<->clean transition, and the array is clean
2145 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002146 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002147 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002148 && mddev->can_decrease_events
2149 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002150 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002151 mddev->can_decrease_events = 0;
2152 } else {
NeilBrown42543762006-06-26 00:27:57 -07002153 /* otherwise we have to go forward and ... */
2154 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002155 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158 if (!mddev->events) {
2159 /*
2160 * oops, this 64-bit counter should never wrap.
2161 * Either we are in around ~1 trillion A.C., assuming
2162 * 1 reboot per second, or we have a bug:
2163 */
2164 MD_BUG();
2165 mddev->events --;
2166 }
NeilBrowne6910632008-02-06 01:39:51 -08002167 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002168 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 dprintk(KERN_INFO
2171 "md: updating %s RAID superblock on device (in sync %d)\n",
2172 mdname(mddev),mddev->in_sync);
2173
NeilBrown4ad13662007-07-17 04:06:13 -07002174 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002175 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 char b[BDEVNAME_SIZE];
2177 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002178 if (rdev->sb_loaded != 1)
2179 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002180 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 dprintk("(skipping faulty ");
2182
2183 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002184 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002185 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002186 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002187 rdev->sb_page);
2188 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2189 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002190 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002191 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 } else
2194 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002195 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 /* only need to write one superblock... */
2197 break;
2198 }
NeilBrowna9701a32005-11-08 21:39:34 -08002199 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002200 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002201
NeilBrowna9701a32005-11-08 21:39:34 -08002202 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002203 if (mddev->in_sync != sync_req ||
2204 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002205 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002206 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002207 goto repeat;
2208 }
NeilBrown850b2b42006-10-03 01:15:46 -07002209 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002210 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002211 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002212 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2213 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
Andre Noll7f6ce762008-03-23 18:34:54 +01002217/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002218 * We want to accept with case. For this we use cmd_match.
2219 */
2220static int cmd_match(const char *cmd, const char *str)
2221{
2222 /* See if cmd, written into a sysfs file, matches
2223 * str. They must either be the same, or cmd can
2224 * have a trailing newline
2225 */
2226 while (*cmd && *str && *cmd == *str) {
2227 cmd++;
2228 str++;
2229 }
2230 if (*cmd == '\n')
2231 cmd++;
2232 if (*str || *cmd)
2233 return 0;
2234 return 1;
2235}
2236
NeilBrown86e6ffd2005-11-08 21:39:24 -08002237struct rdev_sysfs_entry {
2238 struct attribute attr;
2239 ssize_t (*show)(mdk_rdev_t *, char *);
2240 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2241};
2242
2243static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002244state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002245{
2246 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002247 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002248
NeilBrownb2d444d2005-11-08 21:39:31 -08002249 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002250 len+= sprintf(page+len, "%sfaulty",sep);
2251 sep = ",";
2252 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002253 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002254 len += sprintf(page+len, "%sin_sync",sep);
2255 sep = ",";
2256 }
NeilBrownf6556752006-06-26 00:28:01 -07002257 if (test_bit(WriteMostly, &rdev->flags)) {
2258 len += sprintf(page+len, "%swrite_mostly",sep);
2259 sep = ",";
2260 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002261 if (test_bit(Blocked, &rdev->flags)) {
2262 len += sprintf(page+len, "%sblocked", sep);
2263 sep = ",";
2264 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002265 if (!test_bit(Faulty, &rdev->flags) &&
2266 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002267 len += sprintf(page+len, "%sspare", sep);
2268 sep = ",";
2269 }
2270 return len+sprintf(page+len, "\n");
2271}
2272
NeilBrown45dc2de2006-06-26 00:27:58 -07002273static ssize_t
2274state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2275{
2276 /* can write
2277 * faulty - simulates and error
2278 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002279 * writemostly - sets write_mostly
2280 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002281 * blocked - sets the Blocked flag
2282 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002283 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002284 */
2285 int err = -EINVAL;
2286 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2287 md_error(rdev->mddev, rdev);
2288 err = 0;
2289 } else if (cmd_match(buf, "remove")) {
2290 if (rdev->raid_disk >= 0)
2291 err = -EBUSY;
2292 else {
2293 mddev_t *mddev = rdev->mddev;
2294 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002295 if (mddev->pers)
2296 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002297 md_new_event(mddev);
2298 err = 0;
2299 }
NeilBrownf6556752006-06-26 00:28:01 -07002300 } else if (cmd_match(buf, "writemostly")) {
2301 set_bit(WriteMostly, &rdev->flags);
2302 err = 0;
2303 } else if (cmd_match(buf, "-writemostly")) {
2304 clear_bit(WriteMostly, &rdev->flags);
2305 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002306 } else if (cmd_match(buf, "blocked")) {
2307 set_bit(Blocked, &rdev->flags);
2308 err = 0;
2309 } else if (cmd_match(buf, "-blocked")) {
2310 clear_bit(Blocked, &rdev->flags);
2311 wake_up(&rdev->blocked_wait);
2312 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2313 md_wakeup_thread(rdev->mddev->thread);
2314
2315 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002316 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2317 set_bit(In_sync, &rdev->flags);
2318 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002319 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002320 if (!err)
2321 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002322 return err ? err : len;
2323}
NeilBrown80ca3a42006-07-10 04:44:18 -07002324static struct rdev_sysfs_entry rdev_state =
2325__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002326
2327static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002328errors_show(mdk_rdev_t *rdev, char *page)
2329{
2330 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2331}
2332
2333static ssize_t
2334errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2335{
2336 char *e;
2337 unsigned long n = simple_strtoul(buf, &e, 10);
2338 if (*buf && (*e == 0 || *e == '\n')) {
2339 atomic_set(&rdev->corrected_errors, n);
2340 return len;
2341 }
2342 return -EINVAL;
2343}
2344static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002345__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002346
NeilBrown014236d2006-01-06 00:20:55 -08002347static ssize_t
2348slot_show(mdk_rdev_t *rdev, char *page)
2349{
2350 if (rdev->raid_disk < 0)
2351 return sprintf(page, "none\n");
2352 else
2353 return sprintf(page, "%d\n", rdev->raid_disk);
2354}
2355
2356static ssize_t
2357slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2358{
2359 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002360 int err;
2361 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002362 int slot = simple_strtoul(buf, &e, 10);
2363 if (strncmp(buf, "none", 4)==0)
2364 slot = -1;
2365 else if (e==buf || (*e && *e!= '\n'))
2366 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002367 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002368 /* Setting 'slot' on an active array requires also
2369 * updating the 'rd%d' link, and communicating
2370 * with the personality with ->hot_*_disk.
2371 * For now we only support removing
2372 * failed/spare devices. This normally happens automatically,
2373 * but not when the metadata is externally managed.
2374 */
NeilBrownc303da62008-02-06 01:39:51 -08002375 if (rdev->raid_disk == -1)
2376 return -EEXIST;
2377 /* personality does all needed checks */
2378 if (rdev->mddev->pers->hot_add_disk == NULL)
2379 return -EINVAL;
2380 err = rdev->mddev->pers->
2381 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2382 if (err)
2383 return err;
2384 sprintf(nm, "rd%d", rdev->raid_disk);
2385 sysfs_remove_link(&rdev->mddev->kobj, nm);
Maciej Trelab7103102010-04-14 16:58:16 +10002386 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002387 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2388 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002389 } else if (rdev->mddev->pers) {
2390 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002391 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002392 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002393 */
2394
2395 if (rdev->raid_disk != -1)
2396 return -EBUSY;
2397
2398 if (rdev->mddev->pers->hot_add_disk == NULL)
2399 return -EINVAL;
2400
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002401 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002402 if (rdev2->raid_disk == slot)
2403 return -EEXIST;
2404
2405 rdev->raid_disk = slot;
2406 if (test_bit(In_sync, &rdev->flags))
2407 rdev->saved_raid_disk = slot;
2408 else
2409 rdev->saved_raid_disk = -1;
2410 err = rdev->mddev->pers->
2411 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002412 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002413 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002414 return err;
Neil Brown52664732008-06-28 08:31:44 +10002415 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002416 sysfs_notify_dirent_safe(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002417 sprintf(nm, "rd%d", rdev->raid_disk);
2418 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002419 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002420 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002421 } else {
2422 if (slot >= rdev->mddev->raid_disks)
2423 return -ENOSPC;
2424 rdev->raid_disk = slot;
2425 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002426 clear_bit(Faulty, &rdev->flags);
2427 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002428 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002429 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002430 }
NeilBrown014236d2006-01-06 00:20:55 -08002431 return len;
2432}
2433
2434
2435static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002436__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002437
NeilBrown93c8cad2006-01-06 00:20:56 -08002438static ssize_t
2439offset_show(mdk_rdev_t *rdev, char *page)
2440{
Andrew Morton6961ece2006-01-06 00:20:59 -08002441 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002442}
2443
2444static ssize_t
2445offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2446{
2447 char *e;
2448 unsigned long long offset = simple_strtoull(buf, &e, 10);
2449 if (e==buf || (*e && *e != '\n'))
2450 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002451 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002452 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002453 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002454 /* Must set offset before size, so overlap checks
2455 * can be sane */
2456 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002457 rdev->data_offset = offset;
2458 return len;
2459}
2460
2461static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002462__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002463
NeilBrown83303b62006-01-06 00:21:06 -08002464static ssize_t
2465rdev_size_show(mdk_rdev_t *rdev, char *page)
2466{
Andre Nolldd8ac332009-03-31 14:33:13 +11002467 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002468}
2469
NeilBrownc5d79ad2008-02-06 01:39:54 -08002470static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2471{
2472 /* check if two start/length pairs overlap */
2473 if (s1+l1 <= s2)
2474 return 0;
2475 if (s2+l2 <= s1)
2476 return 0;
2477 return 1;
2478}
2479
Dan Williamsb522adc2009-03-31 15:00:31 +11002480static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2481{
2482 unsigned long long blocks;
2483 sector_t new;
2484
2485 if (strict_strtoull(buf, 10, &blocks) < 0)
2486 return -EINVAL;
2487
2488 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2489 return -EINVAL; /* sector conversion overflow */
2490
2491 new = blocks * 2;
2492 if (new != blocks * 2)
2493 return -EINVAL; /* unsigned long long to sector_t overflow */
2494
2495 *sectors = new;
2496 return 0;
2497}
2498
NeilBrown83303b62006-01-06 00:21:06 -08002499static ssize_t
2500rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2501{
NeilBrown27c529b2008-03-04 14:29:33 -08002502 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002503 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002504 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002505
Dan Williamsb522adc2009-03-31 15:00:31 +11002506 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002507 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002508 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002509 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002510 sectors = super_types[my_mddev->major_version].
2511 rdev_size_change(rdev, sectors);
2512 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002513 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002514 } else if (!sectors)
2515 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2516 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002517 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002518 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002519 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002520
Andre Nolldd8ac332009-03-31 14:33:13 +11002521 rdev->sectors = sectors;
2522 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002523 /* need to check that all other rdevs with the same ->bdev
2524 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002525 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002526 * we have to change it back, we will have the lock again.
2527 */
2528 mddev_t *mddev;
2529 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002530 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002531
NeilBrown27c529b2008-03-04 14:29:33 -08002532 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002533 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002534 mdk_rdev_t *rdev2;
2535
2536 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002537 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002538 if (test_bit(AllReserved, &rdev2->flags) ||
2539 (rdev->bdev == rdev2->bdev &&
2540 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002541 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002542 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002543 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002544 overlap = 1;
2545 break;
2546 }
2547 mddev_unlock(mddev);
2548 if (overlap) {
2549 mddev_put(mddev);
2550 break;
2551 }
2552 }
NeilBrown27c529b2008-03-04 14:29:33 -08002553 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002554 if (overlap) {
2555 /* Someone else could have slipped in a size
2556 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002557 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002558 * safe, and trust userspace not to race with
2559 * itself
2560 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002561 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002562 return -EBUSY;
2563 }
2564 }
NeilBrown83303b62006-01-06 00:21:06 -08002565 return len;
2566}
2567
2568static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002569__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002570
Dan Williams06e3c812009-12-12 21:17:12 -07002571
2572static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2573{
2574 unsigned long long recovery_start = rdev->recovery_offset;
2575
2576 if (test_bit(In_sync, &rdev->flags) ||
2577 recovery_start == MaxSector)
2578 return sprintf(page, "none\n");
2579
2580 return sprintf(page, "%llu\n", recovery_start);
2581}
2582
2583static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2584{
2585 unsigned long long recovery_start;
2586
2587 if (cmd_match(buf, "none"))
2588 recovery_start = MaxSector;
2589 else if (strict_strtoull(buf, 10, &recovery_start))
2590 return -EINVAL;
2591
2592 if (rdev->mddev->pers &&
2593 rdev->raid_disk >= 0)
2594 return -EBUSY;
2595
2596 rdev->recovery_offset = recovery_start;
2597 if (recovery_start == MaxSector)
2598 set_bit(In_sync, &rdev->flags);
2599 else
2600 clear_bit(In_sync, &rdev->flags);
2601 return len;
2602}
2603
2604static struct rdev_sysfs_entry rdev_recovery_start =
2605__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2606
NeilBrown86e6ffd2005-11-08 21:39:24 -08002607static struct attribute *rdev_default_attrs[] = {
2608 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002609 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002610 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002611 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002612 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002613 &rdev_recovery_start.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002614 NULL,
2615};
2616static ssize_t
2617rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2618{
2619 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2620 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002621 mddev_t *mddev = rdev->mddev;
2622 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002623
2624 if (!entry->show)
2625 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002626
2627 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2628 if (!rv) {
2629 if (rdev->mddev == NULL)
2630 rv = -EBUSY;
2631 else
2632 rv = entry->show(rdev, page);
2633 mddev_unlock(mddev);
2634 }
2635 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002636}
2637
2638static ssize_t
2639rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2640 const char *page, size_t length)
2641{
2642 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2643 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002644 ssize_t rv;
2645 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002646
2647 if (!entry->store)
2648 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002649 if (!capable(CAP_SYS_ADMIN))
2650 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002651 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002652 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002653 if (rdev->mddev == NULL)
2654 rv = -EBUSY;
2655 else
2656 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002657 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002658 }
2659 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002660}
2661
2662static void rdev_free(struct kobject *ko)
2663{
2664 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2665 kfree(rdev);
2666}
Emese Revfy52cf25d2010-01-19 02:58:23 +01002667static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002668 .show = rdev_attr_show,
2669 .store = rdev_attr_store,
2670};
2671static struct kobj_type rdev_ktype = {
2672 .release = rdev_free,
2673 .sysfs_ops = &rdev_sysfs_ops,
2674 .default_attrs = rdev_default_attrs,
2675};
2676
NeilBrowne8bb9a82010-06-01 19:37:26 +10002677void md_rdev_init(mdk_rdev_t *rdev)
2678{
2679 rdev->desc_nr = -1;
2680 rdev->saved_raid_disk = -1;
2681 rdev->raid_disk = -1;
2682 rdev->flags = 0;
2683 rdev->data_offset = 0;
2684 rdev->sb_events = 0;
2685 rdev->last_read_error.tv_sec = 0;
2686 rdev->last_read_error.tv_nsec = 0;
2687 atomic_set(&rdev->nr_pending, 0);
2688 atomic_set(&rdev->read_errors, 0);
2689 atomic_set(&rdev->corrected_errors, 0);
2690
2691 INIT_LIST_HEAD(&rdev->same_set);
2692 init_waitqueue_head(&rdev->blocked_wait);
2693}
2694EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695/*
2696 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2697 *
2698 * mark the device faulty if:
2699 *
2700 * - the device is nonexistent (zero size)
2701 * - the device has no valid superblock
2702 *
2703 * a faulty rdev _never_ has rdev->sb set.
2704 */
2705static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2706{
2707 char b[BDEVNAME_SIZE];
2708 int err;
2709 mdk_rdev_t *rdev;
2710 sector_t size;
2711
NeilBrown9ffae0c2006-01-06 00:20:32 -08002712 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 if (!rdev) {
2714 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2715 return ERR_PTR(-ENOMEM);
2716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
NeilBrowne8bb9a82010-06-01 19:37:26 +10002718 md_rdev_init(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 if ((err = alloc_disk_sb(rdev)))
2720 goto abort_free;
2721
NeilBrownc5d79ad2008-02-06 01:39:54 -08002722 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 if (err)
2724 goto abort_free;
2725
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002726 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2729 if (!size) {
2730 printk(KERN_WARNING
2731 "md: %s has zero or unknown size, marking faulty!\n",
2732 bdevname(rdev->bdev,b));
2733 err = -EINVAL;
2734 goto abort_free;
2735 }
2736
2737 if (super_format >= 0) {
2738 err = super_types[super_format].
2739 load_super(rdev, NULL, super_minor);
2740 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002741 printk(KERN_WARNING
2742 "md: %s does not have a valid v%d.%d "
2743 "superblock, not importing!\n",
2744 bdevname(rdev->bdev,b),
2745 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 goto abort_free;
2747 }
2748 if (err < 0) {
2749 printk(KERN_WARNING
2750 "md: could not read %s's sb, not importing!\n",
2751 bdevname(rdev->bdev,b));
2752 goto abort_free;
2753 }
2754 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002755
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 return rdev;
2757
2758abort_free:
2759 if (rdev->sb_page) {
2760 if (rdev->bdev)
2761 unlock_rdev(rdev);
2762 free_disk_sb(rdev);
2763 }
2764 kfree(rdev);
2765 return ERR_PTR(err);
2766}
2767
2768/*
2769 * Check a full RAID array for plausibility
2770 */
2771
2772
NeilBrowna757e642005-04-16 15:26:42 -07002773static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774{
2775 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002776 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 char b[BDEVNAME_SIZE];
2778
2779 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002780 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 switch (super_types[mddev->major_version].
2782 load_super(rdev, freshest, mddev->minor_version)) {
2783 case 1:
2784 freshest = rdev;
2785 break;
2786 case 0:
2787 break;
2788 default:
2789 printk( KERN_ERR \
2790 "md: fatal superblock inconsistency in %s"
2791 " -- removing from array\n",
2792 bdevname(rdev->bdev,b));
2793 kick_rdev_from_array(rdev);
2794 }
2795
2796
2797 super_types[mddev->major_version].
2798 validate_super(mddev, freshest);
2799
2800 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002801 rdev_for_each(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10002802 if (mddev->max_disks &&
2803 (rdev->desc_nr >= mddev->max_disks ||
2804 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002805 printk(KERN_WARNING
2806 "md: %s: %s: only %d devices permitted\n",
2807 mdname(mddev), bdevname(rdev->bdev, b),
2808 mddev->max_disks);
2809 kick_rdev_from_array(rdev);
2810 continue;
2811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 if (rdev != freshest)
2813 if (super_types[mddev->major_version].
2814 validate_super(mddev, rdev)) {
2815 printk(KERN_WARNING "md: kicking non-fresh %s"
2816 " from array!\n",
2817 bdevname(rdev->bdev,b));
2818 kick_rdev_from_array(rdev);
2819 continue;
2820 }
2821 if (mddev->level == LEVEL_MULTIPATH) {
2822 rdev->desc_nr = i++;
2823 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002824 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002825 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002826 rdev->raid_disk = -1;
2827 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 }
2829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830}
2831
NeilBrown72e02072009-12-14 12:49:55 +11002832/* Read a fixed-point number.
2833 * Numbers in sysfs attributes should be in "standard" units where
2834 * possible, so time should be in seconds.
2835 * However we internally use a a much smaller unit such as
2836 * milliseconds or jiffies.
2837 * This function takes a decimal number with a possible fractional
2838 * component, and produces an integer which is the result of
2839 * multiplying that number by 10^'scale'.
2840 * all without any floating-point arithmetic.
2841 */
2842int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2843{
2844 unsigned long result = 0;
2845 long decimals = -1;
2846 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2847 if (*cp == '.')
2848 decimals = 0;
2849 else if (decimals < scale) {
2850 unsigned int value;
2851 value = *cp - '0';
2852 result = result * 10 + value;
2853 if (decimals >= 0)
2854 decimals++;
2855 }
2856 cp++;
2857 }
2858 if (*cp == '\n')
2859 cp++;
2860 if (*cp)
2861 return -EINVAL;
2862 if (decimals < 0)
2863 decimals = 0;
2864 while (decimals < scale) {
2865 result *= 10;
2866 decimals ++;
2867 }
2868 *res = result;
2869 return 0;
2870}
2871
2872
NeilBrown19052c02008-08-05 15:54:13 +10002873static void md_safemode_timeout(unsigned long data);
2874
NeilBrowneae17012005-11-08 21:39:23 -08002875static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002876safe_delay_show(mddev_t *mddev, char *page)
2877{
2878 int msec = (mddev->safemode_delay*1000)/HZ;
2879 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2880}
2881static ssize_t
2882safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2883{
NeilBrown16f17b32006-06-26 00:27:37 -07002884 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07002885
NeilBrown72e02072009-12-14 12:49:55 +11002886 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002887 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07002888 if (msec == 0)
2889 mddev->safemode_delay = 0;
2890 else {
NeilBrown19052c02008-08-05 15:54:13 +10002891 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002892 mddev->safemode_delay = (msec*HZ)/1000;
2893 if (mddev->safemode_delay == 0)
2894 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002895 if (mddev->safemode_delay < old_delay)
2896 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002897 }
2898 return len;
2899}
2900static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002901__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002902
2903static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002904level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002905{
NeilBrown2604b702006-01-06 00:20:36 -08002906 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002907 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002908 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002909 else if (mddev->clevel[0])
2910 return sprintf(page, "%s\n", mddev->clevel);
2911 else if (mddev->level != LEVEL_NONE)
2912 return sprintf(page, "%d\n", mddev->level);
2913 else
2914 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002915}
2916
NeilBrownd9d166c2006-01-06 00:20:51 -08002917static ssize_t
2918level_store(mddev_t *mddev, const char *buf, size_t len)
2919{
Dan Williamsf2859af2010-05-02 10:04:16 -07002920 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002921 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002922 struct mdk_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07002923 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11002924 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002925 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002926
2927 if (mddev->pers == NULL) {
2928 if (len == 0)
2929 return 0;
2930 if (len >= sizeof(mddev->clevel))
2931 return -ENOSPC;
2932 strncpy(mddev->clevel, buf, len);
2933 if (mddev->clevel[len-1] == '\n')
2934 len--;
2935 mddev->clevel[len] = 0;
2936 mddev->level = LEVEL_NONE;
2937 return rv;
2938 }
2939
2940 /* request to change the personality. Need to ensure:
2941 * - array is not engaged in resync/recovery/reshape
2942 * - old personality can be suspended
2943 * - new personality will access other array.
2944 */
2945
NeilBrownbb4f1e92010-08-08 21:18:03 +10002946 if (mddev->sync_thread ||
2947 mddev->reshape_position != MaxSector ||
2948 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08002949 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002950
2951 if (!mddev->pers->quiesce) {
2952 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2953 mdname(mddev), mddev->pers->name);
2954 return -EINVAL;
2955 }
2956
2957 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07002958 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11002959 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07002960 strncpy(clevel, buf, len);
2961 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002962 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07002963 clevel[len] = 0;
2964 if (strict_strtol(clevel, 10, &level))
2965 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11002966
Dan Williamsf2859af2010-05-02 10:04:16 -07002967 if (request_module("md-%s", clevel) != 0)
2968 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11002969 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07002970 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11002971 if (!pers || !try_module_get(pers->owner)) {
2972 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07002973 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11002974 return -EINVAL;
2975 }
2976 spin_unlock(&pers_lock);
2977
2978 if (pers == mddev->pers) {
2979 /* Nothing to do! */
2980 module_put(pers->owner);
2981 return rv;
2982 }
2983 if (!pers->takeover) {
2984 module_put(pers->owner);
2985 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07002986 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11002987 return -EINVAL;
2988 }
2989
NeilBrowne93f68a2010-06-15 09:36:03 +01002990 list_for_each_entry(rdev, &mddev->disks, same_set)
2991 rdev->new_raid_disk = rdev->raid_disk;
2992
NeilBrown245f46c2009-03-31 14:39:39 +11002993 /* ->takeover must set new_* and/or delta_disks
2994 * if it succeeds, and may set them when it fails.
2995 */
2996 priv = pers->takeover(mddev);
2997 if (IS_ERR(priv)) {
2998 mddev->new_level = mddev->level;
2999 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003000 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003001 mddev->raid_disks -= mddev->delta_disks;
3002 mddev->delta_disks = 0;
3003 module_put(pers->owner);
3004 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003005 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003006 return PTR_ERR(priv);
3007 }
3008
3009 /* Looks like we have a winner */
3010 mddev_suspend(mddev);
3011 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003012
3013 if (mddev->pers->sync_request == NULL &&
3014 pers->sync_request != NULL) {
3015 /* need to add the md_redundancy_group */
3016 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3017 printk(KERN_WARNING
3018 "md: cannot register extra attributes for %s\n",
3019 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003020 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003021 }
3022 if (mddev->pers->sync_request != NULL &&
3023 pers->sync_request == NULL) {
3024 /* need to remove the md_redundancy_group */
3025 if (mddev->to_remove == NULL)
3026 mddev->to_remove = &md_redundancy_group;
3027 }
3028
Trela Maciej54071b32010-03-08 16:02:42 +11003029 if (mddev->pers->sync_request == NULL &&
3030 mddev->external) {
3031 /* We are converting from a no-redundancy array
3032 * to a redundancy array and metadata is managed
3033 * externally so we need to be sure that writes
3034 * won't block due to a need to transition
3035 * clean->dirty
3036 * until external management is started.
3037 */
3038 mddev->in_sync = 0;
3039 mddev->safemode_delay = 0;
3040 mddev->safemode = 0;
3041 }
3042
NeilBrowne93f68a2010-06-15 09:36:03 +01003043 list_for_each_entry(rdev, &mddev->disks, same_set) {
3044 char nm[20];
3045 if (rdev->raid_disk < 0)
3046 continue;
3047 if (rdev->new_raid_disk > mddev->raid_disks)
3048 rdev->new_raid_disk = -1;
3049 if (rdev->new_raid_disk == rdev->raid_disk)
3050 continue;
3051 sprintf(nm, "rd%d", rdev->raid_disk);
3052 sysfs_remove_link(&mddev->kobj, nm);
3053 }
3054 list_for_each_entry(rdev, &mddev->disks, same_set) {
3055 if (rdev->raid_disk < 0)
3056 continue;
3057 if (rdev->new_raid_disk == rdev->raid_disk)
3058 continue;
3059 rdev->raid_disk = rdev->new_raid_disk;
3060 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003061 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003062 else {
3063 char nm[20];
3064 sprintf(nm, "rd%d", rdev->raid_disk);
3065 if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3066 printk("md: cannot register %s for %s after level change\n",
3067 nm, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003068 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003069 }
3070
3071 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003072 mddev->pers = pers;
3073 mddev->private = priv;
3074 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3075 mddev->level = mddev->new_level;
3076 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003077 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003078 mddev->delta_disks = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003079 if (mddev->pers->sync_request == NULL) {
3080 /* this is now an array without redundancy, so
3081 * it must always be in_sync
3082 */
3083 mddev->in_sync = 1;
3084 del_timer_sync(&mddev->safemode_timer);
3085 }
NeilBrown245f46c2009-03-31 14:39:39 +11003086 pers->run(mddev);
3087 mddev_resume(mddev);
3088 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3089 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3090 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003091 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003092 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003093 return rv;
3094}
3095
3096static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003097__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003098
NeilBrownd4dbd022006-06-26 00:27:59 -07003099
3100static ssize_t
3101layout_show(mddev_t *mddev, char *page)
3102{
3103 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003104 if (mddev->reshape_position != MaxSector &&
3105 mddev->layout != mddev->new_layout)
3106 return sprintf(page, "%d (%d)\n",
3107 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003108 return sprintf(page, "%d\n", mddev->layout);
3109}
3110
3111static ssize_t
3112layout_store(mddev_t *mddev, const char *buf, size_t len)
3113{
3114 char *e;
3115 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003116
3117 if (!*buf || (*e && *e != '\n'))
3118 return -EINVAL;
3119
NeilBrownb3546032009-03-31 14:56:41 +11003120 if (mddev->pers) {
3121 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003122 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003123 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003124 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003125 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003126 if (err) {
3127 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003128 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003129 }
NeilBrownb3546032009-03-31 14:56:41 +11003130 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003131 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003132 if (mddev->reshape_position == MaxSector)
3133 mddev->layout = n;
3134 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003135 return len;
3136}
3137static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003138__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003139
3140
NeilBrowneae17012005-11-08 21:39:23 -08003141static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003142raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003143{
NeilBrownbb636542005-11-08 21:39:45 -08003144 if (mddev->raid_disks == 0)
3145 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003146 if (mddev->reshape_position != MaxSector &&
3147 mddev->delta_disks != 0)
3148 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3149 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003150 return sprintf(page, "%d\n", mddev->raid_disks);
3151}
3152
NeilBrownda943b992006-01-06 00:20:54 -08003153static int update_raid_disks(mddev_t *mddev, int raid_disks);
3154
3155static ssize_t
3156raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3157{
NeilBrownda943b992006-01-06 00:20:54 -08003158 char *e;
3159 int rv = 0;
3160 unsigned long n = simple_strtoul(buf, &e, 10);
3161
3162 if (!*buf || (*e && *e != '\n'))
3163 return -EINVAL;
3164
3165 if (mddev->pers)
3166 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003167 else if (mddev->reshape_position != MaxSector) {
3168 int olddisks = mddev->raid_disks - mddev->delta_disks;
3169 mddev->delta_disks = n - olddisks;
3170 mddev->raid_disks = n;
3171 } else
NeilBrownda943b992006-01-06 00:20:54 -08003172 mddev->raid_disks = n;
3173 return rv ? rv : len;
3174}
3175static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003176__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003177
NeilBrown24dd4692005-11-08 21:39:26 -08003178static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003179chunk_size_show(mddev_t *mddev, char *page)
3180{
NeilBrown08a02ec2007-05-09 02:35:38 -07003181 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003182 mddev->chunk_sectors != mddev->new_chunk_sectors)
3183 return sprintf(page, "%d (%d)\n",
3184 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003185 mddev->chunk_sectors << 9);
3186 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003187}
3188
3189static ssize_t
3190chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3191{
NeilBrown3b343802006-01-06 00:20:47 -08003192 char *e;
3193 unsigned long n = simple_strtoul(buf, &e, 10);
3194
NeilBrown3b343802006-01-06 00:20:47 -08003195 if (!*buf || (*e && *e != '\n'))
3196 return -EINVAL;
3197
NeilBrownb3546032009-03-31 14:56:41 +11003198 if (mddev->pers) {
3199 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003200 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003201 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003202 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003203 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003204 if (err) {
3205 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003206 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003207 }
NeilBrownb3546032009-03-31 14:56:41 +11003208 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003209 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003210 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003211 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003212 }
NeilBrown3b343802006-01-06 00:20:47 -08003213 return len;
3214}
3215static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003216__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003217
NeilBrowna94213b2006-06-26 00:28:00 -07003218static ssize_t
3219resync_start_show(mddev_t *mddev, char *page)
3220{
NeilBrownd1a7c502009-03-31 15:24:32 +11003221 if (mddev->recovery_cp == MaxSector)
3222 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003223 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3224}
3225
3226static ssize_t
3227resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3228{
NeilBrowna94213b2006-06-26 00:28:00 -07003229 char *e;
3230 unsigned long long n = simple_strtoull(buf, &e, 10);
3231
3232 if (mddev->pers)
3233 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003234 if (cmd_match(buf, "none"))
3235 n = MaxSector;
3236 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003237 return -EINVAL;
3238
3239 mddev->recovery_cp = n;
3240 return len;
3241}
3242static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003243__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003244
NeilBrown9e653b62006-06-26 00:27:58 -07003245/*
3246 * The array state can be:
3247 *
3248 * clear
3249 * No devices, no size, no level
3250 * Equivalent to STOP_ARRAY ioctl
3251 * inactive
3252 * May have some settings, but array is not active
3253 * all IO results in error
3254 * When written, doesn't tear down array, but just stops it
3255 * suspended (not supported yet)
3256 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003257 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003258 * readonly
3259 * no resync can happen. no superblocks get written.
3260 * write requests fail
3261 * read-auto
3262 * like readonly, but behaves like 'clean' on a write request.
3263 *
3264 * clean - no pending writes, but otherwise active.
3265 * When written to inactive array, starts without resync
3266 * If a write request arrives then
3267 * if metadata is known, mark 'dirty' and switch to 'active'.
3268 * if not known, block and switch to write-pending
3269 * If written to an active array that has pending writes, then fails.
3270 * active
3271 * fully active: IO and resync can be happening.
3272 * When written to inactive array, starts with resync
3273 *
3274 * write-pending
3275 * clean, but writes are blocked waiting for 'active' to be written.
3276 *
3277 * active-idle
3278 * like active, but no writes have been seen for a while (100msec).
3279 *
3280 */
3281enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3282 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003283static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003284 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3285 "write-pending", "active-idle", NULL };
3286
3287static int match_word(const char *word, char **list)
3288{
3289 int n;
3290 for (n=0; list[n]; n++)
3291 if (cmd_match(word, list[n]))
3292 break;
3293 return n;
3294}
3295
3296static ssize_t
3297array_state_show(mddev_t *mddev, char *page)
3298{
3299 enum array_state st = inactive;
3300
3301 if (mddev->pers)
3302 switch(mddev->ro) {
3303 case 1:
3304 st = readonly;
3305 break;
3306 case 2:
3307 st = read_auto;
3308 break;
3309 case 0:
3310 if (mddev->in_sync)
3311 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003312 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003313 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003314 else if (mddev->safemode)
3315 st = active_idle;
3316 else
3317 st = active;
3318 }
3319 else {
3320 if (list_empty(&mddev->disks) &&
3321 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003322 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003323 st = clear;
3324 else
3325 st = inactive;
3326 }
3327 return sprintf(page, "%s\n", array_states[st]);
3328}
3329
Neil Browndf5b20c2008-07-11 22:02:22 +10003330static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrowna4bd82d2010-03-29 13:23:10 +11003331static int md_set_readonly(mddev_t * mddev, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003332static int do_md_run(mddev_t * mddev);
3333static int restart_array(mddev_t *mddev);
3334
3335static ssize_t
3336array_state_store(mddev_t *mddev, const char *buf, size_t len)
3337{
3338 int err = -EINVAL;
3339 enum array_state st = match_word(buf, array_states);
3340 switch(st) {
3341 case bad_word:
3342 break;
3343 case clear:
3344 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003345 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003346 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003347 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003348 break;
3349 case inactive:
3350 /* stopping an active array */
3351 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003352 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003353 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003354 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003355 } else
3356 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003357 break;
3358 case suspended:
3359 break; /* not supported yet */
3360 case readonly:
3361 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003362 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003363 else {
3364 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003365 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003366 err = do_md_run(mddev);
3367 }
3368 break;
3369 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003370 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003371 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003372 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003373 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003374 err = restart_array(mddev);
3375 if (err == 0) {
3376 mddev->ro = 2;
3377 set_disk_ro(mddev->gendisk, 0);
3378 }
NeilBrown9e653b62006-06-26 00:27:58 -07003379 } else {
3380 mddev->ro = 2;
3381 err = do_md_run(mddev);
3382 }
3383 break;
3384 case clean:
3385 if (mddev->pers) {
3386 restart_array(mddev);
3387 spin_lock_irq(&mddev->write_lock);
3388 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003389 if (mddev->in_sync == 0) {
3390 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003391 if (mddev->safemode == 1)
3392 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003393 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003394 }
3395 err = 0;
3396 } else
3397 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003398 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003399 } else
3400 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003401 break;
3402 case active:
3403 if (mddev->pers) {
3404 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003405 clear_bit(MD_CHANGE_PENDING, &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
Tejun Heoe804ac72010-10-15 15:36:08 +02004196 /* wait for any previous instance of this device to be
4197 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004198 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004199 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004200
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
NeilBrownd3374822009-01-09 08:31:10 +11005889 if (mddev->gendisk != bdev->bd_disk) {
5890 /* we are racing with mddev_put which is discarding this
5891 * bd_disk.
5892 */
5893 mddev_put(mddev);
5894 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02005895 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11005896 /* Then retry the open from the top */
5897 return -ERESTARTSYS;
5898 }
5899 BUG_ON(mddev != bdev->bd_disk->private_data);
5900
NeilBrownc8c00a62009-08-10 12:50:52 +10005901 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902 goto out;
5903
5904 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005905 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005906 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907
NeilBrownf3b99be2010-06-24 13:31:03 +10005908 check_disk_size_change(mddev->gendisk, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 out:
5910 return err;
5911}
5912
Al Viroa39907f2008-03-02 10:31:15 -05005913static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914{
Al Viroa39907f2008-03-02 10:31:15 -05005915 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005917 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005918 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 mddev_put(mddev);
5920
5921 return 0;
5922}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005923static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924{
5925 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005926 .open = md_open,
5927 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005928 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005929#ifdef CONFIG_COMPAT
5930 .compat_ioctl = md_compat_ioctl,
5931#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005932 .getgeo = md_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933};
5934
Adrian Bunk75c96f82005-05-05 16:16:09 -07005935static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936{
5937 mdk_thread_t *thread = arg;
5938
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 /*
5940 * md_thread is a 'system-thread', it's priority should be very
5941 * high. We avoid resource deadlocks individually in each
5942 * raid personality. (RAID5 does preallocation) We also use RR and
5943 * the very same RT priority as kswapd, thus we will never get
5944 * into a priority inversion deadlock.
5945 *
5946 * we definitely have to have equal or higher priority than
5947 * bdflush, otherwise bdflush will deadlock if there are too
5948 * many dirty RAID5 blocks.
5949 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950
NeilBrown6985c432005-10-19 21:23:47 -07005951 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005952 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953
NeilBrown93588e22005-11-15 00:09:12 -08005954 /* We need to wait INTERRUPTIBLE so that
5955 * we don't add to the load-average.
5956 * That means we need to be sure no signals are
5957 * pending
5958 */
5959 if (signal_pending(current))
5960 flush_signals(current);
5961
5962 wait_event_interruptible_timeout
5963 (thread->wqueue,
5964 test_bit(THREAD_WAKEUP, &thread->flags)
5965 || kthread_should_stop(),
5966 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967
5968 clear_bit(THREAD_WAKEUP, &thread->flags);
5969
NeilBrown787453c2005-11-08 21:39:43 -08005970 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005972
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973 return 0;
5974}
5975
5976void md_wakeup_thread(mdk_thread_t *thread)
5977{
5978 if (thread) {
5979 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5980 set_bit(THREAD_WAKEUP, &thread->flags);
5981 wake_up(&thread->wqueue);
5982 }
5983}
5984
5985mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5986 const char *name)
5987{
5988 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989
NeilBrown9ffae0c2006-01-06 00:20:32 -08005990 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 if (!thread)
5992 return NULL;
5993
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994 init_waitqueue_head(&thread->wqueue);
5995
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 thread->run = run;
5997 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005998 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10005999 thread->tsk = kthread_run(md_thread, thread,
6000 "%s_%s",
6001 mdname(thread->mddev),
6002 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006003 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 kfree(thread);
6005 return NULL;
6006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 return thread;
6008}
6009
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010void md_unregister_thread(mdk_thread_t *thread)
6011{
NeilBrowne0cf8f02009-03-31 14:39:39 +11006012 if (!thread)
6013 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07006014 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07006015
6016 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 kfree(thread);
6018}
6019
6020void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6021{
6022 if (!mddev) {
6023 MD_BUG();
6024 return;
6025 }
6026
NeilBrownb2d444d2005-11-08 21:39:31 -08006027 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006029
6030 if (mddev->external)
6031 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07006032/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6034 mdname(mddev),
6035 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6036 __builtin_return_address(0),__builtin_return_address(1),
6037 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07006038*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07006039 if (!mddev->pers)
6040 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041 if (!mddev->pers->error_handler)
6042 return;
6043 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006044 if (mddev->degraded)
6045 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006046 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6048 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6049 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006050 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006051 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006052 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053}
6054
6055/* seq_file implementation /proc/mdstat */
6056
6057static void status_unused(struct seq_file *seq)
6058{
6059 int i = 0;
6060 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061
6062 seq_printf(seq, "unused devices: ");
6063
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006064 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 char b[BDEVNAME_SIZE];
6066 i++;
6067 seq_printf(seq, "%s ",
6068 bdevname(rdev->bdev,b));
6069 }
6070 if (!i)
6071 seq_printf(seq, "<none>");
6072
6073 seq_printf(seq, "\n");
6074}
6075
6076
6077static void status_resync(struct seq_file *seq, mddev_t * mddev)
6078{
NeilBrowndd71cf62009-05-07 12:49:35 +10006079 sector_t max_sectors, resync, res;
6080 unsigned long dt, db;
6081 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006082 int scale;
6083 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084
NeilBrowndd71cf62009-05-07 12:49:35 +10006085 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086
6087 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006088 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006090 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091
6092 /*
6093 * Should not happen.
6094 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006095 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 MD_BUG();
6097 return;
6098 }
NeilBrown4588b422006-03-27 01:18:04 -08006099 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006100 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006101 * u32, as those are the requirements for sector_div.
6102 * Thus 'scale' must be at least 10
6103 */
6104 scale = 10;
6105 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006106 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006107 scale++;
6108 }
6109 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006110 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006111
6112 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 {
NeilBrown4588b422006-03-27 01:18:04 -08006114 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 seq_printf(seq, "[");
6116 for (i = 0; i < x; i++)
6117 seq_printf(seq, "=");
6118 seq_printf(seq, ">");
6119 for (i = 0; i < y; i++)
6120 seq_printf(seq, ".");
6121 seq_printf(seq, "] ");
6122 }
NeilBrown4588b422006-03-27 01:18:04 -08006123 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006124 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6125 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006126 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6127 "check" :
6128 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6129 "resync" : "recovery"))),
6130 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006131 (unsigned long long) resync/2,
6132 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133
6134 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 * dt: time from mark until now
6136 * db: blocks written from mark until now
6137 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006138 *
6139 * rt is a sector_t, so could be 32bit or 64bit.
6140 * So we divide before multiply in case it is 32bit and close
6141 * to the limit.
6142 * We scale the divisor (db) by 32 to avoid loosing precision
6143 * near the end of resync when the number of remaining sectors
6144 * is close to 'db'.
6145 * We then divide rt by 32 after multiplying by db to compensate.
6146 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 */
6148 dt = ((jiffies - mddev->resync_mark) / HZ);
6149 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006150 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6151 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152
NeilBrowndd71cf62009-05-07 12:49:35 +10006153 rt = max_sectors - resync; /* number of remaining sectors */
6154 sector_div(rt, db/32+1);
6155 rt *= dt;
6156 rt >>= 5;
6157
6158 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6159 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160
NeilBrownff4e8d92006-07-10 04:44:16 -07006161 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162}
6163
6164static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6165{
6166 struct list_head *tmp;
6167 loff_t l = *pos;
6168 mddev_t *mddev;
6169
6170 if (l >= 0x10000)
6171 return NULL;
6172 if (!l--)
6173 /* header */
6174 return (void*)1;
6175
6176 spin_lock(&all_mddevs_lock);
6177 list_for_each(tmp,&all_mddevs)
6178 if (!l--) {
6179 mddev = list_entry(tmp, mddev_t, all_mddevs);
6180 mddev_get(mddev);
6181 spin_unlock(&all_mddevs_lock);
6182 return mddev;
6183 }
6184 spin_unlock(&all_mddevs_lock);
6185 if (!l--)
6186 return (void*)2;/* tail */
6187 return NULL;
6188}
6189
6190static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6191{
6192 struct list_head *tmp;
6193 mddev_t *next_mddev, *mddev = v;
6194
6195 ++*pos;
6196 if (v == (void*)2)
6197 return NULL;
6198
6199 spin_lock(&all_mddevs_lock);
6200 if (v == (void*)1)
6201 tmp = all_mddevs.next;
6202 else
6203 tmp = mddev->all_mddevs.next;
6204 if (tmp != &all_mddevs)
6205 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6206 else {
6207 next_mddev = (void*)2;
6208 *pos = 0x10000;
6209 }
6210 spin_unlock(&all_mddevs_lock);
6211
6212 if (v != (void*)1)
6213 mddev_put(mddev);
6214 return next_mddev;
6215
6216}
6217
6218static void md_seq_stop(struct seq_file *seq, void *v)
6219{
6220 mddev_t *mddev = v;
6221
6222 if (mddev && v != (void*)1 && v != (void*)2)
6223 mddev_put(mddev);
6224}
6225
NeilBrownd7603b72006-01-06 00:20:30 -08006226struct mdstat_info {
6227 int event;
6228};
6229
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230static int md_seq_show(struct seq_file *seq, void *v)
6231{
6232 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006233 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006235 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006236 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237
6238 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006239 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240 seq_printf(seq, "Personalities : ");
6241 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006242 list_for_each_entry(pers, &pers_list, list)
6243 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244
6245 spin_unlock(&pers_lock);
6246 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006247 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 return 0;
6249 }
6250 if (v == (void*)2) {
6251 status_unused(seq);
6252 return 0;
6253 }
6254
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006255 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006257
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6259 seq_printf(seq, "%s : %sactive", mdname(mddev),
6260 mddev->pers ? "" : "in");
6261 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006262 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006264 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006265 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266 seq_printf(seq, " %s", mddev->pers->name);
6267 }
6268
Andre Nolldd8ac332009-03-31 14:33:13 +11006269 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006270 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 char b[BDEVNAME_SIZE];
6272 seq_printf(seq, " %s[%d]",
6273 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006274 if (test_bit(WriteMostly, &rdev->flags))
6275 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006276 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 seq_printf(seq, "(F)");
6278 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006279 } else if (rdev->raid_disk < 0)
6280 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006281 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282 }
6283
6284 if (!list_empty(&mddev->disks)) {
6285 if (mddev->pers)
6286 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006287 (unsigned long long)
6288 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289 else
6290 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006291 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006293 if (mddev->persistent) {
6294 if (mddev->major_version != 0 ||
6295 mddev->minor_version != 90) {
6296 seq_printf(seq," super %d.%d",
6297 mddev->major_version,
6298 mddev->minor_version);
6299 }
NeilBrowne6910632008-02-06 01:39:51 -08006300 } else if (mddev->external)
6301 seq_printf(seq, " super external:%s",
6302 mddev->metadata_type);
6303 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006304 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305
6306 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006307 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006309 if (mddev->pers->sync_request) {
6310 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006311 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006312 seq_printf(seq, "\n ");
6313 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6314 seq_printf(seq, "\tresync=DELAYED\n ");
6315 else if (mddev->recovery_cp < MaxSector)
6316 seq_printf(seq, "\tresync=PENDING\n ");
6317 }
NeilBrown32a76272005-06-21 17:17:14 -07006318 } else
6319 seq_printf(seq, "\n ");
6320
6321 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006322 unsigned long chunk_kb;
6323 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006324 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006325 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006326 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6327 "%lu%s chunk",
6328 bitmap->pages - bitmap->missing_pages,
6329 bitmap->pages,
6330 (bitmap->pages - bitmap->missing_pages)
6331 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006332 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006333 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006334 if (bitmap->file) {
6335 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006336 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006337 }
NeilBrown78d742d2005-06-21 17:17:15 -07006338
NeilBrown32a76272005-06-21 17:17:14 -07006339 seq_printf(seq, "\n");
6340 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 }
6342
6343 seq_printf(seq, "\n");
6344 }
6345 mddev_unlock(mddev);
6346
6347 return 0;
6348}
6349
Jan Engelhardt110518b2009-05-07 12:49:37 +10006350static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 .start = md_seq_start,
6352 .next = md_seq_next,
6353 .stop = md_seq_stop,
6354 .show = md_seq_show,
6355};
6356
6357static int md_seq_open(struct inode *inode, struct file *file)
6358{
6359 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006360 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6361 if (mi == NULL)
6362 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363
6364 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006365 if (error)
6366 kfree(mi);
6367 else {
6368 struct seq_file *p = file->private_data;
6369 p->private = mi;
6370 mi->event = atomic_read(&md_event_count);
6371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372 return error;
6373}
6374
NeilBrownd7603b72006-01-06 00:20:30 -08006375static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6376{
6377 struct seq_file *m = filp->private_data;
6378 struct mdstat_info *mi = m->private;
6379 int mask;
6380
6381 poll_wait(filp, &md_event_waiters, wait);
6382
6383 /* always allow read */
6384 mask = POLLIN | POLLRDNORM;
6385
6386 if (mi->event != atomic_read(&md_event_count))
6387 mask |= POLLERR | POLLPRI;
6388 return mask;
6389}
6390
Arjan van de Venfa027c22007-02-12 00:55:33 -08006391static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006392 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393 .open = md_seq_open,
6394 .read = seq_read,
6395 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006396 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006397 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398};
6399
NeilBrown2604b702006-01-06 00:20:36 -08006400int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006403 list_add_tail(&p->list, &pers_list);
6404 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405 spin_unlock(&pers_lock);
6406 return 0;
6407}
6408
NeilBrown2604b702006-01-06 00:20:36 -08006409int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410{
NeilBrown2604b702006-01-06 00:20:36 -08006411 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006413 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414 spin_unlock(&pers_lock);
6415 return 0;
6416}
6417
NeilBrowneea1bf32009-03-31 14:27:02 +11006418static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419{
6420 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006422 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423
6424 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006425 rcu_read_lock();
6426 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006428 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6429 (int)part_stat_read(&disk->part0, sectors[1]) -
6430 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006431 /* sync IO will cause sync_io to increase before the disk_stats
6432 * as sync_io is counted when a request starts, and
6433 * disk_stats is counted when it completes.
6434 * So resync activity will cause curr_events to be smaller than
6435 * when there was no such activity.
6436 * non-sync IO will cause disk_stat to increase without
6437 * increasing sync_io so curr_events will (eventually)
6438 * be larger than it was before. Once it becomes
6439 * substantially larger, the test below will cause
6440 * the array to appear non-idle, and resync will slow
6441 * down.
6442 * If there is a lot of outstanding resync activity when
6443 * we set last_event to curr_events, then all that activity
6444 * completing might cause the array to appear non-idle
6445 * and resync will be slowed down even though there might
6446 * not have been non-resync activity. This will only
6447 * happen once though. 'last_events' will soon reflect
6448 * the state where there is little or no outstanding
6449 * resync requests, and further resync activity will
6450 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006451 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006453 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454 rdev->last_events = curr_events;
6455 idle = 0;
6456 }
6457 }
NeilBrown4b809912008-07-21 17:05:25 +10006458 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459 return idle;
6460}
6461
6462void md_done_sync(mddev_t *mddev, int blocks, int ok)
6463{
6464 /* another "blocks" (512byte) blocks have been synced */
6465 atomic_sub(blocks, &mddev->recovery_active);
6466 wake_up(&mddev->recovery_wait);
6467 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006468 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469 md_wakeup_thread(mddev->thread);
6470 // stop recovery, signal do_sync ....
6471 }
6472}
6473
6474
NeilBrown06d91a52005-06-21 17:17:12 -07006475/* md_write_start(mddev, bi)
6476 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006477 * in superblock) before writing, schedule a superblock update
6478 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006479 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006480void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481{
Neil Brown0fd62b82008-06-28 08:31:36 +10006482 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006483 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006484 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006485
NeilBrownf91de922005-11-08 21:39:36 -08006486 BUG_ON(mddev->ro == 1);
6487 if (mddev->ro == 2) {
6488 /* need to switch to read/write */
6489 mddev->ro = 0;
6490 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6491 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006492 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006493 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006494 }
NeilBrown06d91a52005-06-21 17:17:12 -07006495 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006496 if (mddev->safemode == 1)
6497 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006498 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006499 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006500 if (mddev->in_sync) {
6501 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006502 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006503 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006504 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006505 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006506 }
NeilBrowna9701a32005-11-08 21:39:34 -08006507 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006508 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006509 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10006510 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006511 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07006512 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513}
6514
6515void md_write_end(mddev_t *mddev)
6516{
6517 if (atomic_dec_and_test(&mddev->writes_pending)) {
6518 if (mddev->safemode == 2)
6519 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006520 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6522 }
6523}
6524
NeilBrown2a2275d2007-01-26 00:57:11 -08006525/* md_allow_write(mddev)
6526 * Calling this ensures that the array is marked 'active' so that writes
6527 * may proceed without blocking. It is important to call this before
6528 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6529 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006530 *
6531 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6532 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006533 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006534int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006535{
6536 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006537 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006538 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006539 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006540 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006541 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006542
6543 spin_lock_irq(&mddev->write_lock);
6544 if (mddev->in_sync) {
6545 mddev->in_sync = 0;
6546 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006547 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08006548 if (mddev->safemode_delay &&
6549 mddev->safemode == 0)
6550 mddev->safemode = 1;
6551 spin_unlock_irq(&mddev->write_lock);
6552 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006553 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006554 } else
6555 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006556
NeilBrown070dc6d2010-08-30 17:33:34 +10006557 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07006558 return -EAGAIN;
6559 else
6560 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006561}
6562EXPORT_SYMBOL_GPL(md_allow_write);
6563
NeilBrownb63d7c22010-06-01 19:37:33 +10006564void md_unplug(mddev_t *mddev)
NeilBrown252ac522010-06-01 19:37:29 +10006565{
6566 if (mddev->queue)
6567 blk_unplug(mddev->queue);
6568 if (mddev->plug)
6569 mddev->plug->unplug_fn(mddev->plug);
6570}
6571
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572#define SYNC_MARKS 10
6573#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006574void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575{
6576 mddev_t *mddev2;
6577 unsigned int currspeed = 0,
6578 window;
NeilBrown57afd892005-06-21 17:17:13 -07006579 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580 unsigned long mark[SYNC_MARKS];
6581 sector_t mark_cnt[SYNC_MARKS];
6582 int last_mark,m;
6583 struct list_head *tmp;
6584 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006585 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006586 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006587 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588
6589 /* just incase thread restarts... */
6590 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6591 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006592 if (mddev->ro) /* never try to sync a read-only array */
6593 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594
NeilBrown61df9d92006-10-03 01:15:57 -07006595 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6596 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6597 desc = "data-check";
6598 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6599 desc = "requested-resync";
6600 else
6601 desc = "resync";
6602 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6603 desc = "reshape";
6604 else
6605 desc = "recovery";
6606
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607 /* we overload curr_resync somewhat here.
6608 * 0 == not engaged in resync at all
6609 * 2 == checking that there is no conflict with another sync
6610 * 1 == like 2, but have yielded to allow conflicting resync to
6611 * commense
6612 * other == active in resync - this many blocks
6613 *
6614 * Before starting a resync we must have set curr_resync to
6615 * 2, and then checked that every "conflicting" array has curr_resync
6616 * less than ours. When we find one that is the same or higher
6617 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6618 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6619 * This will mean we have to start checking from the beginning again.
6620 *
6621 */
6622
6623 do {
6624 mddev->curr_resync = 2;
6625
6626 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006627 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006628 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006629
6630 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006632 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633 if (mddev2 == mddev)
6634 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006635 if (!mddev->parallel_resync
6636 && mddev2->curr_resync
6637 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638 DEFINE_WAIT(wq);
6639 if (mddev < mddev2 && mddev->curr_resync == 2) {
6640 /* arbitrarily yield */
6641 mddev->curr_resync = 1;
6642 wake_up(&resync_wait);
6643 }
6644 if (mddev > mddev2 && mddev->curr_resync == 1)
6645 /* no need to wait here, we can wait the next
6646 * time 'round when curr_resync == 2
6647 */
6648 continue;
NeilBrown97441972008-09-19 11:49:54 +10006649 /* We need to wait 'interruptible' so as not to
6650 * contribute to the load average, and not to
6651 * be caught by 'softlockup'
6652 */
6653 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006654 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006655 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006656 printk(KERN_INFO "md: delaying %s of %s"
6657 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006659 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006661 if (signal_pending(current))
6662 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663 schedule();
6664 finish_wait(&resync_wait, &wq);
6665 goto try_again;
6666 }
6667 finish_wait(&resync_wait, &wq);
6668 }
6669 }
6670 } while (mddev->curr_resync < 2);
6671
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006672 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006673 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006674 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006675 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676 */
6677 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006678 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006679 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006680 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6681 j = mddev->resync_min;
6682 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006683 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006684
NeilBrownccfcc3c2006-03-27 01:18:09 -08006685 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006686 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006687 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006688 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006689 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006690 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006691 rcu_read_lock();
6692 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006693 if (rdev->raid_disk >= 0 &&
6694 !test_bit(Faulty, &rdev->flags) &&
6695 !test_bit(In_sync, &rdev->flags) &&
6696 rdev->recovery_offset < j)
6697 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006698 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006700
NeilBrown61df9d92006-10-03 01:15:57 -07006701 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6702 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6703 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006704 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006705 "(but not more than %d KB/sec) for %s.\n",
6706 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707
NeilBrowneea1bf32009-03-31 14:27:02 +11006708 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006709
NeilBrown57afd892005-06-21 17:17:13 -07006710 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711 for (m = 0; m < SYNC_MARKS; m++) {
6712 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006713 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714 }
6715 last_mark = 0;
6716 mddev->resync_mark = mark[last_mark];
6717 mddev->resync_mark_cnt = mark_cnt[last_mark];
6718
6719 /*
6720 * Tune reconstruction:
6721 */
6722 window = 32*(PAGE_SIZE/512);
6723 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6724 window/2,(unsigned long long) max_sectors/2);
6725
6726 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727 last_check = 0;
6728
6729 if (j>2) {
6730 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006731 "md: resuming %s of %s from checkpoint.\n",
6732 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733 mddev->curr_resync = j;
6734 }
NeilBrownefa59332009-12-14 12:49:49 +11006735 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736
6737 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006738 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739
NeilBrown57afd892005-06-21 17:17:13 -07006740 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006741
NeilBrown7a91ee12009-05-26 12:57:21 +10006742 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6743 ((mddev->curr_resync > mddev->curr_resync_completed &&
6744 (mddev->curr_resync - mddev->curr_resync_completed)
6745 > (max_sectors >> 4)) ||
6746 (j - mddev->curr_resync_completed)*2
6747 >= mddev->resync_max - mddev->curr_resync_completed
6748 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006749 /* time to update curr_resync_completed */
NeilBrown252ac522010-06-01 19:37:29 +10006750 md_unplug(mddev);
NeilBrown97e4f422009-03-31 14:33:13 +11006751 wait_event(mddev->recovery_wait,
6752 atomic_read(&mddev->recovery_active) == 0);
6753 mddev->curr_resync_completed =
6754 mddev->curr_resync;
NeilBrown070dc6d2010-08-30 17:33:34 +10006755 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006756 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006757 }
NeilBrownacb180b2009-04-14 16:28:34 +10006758
NeilBrowne62e58a2009-07-01 13:15:35 +10006759 while (j >= mddev->resync_max && !kthread_should_stop()) {
6760 /* As this condition is controlled by user-space,
6761 * we can block indefinitely, so use '_interruptible'
6762 * to avoid triggering warnings.
6763 */
6764 flush_signals(current); /* just in case */
6765 wait_event_interruptible(mddev->recovery_wait,
6766 mddev->resync_max > j
6767 || kthread_should_stop());
6768 }
NeilBrownacb180b2009-04-14 16:28:34 +10006769
6770 if (kthread_should_stop())
6771 goto interrupted;
6772
NeilBrown57afd892005-06-21 17:17:13 -07006773 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006774 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006775 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006776 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777 goto out;
6778 }
NeilBrown57afd892005-06-21 17:17:13 -07006779
6780 if (!skipped) { /* actual IO requested */
6781 io_sectors += sectors;
6782 atomic_add(sectors, &mddev->recovery_active);
6783 }
6784
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785 j += sectors;
6786 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006787 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006788 if (last_check == 0)
6789 /* this is the earliers that rebuilt will be
6790 * visible in /proc/mdstat
6791 */
6792 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006793
6794 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795 continue;
6796
NeilBrown57afd892005-06-21 17:17:13 -07006797 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798
NeilBrowndfc70642008-05-23 13:04:39 -07006799 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800 break;
6801
6802 repeat:
6803 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6804 /* step marks */
6805 int next = (last_mark+1) % SYNC_MARKS;
6806
6807 mddev->resync_mark = mark[next];
6808 mddev->resync_mark_cnt = mark_cnt[next];
6809 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006810 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811 last_mark = next;
6812 }
6813
6814
NeilBrownc6207272008-02-06 01:39:52 -08006815 if (kthread_should_stop())
6816 goto interrupted;
6817
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818
6819 /*
6820 * this loop exits only if either when we are slower than
6821 * the 'hard' speed limit, or the system was IO-idle for
6822 * a jiffy.
6823 * the system might be non-idle CPU-wise, but we only care
6824 * about not overloading the IO subsystem. (things like an
6825 * e2fsck being done on the RAID array should execute fast)
6826 */
NeilBrown252ac522010-06-01 19:37:29 +10006827 md_unplug(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828 cond_resched();
6829
NeilBrown57afd892005-06-21 17:17:13 -07006830 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6831 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832
NeilBrown88202a02006-01-06 00:21:36 -08006833 if (currspeed > speed_min(mddev)) {
6834 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006835 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006836 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837 goto repeat;
6838 }
6839 }
6840 }
NeilBrown61df9d92006-10-03 01:15:57 -07006841 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842 /*
6843 * this also signals 'finished resyncing' to md_stop
6844 */
6845 out:
NeilBrown252ac522010-06-01 19:37:29 +10006846 md_unplug(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847
6848 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6849
6850 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006851 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852
NeilBrowndfc70642008-05-23 13:04:39 -07006853 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006854 mddev->curr_resync > 2) {
6855 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6856 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6857 if (mddev->curr_resync >= mddev->recovery_cp) {
6858 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006859 "md: checkpointing %s of %s.\n",
6860 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006861 mddev->recovery_cp = mddev->curr_resync;
6862 }
6863 } else
6864 mddev->recovery_cp = MaxSector;
6865 } else {
6866 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6867 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006868 rcu_read_lock();
6869 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006870 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10006871 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006872 !test_bit(Faulty, &rdev->flags) &&
6873 !test_bit(In_sync, &rdev->flags) &&
6874 rdev->recovery_offset < mddev->curr_resync)
6875 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07006876 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878 }
NeilBrown17571282006-12-10 02:20:52 -08006879 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880
Linus Torvalds1da177e2005-04-16 15:20:36 -07006881 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006882 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6883 /* We completed so min/max setting can be forgotten if used. */
6884 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6885 mddev->resync_min = 0;
6886 mddev->resync_max = MaxSector;
6887 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6888 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006889 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006890 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6891 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006892 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893 wake_up(&resync_wait);
6894 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6895 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006896 return;
6897
6898 interrupted:
6899 /*
6900 * got a signal, exit.
6901 */
6902 printk(KERN_INFO
6903 "md: md_do_sync() got signal ... exiting\n");
6904 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6905 goto out;
6906
Linus Torvalds1da177e2005-04-16 15:20:36 -07006907}
NeilBrown29269552006-03-27 01:18:10 -08006908EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006909
6910
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006911static int remove_and_add_spares(mddev_t *mddev)
6912{
6913 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006914 int spares = 0;
6915
NeilBrown97e4f422009-03-31 14:33:13 +11006916 mddev->curr_resync_completed = 0;
6917
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006918 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006919 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006920 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006921 (test_bit(Faulty, &rdev->flags) ||
6922 ! test_bit(In_sync, &rdev->flags)) &&
6923 atomic_read(&rdev->nr_pending)==0) {
6924 if (mddev->pers->hot_remove_disk(
6925 mddev, rdev->raid_disk)==0) {
6926 char nm[20];
6927 sprintf(nm,"rd%d", rdev->raid_disk);
6928 sysfs_remove_link(&mddev->kobj, nm);
6929 rdev->raid_disk = -1;
6930 }
6931 }
6932
NeilBrown4044ba52009-01-09 08:31:11 +11006933 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006934 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006935 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006936 !test_bit(In_sync, &rdev->flags) &&
6937 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006938 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006939 if (rdev->raid_disk < 0
6940 && !test_bit(Faulty, &rdev->flags)) {
6941 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006942 if (mddev->pers->
6943 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006944 char nm[20];
6945 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006946 if (sysfs_create_link(&mddev->kobj,
6947 &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10006948 /* failure here is OK */;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006949 spares++;
6950 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11006951 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006952 } else
6953 break;
6954 }
NeilBrowndfc70642008-05-23 13:04:39 -07006955 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006956 }
6957 return spares;
6958}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006959/*
6960 * This routine is regularly called by all per-raid-array threads to
6961 * deal with generic issues like resync and super-block update.
6962 * Raid personalities that don't have a thread (linear/raid0) do not
6963 * need this as they never do any recovery or update the superblock.
6964 *
6965 * It does not do any resync itself, but rather "forks" off other threads
6966 * to do that as needed.
6967 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6968 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006969 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970 * and wakeups up this thread which will reap the thread and finish up.
6971 * This thread also removes any faulty devices (with nr_pending == 0).
6972 *
6973 * The overall approach is:
6974 * 1/ if the superblock needs updating, update it.
6975 * 2/ If a recovery thread is running, don't do anything else.
6976 * 3/ If recovery has finished, clean up, possibly marking spares active.
6977 * 4/ If there are any faulty devices, remove them.
6978 * 5/ If array is degraded, try to add spares devices
6979 * 6/ If array has spares or is not in-sync, start a resync thread.
6980 */
6981void md_check_recovery(mddev_t *mddev)
6982{
6983 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984
6985
NeilBrown5f404022005-06-21 17:17:16 -07006986 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11006987 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006988
6989 if (mddev->ro)
6990 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006991
6992 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006993 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006994 printk(KERN_INFO "md: %s in immediate safe mode\n",
6995 mdname(mddev));
6996 mddev->safemode = 2;
6997 }
6998 flush_signals(current);
6999 }
7000
NeilBrownc89a8ee2008-08-05 15:54:13 +10007001 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7002 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007003 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007004 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007006 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007007 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007008 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7009 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010 ))
7011 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007012
NeilBrowndf5b89b2006-03-27 01:18:20 -08007013 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007014 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007015
NeilBrownc89a8ee2008-08-05 15:54:13 +10007016 if (mddev->ro) {
7017 /* Only thing we do on a ro array is remove
7018 * failed devices.
7019 */
7020 remove_and_add_spares(mddev);
7021 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7022 goto unlock;
7023 }
7024
NeilBrown31a59e32008-04-30 00:52:30 -07007025 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007026 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007027 spin_lock_irq(&mddev->write_lock);
7028 if (mddev->safemode &&
7029 !atomic_read(&mddev->writes_pending) &&
7030 !mddev->in_sync &&
7031 mddev->recovery_cp == MaxSector) {
7032 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007033 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007034 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007035 }
7036 if (mddev->safemode == 1)
7037 mddev->safemode = 0;
7038 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007039 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007040 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007041 }
NeilBrownfca4d842005-06-21 17:17:11 -07007042
NeilBrown850b2b42006-10-03 01:15:46 -07007043 if (mddev->flags)
7044 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007045
Linus Torvalds1da177e2005-04-16 15:20:36 -07007046 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7047 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7048 /* resync/recovery still happening */
7049 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7050 goto unlock;
7051 }
7052 if (mddev->sync_thread) {
7053 /* resync has finished, collect result */
7054 md_unregister_thread(mddev->sync_thread);
7055 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07007056 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7057 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007058 /* success...*/
7059 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10007060 if (mddev->pers->spare_active(mddev))
7061 sysfs_notify(&mddev->kobj, NULL,
7062 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063 }
NeilBrowncea9c222009-03-31 15:15:05 +11007064 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7065 mddev->pers->finish_reshape)
7066 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07007067 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07007068
7069 /* if array is no-longer degraded, then any saved_raid_disk
7070 * information must be scrapped
7071 */
7072 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007073 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07007074 rdev->saved_raid_disk = -1;
7075
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076 mddev->recovery = 0;
7077 /* flag recovery needed just to double check */
7078 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007079 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007080 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081 goto unlock;
7082 }
Neil Brown72a23c22008-06-28 08:31:41 +10007083 /* Set RUNNING before clearing NEEDED to avoid
7084 * any transients in the value of "sync_action".
7085 */
7086 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7087 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007088 /* Clear some bits that don't mean anything, but
7089 * might be left set
7090 */
NeilBrown24dd4692005-11-08 21:39:26 -08007091 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7092 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007094 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7095 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096 /* no recovery is running.
7097 * remove any failed drives, then
7098 * add spares if possible.
7099 * Spare are also removed and re-added, to allow
7100 * the personality to fail the re-add.
7101 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007103 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007104 if (mddev->pers->check_reshape == NULL ||
7105 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007106 /* Cannot proceed */
7107 goto unlock;
7108 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007109 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007110 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007111 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7112 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007113 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007114 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007115 } else if (mddev->recovery_cp < MaxSector) {
7116 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007117 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007118 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7119 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007121
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007123 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7124 /* We are adding a device or devices to an array
7125 * which has the bitmap stored on all devices.
7126 * So make sure all bitmap pages get written
7127 */
7128 bitmap_write_all(mddev->bitmap);
7129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007130 mddev->sync_thread = md_register_thread(md_do_sync,
7131 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007132 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007133 if (!mddev->sync_thread) {
7134 printk(KERN_ERR "%s: could not start resync"
7135 " thread...\n",
7136 mdname(mddev));
7137 /* leave the spares where they are, it shouldn't hurt */
7138 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08007139 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007141 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007142 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143 }
7144 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007145 if (!mddev->sync_thread) {
7146 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7147 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7148 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007149 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007150 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152 mddev_unlock(mddev);
7153 }
7154}
7155
Dan Williams6bfe0b42008-04-30 00:52:32 -07007156void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7157{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007158 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007159 wait_event_timeout(rdev->blocked_wait,
7160 !test_bit(Blocked, &rdev->flags),
7161 msecs_to_jiffies(5000));
7162 rdev_dec_pending(rdev, mddev);
7163}
7164EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7165
Adrian Bunk75c96f82005-05-05 16:16:09 -07007166static int md_notify_reboot(struct notifier_block *this,
7167 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007168{
7169 struct list_head *tmp;
7170 mddev_t *mddev;
7171
7172 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7173
7174 printk(KERN_INFO "md: stopping all md devices.\n");
7175
NeilBrown29ac4aa2008-02-06 01:39:58 -08007176 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007177 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007178 /* Force a switch to readonly even array
7179 * appears to still be in use. Hence
7180 * the '100'.
7181 */
NeilBrowna4bd82d2010-03-29 13:23:10 +11007182 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007183 mddev_unlock(mddev);
7184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185 /*
7186 * certain more exotic SCSI devices are known to be
7187 * volatile wrt too early system reboots. While the
7188 * right place to handle this issue is the given
7189 * driver, we do want to have a safe RAID driver ...
7190 */
7191 mdelay(1000*1);
7192 }
7193 return NOTIFY_DONE;
7194}
7195
Adrian Bunk75c96f82005-05-05 16:16:09 -07007196static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197 .notifier_call = md_notify_reboot,
7198 .next = NULL,
7199 .priority = INT_MAX, /* before any real devices */
7200};
7201
7202static void md_geninit(void)
7203{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007204 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7205
Denis V. Lunevc7705f32008-04-29 01:02:35 -07007206 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207}
7208
Adrian Bunk75c96f82005-05-05 16:16:09 -07007209static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210{
Tejun Heoe804ac72010-10-15 15:36:08 +02007211 int ret = -ENOMEM;
7212
7213 md_wq = alloc_workqueue("md", WQ_RESCUER, 0);
7214 if (!md_wq)
7215 goto err_wq;
7216
7217 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
7218 if (!md_misc_wq)
7219 goto err_misc_wq;
7220
7221 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
7222 goto err_md;
7223
7224 if ((ret = register_blkdev(0, "mdp")) < 0)
7225 goto err_mdp;
7226 mdp_major = ret;
7227
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007228 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007229 md_probe, NULL, NULL);
7230 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231 md_probe, NULL, NULL);
7232
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007234 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235
7236 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007237 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238
Tejun Heoe804ac72010-10-15 15:36:08 +02007239err_mdp:
7240 unregister_blkdev(MD_MAJOR, "md");
7241err_md:
7242 destroy_workqueue(md_misc_wq);
7243err_misc_wq:
7244 destroy_workqueue(md_wq);
7245err_wq:
7246 return ret;
7247}
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248
7249#ifndef MODULE
7250
7251/*
7252 * Searches all registered partitions for autorun RAID arrays
7253 * at boot time.
7254 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007255
7256static LIST_HEAD(all_detected_devices);
7257struct detected_devices_node {
7258 struct list_head list;
7259 dev_t dev;
7260};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007261
7262void md_autodetect_dev(dev_t dev)
7263{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007264 struct detected_devices_node *node_detected_dev;
7265
7266 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7267 if (node_detected_dev) {
7268 node_detected_dev->dev = dev;
7269 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7270 } else {
7271 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7272 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007274}
7275
7276
7277static void autostart_arrays(int part)
7278{
7279 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007280 struct detected_devices_node *node_detected_dev;
7281 dev_t dev;
7282 int i_scanned, i_passed;
7283
7284 i_scanned = 0;
7285 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007286
7287 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7288
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007289 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7290 i_scanned++;
7291 node_detected_dev = list_entry(all_detected_devices.next,
7292 struct detected_devices_node, list);
7293 list_del(&node_detected_dev->list);
7294 dev = node_detected_dev->dev;
7295 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007296 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007297 if (IS_ERR(rdev))
7298 continue;
7299
NeilBrownb2d444d2005-11-08 21:39:31 -08007300 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007301 MD_BUG();
7302 continue;
7303 }
NeilBrownd0fae182008-03-04 14:29:31 -08007304 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007306 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007308
7309 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7310 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007311
7312 autorun_devices(part);
7313}
7314
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007315#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007316
7317static __exit void md_exit(void)
7318{
7319 mddev_t *mddev;
7320 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007321
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007322 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007323 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007325 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007326 unregister_blkdev(mdp_major, "mdp");
7327 unregister_reboot_notifier(&md_notifier);
7328 unregister_sysctl_table(raid_table_header);
7329 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007330 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007331 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007332 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007333 }
Tejun Heoe804ac72010-10-15 15:36:08 +02007334 destroy_workqueue(md_misc_wq);
7335 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007336}
7337
Dan Williams685784a2007-07-09 11:56:42 -07007338subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007339module_exit(md_exit)
7340
NeilBrownf91de922005-11-08 21:39:36 -08007341static int get_ro(char *buffer, struct kernel_param *kp)
7342{
7343 return sprintf(buffer, "%d", start_readonly);
7344}
7345static int set_ro(const char *val, struct kernel_param *kp)
7346{
7347 char *e;
7348 int num = simple_strtoul(val, &e, 10);
7349 if (*val && (*e == '\0' || *e == '\n')) {
7350 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007351 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007352 }
7353 return -EINVAL;
7354}
7355
NeilBrown80ca3a42006-07-10 04:44:18 -07007356module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7357module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007358
NeilBrownefeb53c2009-01-09 08:31:10 +11007359module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007360
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361EXPORT_SYMBOL(register_md_personality);
7362EXPORT_SYMBOL(unregister_md_personality);
7363EXPORT_SYMBOL(md_error);
7364EXPORT_SYMBOL(md_done_sync);
7365EXPORT_SYMBOL(md_write_start);
7366EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007367EXPORT_SYMBOL(md_register_thread);
7368EXPORT_SYMBOL(md_unregister_thread);
7369EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370EXPORT_SYMBOL(md_check_recovery);
7371MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007372MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007373MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007374MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);