blob: 64f97168cefa5a846666c3a54b6c75638020dc66 [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
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020060static DEFINE_MUTEX(md_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110063static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#endif
65
NeilBrown2604b702006-01-06 00:20:36 -080066static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static DEFINE_SPINLOCK(pers_lock);
68
Adrian Bunk5e563412006-06-26 00:27:42 -070069static void md_print_devices(void);
70
Bernd Schubert90b08712008-05-23 13:04:38 -070071static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
72
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 */
303 schedule_work(&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 Heoe9c74692010-09-03 11:56:18 +0200372 schedule_work(&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) {
439 /* we did a probe so need to clean up.
440 * Call schedule_work inside the spinlock
441 * so that flush_scheduled_work() after
442 * mddev_find will succeed in waiting for the
443 * work to be done.
444 */
445 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
446 schedule_work(&mddev->del_work);
447 } else
448 kfree(mddev);
449 }
450 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
NeilBrown390ee602010-06-01 19:37:27 +1000453void mddev_init(mddev_t *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100454{
455 mutex_init(&mddev->open_mutex);
456 mutex_init(&mddev->reconfig_mutex);
457 mutex_init(&mddev->bitmap_info.mutex);
458 INIT_LIST_HEAD(&mddev->disks);
459 INIT_LIST_HEAD(&mddev->all_mddevs);
460 init_timer(&mddev->safemode_timer);
461 atomic_set(&mddev->active, 1);
462 atomic_set(&mddev->openers, 0);
463 atomic_set(&mddev->active_io, 0);
464 spin_lock_init(&mddev->write_lock);
465 atomic_set(&mddev->flush_pending, 0);
466 init_waitqueue_head(&mddev->sb_wait);
467 init_waitqueue_head(&mddev->recovery_wait);
468 mddev->reshape_position = MaxSector;
469 mddev->resync_min = 0;
470 mddev->resync_max = MaxSector;
471 mddev->level = LEVEL_NONE;
472}
NeilBrown390ee602010-06-01 19:37:27 +1000473EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475static mddev_t * mddev_find(dev_t unit)
476{
477 mddev_t *mddev, *new = NULL;
478
479 retry:
480 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
NeilBrownefeb53c2009-01-09 08:31:10 +1100482 if (unit) {
483 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
484 if (mddev->unit == unit) {
485 mddev_get(mddev);
486 spin_unlock(&all_mddevs_lock);
487 kfree(new);
488 return mddev;
489 }
490
491 if (new) {
492 list_add(&new->all_mddevs, &all_mddevs);
493 spin_unlock(&all_mddevs_lock);
494 new->hold_active = UNTIL_IOCTL;
495 return new;
496 }
497 } else if (new) {
498 /* find an unused unit number */
499 static int next_minor = 512;
500 int start = next_minor;
501 int is_free = 0;
502 int dev = 0;
503 while (!is_free) {
504 dev = MKDEV(MD_MAJOR, next_minor);
505 next_minor++;
506 if (next_minor > MINORMASK)
507 next_minor = 0;
508 if (next_minor == start) {
509 /* Oh dear, all in use. */
510 spin_unlock(&all_mddevs_lock);
511 kfree(new);
512 return NULL;
513 }
514
515 is_free = 1;
516 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
517 if (mddev->unit == dev) {
518 is_free = 0;
519 break;
520 }
521 }
522 new->unit = dev;
523 new->md_minor = MINOR(dev);
524 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 list_add(&new->all_mddevs, &all_mddevs);
526 spin_unlock(&all_mddevs_lock);
527 return new;
528 }
529 spin_unlock(&all_mddevs_lock);
530
NeilBrown9ffae0c2006-01-06 00:20:32 -0800531 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (!new)
533 return NULL;
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 new->unit = unit;
536 if (MAJOR(unit) == MD_MAJOR)
537 new->md_minor = MINOR(unit);
538 else
539 new->md_minor = MINOR(unit) >> MdpMinorShift;
540
NeilBrownfafd7fb2010-04-01 15:55:30 +1100541 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 goto retry;
544}
545
546static inline int mddev_lock(mddev_t * mddev)
547{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800548 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Dan Williamsb522adc2009-03-31 15:00:31 +1100551static inline int mddev_is_locked(mddev_t *mddev)
552{
553 return mutex_is_locked(&mddev->reconfig_mutex);
554}
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556static inline int mddev_trylock(mddev_t * mddev)
557{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800558 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
NeilBrownb6eb1272010-04-15 10:13:47 +1000561static struct attribute_group md_redundancy_group;
562
NeilBrowna64c8762010-04-14 17:15:37 +1000563static void mddev_unlock(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
NeilBrowna64c8762010-04-14 17:15:37 +1000565 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000566 /* These cannot be removed under reconfig_mutex as
567 * an access to the files will try to take reconfig_mutex
568 * while holding the file unremovable, which leads to
569 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000570 * So hold set sysfs_active while the remove in happeing,
571 * and anything else which might set ->to_remove or my
572 * otherwise change the sysfs namespace will fail with
573 * -EBUSY if sysfs_active is still set.
574 * We set sysfs_active under reconfig_mutex and elsewhere
575 * test it under the same mutex to ensure its correct value
576 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000577 */
NeilBrowna64c8762010-04-14 17:15:37 +1000578 struct attribute_group *to_remove = mddev->to_remove;
579 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000580 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000581 mutex_unlock(&mddev->reconfig_mutex);
582
NeilBrown00bcb4a2010-06-01 19:37:23 +1000583 if (mddev->kobj.sd) {
584 if (to_remove != &md_redundancy_group)
585 sysfs_remove_group(&mddev->kobj, to_remove);
586 if (mddev->pers == NULL ||
587 mddev->pers->sync_request == NULL) {
588 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
589 if (mddev->sysfs_action)
590 sysfs_put(mddev->sysfs_action);
591 mddev->sysfs_action = NULL;
592 }
NeilBrowna64c8762010-04-14 17:15:37 +1000593 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000594 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000595 } else
596 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
NeilBrown005eca52005-08-22 13:11:08 -0700598 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
NeilBrown2989ddb2006-01-06 00:20:43 -0800601static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100603 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100605 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (rdev->desc_nr == nr)
607 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return NULL;
610}
611
612static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
613{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 mdk_rdev_t *rdev;
615
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100616 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (rdev->bdev->bd_dev == dev)
618 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return NULL;
621}
622
NeilBrownd9d166c2006-01-06 00:20:51 -0800623static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800624{
625 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800626 list_for_each_entry(pers, &pers_list, list) {
627 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800628 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800629 if (strcmp(pers->name, clevel)==0)
630 return pers;
631 }
NeilBrown2604b702006-01-06 00:20:36 -0800632 return NULL;
633}
634
Andre Nollb73df2d2008-07-11 22:02:23 +1000635/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700636static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Andre Nollb73df2d2008-07-11 22:02:23 +1000638 sector_t num_sectors = bdev->bd_inode->i_size / 512;
639 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642static int alloc_disk_sb(mdk_rdev_t * rdev)
643{
644 if (rdev->sb_page)
645 MD_BUG();
646
647 rdev->sb_page = alloc_page(GFP_KERNEL);
648 if (!rdev->sb_page) {
649 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000650 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652
653 return 0;
654}
655
656static void free_disk_sb(mdk_rdev_t * rdev)
657{
658 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800659 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 rdev->sb_loaded = 0;
661 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000662 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100663 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665}
666
667
NeilBrown6712ecf2007-09-27 12:47:43 +0200668static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700669{
670 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800671 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700672
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700673 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
674 printk("md: super_written gets error=%d, uptodate=%d\n",
675 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
676 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800677 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700678 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700679
NeilBrowna9701a32005-11-08 21:39:34 -0800680 if (atomic_dec_and_test(&mddev->pending_writes))
681 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700682 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700683}
684
685void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
686 sector_t sector, int size, struct page *page)
687{
688 /* write first size bytes of page to sector of rdev
689 * Increment mddev->pending_writes before returning
690 * and decrement it on completion, waking up sb_wait
691 * if zero is reached.
692 * If an error occurred, call md_error
693 */
694 struct bio *bio = bio_alloc(GFP_NOIO, 1);
695
696 bio->bi_bdev = rdev->bdev;
697 bio->bi_sector = sector;
698 bio_add_page(bio, page, size, 0);
699 bio->bi_private = rdev;
700 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800701
NeilBrown7bfa19f2005-06-21 17:17:28 -0700702 atomic_inc(&mddev->pending_writes);
Tejun Heoe9c74692010-09-03 11:56:18 +0200703 submit_bio(REQ_WRITE | REQ_SYNC | REQ_UNPLUG | REQ_FLUSH | REQ_FUA,
704 bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800705}
706
707void md_super_wait(mddev_t *mddev)
708{
Tejun Heoe9c74692010-09-03 11:56:18 +0200709 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800710 DEFINE_WAIT(wq);
711 for(;;) {
712 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
713 if (atomic_read(&mddev->pending_writes)==0)
714 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800715 schedule();
716 }
717 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700718}
719
NeilBrown6712ecf2007-09-27 12:47:43 +0200720static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
NeilBrowna654b9d82005-06-21 17:17:27 -0700725int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 struct page *page, int rw)
727{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700728 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 struct completion event;
730 int ret;
731
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +0200732 rw |= REQ_SYNC | REQ_UNPLUG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 bio->bi_bdev = bdev;
735 bio->bi_sector = sector;
736 bio_add_page(bio, page, size, 0);
737 init_completion(&event);
738 bio->bi_private = &event;
739 bio->bi_end_io = bi_complete;
740 submit_bio(rw, bio);
741 wait_for_completion(&event);
742
743 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
744 bio_put(bio);
745 return ret;
746}
NeilBrowna8745db2006-01-06 00:20:34 -0800747EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
NeilBrown0002b272005-09-09 16:23:53 -0700749static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
751 char b[BDEVNAME_SIZE];
752 if (!rdev->sb_page) {
753 MD_BUG();
754 return -EINVAL;
755 }
756 if (rdev->sb_loaded)
757 return 0;
758
759
Andre Noll0f420352008-07-11 22:02:23 +1000760 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 goto fail;
762 rdev->sb_loaded = 1;
763 return 0;
764
765fail:
766 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
767 bdevname(rdev->bdev,b));
768 return -EINVAL;
769}
770
771static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
772{
Andre Noll05710462008-07-11 22:02:20 +1000773 return sb1->set_uuid0 == sb2->set_uuid0 &&
774 sb1->set_uuid1 == sb2->set_uuid1 &&
775 sb1->set_uuid2 == sb2->set_uuid2 &&
776 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
780{
781 int ret;
782 mdp_super_t *tmp1, *tmp2;
783
784 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
785 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
786
787 if (!tmp1 || !tmp2) {
788 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100789 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 goto abort;
791 }
792
793 *tmp1 = *sb1;
794 *tmp2 = *sb2;
795
796 /*
797 * nr_disks is not constant
798 */
799 tmp1->nr_disks = 0;
800 tmp2->nr_disks = 0;
801
Andre Nollce0c8e02008-07-11 22:02:20 +1000802 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700804 kfree(tmp1);
805 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return ret;
807}
808
NeilBrown4d167f02007-05-09 02:35:37 -0700809
810static u32 md_csum_fold(u32 csum)
811{
812 csum = (csum & 0xffff) + (csum >> 16);
813 return (csum & 0xffff) + (csum >> 16);
814}
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816static unsigned int calc_sb_csum(mdp_super_t * sb)
817{
NeilBrown4d167f02007-05-09 02:35:37 -0700818 u64 newcsum = 0;
819 u32 *sb32 = (u32*)sb;
820 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 unsigned int disk_csum, csum;
822
823 disk_csum = sb->sb_csum;
824 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700825
826 for (i = 0; i < MD_SB_BYTES/4 ; i++)
827 newcsum += sb32[i];
828 csum = (newcsum & 0xffffffff) + (newcsum>>32);
829
830
831#ifdef CONFIG_ALPHA
832 /* This used to use csum_partial, which was wrong for several
833 * reasons including that different results are returned on
834 * different architectures. It isn't critical that we get exactly
835 * the same return value as before (we always csum_fold before
836 * testing, and that removes any differences). However as we
837 * know that csum_partial always returned a 16bit value on
838 * alphas, do a fold to maximise conformity to previous behaviour.
839 */
840 sb->sb_csum = md_csum_fold(disk_csum);
841#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700843#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return csum;
845}
846
847
848/*
849 * Handle superblock details.
850 * We want to be able to handle multiple superblock formats
851 * so we have a common interface to them all, and an array of
852 * different handlers.
853 * We rely on user-space to write the initial superblock, and support
854 * reading and updating of superblocks.
855 * Interface methods are:
856 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
857 * loads and validates a superblock on dev.
858 * if refdev != NULL, compare superblocks on both devices
859 * Return:
860 * 0 - dev has a superblock that is compatible with refdev
861 * 1 - dev has a superblock that is compatible and newer than refdev
862 * so dev should be used as the refdev in future
863 * -EINVAL superblock incompatible or invalid
864 * -othererror e.g. -EIO
865 *
866 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
867 * Verify that dev is acceptable into mddev.
868 * The first time, mddev->raid_disks will be 0, and data from
869 * dev should be merged in. Subsequent calls check that dev
870 * is new enough. Return 0 or -EINVAL
871 *
872 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
873 * Update the superblock for rdev with data in mddev
874 * This does not write to disc.
875 *
876 */
877
878struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000879 char *name;
880 struct module *owner;
881 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
882 int minor_version);
883 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
884 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
885 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000886 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887};
888
889/*
Andre Noll0894cc32009-06-18 08:49:23 +1000890 * Check that the given mddev has no bitmap.
891 *
892 * This function is called from the run method of all personalities that do not
893 * support bitmaps. It prints an error message and returns non-zero if mddev
894 * has a bitmap. Otherwise, it returns 0.
895 *
896 */
897int md_check_no_bitmap(mddev_t *mddev)
898{
NeilBrownc3d97142009-12-14 12:49:52 +1100899 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000900 return 0;
901 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
902 mdname(mddev), mddev->pers->name);
903 return 1;
904}
905EXPORT_SYMBOL(md_check_no_bitmap);
906
907/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 * load_super for 0.90.0
909 */
910static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
911{
912 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
913 mdp_super_t *sb;
914 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 /*
Andre Noll0f420352008-07-11 22:02:23 +1000917 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 * it's at the end of the disk.
919 *
920 * It also happens to be a multiple of 4Kb.
921 */
Andre Noll0f420352008-07-11 22:02:23 +1000922 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
NeilBrown0002b272005-09-09 16:23:53 -0700924 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (ret) return ret;
926
927 ret = -EINVAL;
928
929 bdevname(rdev->bdev, b);
930 sb = (mdp_super_t*)page_address(rdev->sb_page);
931
932 if (sb->md_magic != MD_SB_MAGIC) {
933 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
934 b);
935 goto abort;
936 }
937
938 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800939 sb->minor_version < 90 ||
940 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
942 sb->major_version, sb->minor_version,
943 b);
944 goto abort;
945 }
946
947 if (sb->raid_disks <= 0)
948 goto abort;
949
NeilBrown4d167f02007-05-09 02:35:37 -0700950 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
952 b);
953 goto abort;
954 }
955
956 rdev->preferred_minor = sb->md_minor;
957 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700958 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 if (sb->level == LEVEL_MULTIPATH)
961 rdev->desc_nr = -1;
962 else
963 rdev->desc_nr = sb->this_disk.number;
964
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700965 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700967 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 __u64 ev1, ev2;
969 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
970 if (!uuid_equal(refsb, sb)) {
971 printk(KERN_WARNING "md: %s has different UUID to %s\n",
972 b, bdevname(refdev->bdev,b2));
973 goto abort;
974 }
975 if (!sb_equal(refsb, sb)) {
976 printk(KERN_WARNING "md: %s has same UUID"
977 " but different superblock to %s\n",
978 b, bdevname(refdev->bdev, b2));
979 goto abort;
980 }
981 ev1 = md_event(sb);
982 ev2 = md_event(refsb);
983 if (ev1 > ev2)
984 ret = 1;
985 else
986 ret = 0;
987 }
NeilBrown8190e752009-06-18 08:48:58 +1000988 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Andre Nolldd8ac332009-03-31 14:33:13 +1100990 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -0800991 /* "this cannot possibly happen" ... */
992 ret = -EINVAL;
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 abort:
995 return ret;
996}
997
998/*
999 * validate_super for 0.90.0
1000 */
1001static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1002{
1003 mdp_disk_t *desc;
1004 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001005 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
NeilBrown41158c72005-06-21 17:17:25 -07001007 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001008 clear_bit(Faulty, &rdev->flags);
1009 clear_bit(In_sync, &rdev->flags);
1010 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (mddev->raid_disks == 0) {
1013 mddev->major_version = 0;
1014 mddev->minor_version = sb->minor_version;
1015 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001016 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001017 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 mddev->ctime = sb->ctime;
1019 mddev->utime = sb->utime;
1020 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001021 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 mddev->layout = sb->layout;
1023 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +11001024 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001025 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001026 mddev->bitmap_info.offset = 0;
1027 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
NeilBrownf6705572006-03-27 01:18:11 -08001029 if (mddev->minor_version >= 91) {
1030 mddev->reshape_position = sb->reshape_position;
1031 mddev->delta_disks = sb->delta_disks;
1032 mddev->new_level = sb->new_level;
1033 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001034 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001035 } else {
1036 mddev->reshape_position = MaxSector;
1037 mddev->delta_disks = 0;
1038 mddev->new_level = mddev->level;
1039 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001040 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001041 }
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 if (sb->state & (1<<MD_SB_CLEAN))
1044 mddev->recovery_cp = MaxSector;
1045 else {
1046 if (sb->events_hi == sb->cp_events_hi &&
1047 sb->events_lo == sb->cp_events_lo) {
1048 mddev->recovery_cp = sb->recovery_cp;
1049 } else
1050 mddev->recovery_cp = 0;
1051 }
1052
1053 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1054 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1055 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1056 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1057
1058 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001059
1060 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001061 mddev->bitmap_info.file == NULL)
1062 mddev->bitmap_info.offset =
1063 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001064
NeilBrown41158c72005-06-21 17:17:25 -07001065 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001066 /* Insist on good event counter while assembling, except
1067 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001069 if (sb->disks[rdev->desc_nr].state & (
1070 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1071 if (ev1 < mddev->events)
1072 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001073 } else if (mddev->bitmap) {
1074 /* if adding to array with a bitmap, then we can accept an
1075 * older device ... but not too old.
1076 */
NeilBrown41158c72005-06-21 17:17:25 -07001077 if (ev1 < mddev->bitmap->events_cleared)
1078 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001079 } else {
1080 if (ev1 < mddev->events)
1081 /* just a hot-add of a new device, leave raid_disk at -1 */
1082 return 0;
1083 }
NeilBrown41158c72005-06-21 17:17:25 -07001084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 desc = sb->disks + rdev->desc_nr;
1087
1088 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001089 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001090 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1091 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001092 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001094 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1095 /* active but not in sync implies recovery up to
1096 * reshape position. We don't know exactly where
1097 * that is, so set to zero for now */
1098 if (mddev->minor_version >= 91) {
1099 rdev->recovery_offset = 0;
1100 rdev->raid_disk = desc->raid_disk;
1101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001103 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1104 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001105 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001106 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return 0;
1108}
1109
1110/*
1111 * sync_super for 0.90.0
1112 */
1113static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1114{
1115 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 mdk_rdev_t *rdev2;
1117 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 /* make rdev->sb match mddev data..
1121 *
1122 * 1/ zero out disks
1123 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1124 * 3/ any empty disks < next_spare become removed
1125 *
1126 * disks[0] gets initialised to REMOVED because
1127 * we cannot be sure from other fields if it has
1128 * been initialised or not.
1129 */
1130 int i;
1131 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1132
NeilBrown61181562005-09-09 16:24:02 -07001133 rdev->sb_size = MD_SB_BYTES;
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 sb = (mdp_super_t*)page_address(rdev->sb_page);
1136
1137 memset(sb, 0, sizeof(*sb));
1138
1139 sb->md_magic = MD_SB_MAGIC;
1140 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 sb->patch_version = mddev->patch_version;
1142 sb->gvalid_words = 0; /* ignored */
1143 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1144 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1145 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1146 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1147
1148 sb->ctime = mddev->ctime;
1149 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001150 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 sb->raid_disks = mddev->raid_disks;
1152 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001153 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 sb->utime = mddev->utime;
1155 sb->state = 0;
1156 sb->events_hi = (mddev->events>>32);
1157 sb->events_lo = (u32)mddev->events;
1158
NeilBrownf6705572006-03-27 01:18:11 -08001159 if (mddev->reshape_position == MaxSector)
1160 sb->minor_version = 90;
1161 else {
1162 sb->minor_version = 91;
1163 sb->reshape_position = mddev->reshape_position;
1164 sb->new_level = mddev->new_level;
1165 sb->delta_disks = mddev->delta_disks;
1166 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001167 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001168 }
1169 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (mddev->in_sync)
1171 {
1172 sb->recovery_cp = mddev->recovery_cp;
1173 sb->cp_events_hi = (mddev->events>>32);
1174 sb->cp_events_lo = (u32)mddev->events;
1175 if (mddev->recovery_cp == MaxSector)
1176 sb->state = (1<< MD_SB_CLEAN);
1177 } else
1178 sb->recovery_cp = 0;
1179
1180 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001181 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
NeilBrownc3d97142009-12-14 12:49:52 +11001183 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001184 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001187 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001189 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001190 int is_active = test_bit(In_sync, &rdev2->flags);
1191
1192 if (rdev2->raid_disk >= 0 &&
1193 sb->minor_version >= 91)
1194 /* we have nowhere to store the recovery_offset,
1195 * but if it is not below the reshape_position,
1196 * we can piggy-back on that.
1197 */
1198 is_active = 1;
1199 if (rdev2->raid_disk < 0 ||
1200 test_bit(Faulty, &rdev2->flags))
1201 is_active = 0;
1202 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001203 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001205 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001206 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 d = &sb->disks[rdev2->desc_nr];
1208 nr_disks++;
1209 d->number = rdev2->desc_nr;
1210 d->major = MAJOR(rdev2->bdev->bd_dev);
1211 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001212 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 d->raid_disk = rdev2->raid_disk;
1214 else
1215 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001216 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001218 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001220 if (test_bit(In_sync, &rdev2->flags))
1221 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 active++;
1223 working++;
1224 } else {
1225 d->state = 0;
1226 spare++;
1227 working++;
1228 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001229 if (test_bit(WriteMostly, &rdev2->flags))
1230 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 /* now set the "removed" and "faulty" bits on any missing devices */
1233 for (i=0 ; i < mddev->raid_disks ; i++) {
1234 mdp_disk_t *d = &sb->disks[i];
1235 if (d->state == 0 && d->number == 0) {
1236 d->number = i;
1237 d->raid_disk = i;
1238 d->state = (1<<MD_DISK_REMOVED);
1239 d->state |= (1<<MD_DISK_FAULTY);
1240 failed++;
1241 }
1242 }
1243 sb->nr_disks = nr_disks;
1244 sb->active_disks = active;
1245 sb->working_disks = working;
1246 sb->failed_disks = failed;
1247 sb->spare_disks = spare;
1248
1249 sb->this_disk = sb->disks[rdev->desc_nr];
1250 sb->sb_csum = calc_sb_csum(sb);
1251}
1252
1253/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001254 * rdev_size_change for 0.90.0
1255 */
1256static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001257super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001258{
Andre Noll58c0fed2009-03-31 14:33:13 +11001259 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001260 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001261 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001262 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001263 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001264 if (!num_sectors || num_sectors > rdev->sb_start)
1265 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001266 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001267 rdev->sb_page);
1268 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001269 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001270}
1271
1272
1273/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 * version 1 superblock
1275 */
1276
NeilBrown1c05b4b2006-10-21 10:24:08 -07001277static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001279 __le32 disk_csum;
1280 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 unsigned long long newcsum;
1282 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001283 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 int i;
1285
1286 disk_csum = sb->sb_csum;
1287 sb->sb_csum = 0;
1288 newcsum = 0;
1289 for (i=0; size>=4; size -= 4 )
1290 newcsum += le32_to_cpu(*isuper++);
1291
1292 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001293 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1296 sb->sb_csum = disk_csum;
1297 return cpu_to_le32(csum);
1298}
1299
1300static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1301{
1302 struct mdp_superblock_1 *sb;
1303 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001304 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001306 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 /*
Andre Noll0f420352008-07-11 22:02:23 +10001309 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 * It is always aligned to a 4K boundary and
1311 * depeding on minor_version, it can be:
1312 * 0: At least 8K, but less than 12K, from end of device
1313 * 1: At start of device
1314 * 2: 4K from start of device.
1315 */
1316 switch(minor_version) {
1317 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001318 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1319 sb_start -= 8*2;
1320 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 break;
1322 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001323 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 break;
1325 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001326 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 break;
1328 default:
1329 return -EINVAL;
1330 }
Andre Noll0f420352008-07-11 22:02:23 +10001331 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
NeilBrown0002b272005-09-09 16:23:53 -07001333 /* superblock is rarely larger than 1K, but it can be larger,
1334 * and it is safe to read 4k, so we do that
1335 */
1336 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (ret) return ret;
1338
1339
1340 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1341
1342 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1343 sb->major_version != cpu_to_le32(1) ||
1344 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001345 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001346 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return -EINVAL;
1348
1349 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1350 printk("md: invalid superblock checksum on %s\n",
1351 bdevname(rdev->bdev,b));
1352 return -EINVAL;
1353 }
1354 if (le64_to_cpu(sb->data_size) < 10) {
1355 printk("md: data_size too small on %s\n",
1356 bdevname(rdev->bdev,b));
1357 return -EINVAL;
1358 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 rdev->preferred_minor = 0xffff;
1361 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001362 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
NeilBrown0002b272005-09-09 16:23:53 -07001364 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001365 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001366 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001367 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1368
1369 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001370 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001371 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001372
NeilBrown31b65a02006-07-10 04:44:14 -07001373 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1374 rdev->desc_nr = -1;
1375 else
1376 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1377
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001378 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001379 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001380 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 __u64 ev1, ev2;
1382 struct mdp_superblock_1 *refsb =
1383 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1384
1385 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1386 sb->level != refsb->level ||
1387 sb->layout != refsb->layout ||
1388 sb->chunksize != refsb->chunksize) {
1389 printk(KERN_WARNING "md: %s has strangely different"
1390 " superblock to %s\n",
1391 bdevname(rdev->bdev,b),
1392 bdevname(refdev->bdev,b2));
1393 return -EINVAL;
1394 }
1395 ev1 = le64_to_cpu(sb->events);
1396 ev2 = le64_to_cpu(refsb->events);
1397
1398 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001399 ret = 1;
1400 else
1401 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
NeilBrowna1801f82008-03-04 14:29:31 -08001403 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001404 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1405 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001407 rdev->sectors = rdev->sb_start;
1408 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001410 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001411 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001412 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001413 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
1416static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1417{
1418 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001419 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
NeilBrown41158c72005-06-21 17:17:25 -07001421 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001422 clear_bit(Faulty, &rdev->flags);
1423 clear_bit(In_sync, &rdev->flags);
1424 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 if (mddev->raid_disks == 0) {
1427 mddev->major_version = 1;
1428 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001429 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001430 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1432 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1433 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001434 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 mddev->layout = le32_to_cpu(sb->layout);
1436 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001437 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001438 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001439 mddev->bitmap_info.offset = 0;
1440 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1443 memcpy(mddev->uuid, sb->set_uuid, 16);
1444
1445 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001446
NeilBrown71c08052005-09-09 16:23:51 -07001447 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001448 mddev->bitmap_info.file == NULL )
1449 mddev->bitmap_info.offset =
1450 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001451
NeilBrownf6705572006-03-27 01:18:11 -08001452 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1453 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1454 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1455 mddev->new_level = le32_to_cpu(sb->new_level);
1456 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001457 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001458 } else {
1459 mddev->reshape_position = MaxSector;
1460 mddev->delta_disks = 0;
1461 mddev->new_level = mddev->level;
1462 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001463 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001464 }
1465
NeilBrown41158c72005-06-21 17:17:25 -07001466 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001467 /* Insist of good event counter while assembling, except for
1468 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001470 if (rdev->desc_nr >= 0 &&
1471 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1472 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1473 if (ev1 < mddev->events)
1474 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001475 } else if (mddev->bitmap) {
1476 /* If adding to array with a bitmap, then we can accept an
1477 * older device, but not too old.
1478 */
NeilBrown41158c72005-06-21 17:17:25 -07001479 if (ev1 < mddev->bitmap->events_cleared)
1480 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001481 } else {
1482 if (ev1 < mddev->events)
1483 /* just a hot-add of a new device, leave raid_disk at -1 */
1484 return 0;
1485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 if (mddev->level != LEVEL_MULTIPATH) {
1487 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001488 if (rdev->desc_nr < 0 ||
1489 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1490 role = 0xffff;
1491 rdev->desc_nr = -1;
1492 } else
1493 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 switch(role) {
1495 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 break;
1497 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001498 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 break;
1500 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001501 if ((le32_to_cpu(sb->feature_map) &
1502 MD_FEATURE_RECOVERY_OFFSET))
1503 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1504 else
1505 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 rdev->raid_disk = role;
1507 break;
1508 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001509 if (sb->devflags & WriteMostly1)
1510 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001511 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001512 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 return 0;
1515}
1516
1517static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1518{
1519 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 mdk_rdev_t *rdev2;
1521 int max_dev, i;
1522 /* make rdev->sb match mddev and rdev data. */
1523
1524 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1525
1526 sb->feature_map = 0;
1527 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001528 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 memset(sb->pad1, 0, sizeof(sb->pad1));
1530 memset(sb->pad2, 0, sizeof(sb->pad2));
1531 memset(sb->pad3, 0, sizeof(sb->pad3));
1532
1533 sb->utime = cpu_to_le64((__u64)mddev->utime);
1534 sb->events = cpu_to_le64(mddev->events);
1535 if (mddev->in_sync)
1536 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1537 else
1538 sb->resync_offset = cpu_to_le64(0);
1539
NeilBrown1c05b4b2006-10-21 10:24:08 -07001540 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001541
NeilBrownf0ca3402006-02-02 14:28:04 -08001542 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001543 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001544 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001545 sb->level = cpu_to_le32(mddev->level);
1546 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001547
NeilBrownc3d97142009-12-14 12:49:52 +11001548 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1549 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001550 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001551 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001552
1553 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001554 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001555 sb->feature_map |=
1556 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1557 sb->recovery_offset =
1558 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001559 }
1560
NeilBrownf6705572006-03-27 01:18:11 -08001561 if (mddev->reshape_position != MaxSector) {
1562 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1563 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1564 sb->new_layout = cpu_to_le32(mddev->new_layout);
1565 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1566 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001567 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001568 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001571 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 if (rdev2->desc_nr+1 > max_dev)
1573 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001574
NeilBrown70471da2009-08-03 10:59:57 +10001575 if (max_dev > le32_to_cpu(sb->max_dev)) {
1576 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001577 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001578 rdev->sb_size = max_dev * 2 + 256;
1579 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1580 if (rdev->sb_size & bmask)
1581 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001582 } else
1583 max_dev = le32_to_cpu(sb->max_dev);
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 for (i=0; i<max_dev;i++)
1586 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1587
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001588 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001590 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001592 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001594 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001595 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 else
1597 sb->dev_roles[i] = cpu_to_le16(0xffff);
1598 }
1599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 sb->sb_csum = calc_sb_1_csum(sb);
1601}
1602
Chris Webb0cd17fe2008-06-28 08:31:46 +10001603static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001604super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001605{
1606 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001607 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001608 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001609 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001610 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001611 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001612 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1613 max_sectors -= rdev->data_offset;
1614 if (!num_sectors || num_sectors > max_sectors)
1615 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001616 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001617 /* minor version 0 with bitmap we can't move */
1618 return 0;
1619 } else {
1620 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001621 sector_t sb_start;
1622 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1623 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001624 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001625 if (!num_sectors || num_sectors > max_sectors)
1626 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001627 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001628 }
1629 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001630 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001631 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001632 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001633 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001634 rdev->sb_page);
1635 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001636 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001637}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Adrian Bunk75c96f82005-05-05 16:16:09 -07001639static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 [0] = {
1641 .name = "0.90.0",
1642 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001643 .load_super = super_90_load,
1644 .validate_super = super_90_validate,
1645 .sync_super = super_90_sync,
1646 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 },
1648 [1] = {
1649 .name = "md-1",
1650 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001651 .load_super = super_1_load,
1652 .validate_super = super_1_validate,
1653 .sync_super = super_1_sync,
1654 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 },
1656};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1659{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001660 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
NeilBrown4b809912008-07-21 17:05:25 +10001662 rcu_read_lock();
1663 rdev_for_each_rcu(rdev, mddev1)
1664 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001665 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001666 rdev2->bdev->bd_contains) {
1667 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001668 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001669 }
1670 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 return 0;
1672}
1673
1674static LIST_HEAD(pending_raid_disks);
1675
Andre Nollac5e7112009-08-03 10:59:47 +10001676/*
1677 * Try to register data integrity profile for an mddev
1678 *
1679 * This is called when an array is started and after a disk has been kicked
1680 * from the array. It only succeeds if all working and active component devices
1681 * are integrity capable with matching profiles.
1682 */
1683int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001684{
Andre Nollac5e7112009-08-03 10:59:47 +10001685 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001686
Andre Nollac5e7112009-08-03 10:59:47 +10001687 if (list_empty(&mddev->disks))
1688 return 0; /* nothing to do */
1689 if (blk_get_integrity(mddev->gendisk))
1690 return 0; /* already registered */
1691 list_for_each_entry(rdev, &mddev->disks, same_set) {
1692 /* skip spares and non-functional disks */
1693 if (test_bit(Faulty, &rdev->flags))
1694 continue;
1695 if (rdev->raid_disk < 0)
1696 continue;
1697 /*
1698 * If at least one rdev is not integrity capable, we can not
1699 * enable data integrity for the md device.
1700 */
1701 if (!bdev_get_integrity(rdev->bdev))
1702 return -EINVAL;
1703 if (!reference) {
1704 /* Use the first rdev as the reference */
1705 reference = rdev;
1706 continue;
1707 }
1708 /* does this rdev's profile match the reference profile? */
1709 if (blk_integrity_compare(reference->bdev->bd_disk,
1710 rdev->bdev->bd_disk) < 0)
1711 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001712 }
Andre Nollac5e7112009-08-03 10:59:47 +10001713 /*
1714 * All component devices are integrity capable and have matching
1715 * profiles, register the common profile for the md device.
1716 */
1717 if (blk_integrity_register(mddev->gendisk,
1718 bdev_get_integrity(reference->bdev)) != 0) {
1719 printk(KERN_ERR "md: failed to register integrity for %s\n",
1720 mdname(mddev));
1721 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001722 }
Andre Nollac5e7112009-08-03 10:59:47 +10001723 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1724 mdname(mddev));
1725 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001726}
Andre Nollac5e7112009-08-03 10:59:47 +10001727EXPORT_SYMBOL(md_integrity_register);
1728
1729/* Disable data integrity if non-capable/non-matching disk is being added */
1730void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1731{
1732 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1733 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1734
1735 if (!bi_mddev) /* nothing to do */
1736 return;
1737 if (rdev->raid_disk < 0) /* skip spares */
1738 return;
1739 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1740 rdev->bdev->bd_disk) >= 0)
1741 return;
1742 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1743 blk_integrity_unregister(mddev->gendisk);
1744}
1745EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1748{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001749 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001750 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001751 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001752 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 if (rdev->mddev) {
1755 MD_BUG();
1756 return -EINVAL;
1757 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001758
1759 /* prevent duplicates */
1760 if (find_rdev(mddev, rdev->bdev->bd_dev))
1761 return -EEXIST;
1762
Andre Nolldd8ac332009-03-31 14:33:13 +11001763 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1764 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1765 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001766 if (mddev->pers) {
1767 /* Cannot change size, so fail
1768 * If mddev->level <= 0, then we don't care
1769 * about aligning sizes (e.g. linear)
1770 */
1771 if (mddev->level > 0)
1772 return -ENOSPC;
1773 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001774 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
1777 /* Verify rdev->desc_nr is unique.
1778 * If it is -1, assign a free number, else
1779 * check number is not in use
1780 */
1781 if (rdev->desc_nr < 0) {
1782 int choice = 0;
1783 if (mddev->pers) choice = mddev->raid_disks;
1784 while (find_rdev_nr(mddev, choice))
1785 choice++;
1786 rdev->desc_nr = choice;
1787 } else {
1788 if (find_rdev_nr(mddev, rdev->desc_nr))
1789 return -EBUSY;
1790 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001791 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1792 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1793 mdname(mddev), mddev->max_disks);
1794 return -EBUSY;
1795 }
NeilBrown19133a42005-11-08 21:39:35 -08001796 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001797 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001798 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001801 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001802
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001803 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001804 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001805
Tejun Heo0762b8b2008-08-25 19:56:12 +09001806 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10001807 if (sysfs_create_link(&rdev->kobj, ko, "block"))
1808 /* failure here is OK */;
1809 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11001810
NeilBrown4b809912008-07-21 17:05:25 +10001811 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001812 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001813
1814 /* May as well allow recovery to be retried once */
1815 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001818
1819 fail:
1820 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1821 b, mdname(mddev));
1822 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
1824
NeilBrown177a99b2008-02-06 01:39:56 -08001825static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001826{
1827 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1828 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001829 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001830}
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1833{
1834 char b[BDEVNAME_SIZE];
1835 if (!rdev->mddev) {
1836 MD_BUG();
1837 return;
1838 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001839 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001840 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1842 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001843 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001844 sysfs_put(rdev->sysfs_state);
1845 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001846 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001847 * writing to 'remove' to "dev/state". We also need
1848 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001849 */
NeilBrown4b809912008-07-21 17:05:25 +10001850 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001851 INIT_WORK(&rdev->del_work, md_delayed_delete);
1852 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001853 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855
1856/*
1857 * prevent the device from being mounted, repartitioned or
1858 * otherwise reused by a RAID array (or any other kernel
1859 * subsystem), by bd_claiming the device.
1860 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001861static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
1863 int err = 0;
1864 struct block_device *bdev;
1865 char b[BDEVNAME_SIZE];
1866
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001867 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 if (IS_ERR(bdev)) {
1869 printk(KERN_ERR "md: could not open %s.\n",
1870 __bdevname(dev, b));
1871 return PTR_ERR(bdev);
1872 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001873 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (err) {
1875 printk(KERN_ERR "md: could not bd_claim %s.\n",
1876 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001877 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 return err;
1879 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001880 if (!shared)
1881 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 rdev->bdev = bdev;
1883 return err;
1884}
1885
1886static void unlock_rdev(mdk_rdev_t *rdev)
1887{
1888 struct block_device *bdev = rdev->bdev;
1889 rdev->bdev = NULL;
1890 if (!bdev)
1891 MD_BUG();
1892 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001893 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
1895
1896void md_autodetect_dev(dev_t dev);
1897
1898static void export_rdev(mdk_rdev_t * rdev)
1899{
1900 char b[BDEVNAME_SIZE];
1901 printk(KERN_INFO "md: export_rdev(%s)\n",
1902 bdevname(rdev->bdev,b));
1903 if (rdev->mddev)
1904 MD_BUG();
1905 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001907 if (test_bit(AutoDetected, &rdev->flags))
1908 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909#endif
1910 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001911 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912}
1913
1914static void kick_rdev_from_array(mdk_rdev_t * rdev)
1915{
1916 unbind_rdev_from_array(rdev);
1917 export_rdev(rdev);
1918}
1919
1920static void export_array(mddev_t *mddev)
1921{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001922 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
NeilBrownd089c6a2008-02-06 01:39:59 -08001924 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 if (!rdev->mddev) {
1926 MD_BUG();
1927 continue;
1928 }
1929 kick_rdev_from_array(rdev);
1930 }
1931 if (!list_empty(&mddev->disks))
1932 MD_BUG();
1933 mddev->raid_disks = 0;
1934 mddev->major_version = 0;
1935}
1936
1937static void print_desc(mdp_disk_t *desc)
1938{
1939 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1940 desc->major,desc->minor,desc->raid_disk,desc->state);
1941}
1942
Cheng Renquancd2ac932009-01-09 08:31:08 +11001943static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944{
1945 int i;
1946
1947 printk(KERN_INFO
1948 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1949 sb->major_version, sb->minor_version, sb->patch_version,
1950 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1951 sb->ctime);
1952 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1953 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1954 sb->md_minor, sb->layout, sb->chunk_size);
1955 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1956 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1957 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1958 sb->failed_disks, sb->spare_disks,
1959 sb->sb_csum, (unsigned long)sb->events_lo);
1960
1961 printk(KERN_INFO);
1962 for (i = 0; i < MD_SB_DISKS; i++) {
1963 mdp_disk_t *desc;
1964
1965 desc = sb->disks + i;
1966 if (desc->number || desc->major || desc->minor ||
1967 desc->raid_disk || (desc->state && (desc->state != 4))) {
1968 printk(" D %2d: ", i);
1969 print_desc(desc);
1970 }
1971 }
1972 printk(KERN_INFO "md: THIS: ");
1973 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974}
1975
Cheng Renquancd2ac932009-01-09 08:31:08 +11001976static void print_sb_1(struct mdp_superblock_1 *sb)
1977{
1978 __u8 *uuid;
1979
1980 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001981 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08001982 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001983 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001984 le32_to_cpu(sb->major_version),
1985 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08001986 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11001987 sb->set_name,
1988 (unsigned long long)le64_to_cpu(sb->ctime)
1989 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1990
1991 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001992 printk(KERN_INFO
1993 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11001994 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08001995 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001996 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1997 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001998 le32_to_cpu(sb->level),
1999 (unsigned long long)le64_to_cpu(sb->size),
2000 le32_to_cpu(sb->raid_disks),
2001 le32_to_cpu(sb->layout),
2002 le32_to_cpu(sb->chunksize),
2003 (unsigned long long)le64_to_cpu(sb->data_offset),
2004 (unsigned long long)le64_to_cpu(sb->data_size),
2005 (unsigned long long)le64_to_cpu(sb->super_offset),
2006 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2007 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002008 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002009 sb->devflags,
2010 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2011 (unsigned long long)le64_to_cpu(sb->events),
2012 (unsigned long long)le64_to_cpu(sb->resync_offset),
2013 le32_to_cpu(sb->sb_csum),
2014 le32_to_cpu(sb->max_dev)
2015 );
2016}
2017
2018static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
2020 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002021 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2022 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002023 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2024 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002026 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2027 switch (major_version) {
2028 case 0:
2029 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
2030 break;
2031 case 1:
2032 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
2033 break;
2034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 } else
2036 printk(KERN_INFO "md: no rdev superblock!\n");
2037}
2038
Adrian Bunk5e563412006-06-26 00:27:42 -07002039static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002041 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 mdk_rdev_t *rdev;
2043 mddev_t *mddev;
2044 char b[BDEVNAME_SIZE];
2045
2046 printk("\n");
2047 printk("md: **********************************\n");
2048 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2049 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002050 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
NeilBrown32a76272005-06-21 17:17:14 -07002052 if (mddev->bitmap)
2053 bitmap_print_sb(mddev->bitmap);
2054 else
2055 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002056 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 printk("<%s>", bdevname(rdev->bdev,b));
2058 printk("\n");
2059
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002060 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002061 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
2063 printk("md: **********************************\n");
2064 printk("\n");
2065}
2066
2067
NeilBrown42543762006-06-26 00:27:57 -07002068static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069{
NeilBrown42543762006-06-26 00:27:57 -07002070 /* Update each superblock (in-memory image), but
2071 * if we are allowed to, skip spares which already
2072 * have the right event counter, or have one earlier
2073 * (which would mean they aren't being marked as dirty
2074 * with the rest of the array)
2075 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 mdk_rdev_t *rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002077 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002078 if (rdev->sb_events == mddev->events ||
2079 (nospares &&
2080 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002081 rdev->sb_events+1 == mddev->events)) {
2082 /* Don't update this superblock */
2083 rdev->sb_loaded = 2;
2084 } else {
2085 super_types[mddev->major_version].
2086 sync_super(mddev, rdev);
2087 rdev->sb_loaded = 1;
2088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 }
2090}
2091
NeilBrown850b2b422006-10-03 01:15:46 -07002092static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002095 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002096 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002099 /* First make sure individual recovery_offsets are correct */
2100 list_for_each_entry(rdev, &mddev->disks, same_set) {
2101 if (rdev->raid_disk >= 0 &&
2102 mddev->delta_disks >= 0 &&
2103 !test_bit(In_sync, &rdev->flags) &&
2104 mddev->curr_resync_completed > rdev->recovery_offset)
2105 rdev->recovery_offset = mddev->curr_resync_completed;
2106
2107 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002108 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002109 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002110 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownd97a41d2010-10-28 17:30:20 +11002111 if (!mddev->external)
2112 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002113 wake_up(&mddev->sb_wait);
2114 return;
2115 }
2116
NeilBrowna9701a32005-11-08 21:39:34 -08002117 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002118
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002119 mddev->utime = get_seconds();
2120
NeilBrown850b2b422006-10-03 01:15:46 -07002121 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2122 force_change = 1;
2123 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2124 /* just a clean<-> dirty transition, possibly leave spares alone,
2125 * though if events isn't the right even/odd, we will have to do
2126 * spares after all
2127 */
2128 nospares = 1;
2129 if (force_change)
2130 nospares = 0;
2131 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002132 /* If the array is degraded, then skipping spares is both
2133 * dangerous and fairly pointless.
2134 * Dangerous because a device that was removed from the array
2135 * might have a event_count that still looks up-to-date,
2136 * so it can be re-added without a resync.
2137 * Pointless because if there are any spares to skip,
2138 * then a recovery will happen and soon that array won't
2139 * be degraded any more and the spare can go back to sleep then.
2140 */
NeilBrown850b2b422006-10-03 01:15:46 -07002141 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002142
NeilBrown06d91a52005-06-21 17:17:12 -07002143 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002144
2145 /* If this is just a dirty<->clean transition, and the array is clean
2146 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002147 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002148 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002149 && mddev->can_decrease_events
2150 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002151 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002152 mddev->can_decrease_events = 0;
2153 } else {
NeilBrown42543762006-06-26 00:27:57 -07002154 /* otherwise we have to go forward and ... */
2155 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002156 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
2159 if (!mddev->events) {
2160 /*
2161 * oops, this 64-bit counter should never wrap.
2162 * Either we are in around ~1 trillion A.C., assuming
2163 * 1 reboot per second, or we have a bug:
2164 */
2165 MD_BUG();
2166 mddev->events --;
2167 }
NeilBrowne6910632008-02-06 01:39:51 -08002168 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002169 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
2171 dprintk(KERN_INFO
2172 "md: updating %s RAID superblock on device (in sync %d)\n",
2173 mdname(mddev),mddev->in_sync);
2174
NeilBrown4ad13662007-07-17 04:06:13 -07002175 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002176 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 char b[BDEVNAME_SIZE];
2178 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002179 if (rdev->sb_loaded != 1)
2180 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002181 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 dprintk("(skipping faulty ");
2183
2184 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002185 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002186 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002187 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002188 rdev->sb_page);
2189 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2190 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002191 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002192 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 } else
2195 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002196 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 /* only need to write one superblock... */
2198 break;
2199 }
NeilBrowna9701a32005-11-08 21:39:34 -08002200 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002201 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002202
NeilBrowna9701a32005-11-08 21:39:34 -08002203 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b422006-10-03 01:15:46 -07002204 if (mddev->in_sync != sync_req ||
2205 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002206 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002207 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002208 goto repeat;
2209 }
NeilBrown850b2b422006-10-03 01:15:46 -07002210 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002211 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002212 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002213 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2214 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}
2217
Andre Noll7f6ce762008-03-23 18:34:54 +01002218/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002219 * We want to accept with case. For this we use cmd_match.
2220 */
2221static int cmd_match(const char *cmd, const char *str)
2222{
2223 /* See if cmd, written into a sysfs file, matches
2224 * str. They must either be the same, or cmd can
2225 * have a trailing newline
2226 */
2227 while (*cmd && *str && *cmd == *str) {
2228 cmd++;
2229 str++;
2230 }
2231 if (*cmd == '\n')
2232 cmd++;
2233 if (*str || *cmd)
2234 return 0;
2235 return 1;
2236}
2237
NeilBrown86e6ffd2005-11-08 21:39:24 -08002238struct rdev_sysfs_entry {
2239 struct attribute attr;
2240 ssize_t (*show)(mdk_rdev_t *, char *);
2241 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2242};
2243
2244static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002245state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002246{
2247 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002248 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002249
NeilBrownb2d444d2005-11-08 21:39:31 -08002250 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002251 len+= sprintf(page+len, "%sfaulty",sep);
2252 sep = ",";
2253 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002254 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002255 len += sprintf(page+len, "%sin_sync",sep);
2256 sep = ",";
2257 }
NeilBrownf6556752006-06-26 00:28:01 -07002258 if (test_bit(WriteMostly, &rdev->flags)) {
2259 len += sprintf(page+len, "%swrite_mostly",sep);
2260 sep = ",";
2261 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002262 if (test_bit(Blocked, &rdev->flags)) {
2263 len += sprintf(page+len, "%sblocked", sep);
2264 sep = ",";
2265 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002266 if (!test_bit(Faulty, &rdev->flags) &&
2267 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002268 len += sprintf(page+len, "%sspare", sep);
2269 sep = ",";
2270 }
2271 return len+sprintf(page+len, "\n");
2272}
2273
NeilBrown45dc2de2006-06-26 00:27:58 -07002274static ssize_t
2275state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2276{
2277 /* can write
2278 * faulty - simulates and error
2279 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002280 * writemostly - sets write_mostly
2281 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002282 * blocked - sets the Blocked flag
2283 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002284 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002285 */
2286 int err = -EINVAL;
2287 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2288 md_error(rdev->mddev, rdev);
2289 err = 0;
2290 } else if (cmd_match(buf, "remove")) {
2291 if (rdev->raid_disk >= 0)
2292 err = -EBUSY;
2293 else {
2294 mddev_t *mddev = rdev->mddev;
2295 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002296 if (mddev->pers)
2297 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002298 md_new_event(mddev);
2299 err = 0;
2300 }
NeilBrownf6556752006-06-26 00:28:01 -07002301 } else if (cmd_match(buf, "writemostly")) {
2302 set_bit(WriteMostly, &rdev->flags);
2303 err = 0;
2304 } else if (cmd_match(buf, "-writemostly")) {
2305 clear_bit(WriteMostly, &rdev->flags);
2306 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002307 } else if (cmd_match(buf, "blocked")) {
2308 set_bit(Blocked, &rdev->flags);
2309 err = 0;
2310 } else if (cmd_match(buf, "-blocked")) {
2311 clear_bit(Blocked, &rdev->flags);
2312 wake_up(&rdev->blocked_wait);
2313 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2314 md_wakeup_thread(rdev->mddev->thread);
2315
2316 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002317 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2318 set_bit(In_sync, &rdev->flags);
2319 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002320 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002321 if (!err)
2322 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002323 return err ? err : len;
2324}
NeilBrown80ca3a42006-07-10 04:44:18 -07002325static struct rdev_sysfs_entry rdev_state =
2326__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002327
2328static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002329errors_show(mdk_rdev_t *rdev, char *page)
2330{
2331 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2332}
2333
2334static ssize_t
2335errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2336{
2337 char *e;
2338 unsigned long n = simple_strtoul(buf, &e, 10);
2339 if (*buf && (*e == 0 || *e == '\n')) {
2340 atomic_set(&rdev->corrected_errors, n);
2341 return len;
2342 }
2343 return -EINVAL;
2344}
2345static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002346__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002347
NeilBrown014236d2006-01-06 00:20:55 -08002348static ssize_t
2349slot_show(mdk_rdev_t *rdev, char *page)
2350{
2351 if (rdev->raid_disk < 0)
2352 return sprintf(page, "none\n");
2353 else
2354 return sprintf(page, "%d\n", rdev->raid_disk);
2355}
2356
2357static ssize_t
2358slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2359{
2360 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002361 int err;
2362 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002363 int slot = simple_strtoul(buf, &e, 10);
2364 if (strncmp(buf, "none", 4)==0)
2365 slot = -1;
2366 else if (e==buf || (*e && *e!= '\n'))
2367 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002368 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002369 /* Setting 'slot' on an active array requires also
2370 * updating the 'rd%d' link, and communicating
2371 * with the personality with ->hot_*_disk.
2372 * For now we only support removing
2373 * failed/spare devices. This normally happens automatically,
2374 * but not when the metadata is externally managed.
2375 */
NeilBrownc303da62008-02-06 01:39:51 -08002376 if (rdev->raid_disk == -1)
2377 return -EEXIST;
2378 /* personality does all needed checks */
2379 if (rdev->mddev->pers->hot_add_disk == NULL)
2380 return -EINVAL;
2381 err = rdev->mddev->pers->
2382 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2383 if (err)
2384 return err;
2385 sprintf(nm, "rd%d", rdev->raid_disk);
2386 sysfs_remove_link(&rdev->mddev->kobj, nm);
Maciej Trelab7103102010-04-14 16:58:16 +10002387 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002388 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2389 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002390 } else if (rdev->mddev->pers) {
2391 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002392 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002393 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002394 */
2395
2396 if (rdev->raid_disk != -1)
2397 return -EBUSY;
2398
2399 if (rdev->mddev->pers->hot_add_disk == NULL)
2400 return -EINVAL;
2401
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002402 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002403 if (rdev2->raid_disk == slot)
2404 return -EEXIST;
2405
2406 rdev->raid_disk = slot;
2407 if (test_bit(In_sync, &rdev->flags))
2408 rdev->saved_raid_disk = slot;
2409 else
2410 rdev->saved_raid_disk = -1;
2411 err = rdev->mddev->pers->
2412 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002413 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002414 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002415 return err;
Neil Brown52664732008-06-28 08:31:44 +10002416 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002417 sysfs_notify_dirent_safe(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002418 sprintf(nm, "rd%d", rdev->raid_disk);
2419 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002420 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002421 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002422 } else {
2423 if (slot >= rdev->mddev->raid_disks)
2424 return -ENOSPC;
2425 rdev->raid_disk = slot;
2426 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002427 clear_bit(Faulty, &rdev->flags);
2428 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002429 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002430 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002431 }
NeilBrown014236d2006-01-06 00:20:55 -08002432 return len;
2433}
2434
2435
2436static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002437__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002438
NeilBrown93c8cad2006-01-06 00:20:56 -08002439static ssize_t
2440offset_show(mdk_rdev_t *rdev, char *page)
2441{
Andrew Morton6961ece2006-01-06 00:20:59 -08002442 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002443}
2444
2445static ssize_t
2446offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2447{
2448 char *e;
2449 unsigned long long offset = simple_strtoull(buf, &e, 10);
2450 if (e==buf || (*e && *e != '\n'))
2451 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002452 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002453 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002454 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002455 /* Must set offset before size, so overlap checks
2456 * can be sane */
2457 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002458 rdev->data_offset = offset;
2459 return len;
2460}
2461
2462static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002463__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002464
NeilBrown83303b62006-01-06 00:21:06 -08002465static ssize_t
2466rdev_size_show(mdk_rdev_t *rdev, char *page)
2467{
Andre Nolldd8ac332009-03-31 14:33:13 +11002468 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002469}
2470
NeilBrownc5d79ad2008-02-06 01:39:54 -08002471static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2472{
2473 /* check if two start/length pairs overlap */
2474 if (s1+l1 <= s2)
2475 return 0;
2476 if (s2+l2 <= s1)
2477 return 0;
2478 return 1;
2479}
2480
Dan Williamsb522adc2009-03-31 15:00:31 +11002481static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2482{
2483 unsigned long long blocks;
2484 sector_t new;
2485
2486 if (strict_strtoull(buf, 10, &blocks) < 0)
2487 return -EINVAL;
2488
2489 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2490 return -EINVAL; /* sector conversion overflow */
2491
2492 new = blocks * 2;
2493 if (new != blocks * 2)
2494 return -EINVAL; /* unsigned long long to sector_t overflow */
2495
2496 *sectors = new;
2497 return 0;
2498}
2499
NeilBrown83303b62006-01-06 00:21:06 -08002500static ssize_t
2501rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2502{
NeilBrown27c529b2008-03-04 14:29:33 -08002503 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002504 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002505 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002506
Dan Williamsb522adc2009-03-31 15:00:31 +11002507 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002508 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002509 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002510 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002511 sectors = super_types[my_mddev->major_version].
2512 rdev_size_change(rdev, sectors);
2513 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002514 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002515 } else if (!sectors)
2516 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2517 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002518 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002519 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002520 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002521
Andre Nolldd8ac332009-03-31 14:33:13 +11002522 rdev->sectors = sectors;
2523 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002524 /* need to check that all other rdevs with the same ->bdev
2525 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002526 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002527 * we have to change it back, we will have the lock again.
2528 */
2529 mddev_t *mddev;
2530 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002531 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002532
NeilBrown27c529b2008-03-04 14:29:33 -08002533 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002534 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002535 mdk_rdev_t *rdev2;
2536
2537 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002538 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002539 if (test_bit(AllReserved, &rdev2->flags) ||
2540 (rdev->bdev == rdev2->bdev &&
2541 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002542 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002543 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002544 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002545 overlap = 1;
2546 break;
2547 }
2548 mddev_unlock(mddev);
2549 if (overlap) {
2550 mddev_put(mddev);
2551 break;
2552 }
2553 }
NeilBrown27c529b2008-03-04 14:29:33 -08002554 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002555 if (overlap) {
2556 /* Someone else could have slipped in a size
2557 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002558 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002559 * safe, and trust userspace not to race with
2560 * itself
2561 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002562 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002563 return -EBUSY;
2564 }
2565 }
NeilBrown83303b62006-01-06 00:21:06 -08002566 return len;
2567}
2568
2569static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002570__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002571
Dan Williams06e3c812009-12-12 21:17:12 -07002572
2573static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2574{
2575 unsigned long long recovery_start = rdev->recovery_offset;
2576
2577 if (test_bit(In_sync, &rdev->flags) ||
2578 recovery_start == MaxSector)
2579 return sprintf(page, "none\n");
2580
2581 return sprintf(page, "%llu\n", recovery_start);
2582}
2583
2584static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2585{
2586 unsigned long long recovery_start;
2587
2588 if (cmd_match(buf, "none"))
2589 recovery_start = MaxSector;
2590 else if (strict_strtoull(buf, 10, &recovery_start))
2591 return -EINVAL;
2592
2593 if (rdev->mddev->pers &&
2594 rdev->raid_disk >= 0)
2595 return -EBUSY;
2596
2597 rdev->recovery_offset = recovery_start;
2598 if (recovery_start == MaxSector)
2599 set_bit(In_sync, &rdev->flags);
2600 else
2601 clear_bit(In_sync, &rdev->flags);
2602 return len;
2603}
2604
2605static struct rdev_sysfs_entry rdev_recovery_start =
2606__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2607
NeilBrown86e6ffd2005-11-08 21:39:24 -08002608static struct attribute *rdev_default_attrs[] = {
2609 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002610 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002611 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002612 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002613 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002614 &rdev_recovery_start.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002615 NULL,
2616};
2617static ssize_t
2618rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2619{
2620 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2621 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002622 mddev_t *mddev = rdev->mddev;
2623 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002624
2625 if (!entry->show)
2626 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002627
2628 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2629 if (!rv) {
2630 if (rdev->mddev == NULL)
2631 rv = -EBUSY;
2632 else
2633 rv = entry->show(rdev, page);
2634 mddev_unlock(mddev);
2635 }
2636 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002637}
2638
2639static ssize_t
2640rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2641 const char *page, size_t length)
2642{
2643 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2644 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002645 ssize_t rv;
2646 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002647
2648 if (!entry->store)
2649 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002650 if (!capable(CAP_SYS_ADMIN))
2651 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002652 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002653 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002654 if (rdev->mddev == NULL)
2655 rv = -EBUSY;
2656 else
2657 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002658 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002659 }
2660 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002661}
2662
2663static void rdev_free(struct kobject *ko)
2664{
2665 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2666 kfree(rdev);
2667}
Emese Revfy52cf25d2010-01-19 02:58:23 +01002668static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002669 .show = rdev_attr_show,
2670 .store = rdev_attr_store,
2671};
2672static struct kobj_type rdev_ktype = {
2673 .release = rdev_free,
2674 .sysfs_ops = &rdev_sysfs_ops,
2675 .default_attrs = rdev_default_attrs,
2676};
2677
NeilBrowne8bb9a82010-06-01 19:37:26 +10002678void md_rdev_init(mdk_rdev_t *rdev)
2679{
2680 rdev->desc_nr = -1;
2681 rdev->saved_raid_disk = -1;
2682 rdev->raid_disk = -1;
2683 rdev->flags = 0;
2684 rdev->data_offset = 0;
2685 rdev->sb_events = 0;
2686 rdev->last_read_error.tv_sec = 0;
2687 rdev->last_read_error.tv_nsec = 0;
2688 atomic_set(&rdev->nr_pending, 0);
2689 atomic_set(&rdev->read_errors, 0);
2690 atomic_set(&rdev->corrected_errors, 0);
2691
2692 INIT_LIST_HEAD(&rdev->same_set);
2693 init_waitqueue_head(&rdev->blocked_wait);
2694}
2695EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696/*
2697 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2698 *
2699 * mark the device faulty if:
2700 *
2701 * - the device is nonexistent (zero size)
2702 * - the device has no valid superblock
2703 *
2704 * a faulty rdev _never_ has rdev->sb set.
2705 */
2706static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2707{
2708 char b[BDEVNAME_SIZE];
2709 int err;
2710 mdk_rdev_t *rdev;
2711 sector_t size;
2712
NeilBrown9ffae0c2006-01-06 00:20:32 -08002713 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 if (!rdev) {
2715 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2716 return ERR_PTR(-ENOMEM);
2717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
NeilBrowne8bb9a82010-06-01 19:37:26 +10002719 md_rdev_init(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 if ((err = alloc_disk_sb(rdev)))
2721 goto abort_free;
2722
NeilBrownc5d79ad2008-02-06 01:39:54 -08002723 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 if (err)
2725 goto abort_free;
2726
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002727 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002728
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2730 if (!size) {
2731 printk(KERN_WARNING
2732 "md: %s has zero or unknown size, marking faulty!\n",
2733 bdevname(rdev->bdev,b));
2734 err = -EINVAL;
2735 goto abort_free;
2736 }
2737
2738 if (super_format >= 0) {
2739 err = super_types[super_format].
2740 load_super(rdev, NULL, super_minor);
2741 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002742 printk(KERN_WARNING
2743 "md: %s does not have a valid v%d.%d "
2744 "superblock, not importing!\n",
2745 bdevname(rdev->bdev,b),
2746 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 goto abort_free;
2748 }
2749 if (err < 0) {
2750 printk(KERN_WARNING
2751 "md: could not read %s's sb, not importing!\n",
2752 bdevname(rdev->bdev,b));
2753 goto abort_free;
2754 }
2755 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002756
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 return rdev;
2758
2759abort_free:
2760 if (rdev->sb_page) {
2761 if (rdev->bdev)
2762 unlock_rdev(rdev);
2763 free_disk_sb(rdev);
2764 }
2765 kfree(rdev);
2766 return ERR_PTR(err);
2767}
2768
2769/*
2770 * Check a full RAID array for plausibility
2771 */
2772
2773
NeilBrowna757e642005-04-16 15:26:42 -07002774static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775{
2776 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002777 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 char b[BDEVNAME_SIZE];
2779
2780 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002781 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 switch (super_types[mddev->major_version].
2783 load_super(rdev, freshest, mddev->minor_version)) {
2784 case 1:
2785 freshest = rdev;
2786 break;
2787 case 0:
2788 break;
2789 default:
2790 printk( KERN_ERR \
2791 "md: fatal superblock inconsistency in %s"
2792 " -- removing from array\n",
2793 bdevname(rdev->bdev,b));
2794 kick_rdev_from_array(rdev);
2795 }
2796
2797
2798 super_types[mddev->major_version].
2799 validate_super(mddev, freshest);
2800
2801 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002802 rdev_for_each(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10002803 if (mddev->max_disks &&
2804 (rdev->desc_nr >= mddev->max_disks ||
2805 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002806 printk(KERN_WARNING
2807 "md: %s: %s: only %d devices permitted\n",
2808 mdname(mddev), bdevname(rdev->bdev, b),
2809 mddev->max_disks);
2810 kick_rdev_from_array(rdev);
2811 continue;
2812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 if (rdev != freshest)
2814 if (super_types[mddev->major_version].
2815 validate_super(mddev, rdev)) {
2816 printk(KERN_WARNING "md: kicking non-fresh %s"
2817 " from array!\n",
2818 bdevname(rdev->bdev,b));
2819 kick_rdev_from_array(rdev);
2820 continue;
2821 }
2822 if (mddev->level == LEVEL_MULTIPATH) {
2823 rdev->desc_nr = i++;
2824 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002825 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002826 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002827 rdev->raid_disk = -1;
2828 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 }
2830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831}
2832
NeilBrown72e02072009-12-14 12:49:55 +11002833/* Read a fixed-point number.
2834 * Numbers in sysfs attributes should be in "standard" units where
2835 * possible, so time should be in seconds.
2836 * However we internally use a a much smaller unit such as
2837 * milliseconds or jiffies.
2838 * This function takes a decimal number with a possible fractional
2839 * component, and produces an integer which is the result of
2840 * multiplying that number by 10^'scale'.
2841 * all without any floating-point arithmetic.
2842 */
2843int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2844{
2845 unsigned long result = 0;
2846 long decimals = -1;
2847 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2848 if (*cp == '.')
2849 decimals = 0;
2850 else if (decimals < scale) {
2851 unsigned int value;
2852 value = *cp - '0';
2853 result = result * 10 + value;
2854 if (decimals >= 0)
2855 decimals++;
2856 }
2857 cp++;
2858 }
2859 if (*cp == '\n')
2860 cp++;
2861 if (*cp)
2862 return -EINVAL;
2863 if (decimals < 0)
2864 decimals = 0;
2865 while (decimals < scale) {
2866 result *= 10;
2867 decimals ++;
2868 }
2869 *res = result;
2870 return 0;
2871}
2872
2873
NeilBrown19052c02008-08-05 15:54:13 +10002874static void md_safemode_timeout(unsigned long data);
2875
NeilBrowneae17012005-11-08 21:39:23 -08002876static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002877safe_delay_show(mddev_t *mddev, char *page)
2878{
2879 int msec = (mddev->safemode_delay*1000)/HZ;
2880 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2881}
2882static ssize_t
2883safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2884{
NeilBrown16f17b32006-06-26 00:27:37 -07002885 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07002886
NeilBrown72e02072009-12-14 12:49:55 +11002887 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002888 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07002889 if (msec == 0)
2890 mddev->safemode_delay = 0;
2891 else {
NeilBrown19052c02008-08-05 15:54:13 +10002892 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002893 mddev->safemode_delay = (msec*HZ)/1000;
2894 if (mddev->safemode_delay == 0)
2895 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002896 if (mddev->safemode_delay < old_delay)
2897 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002898 }
2899 return len;
2900}
2901static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002902__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002903
2904static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002905level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002906{
NeilBrown2604b702006-01-06 00:20:36 -08002907 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002908 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002909 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002910 else if (mddev->clevel[0])
2911 return sprintf(page, "%s\n", mddev->clevel);
2912 else if (mddev->level != LEVEL_NONE)
2913 return sprintf(page, "%d\n", mddev->level);
2914 else
2915 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002916}
2917
NeilBrownd9d166c2006-01-06 00:20:51 -08002918static ssize_t
2919level_store(mddev_t *mddev, const char *buf, size_t len)
2920{
Dan Williamsf2859af2010-05-02 10:04:16 -07002921 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002922 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002923 struct mdk_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07002924 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11002925 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002926 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002927
2928 if (mddev->pers == NULL) {
2929 if (len == 0)
2930 return 0;
2931 if (len >= sizeof(mddev->clevel))
2932 return -ENOSPC;
2933 strncpy(mddev->clevel, buf, len);
2934 if (mddev->clevel[len-1] == '\n')
2935 len--;
2936 mddev->clevel[len] = 0;
2937 mddev->level = LEVEL_NONE;
2938 return rv;
2939 }
2940
2941 /* request to change the personality. Need to ensure:
2942 * - array is not engaged in resync/recovery/reshape
2943 * - old personality can be suspended
2944 * - new personality will access other array.
2945 */
2946
NeilBrownbb4f1e92010-08-08 21:18:03 +10002947 if (mddev->sync_thread ||
2948 mddev->reshape_position != MaxSector ||
2949 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08002950 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002951
2952 if (!mddev->pers->quiesce) {
2953 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2954 mdname(mddev), mddev->pers->name);
2955 return -EINVAL;
2956 }
2957
2958 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07002959 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11002960 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07002961 strncpy(clevel, buf, len);
2962 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002963 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07002964 clevel[len] = 0;
2965 if (strict_strtol(clevel, 10, &level))
2966 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11002967
Dan Williamsf2859af2010-05-02 10:04:16 -07002968 if (request_module("md-%s", clevel) != 0)
2969 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11002970 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07002971 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11002972 if (!pers || !try_module_get(pers->owner)) {
2973 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07002974 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11002975 return -EINVAL;
2976 }
2977 spin_unlock(&pers_lock);
2978
2979 if (pers == mddev->pers) {
2980 /* Nothing to do! */
2981 module_put(pers->owner);
2982 return rv;
2983 }
2984 if (!pers->takeover) {
2985 module_put(pers->owner);
2986 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07002987 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11002988 return -EINVAL;
2989 }
2990
NeilBrowne93f68a2010-06-15 09:36:03 +01002991 list_for_each_entry(rdev, &mddev->disks, same_set)
2992 rdev->new_raid_disk = rdev->raid_disk;
2993
NeilBrown245f46c2009-03-31 14:39:39 +11002994 /* ->takeover must set new_* and/or delta_disks
2995 * if it succeeds, and may set them when it fails.
2996 */
2997 priv = pers->takeover(mddev);
2998 if (IS_ERR(priv)) {
2999 mddev->new_level = mddev->level;
3000 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003001 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003002 mddev->raid_disks -= mddev->delta_disks;
3003 mddev->delta_disks = 0;
3004 module_put(pers->owner);
3005 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003006 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003007 return PTR_ERR(priv);
3008 }
3009
3010 /* Looks like we have a winner */
3011 mddev_suspend(mddev);
3012 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003013
3014 if (mddev->pers->sync_request == NULL &&
3015 pers->sync_request != NULL) {
3016 /* need to add the md_redundancy_group */
3017 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3018 printk(KERN_WARNING
3019 "md: cannot register extra attributes for %s\n",
3020 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003021 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003022 }
3023 if (mddev->pers->sync_request != NULL &&
3024 pers->sync_request == NULL) {
3025 /* need to remove the md_redundancy_group */
3026 if (mddev->to_remove == NULL)
3027 mddev->to_remove = &md_redundancy_group;
3028 }
3029
Trela Maciej54071b32010-03-08 16:02:42 +11003030 if (mddev->pers->sync_request == NULL &&
3031 mddev->external) {
3032 /* We are converting from a no-redundancy array
3033 * to a redundancy array and metadata is managed
3034 * externally so we need to be sure that writes
3035 * won't block due to a need to transition
3036 * clean->dirty
3037 * until external management is started.
3038 */
3039 mddev->in_sync = 0;
3040 mddev->safemode_delay = 0;
3041 mddev->safemode = 0;
3042 }
3043
NeilBrowne93f68a2010-06-15 09:36:03 +01003044 list_for_each_entry(rdev, &mddev->disks, same_set) {
3045 char nm[20];
3046 if (rdev->raid_disk < 0)
3047 continue;
3048 if (rdev->new_raid_disk > mddev->raid_disks)
3049 rdev->new_raid_disk = -1;
3050 if (rdev->new_raid_disk == rdev->raid_disk)
3051 continue;
3052 sprintf(nm, "rd%d", rdev->raid_disk);
3053 sysfs_remove_link(&mddev->kobj, nm);
3054 }
3055 list_for_each_entry(rdev, &mddev->disks, same_set) {
3056 if (rdev->raid_disk < 0)
3057 continue;
3058 if (rdev->new_raid_disk == rdev->raid_disk)
3059 continue;
3060 rdev->raid_disk = rdev->new_raid_disk;
3061 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003062 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003063 else {
3064 char nm[20];
3065 sprintf(nm, "rd%d", rdev->raid_disk);
3066 if(sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3067 printk("md: cannot register %s for %s after level change\n",
3068 nm, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003069 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003070 }
3071
3072 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003073 mddev->pers = pers;
3074 mddev->private = priv;
3075 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3076 mddev->level = mddev->new_level;
3077 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003078 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003079 mddev->delta_disks = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003080 if (mddev->pers->sync_request == NULL) {
3081 /* this is now an array without redundancy, so
3082 * it must always be in_sync
3083 */
3084 mddev->in_sync = 1;
3085 del_timer_sync(&mddev->safemode_timer);
3086 }
NeilBrown245f46c2009-03-31 14:39:39 +11003087 pers->run(mddev);
3088 mddev_resume(mddev);
3089 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3090 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3091 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003092 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003093 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003094 return rv;
3095}
3096
3097static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003098__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003099
NeilBrownd4dbd022006-06-26 00:27:59 -07003100
3101static ssize_t
3102layout_show(mddev_t *mddev, char *page)
3103{
3104 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003105 if (mddev->reshape_position != MaxSector &&
3106 mddev->layout != mddev->new_layout)
3107 return sprintf(page, "%d (%d)\n",
3108 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003109 return sprintf(page, "%d\n", mddev->layout);
3110}
3111
3112static ssize_t
3113layout_store(mddev_t *mddev, const char *buf, size_t len)
3114{
3115 char *e;
3116 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003117
3118 if (!*buf || (*e && *e != '\n'))
3119 return -EINVAL;
3120
NeilBrownb3546032009-03-31 14:56:41 +11003121 if (mddev->pers) {
3122 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003123 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003124 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003125 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003126 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003127 if (err) {
3128 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003129 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003130 }
NeilBrownb3546032009-03-31 14:56:41 +11003131 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003132 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003133 if (mddev->reshape_position == MaxSector)
3134 mddev->layout = n;
3135 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003136 return len;
3137}
3138static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003139__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003140
3141
NeilBrowneae17012005-11-08 21:39:23 -08003142static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003143raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003144{
NeilBrownbb636542005-11-08 21:39:45 -08003145 if (mddev->raid_disks == 0)
3146 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003147 if (mddev->reshape_position != MaxSector &&
3148 mddev->delta_disks != 0)
3149 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3150 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003151 return sprintf(page, "%d\n", mddev->raid_disks);
3152}
3153
NeilBrownda943b992006-01-06 00:20:54 -08003154static int update_raid_disks(mddev_t *mddev, int raid_disks);
3155
3156static ssize_t
3157raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3158{
NeilBrownda943b992006-01-06 00:20:54 -08003159 char *e;
3160 int rv = 0;
3161 unsigned long n = simple_strtoul(buf, &e, 10);
3162
3163 if (!*buf || (*e && *e != '\n'))
3164 return -EINVAL;
3165
3166 if (mddev->pers)
3167 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003168 else if (mddev->reshape_position != MaxSector) {
3169 int olddisks = mddev->raid_disks - mddev->delta_disks;
3170 mddev->delta_disks = n - olddisks;
3171 mddev->raid_disks = n;
3172 } else
NeilBrownda943b992006-01-06 00:20:54 -08003173 mddev->raid_disks = n;
3174 return rv ? rv : len;
3175}
3176static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003177__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003178
NeilBrown24dd4692005-11-08 21:39:26 -08003179static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003180chunk_size_show(mddev_t *mddev, char *page)
3181{
NeilBrown08a02ec2007-05-09 02:35:38 -07003182 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003183 mddev->chunk_sectors != mddev->new_chunk_sectors)
3184 return sprintf(page, "%d (%d)\n",
3185 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003186 mddev->chunk_sectors << 9);
3187 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003188}
3189
3190static ssize_t
3191chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3192{
NeilBrown3b343802006-01-06 00:20:47 -08003193 char *e;
3194 unsigned long n = simple_strtoul(buf, &e, 10);
3195
NeilBrown3b343802006-01-06 00:20:47 -08003196 if (!*buf || (*e && *e != '\n'))
3197 return -EINVAL;
3198
NeilBrownb3546032009-03-31 14:56:41 +11003199 if (mddev->pers) {
3200 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003201 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003202 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003203 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003204 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003205 if (err) {
3206 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003207 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003208 }
NeilBrownb3546032009-03-31 14:56:41 +11003209 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003210 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003211 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003212 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003213 }
NeilBrown3b343802006-01-06 00:20:47 -08003214 return len;
3215}
3216static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003217__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003218
NeilBrowna94213b2006-06-26 00:28:00 -07003219static ssize_t
3220resync_start_show(mddev_t *mddev, char *page)
3221{
NeilBrownd1a7c502009-03-31 15:24:32 +11003222 if (mddev->recovery_cp == MaxSector)
3223 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003224 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3225}
3226
3227static ssize_t
3228resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3229{
NeilBrowna94213b2006-06-26 00:28:00 -07003230 char *e;
3231 unsigned long long n = simple_strtoull(buf, &e, 10);
3232
3233 if (mddev->pers)
3234 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003235 if (cmd_match(buf, "none"))
3236 n = MaxSector;
3237 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003238 return -EINVAL;
3239
3240 mddev->recovery_cp = n;
3241 return len;
3242}
3243static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003244__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003245
NeilBrown9e653b62006-06-26 00:27:58 -07003246/*
3247 * The array state can be:
3248 *
3249 * clear
3250 * No devices, no size, no level
3251 * Equivalent to STOP_ARRAY ioctl
3252 * inactive
3253 * May have some settings, but array is not active
3254 * all IO results in error
3255 * When written, doesn't tear down array, but just stops it
3256 * suspended (not supported yet)
3257 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003258 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003259 * readonly
3260 * no resync can happen. no superblocks get written.
3261 * write requests fail
3262 * read-auto
3263 * like readonly, but behaves like 'clean' on a write request.
3264 *
3265 * clean - no pending writes, but otherwise active.
3266 * When written to inactive array, starts without resync
3267 * If a write request arrives then
3268 * if metadata is known, mark 'dirty' and switch to 'active'.
3269 * if not known, block and switch to write-pending
3270 * If written to an active array that has pending writes, then fails.
3271 * active
3272 * fully active: IO and resync can be happening.
3273 * When written to inactive array, starts with resync
3274 *
3275 * write-pending
3276 * clean, but writes are blocked waiting for 'active' to be written.
3277 *
3278 * active-idle
3279 * like active, but no writes have been seen for a while (100msec).
3280 *
3281 */
3282enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3283 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003284static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003285 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3286 "write-pending", "active-idle", NULL };
3287
3288static int match_word(const char *word, char **list)
3289{
3290 int n;
3291 for (n=0; list[n]; n++)
3292 if (cmd_match(word, list[n]))
3293 break;
3294 return n;
3295}
3296
3297static ssize_t
3298array_state_show(mddev_t *mddev, char *page)
3299{
3300 enum array_state st = inactive;
3301
3302 if (mddev->pers)
3303 switch(mddev->ro) {
3304 case 1:
3305 st = readonly;
3306 break;
3307 case 2:
3308 st = read_auto;
3309 break;
3310 case 0:
3311 if (mddev->in_sync)
3312 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003313 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003314 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003315 else if (mddev->safemode)
3316 st = active_idle;
3317 else
3318 st = active;
3319 }
3320 else {
3321 if (list_empty(&mddev->disks) &&
3322 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003323 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003324 st = clear;
3325 else
3326 st = inactive;
3327 }
3328 return sprintf(page, "%s\n", array_states[st]);
3329}
3330
Neil Browndf5b20c2008-07-11 22:02:22 +10003331static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrowna4bd82d2010-03-29 13:23:10 +11003332static int md_set_readonly(mddev_t * mddev, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003333static int do_md_run(mddev_t * mddev);
3334static int restart_array(mddev_t *mddev);
3335
3336static ssize_t
3337array_state_store(mddev_t *mddev, const char *buf, size_t len)
3338{
3339 int err = -EINVAL;
3340 enum array_state st = match_word(buf, array_states);
3341 switch(st) {
3342 case bad_word:
3343 break;
3344 case clear:
3345 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003346 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003347 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003348 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003349 break;
3350 case inactive:
3351 /* stopping an active array */
3352 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003353 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003354 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003355 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003356 } else
3357 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003358 break;
3359 case suspended:
3360 break; /* not supported yet */
3361 case readonly:
3362 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003363 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003364 else {
3365 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003366 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003367 err = do_md_run(mddev);
3368 }
3369 break;
3370 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003371 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003372 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003373 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003374 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003375 err = restart_array(mddev);
3376 if (err == 0) {
3377 mddev->ro = 2;
3378 set_disk_ro(mddev->gendisk, 0);
3379 }
NeilBrown9e653b62006-06-26 00:27:58 -07003380 } else {
3381 mddev->ro = 2;
3382 err = do_md_run(mddev);
3383 }
3384 break;
3385 case clean:
3386 if (mddev->pers) {
3387 restart_array(mddev);
3388 spin_lock_irq(&mddev->write_lock);
3389 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003390 if (mddev->in_sync == 0) {
3391 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003392 if (mddev->safemode == 1)
3393 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003394 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003395 }
3396 err = 0;
3397 } else
3398 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003399 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003400 } else
3401 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003402 break;
3403 case active:
3404 if (mddev->pers) {
3405 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003406 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003407 wake_up(&mddev->sb_wait);
3408 err = 0;
3409 } else {
3410 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003411 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003412 err = do_md_run(mddev);
3413 }
3414 break;
3415 case write_pending:
3416 case active_idle:
3417 /* these cannot be set */
3418 break;
3419 }
3420 if (err)
3421 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003422 else {
NeilBrown00bcb4a2010-06-01 19:37:23 +10003423 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003424 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003425 }
NeilBrown9e653b62006-06-26 00:27:58 -07003426}
NeilBrown80ca3a42006-07-10 04:44:18 -07003427static struct md_sysfs_entry md_array_state =
3428__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003429
NeilBrown6d7ff7382006-01-06 00:21:16 -08003430static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003431max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3432 return sprintf(page, "%d\n",
3433 atomic_read(&mddev->max_corr_read_errors));
3434}
3435
3436static ssize_t
3437max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3438{
3439 char *e;
3440 unsigned long n = simple_strtoul(buf, &e, 10);
3441
3442 if (*buf && (*e == 0 || *e == '\n')) {
3443 atomic_set(&mddev->max_corr_read_errors, n);
3444 return len;
3445 }
3446 return -EINVAL;
3447}
3448
3449static struct md_sysfs_entry max_corr_read_errors =
3450__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3451 max_corrected_read_errors_store);
3452
3453static ssize_t
NeilBrown6d7ff7382006-01-06 00:21:16 -08003454null_show(mddev_t *mddev, char *page)
3455{
3456 return -EINVAL;
3457}
3458
3459static ssize_t
3460new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3461{
3462 /* buf must be %d:%d\n? giving major and minor numbers */
3463 /* The new device is added to the array.
3464 * If the array has a persistent superblock, we read the
3465 * superblock to initialise info and check validity.
3466 * Otherwise, only checking done is that in bind_rdev_to_array,
3467 * which mainly checks size.
3468 */
3469 char *e;
3470 int major = simple_strtoul(buf, &e, 10);
3471 int minor;
3472 dev_t dev;
3473 mdk_rdev_t *rdev;
3474 int err;
3475
3476 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3477 return -EINVAL;
3478 minor = simple_strtoul(e+1, &e, 10);
3479 if (*e && *e != '\n')
3480 return -EINVAL;
3481 dev = MKDEV(major, minor);
3482 if (major != MAJOR(dev) ||
3483 minor != MINOR(dev))
3484 return -EOVERFLOW;
3485
3486
3487 if (mddev->persistent) {
3488 rdev = md_import_device(dev, mddev->major_version,
3489 mddev->minor_version);
3490 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3491 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3492 mdk_rdev_t, same_set);
3493 err = super_types[mddev->major_version]
3494 .load_super(rdev, rdev0, mddev->minor_version);
3495 if (err < 0)
3496 goto out;
3497 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003498 } else if (mddev->external)
3499 rdev = md_import_device(dev, -2, -1);
3500 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003501 rdev = md_import_device(dev, -1, -1);
3502
3503 if (IS_ERR(rdev))
3504 return PTR_ERR(rdev);
3505 err = bind_rdev_to_array(rdev, mddev);
3506 out:
3507 if (err)
3508 export_rdev(rdev);
3509 return err ? err : len;
3510}
3511
3512static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003513__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003514
3515static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003516bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3517{
3518 char *end;
3519 unsigned long chunk, end_chunk;
3520
3521 if (!mddev->bitmap)
3522 goto out;
3523 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3524 while (*buf) {
3525 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3526 if (buf == end) break;
3527 if (*end == '-') { /* range */
3528 buf = end + 1;
3529 end_chunk = simple_strtoul(buf, &end, 0);
3530 if (buf == end) break;
3531 }
3532 if (*end && !isspace(*end)) break;
3533 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003534 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003535 }
3536 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3537out:
3538 return len;
3539}
3540
3541static struct md_sysfs_entry md_bitmap =
3542__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3543
3544static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003545size_show(mddev_t *mddev, char *page)
3546{
Andre Noll58c0fed2009-03-31 14:33:13 +11003547 return sprintf(page, "%llu\n",
3548 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003549}
3550
Andre Nolld71f9f82008-07-11 22:02:22 +10003551static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003552
3553static ssize_t
3554size_store(mddev_t *mddev, const char *buf, size_t len)
3555{
3556 /* If array is inactive, we can reduce the component size, but
3557 * not increase it (except from 0).
3558 * If array is active, we can try an on-line resize
3559 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003560 sector_t sectors;
3561 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003562
Andre Noll58c0fed2009-03-31 14:33:13 +11003563 if (err < 0)
3564 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003565 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003566 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07003567 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003568 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003569 if (mddev->dev_sectors == 0 ||
3570 mddev->dev_sectors > sectors)
3571 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003572 else
3573 err = -ENOSPC;
3574 }
3575 return err ? err : len;
3576}
3577
3578static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003579__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003580
NeilBrown8bb93aa2006-01-06 00:20:50 -08003581
3582/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003583 * This is one of
3584 * 'none' for arrays with no metadata (good luck...)
3585 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003586 * or N.M for internally known formats
3587 */
3588static ssize_t
3589metadata_show(mddev_t *mddev, char *page)
3590{
3591 if (mddev->persistent)
3592 return sprintf(page, "%d.%d\n",
3593 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003594 else if (mddev->external)
3595 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003596 else
3597 return sprintf(page, "none\n");
3598}
3599
3600static ssize_t
3601metadata_store(mddev_t *mddev, const char *buf, size_t len)
3602{
3603 int major, minor;
3604 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003605 /* Changing the details of 'external' metadata is
3606 * always permitted. Otherwise there must be
3607 * no devices attached to the array.
3608 */
3609 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3610 ;
3611 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003612 return -EBUSY;
3613
3614 if (cmd_match(buf, "none")) {
3615 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003616 mddev->external = 0;
3617 mddev->major_version = 0;
3618 mddev->minor_version = 90;
3619 return len;
3620 }
3621 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003622 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003623 if (namelen >= sizeof(mddev->metadata_type))
3624 namelen = sizeof(mddev->metadata_type)-1;
3625 strncpy(mddev->metadata_type, buf+9, namelen);
3626 mddev->metadata_type[namelen] = 0;
3627 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3628 mddev->metadata_type[--namelen] = 0;
3629 mddev->persistent = 0;
3630 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003631 mddev->major_version = 0;
3632 mddev->minor_version = 90;
3633 return len;
3634 }
3635 major = simple_strtoul(buf, &e, 10);
3636 if (e==buf || *e != '.')
3637 return -EINVAL;
3638 buf = e+1;
3639 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003640 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003641 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003642 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003643 return -ENOENT;
3644 mddev->major_version = major;
3645 mddev->minor_version = minor;
3646 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003647 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003648 return len;
3649}
3650
3651static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003652__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003653
NeilBrowna35b0d62006-01-06 00:20:49 -08003654static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003655action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003656{
NeilBrown7eec3142005-11-08 21:39:44 -08003657 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003658 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3659 type = "frozen";
3660 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003661 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003662 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3663 type = "reshape";
3664 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003665 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3666 type = "resync";
3667 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3668 type = "check";
3669 else
3670 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003671 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003672 type = "recover";
3673 }
3674 return sprintf(page, "%s\n", type);
3675}
3676
3677static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003678action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003679{
NeilBrown7eec3142005-11-08 21:39:44 -08003680 if (!mddev->pers || !mddev->pers->sync_request)
3681 return -EINVAL;
3682
NeilBrownb6a9ce62009-05-26 09:41:17 +10003683 if (cmd_match(page, "frozen"))
3684 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3685 else
3686 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3687
3688 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003689 if (mddev->sync_thread) {
3690 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3691 md_unregister_thread(mddev->sync_thread);
3692 mddev->sync_thread = NULL;
3693 mddev->recovery = 0;
3694 }
NeilBrown03c902e2006-01-06 00:20:46 -08003695 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3696 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003697 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003698 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003699 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003700 else if (cmd_match(page, "recover")) {
3701 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3702 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3703 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003704 int err;
3705 if (mddev->pers->start_reshape == NULL)
3706 return -EINVAL;
3707 err = mddev->pers->start_reshape(mddev);
3708 if (err)
3709 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003710 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003711 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003712 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003713 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003714 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003715 return -EINVAL;
3716 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3717 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003718 }
NeilBrown03c902e2006-01-06 00:20:46 -08003719 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003720 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10003721 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003722 return len;
3723}
3724
NeilBrown9d888832005-11-08 21:39:26 -08003725static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003726mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003727{
3728 return sprintf(page, "%llu\n",
3729 (unsigned long long) mddev->resync_mismatches);
3730}
3731
NeilBrown80ca3a42006-07-10 04:44:18 -07003732static struct md_sysfs_entry md_scan_mode =
3733__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003734
NeilBrown96de1e62005-11-08 21:39:39 -08003735
NeilBrown80ca3a42006-07-10 04:44:18 -07003736static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003737
NeilBrown88202a02006-01-06 00:21:36 -08003738static ssize_t
3739sync_min_show(mddev_t *mddev, char *page)
3740{
3741 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3742 mddev->sync_speed_min ? "local": "system");
3743}
3744
3745static ssize_t
3746sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3747{
3748 int min;
3749 char *e;
3750 if (strncmp(buf, "system", 6)==0) {
3751 mddev->sync_speed_min = 0;
3752 return len;
3753 }
3754 min = simple_strtoul(buf, &e, 10);
3755 if (buf == e || (*e && *e != '\n') || min <= 0)
3756 return -EINVAL;
3757 mddev->sync_speed_min = min;
3758 return len;
3759}
3760
3761static struct md_sysfs_entry md_sync_min =
3762__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3763
3764static ssize_t
3765sync_max_show(mddev_t *mddev, char *page)
3766{
3767 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3768 mddev->sync_speed_max ? "local": "system");
3769}
3770
3771static ssize_t
3772sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3773{
3774 int max;
3775 char *e;
3776 if (strncmp(buf, "system", 6)==0) {
3777 mddev->sync_speed_max = 0;
3778 return len;
3779 }
3780 max = simple_strtoul(buf, &e, 10);
3781 if (buf == e || (*e && *e != '\n') || max <= 0)
3782 return -EINVAL;
3783 mddev->sync_speed_max = max;
3784 return len;
3785}
3786
3787static struct md_sysfs_entry md_sync_max =
3788__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3789
Iustin Popd7f3d292007-10-16 23:30:54 -07003790static ssize_t
3791degraded_show(mddev_t *mddev, char *page)
3792{
3793 return sprintf(page, "%d\n", mddev->degraded);
3794}
3795static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003796
3797static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003798sync_force_parallel_show(mddev_t *mddev, char *page)
3799{
3800 return sprintf(page, "%d\n", mddev->parallel_resync);
3801}
3802
3803static ssize_t
3804sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3805{
3806 long n;
3807
3808 if (strict_strtol(buf, 10, &n))
3809 return -EINVAL;
3810
3811 if (n != 0 && n != 1)
3812 return -EINVAL;
3813
3814 mddev->parallel_resync = n;
3815
3816 if (mddev->sync_thread)
3817 wake_up(&resync_wait);
3818
3819 return len;
3820}
3821
3822/* force parallel resync, even with shared block devices */
3823static struct md_sysfs_entry md_sync_force_parallel =
3824__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3825 sync_force_parallel_show, sync_force_parallel_store);
3826
3827static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003828sync_speed_show(mddev_t *mddev, char *page)
3829{
3830 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003831 if (mddev->curr_resync == 0)
3832 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003833 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3834 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003835 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003836 db = resync - mddev->resync_mark_cnt;
3837 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003838}
3839
NeilBrown80ca3a42006-07-10 04:44:18 -07003840static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003841
3842static ssize_t
3843sync_completed_show(mddev_t *mddev, char *page)
3844{
Andre Noll58c0fed2009-03-31 14:33:13 +11003845 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003846
NeilBrownacb180b2009-04-14 16:28:34 +10003847 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3848 return sprintf(page, "none\n");
3849
NeilBrown88202a02006-01-06 00:21:36 -08003850 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003851 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003852 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003853 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003854
NeilBrownacb180b2009-04-14 16:28:34 +10003855 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003856 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003857}
3858
NeilBrown80ca3a42006-07-10 04:44:18 -07003859static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003860
NeilBrowne464eaf2006-03-27 01:18:14 -08003861static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003862min_sync_show(mddev_t *mddev, char *page)
3863{
3864 return sprintf(page, "%llu\n",
3865 (unsigned long long)mddev->resync_min);
3866}
3867static ssize_t
3868min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3869{
3870 unsigned long long min;
3871 if (strict_strtoull(buf, 10, &min))
3872 return -EINVAL;
3873 if (min > mddev->resync_max)
3874 return -EINVAL;
3875 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3876 return -EBUSY;
3877
3878 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003879 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003880 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003881 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003882 return -EINVAL;
3883 }
3884 mddev->resync_min = min;
3885
3886 return len;
3887}
3888
3889static struct md_sysfs_entry md_min_sync =
3890__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3891
3892static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003893max_sync_show(mddev_t *mddev, char *page)
3894{
3895 if (mddev->resync_max == MaxSector)
3896 return sprintf(page, "max\n");
3897 else
3898 return sprintf(page, "%llu\n",
3899 (unsigned long long)mddev->resync_max);
3900}
3901static ssize_t
3902max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3903{
3904 if (strncmp(buf, "max", 3) == 0)
3905 mddev->resync_max = MaxSector;
3906 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003907 unsigned long long max;
3908 if (strict_strtoull(buf, 10, &max))
3909 return -EINVAL;
3910 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003911 return -EINVAL;
3912 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003913 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003914 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3915 return -EBUSY;
3916
3917 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003918 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003919 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003920 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003921 return -EINVAL;
3922 }
3923 mddev->resync_max = max;
3924 }
3925 wake_up(&mddev->recovery_wait);
3926 return len;
3927}
3928
3929static struct md_sysfs_entry md_max_sync =
3930__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3931
3932static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003933suspend_lo_show(mddev_t *mddev, char *page)
3934{
3935 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3936}
3937
3938static ssize_t
3939suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3940{
3941 char *e;
3942 unsigned long long new = simple_strtoull(buf, &e, 10);
3943
NeilBrownb8d966e2009-07-01 11:14:04 +10003944 if (mddev->pers == NULL ||
3945 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003946 return -EINVAL;
3947 if (buf == e || (*e && *e != '\n'))
3948 return -EINVAL;
3949 if (new >= mddev->suspend_hi ||
3950 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3951 mddev->suspend_lo = new;
3952 mddev->pers->quiesce(mddev, 2);
3953 return len;
3954 } else
3955 return -EINVAL;
3956}
3957static struct md_sysfs_entry md_suspend_lo =
3958__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3959
3960
3961static ssize_t
3962suspend_hi_show(mddev_t *mddev, char *page)
3963{
3964 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3965}
3966
3967static ssize_t
3968suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3969{
3970 char *e;
3971 unsigned long long new = simple_strtoull(buf, &e, 10);
3972
NeilBrownb8d966e2009-07-01 11:14:04 +10003973 if (mddev->pers == NULL ||
3974 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003975 return -EINVAL;
3976 if (buf == e || (*e && *e != '\n'))
3977 return -EINVAL;
3978 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3979 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3980 mddev->suspend_hi = new;
3981 mddev->pers->quiesce(mddev, 1);
3982 mddev->pers->quiesce(mddev, 0);
3983 return len;
3984 } else
3985 return -EINVAL;
3986}
3987static struct md_sysfs_entry md_suspend_hi =
3988__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3989
NeilBrown08a02ec2007-05-09 02:35:38 -07003990static ssize_t
3991reshape_position_show(mddev_t *mddev, char *page)
3992{
3993 if (mddev->reshape_position != MaxSector)
3994 return sprintf(page, "%llu\n",
3995 (unsigned long long)mddev->reshape_position);
3996 strcpy(page, "none\n");
3997 return 5;
3998}
3999
4000static ssize_t
4001reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
4002{
4003 char *e;
4004 unsigned long long new = simple_strtoull(buf, &e, 10);
4005 if (mddev->pers)
4006 return -EBUSY;
4007 if (buf == e || (*e && *e != '\n'))
4008 return -EINVAL;
4009 mddev->reshape_position = new;
4010 mddev->delta_disks = 0;
4011 mddev->new_level = mddev->level;
4012 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004013 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004014 return len;
4015}
4016
4017static struct md_sysfs_entry md_reshape_position =
4018__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4019 reshape_position_store);
4020
Dan Williamsb522adc2009-03-31 15:00:31 +11004021static ssize_t
4022array_size_show(mddev_t *mddev, char *page)
4023{
4024 if (mddev->external_size)
4025 return sprintf(page, "%llu\n",
4026 (unsigned long long)mddev->array_sectors/2);
4027 else
4028 return sprintf(page, "default\n");
4029}
4030
4031static ssize_t
4032array_size_store(mddev_t *mddev, const char *buf, size_t len)
4033{
4034 sector_t sectors;
4035
4036 if (strncmp(buf, "default", 7) == 0) {
4037 if (mddev->pers)
4038 sectors = mddev->pers->size(mddev, 0, 0);
4039 else
4040 sectors = mddev->array_sectors;
4041
4042 mddev->external_size = 0;
4043 } else {
4044 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4045 return -EINVAL;
4046 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004047 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004048
4049 mddev->external_size = 1;
4050 }
4051
4052 mddev->array_sectors = sectors;
4053 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004054 if (mddev->pers)
4055 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11004056
4057 return len;
4058}
4059
4060static struct md_sysfs_entry md_array_size =
4061__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4062 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004063
NeilBrowneae17012005-11-08 21:39:23 -08004064static struct attribute *md_default_attrs[] = {
4065 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004066 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004067 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004068 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004069 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004070 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004071 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004072 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004073 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004074 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004075 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004076 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004077 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004078 NULL,
4079};
4080
4081static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004082 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004083 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004084 &md_sync_min.attr,
4085 &md_sync_max.attr,
4086 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004087 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004088 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004089 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004090 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004091 &md_suspend_lo.attr,
4092 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004093 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004094 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004095 NULL,
4096};
NeilBrown411036f2005-11-08 21:39:40 -08004097static struct attribute_group md_redundancy_group = {
4098 .name = NULL,
4099 .attrs = md_redundancy_attrs,
4100};
4101
NeilBrowneae17012005-11-08 21:39:23 -08004102
4103static ssize_t
4104md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4105{
4106 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4107 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004108 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004109
4110 if (!entry->show)
4111 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004112 rv = mddev_lock(mddev);
4113 if (!rv) {
4114 rv = entry->show(mddev, page);
4115 mddev_unlock(mddev);
4116 }
NeilBrown96de1e62005-11-08 21:39:39 -08004117 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004118}
4119
4120static ssize_t
4121md_attr_store(struct kobject *kobj, struct attribute *attr,
4122 const char *page, size_t length)
4123{
4124 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4125 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004126 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004127
4128 if (!entry->store)
4129 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004130 if (!capable(CAP_SYS_ADMIN))
4131 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004132 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004133 if (mddev->hold_active == UNTIL_IOCTL)
4134 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004135 if (!rv) {
4136 rv = entry->store(mddev, page, length);
4137 mddev_unlock(mddev);
4138 }
NeilBrown96de1e62005-11-08 21:39:39 -08004139 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004140}
4141
4142static void md_free(struct kobject *ko)
4143{
4144 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004145
4146 if (mddev->sysfs_state)
4147 sysfs_put(mddev->sysfs_state);
4148
4149 if (mddev->gendisk) {
4150 del_gendisk(mddev->gendisk);
4151 put_disk(mddev->gendisk);
4152 }
4153 if (mddev->queue)
4154 blk_cleanup_queue(mddev->queue);
4155
NeilBrowneae17012005-11-08 21:39:23 -08004156 kfree(mddev);
4157}
4158
Emese Revfy52cf25d2010-01-19 02:58:23 +01004159static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004160 .show = md_attr_show,
4161 .store = md_attr_store,
4162};
4163static struct kobj_type md_ktype = {
4164 .release = md_free,
4165 .sysfs_ops = &md_sysfs_ops,
4166 .default_attrs = md_default_attrs,
4167};
4168
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169int mdp_major = 0;
4170
Dan Williams5fd3a172009-03-04 00:57:25 -07004171static void mddev_delayed_delete(struct work_struct *ws)
4172{
4173 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4174
NeilBrown43a70502009-12-14 12:49:55 +11004175 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004176 kobject_del(&mddev->kobj);
4177 kobject_put(&mddev->kobj);
4178}
4179
NeilBrownefeb53c2009-01-09 08:31:10 +11004180static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004182 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 mddev_t *mddev = mddev_find(dev);
4184 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004185 int partitioned;
4186 int shift;
4187 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004188 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189
4190 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004191 return -ENODEV;
4192
4193 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4194 shift = partitioned ? MdpMinorShift : 0;
4195 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
NeilBrownd3374822009-01-09 08:31:10 +11004197 /* wait for any previous instance if this device
4198 * to be completed removed (mddev_delayed_delete).
4199 */
4200 flush_scheduled_work();
4201
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004202 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004203 error = -EEXIST;
4204 if (mddev->gendisk)
4205 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004206
4207 if (name) {
4208 /* Need to ensure that 'name' is not a duplicate.
4209 */
4210 mddev_t *mddev2;
4211 spin_lock(&all_mddevs_lock);
4212
4213 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4214 if (mddev2->gendisk &&
4215 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4216 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004217 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004218 }
4219 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 }
NeilBrown8b765392009-01-09 08:31:08 +11004221
NeilBrown0909dc42009-07-01 12:27:21 +10004222 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004223 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004224 if (!mddev->queue)
4225 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004226 mddev->queue->queuedata = mddev;
4227
NeilBrown8b765392009-01-09 08:31:08 +11004228 /* Can be unlocked because the queue is new: no concurrency */
4229 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4230
NeilBrown409c57f2009-03-31 14:39:39 +11004231 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004232
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 disk = alloc_disk(1 << shift);
4234 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004235 blk_cleanup_queue(mddev->queue);
4236 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004237 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004239 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004241 if (name)
4242 strcpy(disk->disk_name, name);
4243 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004245 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 disk->fops = &md_fops;
4248 disk->private_data = mddev;
4249 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004250 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004251 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004252 * remove it now.
4253 */
4254 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 add_disk(disk);
4256 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004257 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4258 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004259 if (error) {
4260 /* This isn't possible, but as kobject_init_and_add is marked
4261 * __must_check, we must do something with the result
4262 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004263 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4264 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004265 error = 0;
4266 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004267 if (mddev->kobj.sd &&
4268 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004269 printk(KERN_DEBUG "pointless warning\n");
NeilBrown0909dc42009-07-01 12:27:21 +10004270 abort:
4271 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004272 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004273 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004274 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004275 }
NeilBrownd3374822009-01-09 08:31:10 +11004276 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004277 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004278}
4279
4280static struct kobject *md_probe(dev_t dev, int *part, void *data)
4281{
4282 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 return NULL;
4284}
4285
NeilBrownefeb53c2009-01-09 08:31:10 +11004286static int add_named_array(const char *val, struct kernel_param *kp)
4287{
4288 /* val must be "md_*" where * is not all digits.
4289 * We allocate an array with a large free minor number, and
4290 * set the name to val. val must not already be an active name.
4291 */
4292 int len = strlen(val);
4293 char buf[DISK_NAME_LEN];
4294
4295 while (len && val[len-1] == '\n')
4296 len--;
4297 if (len >= DISK_NAME_LEN)
4298 return -E2BIG;
4299 strlcpy(buf, val, len+1);
4300 if (strncmp(buf, "md_", 3) != 0)
4301 return -EINVAL;
4302 return md_alloc(0, buf);
4303}
4304
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305static void md_safemode_timeout(unsigned long data)
4306{
4307 mddev_t *mddev = (mddev_t *) data;
4308
Neil Brown0fd62b82008-06-28 08:31:36 +10004309 if (!atomic_read(&mddev->writes_pending)) {
4310 mddev->safemode = 1;
4311 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004312 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 md_wakeup_thread(mddev->thread);
4315}
4316
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004317static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
NeilBrown390ee602010-06-01 19:37:27 +10004319int md_run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320{
NeilBrown2604b702006-01-06 00:20:36 -08004321 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 mdk_rdev_t *rdev;
NeilBrown2604b702006-01-06 00:20:36 -08004323 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324
NeilBrowna757e642005-04-16 15:26:42 -07004325 if (list_empty(&mddev->disks))
4326 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328
4329 if (mddev->pers)
4330 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004331 /* Cannot run until previous stop completes properly */
4332 if (mddev->sysfs_active)
4333 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004334
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 /*
4336 * Analyze all RAID superblock(s)
4337 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004338 if (!mddev->raid_disks) {
4339 if (!mddev->persistent)
4340 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004341 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
NeilBrownd9d166c2006-01-06 00:20:51 -08004344 if (mddev->level != LEVEL_NONE)
4345 request_module("md-level-%d", mddev->level);
4346 else if (mddev->clevel[0])
4347 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348
4349 /*
4350 * Drop all container device buffers, from now on
4351 * the only valid external interface is through the md
4352 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004354 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004355 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 continue;
4357 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004358 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004359
4360 /* perform some consistency tests on the device.
4361 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004362 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004363 */
Andre Noll0f420352008-07-11 22:02:23 +10004364 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004365 if (mddev->dev_sectors &&
4366 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004367 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004368 printk("md: %s: data overlaps metadata\n",
4369 mdname(mddev));
4370 return -EINVAL;
4371 }
4372 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004373 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004374 > rdev->data_offset) {
4375 printk("md: %s: metadata overlaps data\n",
4376 mdname(mddev));
4377 return -EINVAL;
4378 }
4379 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004380 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 }
4382
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004384 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004385 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004387 if (mddev->level != LEVEL_NONE)
4388 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4389 mddev->level);
4390 else
4391 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4392 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 return -EINVAL;
4394 }
NeilBrown2604b702006-01-06 00:20:36 -08004395 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004397 if (mddev->level != pers->level) {
4398 mddev->level = pers->level;
4399 mddev->new_level = pers->level;
4400 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004401 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402
NeilBrownf6705572006-03-27 01:18:11 -08004403 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004404 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004405 /* This personality cannot handle reshaping... */
4406 mddev->pers = NULL;
4407 module_put(pers->owner);
4408 return -EINVAL;
4409 }
4410
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004411 if (pers->sync_request) {
4412 /* Warn if this is a potentially silly
4413 * configuration.
4414 */
4415 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4416 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004417 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004418
4419 list_for_each_entry(rdev, &mddev->disks, same_set)
4420 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004421 if (rdev < rdev2 &&
4422 rdev->bdev->bd_contains ==
4423 rdev2->bdev->bd_contains) {
4424 printk(KERN_WARNING
4425 "%s: WARNING: %s appears to be"
4426 " on the same physical disk as"
4427 " %s.\n",
4428 mdname(mddev),
4429 bdevname(rdev->bdev,b),
4430 bdevname(rdev2->bdev,b2));
4431 warned = 1;
4432 }
4433 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004434
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004435 if (warned)
4436 printk(KERN_WARNING
4437 "True protection against single-disk"
4438 " failure might be compromised.\n");
4439 }
4440
NeilBrown657390d2005-08-26 18:34:16 -07004441 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004442 /* may be over-ridden by personality */
4443 mddev->resync_max_sectors = mddev->dev_sectors;
4444
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004445 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
NeilBrown0f9552b52009-12-30 12:08:50 +11004447 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004448 mddev->ro = 2; /* read-only, but switch on first write */
4449
NeilBrownb15c2e52006-01-06 00:20:16 -08004450 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004451 if (err)
4452 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004453 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4454 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4455 " but 'external_size' not in effect?\n", __func__);
4456 printk(KERN_ERR
4457 "md: invalid array_size %llu > default size %llu\n",
4458 (unsigned long long)mddev->array_sectors / 2,
4459 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4460 err = -EINVAL;
4461 mddev->pers->stop(mddev);
4462 }
4463 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004464 err = bitmap_create(mddev);
4465 if (err) {
4466 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4467 mdname(mddev), err);
4468 mddev->pers->stop(mddev);
4469 }
4470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 module_put(mddev->pers->owner);
4473 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004474 bitmap_destroy(mddev);
4475 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004477 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004478 if (mddev->kobj.sd &&
4479 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004480 printk(KERN_WARNING
4481 "md: cannot register extra attributes for %s\n",
4482 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004483 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004484 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004485 mddev->ro = 0;
4486
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004488 atomic_set(&mddev->max_corr_read_errors,
4489 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 mddev->safemode = 0;
4491 mddev->safemode_timer.function = md_safemode_timeout;
4492 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004493 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004495
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004496 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004497 if (rdev->raid_disk >= 0) {
4498 char nm[20];
4499 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004500 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004501 /* failure here is OK */;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
4504 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4505
NeilBrown850b2b422006-10-03 01:15:46 -07004506 if (mddev->flags)
4507 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508
NeilBrown0b8c9de2006-07-10 04:44:15 -07004509 md_wakeup_thread(mddev->thread);
4510 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004511
NeilBrownd7603b72006-01-06 00:20:30 -08004512 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004513 sysfs_notify_dirent_safe(mddev->sysfs_state);
4514 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004515 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 return 0;
4517}
NeilBrown390ee602010-06-01 19:37:27 +10004518EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519
NeilBrownfe60b012010-03-29 11:10:42 +11004520static int do_md_run(mddev_t *mddev)
4521{
4522 int err;
4523
4524 err = md_run(mddev);
4525 if (err)
4526 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10004527 err = bitmap_load(mddev);
4528 if (err) {
4529 bitmap_destroy(mddev);
4530 goto out;
4531 }
NeilBrownfe60b012010-03-29 11:10:42 +11004532 set_capacity(mddev->gendisk, mddev->array_sectors);
4533 revalidate_disk(mddev->gendisk);
4534 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4535out:
4536 return err;
4537}
4538
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539static int restart_array(mddev_t *mddev)
4540{
4541 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542
Andre Noll80fab1d2008-07-11 22:02:21 +10004543 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004545 return -ENXIO;
4546 if (!mddev->pers)
4547 return -EINVAL;
4548 if (!mddev->ro)
4549 return -EBUSY;
4550 mddev->safemode = 0;
4551 mddev->ro = 0;
4552 set_disk_ro(disk, 0);
4553 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4554 mdname(mddev));
4555 /* Kick recovery or resync if necessary */
4556 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4557 md_wakeup_thread(mddev->thread);
4558 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004559 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004560 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561}
4562
NeilBrownacc55e22006-06-26 00:27:47 -07004563/* similar to deny_write_access, but accounts for our holding a reference
4564 * to the file ourselves */
4565static int deny_bitmap_write_access(struct file * file)
4566{
4567 struct inode *inode = file->f_mapping->host;
4568
4569 spin_lock(&inode->i_lock);
4570 if (atomic_read(&inode->i_writecount) > 1) {
4571 spin_unlock(&inode->i_lock);
4572 return -ETXTBSY;
4573 }
4574 atomic_set(&inode->i_writecount, -1);
4575 spin_unlock(&inode->i_lock);
4576
4577 return 0;
4578}
4579
NeilBrown43a70502009-12-14 12:49:55 +11004580void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004581{
4582 struct inode *inode = file->f_mapping->host;
4583
4584 spin_lock(&inode->i_lock);
4585 atomic_set(&inode->i_writecount, 1);
4586 spin_unlock(&inode->i_lock);
4587}
4588
NeilBrown6177b472010-03-29 11:37:13 +11004589static void md_clean(mddev_t *mddev)
4590{
4591 mddev->array_sectors = 0;
4592 mddev->external_size = 0;
4593 mddev->dev_sectors = 0;
4594 mddev->raid_disks = 0;
4595 mddev->recovery_cp = 0;
4596 mddev->resync_min = 0;
4597 mddev->resync_max = MaxSector;
4598 mddev->reshape_position = MaxSector;
4599 mddev->external = 0;
4600 mddev->persistent = 0;
4601 mddev->level = LEVEL_NONE;
4602 mddev->clevel[0] = 0;
4603 mddev->flags = 0;
4604 mddev->ro = 0;
4605 mddev->metadata_type[0] = 0;
4606 mddev->chunk_sectors = 0;
4607 mddev->ctime = mddev->utime = 0;
4608 mddev->layout = 0;
4609 mddev->max_disks = 0;
4610 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10004611 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004612 mddev->delta_disks = 0;
4613 mddev->new_level = LEVEL_NONE;
4614 mddev->new_layout = 0;
4615 mddev->new_chunk_sectors = 0;
4616 mddev->curr_resync = 0;
4617 mddev->resync_mismatches = 0;
4618 mddev->suspend_lo = mddev->suspend_hi = 0;
4619 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4620 mddev->recovery = 0;
4621 mddev->in_sync = 0;
4622 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004623 mddev->safemode = 0;
4624 mddev->bitmap_info.offset = 0;
4625 mddev->bitmap_info.default_offset = 0;
4626 mddev->bitmap_info.chunksize = 0;
4627 mddev->bitmap_info.daemon_sleep = 0;
4628 mddev->bitmap_info.max_write_behind = 0;
NeilBrown252ac522010-06-01 19:37:29 +10004629 mddev->plug = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004630}
4631
NeilBrown390ee602010-06-01 19:37:27 +10004632void md_stop_writes(mddev_t *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11004633{
4634 if (mddev->sync_thread) {
4635 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4636 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4637 md_unregister_thread(mddev->sync_thread);
4638 mddev->sync_thread = NULL;
4639 }
4640
4641 del_timer_sync(&mddev->safemode_timer);
4642
4643 bitmap_flush(mddev);
4644 md_super_wait(mddev);
4645
4646 if (!mddev->in_sync || mddev->flags) {
4647 /* mark array as shutdown cleanly */
4648 mddev->in_sync = 1;
4649 md_update_sb(mddev, 1);
4650 }
4651}
NeilBrown390ee602010-06-01 19:37:27 +10004652EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11004653
NeilBrown390ee602010-06-01 19:37:27 +10004654void md_stop(mddev_t *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11004655{
4656 mddev->pers->stop(mddev);
4657 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4658 mddev->to_remove = &md_redundancy_group;
4659 module_put(mddev->pers->owner);
4660 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11004661 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11004662}
NeilBrown390ee602010-06-01 19:37:27 +10004663EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11004664
NeilBrowna4bd82d2010-03-29 13:23:10 +11004665static int md_set_readonly(mddev_t *mddev, int is_open)
4666{
4667 int err = 0;
4668 mutex_lock(&mddev->open_mutex);
4669 if (atomic_read(&mddev->openers) > is_open) {
4670 printk("md: %s still in use.\n",mdname(mddev));
4671 err = -EBUSY;
4672 goto out;
4673 }
4674 if (mddev->pers) {
4675 md_stop_writes(mddev);
4676
4677 err = -ENXIO;
4678 if (mddev->ro==1)
4679 goto out;
4680 mddev->ro = 1;
4681 set_disk_ro(mddev->gendisk, 1);
4682 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004683 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004684 err = 0;
4685 }
4686out:
4687 mutex_unlock(&mddev->open_mutex);
4688 return err;
4689}
4690
NeilBrown9e653b62006-06-26 00:27:58 -07004691/* mode:
4692 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07004693 * 2 - stop but do not disassemble array
4694 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004695static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004698 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699
NeilBrownc8c00a62009-08-10 12:50:52 +10004700 mutex_lock(&mddev->open_mutex);
NeilBrownbb4f1e92010-08-08 21:18:03 +10004701 if (atomic_read(&mddev->openers) > is_open ||
4702 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004703 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10004704 mutex_unlock(&mddev->open_mutex);
4705 return -EBUSY;
4706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707
NeilBrown6e17b022010-08-07 21:41:19 +10004708 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11004709 if (mddev->ro)
4710 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004711
NeilBrown390ee602010-06-01 19:37:27 +10004712 md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004713 md_stop(mddev);
4714 mddev->queue->merge_bvec_fn = NULL;
4715 mddev->queue->unplug_fn = NULL;
4716 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004717
NeilBrowna4bd82d2010-03-29 13:23:10 +11004718 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10004719 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004720
NeilBrowna4bd82d2010-03-29 13:23:10 +11004721 list_for_each_entry(rdev, &mddev->disks, same_set)
4722 if (rdev->raid_disk >= 0) {
4723 char nm[20];
4724 sprintf(nm, "rd%d", rdev->raid_disk);
4725 sysfs_remove_link(&mddev->kobj, nm);
4726 }
NeilBrownc4647292009-05-07 12:51:06 +10004727
NeilBrowna4bd82d2010-03-29 13:23:10 +11004728 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10004729 mutex_unlock(&mddev->open_mutex);
NeilBrowna4bd82d2010-03-29 13:23:10 +11004730 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08004731
NeilBrowna4bd82d2010-03-29 13:23:10 +11004732 if (mddev->ro)
4733 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10004734 } else
4735 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 /*
4737 * Free resources if final stop
4738 */
NeilBrown9e653b62006-06-26 00:27:58 -07004739 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4741
NeilBrown978f9462006-02-02 14:28:05 -08004742 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004743 if (mddev->bitmap_info.file) {
4744 restore_bitmap_write_access(mddev->bitmap_info.file);
4745 fput(mddev->bitmap_info.file);
4746 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004747 }
NeilBrownc3d97142009-12-14 12:49:52 +11004748 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004749
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 export_array(mddev);
4751
NeilBrown6177b472010-03-29 11:37:13 +11004752 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11004753 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004754 if (mddev->hold_active == UNTIL_STOP)
4755 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11004756 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004757 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004758 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004759 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10004760 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761}
4762
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004763#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764static void autorun_array(mddev_t *mddev)
4765{
4766 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 int err;
4768
NeilBrowna757e642005-04-16 15:26:42 -07004769 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771
4772 printk(KERN_INFO "md: running: ");
4773
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004774 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 char b[BDEVNAME_SIZE];
4776 printk("<%s>", bdevname(rdev->bdev,b));
4777 }
4778 printk("\n");
4779
NeilBrownd710e132008-10-13 11:55:12 +11004780 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 if (err) {
4782 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004783 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 }
4785}
4786
4787/*
4788 * lets try to run arrays based on all disks that have arrived
4789 * until now. (those are in pending_raid_disks)
4790 *
4791 * the method: pick the first pending disk, collect all disks with
4792 * the same UUID, remove all from the pending list and put them into
4793 * the 'same_array' list. Then order this list based on superblock
4794 * update time (freshest comes first), kick out 'old' disks and
4795 * compare superblocks. If everything's fine then run it.
4796 *
4797 * If "unit" is allocated, then bump its reference count
4798 */
4799static void autorun_devices(int part)
4800{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004801 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 mddev_t *mddev;
4803 char b[BDEVNAME_SIZE];
4804
4805 printk(KERN_INFO "md: autorun ...\n");
4806 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004807 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004809 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 rdev0 = list_entry(pending_raid_disks.next,
4811 mdk_rdev_t, same_set);
4812
4813 printk(KERN_INFO "md: considering %s ...\n",
4814 bdevname(rdev0->bdev,b));
4815 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004816 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 if (super_90_load(rdev, rdev0, 0) >= 0) {
4818 printk(KERN_INFO "md: adding %s ...\n",
4819 bdevname(rdev->bdev,b));
4820 list_move(&rdev->same_set, &candidates);
4821 }
4822 /*
4823 * now we have a set of devices, with all of them having
4824 * mostly sane superblocks. It's time to allocate the
4825 * mddev.
4826 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004827 if (part) {
4828 dev = MKDEV(mdp_major,
4829 rdev0->preferred_minor << MdpMinorShift);
4830 unit = MINOR(dev) >> MdpMinorShift;
4831 } else {
4832 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4833 unit = MINOR(dev);
4834 }
4835 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4837 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4838 break;
4839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840
4841 md_probe(dev, NULL, NULL);
4842 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004843 if (!mddev || !mddev->gendisk) {
4844 if (mddev)
4845 mddev_put(mddev);
4846 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 "md: cannot allocate memory for md drive.\n");
4848 break;
4849 }
4850 if (mddev_lock(mddev))
4851 printk(KERN_WARNING "md: %s locked, cannot run\n",
4852 mdname(mddev));
4853 else if (mddev->raid_disks || mddev->major_version
4854 || !list_empty(&mddev->disks)) {
4855 printk(KERN_WARNING
4856 "md: %s already running, cannot run %s\n",
4857 mdname(mddev), bdevname(rdev0->bdev,b));
4858 mddev_unlock(mddev);
4859 } else {
4860 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004861 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004862 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 list_del_init(&rdev->same_set);
4864 if (bind_rdev_to_array(rdev, mddev))
4865 export_rdev(rdev);
4866 }
4867 autorun_array(mddev);
4868 mddev_unlock(mddev);
4869 }
4870 /* on success, candidates will be empty, on error
4871 * it won't...
4872 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004873 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004874 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 mddev_put(mddev);
4878 }
4879 printk(KERN_INFO "md: ... autorun DONE.\n");
4880}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004881#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883static int get_version(void __user * arg)
4884{
4885 mdu_version_t ver;
4886
4887 ver.major = MD_MAJOR_VERSION;
4888 ver.minor = MD_MINOR_VERSION;
4889 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4890
4891 if (copy_to_user(arg, &ver, sizeof(ver)))
4892 return -EFAULT;
4893
4894 return 0;
4895}
4896
4897static int get_array_info(mddev_t * mddev, void __user * arg)
4898{
4899 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004900 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902
NeilBrowna9f326e2009-09-23 18:06:41 +10004903 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004904 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004906 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 failed++;
4908 else {
4909 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004910 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004911 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 else
4913 spare++;
4914 }
4915 }
4916
4917 info.major_version = mddev->major_version;
4918 info.minor_version = mddev->minor_version;
4919 info.patch_version = MD_PATCHLEVEL_VERSION;
4920 info.ctime = mddev->ctime;
4921 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004922 info.size = mddev->dev_sectors / 2;
4923 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004924 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 info.nr_disks = nr;
4926 info.raid_disks = mddev->raid_disks;
4927 info.md_minor = mddev->md_minor;
4928 info.not_persistent= !mddev->persistent;
4929
4930 info.utime = mddev->utime;
4931 info.state = 0;
4932 if (mddev->in_sync)
4933 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11004934 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07004935 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004936 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 info.working_disks = working;
4938 info.failed_disks = failed;
4939 info.spare_disks = spare;
4940
4941 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004942 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943
4944 if (copy_to_user(arg, &info, sizeof(info)))
4945 return -EFAULT;
4946
4947 return 0;
4948}
4949
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004950static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004951{
4952 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4953 char *ptr, *buf = NULL;
4954 int err = -ENOMEM;
4955
Dan Williamsb5470dc2008-06-27 21:44:04 -07004956 if (md_allow_write(mddev))
4957 file = kmalloc(sizeof(*file), GFP_NOIO);
4958 else
4959 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004960
NeilBrown32a76272005-06-21 17:17:14 -07004961 if (!file)
4962 goto out;
4963
4964 /* bitmap disabled, zero the first byte and copy out */
4965 if (!mddev->bitmap || !mddev->bitmap->file) {
4966 file->pathname[0] = '\0';
4967 goto copy_out;
4968 }
4969
4970 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4971 if (!buf)
4972 goto out;
4973
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004974 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4975 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004976 goto out;
4977
4978 strcpy(file->pathname, ptr);
4979
4980copy_out:
4981 err = 0;
4982 if (copy_to_user(arg, file, sizeof(*file)))
4983 err = -EFAULT;
4984out:
4985 kfree(buf);
4986 kfree(file);
4987 return err;
4988}
4989
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990static int get_disk_info(mddev_t * mddev, void __user * arg)
4991{
4992 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 mdk_rdev_t *rdev;
4994
4995 if (copy_from_user(&info, arg, sizeof(info)))
4996 return -EFAULT;
4997
Andre Noll26ef3792008-07-11 22:02:21 +10004998 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 if (rdev) {
5000 info.major = MAJOR(rdev->bdev->bd_dev);
5001 info.minor = MINOR(rdev->bdev->bd_dev);
5002 info.raid_disk = rdev->raid_disk;
5003 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005004 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005006 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 info.state |= (1<<MD_DISK_ACTIVE);
5008 info.state |= (1<<MD_DISK_SYNC);
5009 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005010 if (test_bit(WriteMostly, &rdev->flags))
5011 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 } else {
5013 info.major = info.minor = 0;
5014 info.raid_disk = -1;
5015 info.state = (1<<MD_DISK_REMOVED);
5016 }
5017
5018 if (copy_to_user(arg, &info, sizeof(info)))
5019 return -EFAULT;
5020
5021 return 0;
5022}
5023
5024static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5025{
5026 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5027 mdk_rdev_t *rdev;
5028 dev_t dev = MKDEV(info->major,info->minor);
5029
5030 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5031 return -EOVERFLOW;
5032
5033 if (!mddev->raid_disks) {
5034 int err;
5035 /* expecting a device which has a superblock */
5036 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5037 if (IS_ERR(rdev)) {
5038 printk(KERN_WARNING
5039 "md: md_import_device returned %ld\n",
5040 PTR_ERR(rdev));
5041 return PTR_ERR(rdev);
5042 }
5043 if (!list_empty(&mddev->disks)) {
5044 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5045 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005046 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 .load_super(rdev, rdev0, mddev->minor_version);
5048 if (err < 0) {
5049 printk(KERN_WARNING
5050 "md: %s has different UUID to %s\n",
5051 bdevname(rdev->bdev,b),
5052 bdevname(rdev0->bdev,b2));
5053 export_rdev(rdev);
5054 return -EINVAL;
5055 }
5056 }
5057 err = bind_rdev_to_array(rdev, mddev);
5058 if (err)
5059 export_rdev(rdev);
5060 return err;
5061 }
5062
5063 /*
5064 * add_new_disk can be used once the array is assembled
5065 * to add "hot spares". They must already have a superblock
5066 * written
5067 */
5068 if (mddev->pers) {
5069 int err;
5070 if (!mddev->pers->hot_add_disk) {
5071 printk(KERN_WARNING
5072 "%s: personality does not support diskops!\n",
5073 mdname(mddev));
5074 return -EINVAL;
5075 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005076 if (mddev->persistent)
5077 rdev = md_import_device(dev, mddev->major_version,
5078 mddev->minor_version);
5079 else
5080 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 if (IS_ERR(rdev)) {
5082 printk(KERN_WARNING
5083 "md: md_import_device returned %ld\n",
5084 PTR_ERR(rdev));
5085 return PTR_ERR(rdev);
5086 }
NeilBrown41158c72005-06-21 17:17:25 -07005087 /* set save_raid_disk if appropriate */
5088 if (!mddev->persistent) {
5089 if (info->state & (1<<MD_DISK_SYNC) &&
5090 info->raid_disk < mddev->raid_disks)
5091 rdev->raid_disk = info->raid_disk;
5092 else
5093 rdev->raid_disk = -1;
5094 } else
5095 super_types[mddev->major_version].
5096 validate_super(mddev, rdev);
5097 rdev->saved_raid_disk = rdev->raid_disk;
5098
NeilBrownb2d444d2005-11-08 21:39:31 -08005099 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005100 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5101 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005102 else
5103 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005104
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 rdev->raid_disk = -1;
5106 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005107 if (!err && !mddev->pers->hot_remove_disk) {
5108 /* If there is hot_add_disk but no hot_remove_disk
5109 * then added disks for geometry changes,
5110 * and should be added immediately.
5111 */
5112 super_types[mddev->major_version].
5113 validate_super(mddev, rdev);
5114 err = mddev->pers->hot_add_disk(mddev, rdev);
5115 if (err)
5116 unbind_rdev_from_array(rdev);
5117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 if (err)
5119 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005120 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005121 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005122
NeilBrown17571282006-12-10 02:20:52 -08005123 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005124 if (mddev->degraded)
5125 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005126 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07005127 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 return err;
5129 }
5130
5131 /* otherwise, add_new_disk is only allowed
5132 * for major_version==0 superblocks
5133 */
5134 if (mddev->major_version != 0) {
5135 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5136 mdname(mddev));
5137 return -EINVAL;
5138 }
5139
5140 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5141 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005142 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 if (IS_ERR(rdev)) {
5144 printk(KERN_WARNING
5145 "md: error, md_import_device() returned %ld\n",
5146 PTR_ERR(rdev));
5147 return PTR_ERR(rdev);
5148 }
5149 rdev->desc_nr = info->number;
5150 if (info->raid_disk < mddev->raid_disks)
5151 rdev->raid_disk = info->raid_disk;
5152 else
5153 rdev->raid_disk = -1;
5154
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005156 if (info->state & (1<<MD_DISK_SYNC))
5157 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005159 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5160 set_bit(WriteMostly, &rdev->flags);
5161
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 if (!mddev->persistent) {
5163 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10005164 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 } else
Andre Noll0f420352008-07-11 22:02:23 +10005166 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10005167 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168
NeilBrown2bf071b2006-01-06 00:20:55 -08005169 err = bind_rdev_to_array(rdev, mddev);
5170 if (err) {
5171 export_rdev(rdev);
5172 return err;
5173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 }
5175
5176 return 0;
5177}
5178
5179static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5180{
5181 char b[BDEVNAME_SIZE];
5182 mdk_rdev_t *rdev;
5183
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 rdev = find_rdev(mddev, dev);
5185 if (!rdev)
5186 return -ENXIO;
5187
5188 if (rdev->raid_disk >= 0)
5189 goto busy;
5190
5191 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07005192 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005193 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194
5195 return 0;
5196busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005197 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 bdevname(rdev->bdev,b), mdname(mddev));
5199 return -EBUSY;
5200}
5201
5202static int hot_add_disk(mddev_t * mddev, dev_t dev)
5203{
5204 char b[BDEVNAME_SIZE];
5205 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 mdk_rdev_t *rdev;
5207
5208 if (!mddev->pers)
5209 return -ENODEV;
5210
5211 if (mddev->major_version != 0) {
5212 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5213 " version-0 superblocks.\n",
5214 mdname(mddev));
5215 return -EINVAL;
5216 }
5217 if (!mddev->pers->hot_add_disk) {
5218 printk(KERN_WARNING
5219 "%s: personality does not support diskops!\n",
5220 mdname(mddev));
5221 return -EINVAL;
5222 }
5223
NeilBrownd710e132008-10-13 11:55:12 +11005224 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 if (IS_ERR(rdev)) {
5226 printk(KERN_WARNING
5227 "md: error, md_import_device() returned %ld\n",
5228 PTR_ERR(rdev));
5229 return -EINVAL;
5230 }
5231
5232 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10005233 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 else
Andre Noll0f420352008-07-11 22:02:23 +10005235 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236
NeilBrown8190e752009-06-18 08:48:58 +10005237 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238
NeilBrownb2d444d2005-11-08 21:39:31 -08005239 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 printk(KERN_WARNING
5241 "md: can not hot-add faulty %s disk to %s!\n",
5242 bdevname(rdev->bdev,b), mdname(mddev));
5243 err = -EINVAL;
5244 goto abort_export;
5245 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005246 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005248 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005249 err = bind_rdev_to_array(rdev, mddev);
5250 if (err)
5251 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252
5253 /*
5254 * The rest should better be atomic, we can have disk failures
5255 * noticed in interrupt contexts ...
5256 */
5257
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 rdev->raid_disk = -1;
5259
NeilBrown850b2b422006-10-03 01:15:46 -07005260 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261
5262 /*
5263 * Kick recovery, maybe this spare has to be added to the
5264 * array immediately.
5265 */
5266 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5267 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005268 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 return 0;
5270
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271abort_export:
5272 export_rdev(rdev);
5273 return err;
5274}
5275
NeilBrown32a76272005-06-21 17:17:14 -07005276static int set_bitmap_file(mddev_t *mddev, int fd)
5277{
5278 int err;
5279
NeilBrown36fa3062005-09-09 16:23:45 -07005280 if (mddev->pers) {
5281 if (!mddev->pers->quiesce)
5282 return -EBUSY;
5283 if (mddev->recovery || mddev->sync_thread)
5284 return -EBUSY;
5285 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005286 }
5287
NeilBrown36fa3062005-09-09 16:23:45 -07005288
5289 if (fd >= 0) {
5290 if (mddev->bitmap)
5291 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005292 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005293
NeilBrownc3d97142009-12-14 12:49:52 +11005294 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005295 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5296 mdname(mddev));
5297 return -EBADF;
5298 }
5299
NeilBrownc3d97142009-12-14 12:49:52 +11005300 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005301 if (err) {
5302 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5303 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005304 fput(mddev->bitmap_info.file);
5305 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005306 return err;
5307 }
NeilBrownc3d97142009-12-14 12:49:52 +11005308 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005309 } else if (mddev->bitmap == NULL)
5310 return -ENOENT; /* cannot remove what isn't there */
5311 err = 0;
5312 if (mddev->pers) {
5313 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005314 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005315 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005316 if (!err)
5317 err = bitmap_load(mddev);
5318 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005319 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005320 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005321 fd = -1; /* make sure to put the file */
5322 }
NeilBrown36fa3062005-09-09 16:23:45 -07005323 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005324 }
5325 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005326 if (mddev->bitmap_info.file) {
5327 restore_bitmap_write_access(mddev->bitmap_info.file);
5328 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005329 }
NeilBrownc3d97142009-12-14 12:49:52 +11005330 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005331 }
5332
NeilBrown32a76272005-06-21 17:17:14 -07005333 return err;
5334}
5335
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336/*
5337 * set_array_info is used two different ways
5338 * The original usage is when creating a new array.
5339 * In this usage, raid_disks is > 0 and it together with
5340 * level, size, not_persistent,layout,chunksize determine the
5341 * shape of the array.
5342 * This will always create an array with a type-0.90.0 superblock.
5343 * The newer usage is when assembling an array.
5344 * In this case raid_disks will be 0, and the major_version field is
5345 * use to determine which style super-blocks are to be found on the devices.
5346 * The minor and patch _version numbers are also kept incase the
5347 * super_block handler wishes to interpret them.
5348 */
5349static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5350{
5351
5352 if (info->raid_disks == 0) {
5353 /* just setting version number for superblock loading */
5354 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005355 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 super_types[info->major_version].name == NULL) {
5357 /* maybe try to auto-load a module? */
5358 printk(KERN_INFO
5359 "md: superblock version %d not known\n",
5360 info->major_version);
5361 return -EINVAL;
5362 }
5363 mddev->major_version = info->major_version;
5364 mddev->minor_version = info->minor_version;
5365 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005366 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005367 /* ensure mddev_put doesn't delete this now that there
5368 * is some minimal configuration.
5369 */
5370 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 return 0;
5372 }
5373 mddev->major_version = MD_MAJOR_VERSION;
5374 mddev->minor_version = MD_MINOR_VERSION;
5375 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5376 mddev->ctime = get_seconds();
5377
5378 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005379 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005380 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 mddev->raid_disks = info->raid_disks;
5382 /* don't set md_minor, it is determined by which /dev/md* was
5383 * openned
5384 */
5385 if (info->state & (1<<MD_SB_CLEAN))
5386 mddev->recovery_cp = MaxSector;
5387 else
5388 mddev->recovery_cp = 0;
5389 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005390 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391
5392 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005393 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394
5395 mddev->max_disks = MD_SB_DISKS;
5396
NeilBrowne6910632008-02-06 01:39:51 -08005397 if (mddev->persistent)
5398 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07005399 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400
NeilBrownc3d97142009-12-14 12:49:52 +11005401 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5402 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005403
NeilBrownf6705572006-03-27 01:18:11 -08005404 mddev->reshape_position = MaxSector;
5405
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 /*
5407 * Generate a 128 bit UUID
5408 */
5409 get_random_bytes(mddev->uuid, 16);
5410
NeilBrownf6705572006-03-27 01:18:11 -08005411 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005412 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005413 mddev->new_layout = mddev->layout;
5414 mddev->delta_disks = 0;
5415
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 return 0;
5417}
5418
Dan Williams1f403622009-03-31 14:59:03 +11005419void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5420{
Dan Williamsb522adc2009-03-31 15:00:31 +11005421 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5422
5423 if (mddev->external_size)
5424 return;
5425
Dan Williams1f403622009-03-31 14:59:03 +11005426 mddev->array_sectors = array_sectors;
5427}
5428EXPORT_SYMBOL(md_set_array_sectors);
5429
Andre Nolld71f9f82008-07-11 22:02:22 +10005430static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005431{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005432 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005433 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005434 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005435
5436 if (mddev->pers->resize == NULL)
5437 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005438 /* The "num_sectors" is the number of sectors of each device that
5439 * is used. This can only make sense for arrays with redundancy.
5440 * linear and raid0 always use whatever space is available. We can only
5441 * consider changing this number if no resync or reconstruction is
5442 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005443 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005444 * of each device. If num_sectors is zero, we find the largest size
5445 * that fits.
5446
NeilBrowna35b0d62006-01-06 00:20:49 -08005447 */
5448 if (mddev->sync_thread)
5449 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005450 if (mddev->bitmap)
5451 /* Sorry, cannot grow a bitmap yet, just remove it,
5452 * grow, and re-add.
5453 */
5454 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005455 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005456 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005457
Andre Nolld71f9f82008-07-11 22:02:22 +10005458 if (fit && (num_sectors == 0 || num_sectors > avail))
5459 num_sectors = avail;
5460 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005461 return -ENOSPC;
5462 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005463 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005464 if (!rv)
5465 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005466 return rv;
5467}
5468
NeilBrownda943b992006-01-06 00:20:54 -08005469static int update_raid_disks(mddev_t *mddev, int raid_disks)
5470{
5471 int rv;
5472 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005473 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005474 return -EINVAL;
5475 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005476 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08005477 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005478 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005479 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005480 mddev->delta_disks = raid_disks - mddev->raid_disks;
5481
5482 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005483 return rv;
5484}
5485
5486
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487/*
5488 * update_array_info is used to change the configuration of an
5489 * on-line array.
5490 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5491 * fields in the info are checked against the array.
5492 * Any differences that cannot be handled will cause an error.
5493 * Normally, only one change can be managed at a time.
5494 */
5495static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5496{
5497 int rv = 0;
5498 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005499 int state = 0;
5500
5501 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005502 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005503 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504
5505 if (mddev->major_version != info->major_version ||
5506 mddev->minor_version != info->minor_version ||
5507/* mddev->patch_version != info->patch_version || */
5508 mddev->ctime != info->ctime ||
5509 mddev->level != info->level ||
5510/* mddev->layout != info->layout || */
5511 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005512 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005513 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5514 ((state^info->state) & 0xfffffe00)
5515 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 return -EINVAL;
5517 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005518 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5519 cnt++;
5520 if (mddev->raid_disks != info->raid_disks)
5521 cnt++;
5522 if (mddev->layout != info->layout)
5523 cnt++;
5524 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5525 cnt++;
5526 if (cnt == 0)
5527 return 0;
5528 if (cnt > 1)
5529 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530
5531 if (mddev->layout != info->layout) {
5532 /* Change layout
5533 * we don't need to do anything at the md level, the
5534 * personality will take care of it all.
5535 */
NeilBrown50ac1682009-06-18 08:47:55 +10005536 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005538 else {
5539 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005540 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005541 if (rv)
5542 mddev->new_layout = mddev->layout;
5543 return rv;
5544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005546 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005547 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548
NeilBrownda943b992006-01-06 00:20:54 -08005549 if (mddev->raid_disks != info->raid_disks)
5550 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551
NeilBrown36fa3062005-09-09 16:23:45 -07005552 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5553 if (mddev->pers->quiesce == NULL)
5554 return -EINVAL;
5555 if (mddev->recovery || mddev->sync_thread)
5556 return -EBUSY;
5557 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5558 /* add the bitmap */
5559 if (mddev->bitmap)
5560 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005561 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005562 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005563 mddev->bitmap_info.offset =
5564 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005565 mddev->pers->quiesce(mddev, 1);
5566 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005567 if (!rv)
5568 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07005569 if (rv)
5570 bitmap_destroy(mddev);
5571 mddev->pers->quiesce(mddev, 0);
5572 } else {
5573 /* remove the bitmap */
5574 if (!mddev->bitmap)
5575 return -ENOENT;
5576 if (mddev->bitmap->file)
5577 return -EINVAL;
5578 mddev->pers->quiesce(mddev, 1);
5579 bitmap_destroy(mddev);
5580 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005581 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005582 }
5583 }
NeilBrown850b2b422006-10-03 01:15:46 -07005584 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 return rv;
5586}
5587
5588static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5589{
5590 mdk_rdev_t *rdev;
5591
5592 if (mddev->pers == NULL)
5593 return -ENODEV;
5594
5595 rdev = find_rdev(mddev, dev);
5596 if (!rdev)
5597 return -ENODEV;
5598
5599 md_error(mddev, rdev);
5600 return 0;
5601}
5602
Andre Noll2f9618c2008-04-25 18:57:58 +02005603/*
5604 * We have a problem here : there is no easy way to give a CHS
5605 * virtual geometry. We currently pretend that we have a 2 heads
5606 * 4 sectors (with a BIG number of cylinders...). This drives
5607 * dosfs just mad... ;-)
5608 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005609static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5610{
5611 mddev_t *mddev = bdev->bd_disk->private_data;
5612
5613 geo->heads = 2;
5614 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11005615 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005616 return 0;
5617}
5618
Al Viroa39907f2008-03-02 10:31:15 -05005619static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 unsigned int cmd, unsigned long arg)
5621{
5622 int err = 0;
5623 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10005625 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626
5627 if (!capable(CAP_SYS_ADMIN))
5628 return -EACCES;
5629
5630 /*
5631 * Commands dealing with the RAID driver but not any
5632 * particular array:
5633 */
5634 switch (cmd)
5635 {
5636 case RAID_VERSION:
5637 err = get_version(argp);
5638 goto done;
5639
5640 case PRINT_RAID_DEBUG:
5641 err = 0;
5642 md_print_devices();
5643 goto done;
5644
5645#ifndef MODULE
5646 case RAID_AUTORUN:
5647 err = 0;
5648 autostart_arrays(arg);
5649 goto done;
5650#endif
5651 default:;
5652 }
5653
5654 /*
5655 * Commands creating/starting a new array:
5656 */
5657
Al Viroa39907f2008-03-02 10:31:15 -05005658 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659
5660 if (!mddev) {
5661 BUG();
5662 goto abort;
5663 }
5664
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 err = mddev_lock(mddev);
5666 if (err) {
5667 printk(KERN_INFO
5668 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5669 err, cmd);
5670 goto abort;
5671 }
5672
5673 switch (cmd)
5674 {
5675 case SET_ARRAY_INFO:
5676 {
5677 mdu_array_info_t info;
5678 if (!arg)
5679 memset(&info, 0, sizeof(info));
5680 else if (copy_from_user(&info, argp, sizeof(info))) {
5681 err = -EFAULT;
5682 goto abort_unlock;
5683 }
5684 if (mddev->pers) {
5685 err = update_array_info(mddev, &info);
5686 if (err) {
5687 printk(KERN_WARNING "md: couldn't update"
5688 " array info. %d\n", err);
5689 goto abort_unlock;
5690 }
5691 goto done_unlock;
5692 }
5693 if (!list_empty(&mddev->disks)) {
5694 printk(KERN_WARNING
5695 "md: array %s already has disks!\n",
5696 mdname(mddev));
5697 err = -EBUSY;
5698 goto abort_unlock;
5699 }
5700 if (mddev->raid_disks) {
5701 printk(KERN_WARNING
5702 "md: array %s already initialised!\n",
5703 mdname(mddev));
5704 err = -EBUSY;
5705 goto abort_unlock;
5706 }
5707 err = set_array_info(mddev, &info);
5708 if (err) {
5709 printk(KERN_WARNING "md: couldn't set"
5710 " array info. %d\n", err);
5711 goto abort_unlock;
5712 }
5713 }
5714 goto done_unlock;
5715
5716 default:;
5717 }
5718
5719 /*
5720 * Commands querying/configuring an existing array:
5721 */
NeilBrown32a76272005-06-21 17:17:14 -07005722 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005723 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005724 if ((!mddev->raid_disks && !mddev->external)
5725 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5726 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5727 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 err = -ENODEV;
5729 goto abort_unlock;
5730 }
5731
5732 /*
5733 * Commands even a read-only array can execute:
5734 */
5735 switch (cmd)
5736 {
5737 case GET_ARRAY_INFO:
5738 err = get_array_info(mddev, argp);
5739 goto done_unlock;
5740
NeilBrown32a76272005-06-21 17:17:14 -07005741 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005742 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005743 goto done_unlock;
5744
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 case GET_DISK_INFO:
5746 err = get_disk_info(mddev, argp);
5747 goto done_unlock;
5748
5749 case RESTART_ARRAY_RW:
5750 err = restart_array(mddev);
5751 goto done_unlock;
5752
5753 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005754 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 goto done_unlock;
5756
5757 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11005758 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 goto done_unlock;
5760
Dan Williamse2218352010-05-12 08:25:37 +10005761 case BLKROSET:
5762 if (get_user(ro, (int __user *)(arg))) {
5763 err = -EFAULT;
5764 goto done_unlock;
5765 }
5766 err = -EINVAL;
5767
5768 /* if the bdev is going readonly the value of mddev->ro
5769 * does not matter, no writes are coming
5770 */
5771 if (ro)
5772 goto done_unlock;
5773
5774 /* are we are already prepared for writes? */
5775 if (mddev->ro != 1)
5776 goto done_unlock;
5777
5778 /* transitioning to readauto need only happen for
5779 * arrays that call md_write_start
5780 */
5781 if (mddev->pers) {
5782 err = restart_array(mddev);
5783 if (err == 0) {
5784 mddev->ro = 2;
5785 set_disk_ro(mddev->gendisk, 0);
5786 }
5787 }
5788 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 }
5790
5791 /*
5792 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005793 * superblock, so we do not allow them on read-only arrays.
5794 * However non-MD ioctls (e.g. get-size) will still come through
5795 * here and hit the 'default' below, so only disallow
5796 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005798 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005799 if (mddev->ro == 2) {
5800 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10005801 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005802 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5803 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005804 } else {
5805 err = -EROFS;
5806 goto abort_unlock;
5807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 }
5809
5810 switch (cmd)
5811 {
5812 case ADD_NEW_DISK:
5813 {
5814 mdu_disk_info_t info;
5815 if (copy_from_user(&info, argp, sizeof(info)))
5816 err = -EFAULT;
5817 else
5818 err = add_new_disk(mddev, &info);
5819 goto done_unlock;
5820 }
5821
5822 case HOT_REMOVE_DISK:
5823 err = hot_remove_disk(mddev, new_decode_dev(arg));
5824 goto done_unlock;
5825
5826 case HOT_ADD_DISK:
5827 err = hot_add_disk(mddev, new_decode_dev(arg));
5828 goto done_unlock;
5829
5830 case SET_DISK_FAULTY:
5831 err = set_disk_faulty(mddev, new_decode_dev(arg));
5832 goto done_unlock;
5833
5834 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005835 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 goto done_unlock;
5837
NeilBrown32a76272005-06-21 17:17:14 -07005838 case SET_BITMAP_FILE:
5839 err = set_bitmap_file(mddev, (int)arg);
5840 goto done_unlock;
5841
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 err = -EINVAL;
5844 goto abort_unlock;
5845 }
5846
5847done_unlock:
5848abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005849 if (mddev->hold_active == UNTIL_IOCTL &&
5850 err != -EINVAL)
5851 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852 mddev_unlock(mddev);
5853
5854 return err;
5855done:
5856 if (err)
5857 MD_BUG();
5858abort:
5859 return err;
5860}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005861#ifdef CONFIG_COMPAT
5862static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5863 unsigned int cmd, unsigned long arg)
5864{
5865 switch (cmd) {
5866 case HOT_REMOVE_DISK:
5867 case HOT_ADD_DISK:
5868 case SET_DISK_FAULTY:
5869 case SET_BITMAP_FILE:
5870 /* These take in integer arg, do not convert */
5871 break;
5872 default:
5873 arg = (unsigned long)compat_ptr(arg);
5874 break;
5875 }
5876
5877 return md_ioctl(bdev, mode, cmd, arg);
5878}
5879#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880
Al Viroa39907f2008-03-02 10:31:15 -05005881static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882{
5883 /*
5884 * Succeed if we can lock the mddev, which confirms that
5885 * it isn't being stopped right now.
5886 */
NeilBrownd3374822009-01-09 08:31:10 +11005887 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 int err;
5889
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02005890 mutex_lock(&md_mutex);
NeilBrownd3374822009-01-09 08:31:10 +11005891 if (mddev->gendisk != bdev->bd_disk) {
5892 /* we are racing with mddev_put which is discarding this
5893 * bd_disk.
5894 */
5895 mddev_put(mddev);
5896 /* Wait until bdev->bd_disk is definitely gone */
5897 flush_scheduled_work();
5898 /* Then retry the open from the top */
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02005899 mutex_unlock(&md_mutex);
NeilBrownd3374822009-01-09 08:31:10 +11005900 return -ERESTARTSYS;
5901 }
5902 BUG_ON(mddev != bdev->bd_disk->private_data);
5903
NeilBrownc8c00a62009-08-10 12:50:52 +10005904 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 goto out;
5906
5907 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005908 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005909 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910
NeilBrownf3b99be2010-06-24 13:31:03 +10005911 check_disk_size_change(mddev->gendisk, bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 out:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02005913 mutex_unlock(&md_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 return err;
5915}
5916
Al Viroa39907f2008-03-02 10:31:15 -05005917static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918{
Al Viroa39907f2008-03-02 10:31:15 -05005919 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005921 BUG_ON(!mddev);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02005922 mutex_lock(&md_mutex);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005923 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 mddev_put(mddev);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +02005925 mutex_unlock(&md_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926
5927 return 0;
5928}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005929static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930{
5931 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005932 .open = md_open,
5933 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005934 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005935#ifdef CONFIG_COMPAT
5936 .compat_ioctl = md_compat_ioctl,
5937#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005938 .getgeo = md_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939};
5940
Adrian Bunk75c96f82005-05-05 16:16:09 -07005941static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942{
5943 mdk_thread_t *thread = arg;
5944
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 /*
5946 * md_thread is a 'system-thread', it's priority should be very
5947 * high. We avoid resource deadlocks individually in each
5948 * raid personality. (RAID5 does preallocation) We also use RR and
5949 * the very same RT priority as kswapd, thus we will never get
5950 * into a priority inversion deadlock.
5951 *
5952 * we definitely have to have equal or higher priority than
5953 * bdflush, otherwise bdflush will deadlock if there are too
5954 * many dirty RAID5 blocks.
5955 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956
NeilBrown6985c432005-10-19 21:23:47 -07005957 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005958 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959
NeilBrown93588e22005-11-15 00:09:12 -08005960 /* We need to wait INTERRUPTIBLE so that
5961 * we don't add to the load-average.
5962 * That means we need to be sure no signals are
5963 * pending
5964 */
5965 if (signal_pending(current))
5966 flush_signals(current);
5967
5968 wait_event_interruptible_timeout
5969 (thread->wqueue,
5970 test_bit(THREAD_WAKEUP, &thread->flags)
5971 || kthread_should_stop(),
5972 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973
5974 clear_bit(THREAD_WAKEUP, &thread->flags);
5975
NeilBrown787453c2005-11-08 21:39:43 -08005976 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005978
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 return 0;
5980}
5981
5982void md_wakeup_thread(mdk_thread_t *thread)
5983{
5984 if (thread) {
5985 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5986 set_bit(THREAD_WAKEUP, &thread->flags);
5987 wake_up(&thread->wqueue);
5988 }
5989}
5990
5991mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5992 const char *name)
5993{
5994 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995
NeilBrown9ffae0c2006-01-06 00:20:32 -08005996 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 if (!thread)
5998 return NULL;
5999
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 init_waitqueue_head(&thread->wqueue);
6001
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 thread->run = run;
6003 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006004 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006005 thread->tsk = kthread_run(md_thread, thread,
6006 "%s_%s",
6007 mdname(thread->mddev),
6008 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006009 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 kfree(thread);
6011 return NULL;
6012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 return thread;
6014}
6015
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016void md_unregister_thread(mdk_thread_t *thread)
6017{
NeilBrowne0cf8f02009-03-31 14:39:39 +11006018 if (!thread)
6019 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07006020 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07006021
6022 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 kfree(thread);
6024}
6025
6026void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6027{
6028 if (!mddev) {
6029 MD_BUG();
6030 return;
6031 }
6032
NeilBrownb2d444d2005-11-08 21:39:31 -08006033 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006035
6036 if (mddev->external)
6037 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07006038/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6040 mdname(mddev),
6041 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6042 __builtin_return_address(0),__builtin_return_address(1),
6043 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07006044*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07006045 if (!mddev->pers)
6046 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 if (!mddev->pers->error_handler)
6048 return;
6049 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006050 if (mddev->degraded)
6051 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006052 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6054 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6055 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006056 if (mddev->event_work.func)
6057 schedule_work(&mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006058 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059}
6060
6061/* seq_file implementation /proc/mdstat */
6062
6063static void status_unused(struct seq_file *seq)
6064{
6065 int i = 0;
6066 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067
6068 seq_printf(seq, "unused devices: ");
6069
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006070 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 char b[BDEVNAME_SIZE];
6072 i++;
6073 seq_printf(seq, "%s ",
6074 bdevname(rdev->bdev,b));
6075 }
6076 if (!i)
6077 seq_printf(seq, "<none>");
6078
6079 seq_printf(seq, "\n");
6080}
6081
6082
6083static void status_resync(struct seq_file *seq, mddev_t * mddev)
6084{
NeilBrowndd71cf62009-05-07 12:49:35 +10006085 sector_t max_sectors, resync, res;
6086 unsigned long dt, db;
6087 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006088 int scale;
6089 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090
NeilBrowndd71cf62009-05-07 12:49:35 +10006091 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092
6093 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006094 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006096 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097
6098 /*
6099 * Should not happen.
6100 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006101 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102 MD_BUG();
6103 return;
6104 }
NeilBrown4588b422006-03-27 01:18:04 -08006105 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006106 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006107 * u32, as those are the requirements for sector_div.
6108 * Thus 'scale' must be at least 10
6109 */
6110 scale = 10;
6111 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006112 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006113 scale++;
6114 }
6115 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006116 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006117
6118 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 {
NeilBrown4588b422006-03-27 01:18:04 -08006120 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 seq_printf(seq, "[");
6122 for (i = 0; i < x; i++)
6123 seq_printf(seq, "=");
6124 seq_printf(seq, ">");
6125 for (i = 0; i < y; i++)
6126 seq_printf(seq, ".");
6127 seq_printf(seq, "] ");
6128 }
NeilBrown4588b422006-03-27 01:18:04 -08006129 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006130 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6131 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006132 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6133 "check" :
6134 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6135 "resync" : "recovery"))),
6136 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006137 (unsigned long long) resync/2,
6138 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139
6140 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141 * dt: time from mark until now
6142 * db: blocks written from mark until now
6143 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006144 *
6145 * rt is a sector_t, so could be 32bit or 64bit.
6146 * So we divide before multiply in case it is 32bit and close
6147 * to the limit.
6148 * We scale the divisor (db) by 32 to avoid loosing precision
6149 * near the end of resync when the number of remaining sectors
6150 * is close to 'db'.
6151 * We then divide rt by 32 after multiplying by db to compensate.
6152 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153 */
6154 dt = ((jiffies - mddev->resync_mark) / HZ);
6155 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006156 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6157 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158
NeilBrowndd71cf62009-05-07 12:49:35 +10006159 rt = max_sectors - resync; /* number of remaining sectors */
6160 sector_div(rt, db/32+1);
6161 rt *= dt;
6162 rt >>= 5;
6163
6164 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6165 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166
NeilBrownff4e8d92006-07-10 04:44:16 -07006167 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168}
6169
6170static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6171{
6172 struct list_head *tmp;
6173 loff_t l = *pos;
6174 mddev_t *mddev;
6175
6176 if (l >= 0x10000)
6177 return NULL;
6178 if (!l--)
6179 /* header */
6180 return (void*)1;
6181
6182 spin_lock(&all_mddevs_lock);
6183 list_for_each(tmp,&all_mddevs)
6184 if (!l--) {
6185 mddev = list_entry(tmp, mddev_t, all_mddevs);
6186 mddev_get(mddev);
6187 spin_unlock(&all_mddevs_lock);
6188 return mddev;
6189 }
6190 spin_unlock(&all_mddevs_lock);
6191 if (!l--)
6192 return (void*)2;/* tail */
6193 return NULL;
6194}
6195
6196static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6197{
6198 struct list_head *tmp;
6199 mddev_t *next_mddev, *mddev = v;
6200
6201 ++*pos;
6202 if (v == (void*)2)
6203 return NULL;
6204
6205 spin_lock(&all_mddevs_lock);
6206 if (v == (void*)1)
6207 tmp = all_mddevs.next;
6208 else
6209 tmp = mddev->all_mddevs.next;
6210 if (tmp != &all_mddevs)
6211 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6212 else {
6213 next_mddev = (void*)2;
6214 *pos = 0x10000;
6215 }
6216 spin_unlock(&all_mddevs_lock);
6217
6218 if (v != (void*)1)
6219 mddev_put(mddev);
6220 return next_mddev;
6221
6222}
6223
6224static void md_seq_stop(struct seq_file *seq, void *v)
6225{
6226 mddev_t *mddev = v;
6227
6228 if (mddev && v != (void*)1 && v != (void*)2)
6229 mddev_put(mddev);
6230}
6231
NeilBrownd7603b72006-01-06 00:20:30 -08006232struct mdstat_info {
6233 int event;
6234};
6235
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236static int md_seq_show(struct seq_file *seq, void *v)
6237{
6238 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006239 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006241 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006242 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243
6244 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006245 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 seq_printf(seq, "Personalities : ");
6247 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006248 list_for_each_entry(pers, &pers_list, list)
6249 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250
6251 spin_unlock(&pers_lock);
6252 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006253 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254 return 0;
6255 }
6256 if (v == (void*)2) {
6257 status_unused(seq);
6258 return 0;
6259 }
6260
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006261 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006263
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6265 seq_printf(seq, "%s : %sactive", mdname(mddev),
6266 mddev->pers ? "" : "in");
6267 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006268 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006270 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006271 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272 seq_printf(seq, " %s", mddev->pers->name);
6273 }
6274
Andre Nolldd8ac332009-03-31 14:33:13 +11006275 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006276 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 char b[BDEVNAME_SIZE];
6278 seq_printf(seq, " %s[%d]",
6279 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006280 if (test_bit(WriteMostly, &rdev->flags))
6281 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006282 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 seq_printf(seq, "(F)");
6284 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006285 } else if (rdev->raid_disk < 0)
6286 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006287 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 }
6289
6290 if (!list_empty(&mddev->disks)) {
6291 if (mddev->pers)
6292 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006293 (unsigned long long)
6294 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295 else
6296 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006297 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006299 if (mddev->persistent) {
6300 if (mddev->major_version != 0 ||
6301 mddev->minor_version != 90) {
6302 seq_printf(seq," super %d.%d",
6303 mddev->major_version,
6304 mddev->minor_version);
6305 }
NeilBrowne6910632008-02-06 01:39:51 -08006306 } else if (mddev->external)
6307 seq_printf(seq, " super external:%s",
6308 mddev->metadata_type);
6309 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006310 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311
6312 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006313 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006315 if (mddev->pers->sync_request) {
6316 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006317 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006318 seq_printf(seq, "\n ");
6319 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6320 seq_printf(seq, "\tresync=DELAYED\n ");
6321 else if (mddev->recovery_cp < MaxSector)
6322 seq_printf(seq, "\tresync=PENDING\n ");
6323 }
NeilBrown32a76272005-06-21 17:17:14 -07006324 } else
6325 seq_printf(seq, "\n ");
6326
6327 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006328 unsigned long chunk_kb;
6329 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006330 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006331 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006332 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6333 "%lu%s chunk",
6334 bitmap->pages - bitmap->missing_pages,
6335 bitmap->pages,
6336 (bitmap->pages - bitmap->missing_pages)
6337 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006338 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006339 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006340 if (bitmap->file) {
6341 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006342 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006343 }
NeilBrown78d742d2005-06-21 17:17:15 -07006344
NeilBrown32a76272005-06-21 17:17:14 -07006345 seq_printf(seq, "\n");
6346 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347 }
6348
6349 seq_printf(seq, "\n");
6350 }
6351 mddev_unlock(mddev);
6352
6353 return 0;
6354}
6355
Jan Engelhardt110518b2009-05-07 12:49:37 +10006356static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357 .start = md_seq_start,
6358 .next = md_seq_next,
6359 .stop = md_seq_stop,
6360 .show = md_seq_show,
6361};
6362
6363static int md_seq_open(struct inode *inode, struct file *file)
6364{
6365 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006366 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6367 if (mi == NULL)
6368 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369
6370 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006371 if (error)
6372 kfree(mi);
6373 else {
6374 struct seq_file *p = file->private_data;
6375 p->private = mi;
6376 mi->event = atomic_read(&md_event_count);
6377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378 return error;
6379}
6380
NeilBrownd7603b72006-01-06 00:20:30 -08006381static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6382{
6383 struct seq_file *m = filp->private_data;
6384 struct mdstat_info *mi = m->private;
6385 int mask;
6386
6387 poll_wait(filp, &md_event_waiters, wait);
6388
6389 /* always allow read */
6390 mask = POLLIN | POLLRDNORM;
6391
6392 if (mi->event != atomic_read(&md_event_count))
6393 mask |= POLLERR | POLLPRI;
6394 return mask;
6395}
6396
Arjan van de Venfa027c22007-02-12 00:55:33 -08006397static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006398 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399 .open = md_seq_open,
6400 .read = seq_read,
6401 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006402 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006403 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404};
6405
NeilBrown2604b702006-01-06 00:20:36 -08006406int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006409 list_add_tail(&p->list, &pers_list);
6410 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411 spin_unlock(&pers_lock);
6412 return 0;
6413}
6414
NeilBrown2604b702006-01-06 00:20:36 -08006415int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416{
NeilBrown2604b702006-01-06 00:20:36 -08006417 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006419 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420 spin_unlock(&pers_lock);
6421 return 0;
6422}
6423
NeilBrowneea1bf32009-03-31 14:27:02 +11006424static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425{
6426 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006428 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429
6430 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006431 rcu_read_lock();
6432 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006434 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6435 (int)part_stat_read(&disk->part0, sectors[1]) -
6436 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006437 /* sync IO will cause sync_io to increase before the disk_stats
6438 * as sync_io is counted when a request starts, and
6439 * disk_stats is counted when it completes.
6440 * So resync activity will cause curr_events to be smaller than
6441 * when there was no such activity.
6442 * non-sync IO will cause disk_stat to increase without
6443 * increasing sync_io so curr_events will (eventually)
6444 * be larger than it was before. Once it becomes
6445 * substantially larger, the test below will cause
6446 * the array to appear non-idle, and resync will slow
6447 * down.
6448 * If there is a lot of outstanding resync activity when
6449 * we set last_event to curr_events, then all that activity
6450 * completing might cause the array to appear non-idle
6451 * and resync will be slowed down even though there might
6452 * not have been non-resync activity. This will only
6453 * happen once though. 'last_events' will soon reflect
6454 * the state where there is little or no outstanding
6455 * resync requests, and further resync activity will
6456 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006457 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006459 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460 rdev->last_events = curr_events;
6461 idle = 0;
6462 }
6463 }
NeilBrown4b809912008-07-21 17:05:25 +10006464 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465 return idle;
6466}
6467
6468void md_done_sync(mddev_t *mddev, int blocks, int ok)
6469{
6470 /* another "blocks" (512byte) blocks have been synced */
6471 atomic_sub(blocks, &mddev->recovery_active);
6472 wake_up(&mddev->recovery_wait);
6473 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006474 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475 md_wakeup_thread(mddev->thread);
6476 // stop recovery, signal do_sync ....
6477 }
6478}
6479
6480
NeilBrown06d91a52005-06-21 17:17:12 -07006481/* md_write_start(mddev, bi)
6482 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006483 * in superblock) before writing, schedule a superblock update
6484 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006485 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006486void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006487{
Neil Brown0fd62b82008-06-28 08:31:36 +10006488 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006489 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006490 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006491
NeilBrownf91de922005-11-08 21:39:36 -08006492 BUG_ON(mddev->ro == 1);
6493 if (mddev->ro == 2) {
6494 /* need to switch to read/write */
6495 mddev->ro = 0;
6496 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6497 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006498 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006499 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006500 }
NeilBrown06d91a52005-06-21 17:17:12 -07006501 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006502 if (mddev->safemode == 1)
6503 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006504 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006505 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006506 if (mddev->in_sync) {
6507 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006508 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006509 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006510 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006511 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006512 }
NeilBrowna9701a32005-11-08 21:39:34 -08006513 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006514 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006515 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10006516 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006517 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07006518 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006519}
6520
6521void md_write_end(mddev_t *mddev)
6522{
6523 if (atomic_dec_and_test(&mddev->writes_pending)) {
6524 if (mddev->safemode == 2)
6525 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006526 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6528 }
6529}
6530
NeilBrown2a2275d2007-01-26 00:57:11 -08006531/* md_allow_write(mddev)
6532 * Calling this ensures that the array is marked 'active' so that writes
6533 * may proceed without blocking. It is important to call this before
6534 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6535 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006536 *
6537 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6538 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006539 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006540int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006541{
6542 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006543 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006544 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006545 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006546 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006547 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006548
6549 spin_lock_irq(&mddev->write_lock);
6550 if (mddev->in_sync) {
6551 mddev->in_sync = 0;
6552 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006553 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08006554 if (mddev->safemode_delay &&
6555 mddev->safemode == 0)
6556 mddev->safemode = 1;
6557 spin_unlock_irq(&mddev->write_lock);
6558 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006559 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006560 } else
6561 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006562
NeilBrown070dc6d2010-08-30 17:33:34 +10006563 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07006564 return -EAGAIN;
6565 else
6566 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006567}
6568EXPORT_SYMBOL_GPL(md_allow_write);
6569
NeilBrownb63d7c22010-06-01 19:37:33 +10006570void md_unplug(mddev_t *mddev)
NeilBrown252ac522010-06-01 19:37:29 +10006571{
6572 if (mddev->queue)
6573 blk_unplug(mddev->queue);
6574 if (mddev->plug)
6575 mddev->plug->unplug_fn(mddev->plug);
6576}
6577
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578#define SYNC_MARKS 10
6579#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006580void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581{
6582 mddev_t *mddev2;
6583 unsigned int currspeed = 0,
6584 window;
NeilBrown57afd892005-06-21 17:17:13 -07006585 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586 unsigned long mark[SYNC_MARKS];
6587 sector_t mark_cnt[SYNC_MARKS];
6588 int last_mark,m;
6589 struct list_head *tmp;
6590 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006591 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006592 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006593 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594
6595 /* just incase thread restarts... */
6596 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6597 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006598 if (mddev->ro) /* never try to sync a read-only array */
6599 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600
NeilBrown61df9d92006-10-03 01:15:57 -07006601 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6602 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6603 desc = "data-check";
6604 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6605 desc = "requested-resync";
6606 else
6607 desc = "resync";
6608 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6609 desc = "reshape";
6610 else
6611 desc = "recovery";
6612
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613 /* we overload curr_resync somewhat here.
6614 * 0 == not engaged in resync at all
6615 * 2 == checking that there is no conflict with another sync
6616 * 1 == like 2, but have yielded to allow conflicting resync to
6617 * commense
6618 * other == active in resync - this many blocks
6619 *
6620 * Before starting a resync we must have set curr_resync to
6621 * 2, and then checked that every "conflicting" array has curr_resync
6622 * less than ours. When we find one that is the same or higher
6623 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6624 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6625 * This will mean we have to start checking from the beginning again.
6626 *
6627 */
6628
6629 do {
6630 mddev->curr_resync = 2;
6631
6632 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006633 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006634 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006635
6636 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006638 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639 if (mddev2 == mddev)
6640 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006641 if (!mddev->parallel_resync
6642 && mddev2->curr_resync
6643 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644 DEFINE_WAIT(wq);
6645 if (mddev < mddev2 && mddev->curr_resync == 2) {
6646 /* arbitrarily yield */
6647 mddev->curr_resync = 1;
6648 wake_up(&resync_wait);
6649 }
6650 if (mddev > mddev2 && mddev->curr_resync == 1)
6651 /* no need to wait here, we can wait the next
6652 * time 'round when curr_resync == 2
6653 */
6654 continue;
NeilBrown97441972008-09-19 11:49:54 +10006655 /* We need to wait 'interruptible' so as not to
6656 * contribute to the load average, and not to
6657 * be caught by 'softlockup'
6658 */
6659 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006660 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006661 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006662 printk(KERN_INFO "md: delaying %s of %s"
6663 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006665 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006667 if (signal_pending(current))
6668 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669 schedule();
6670 finish_wait(&resync_wait, &wq);
6671 goto try_again;
6672 }
6673 finish_wait(&resync_wait, &wq);
6674 }
6675 }
6676 } while (mddev->curr_resync < 2);
6677
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006678 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006679 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006680 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006681 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682 */
6683 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006684 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006685 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006686 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6687 j = mddev->resync_min;
6688 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006689 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006690
NeilBrownccfcc3c2006-03-27 01:18:09 -08006691 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006692 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006693 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006694 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006695 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006696 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006697 rcu_read_lock();
6698 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006699 if (rdev->raid_disk >= 0 &&
6700 !test_bit(Faulty, &rdev->flags) &&
6701 !test_bit(In_sync, &rdev->flags) &&
6702 rdev->recovery_offset < j)
6703 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006704 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706
NeilBrown61df9d92006-10-03 01:15:57 -07006707 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6708 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6709 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006710 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006711 "(but not more than %d KB/sec) for %s.\n",
6712 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713
NeilBrowneea1bf32009-03-31 14:27:02 +11006714 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006715
NeilBrown57afd892005-06-21 17:17:13 -07006716 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717 for (m = 0; m < SYNC_MARKS; m++) {
6718 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006719 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720 }
6721 last_mark = 0;
6722 mddev->resync_mark = mark[last_mark];
6723 mddev->resync_mark_cnt = mark_cnt[last_mark];
6724
6725 /*
6726 * Tune reconstruction:
6727 */
6728 window = 32*(PAGE_SIZE/512);
6729 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6730 window/2,(unsigned long long) max_sectors/2);
6731
6732 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733 last_check = 0;
6734
6735 if (j>2) {
6736 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006737 "md: resuming %s of %s from checkpoint.\n",
6738 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739 mddev->curr_resync = j;
6740 }
NeilBrownefa59332009-12-14 12:49:49 +11006741 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742
6743 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006744 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006745
NeilBrown57afd892005-06-21 17:17:13 -07006746 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006747
NeilBrown7a91ee12009-05-26 12:57:21 +10006748 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6749 ((mddev->curr_resync > mddev->curr_resync_completed &&
6750 (mddev->curr_resync - mddev->curr_resync_completed)
6751 > (max_sectors >> 4)) ||
6752 (j - mddev->curr_resync_completed)*2
6753 >= mddev->resync_max - mddev->curr_resync_completed
6754 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006755 /* time to update curr_resync_completed */
NeilBrown252ac522010-06-01 19:37:29 +10006756 md_unplug(mddev);
NeilBrown97e4f422009-03-31 14:33:13 +11006757 wait_event(mddev->recovery_wait,
6758 atomic_read(&mddev->recovery_active) == 0);
6759 mddev->curr_resync_completed =
6760 mddev->curr_resync;
NeilBrown070dc6d2010-08-30 17:33:34 +10006761 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006762 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006763 }
NeilBrownacb180b2009-04-14 16:28:34 +10006764
NeilBrowne62e58a2009-07-01 13:15:35 +10006765 while (j >= mddev->resync_max && !kthread_should_stop()) {
6766 /* As this condition is controlled by user-space,
6767 * we can block indefinitely, so use '_interruptible'
6768 * to avoid triggering warnings.
6769 */
6770 flush_signals(current); /* just in case */
6771 wait_event_interruptible(mddev->recovery_wait,
6772 mddev->resync_max > j
6773 || kthread_should_stop());
6774 }
NeilBrownacb180b2009-04-14 16:28:34 +10006775
6776 if (kthread_should_stop())
6777 goto interrupted;
6778
NeilBrown57afd892005-06-21 17:17:13 -07006779 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006780 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006781 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006782 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 goto out;
6784 }
NeilBrown57afd892005-06-21 17:17:13 -07006785
6786 if (!skipped) { /* actual IO requested */
6787 io_sectors += sectors;
6788 atomic_add(sectors, &mddev->recovery_active);
6789 }
6790
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791 j += sectors;
6792 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006793 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006794 if (last_check == 0)
6795 /* this is the earliers that rebuilt will be
6796 * visible in /proc/mdstat
6797 */
6798 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006799
6800 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006801 continue;
6802
NeilBrown57afd892005-06-21 17:17:13 -07006803 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804
NeilBrowndfc70642008-05-23 13:04:39 -07006805 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006806 break;
6807
6808 repeat:
6809 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6810 /* step marks */
6811 int next = (last_mark+1) % SYNC_MARKS;
6812
6813 mddev->resync_mark = mark[next];
6814 mddev->resync_mark_cnt = mark_cnt[next];
6815 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006816 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817 last_mark = next;
6818 }
6819
6820
NeilBrownc6207272008-02-06 01:39:52 -08006821 if (kthread_should_stop())
6822 goto interrupted;
6823
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824
6825 /*
6826 * this loop exits only if either when we are slower than
6827 * the 'hard' speed limit, or the system was IO-idle for
6828 * a jiffy.
6829 * the system might be non-idle CPU-wise, but we only care
6830 * about not overloading the IO subsystem. (things like an
6831 * e2fsck being done on the RAID array should execute fast)
6832 */
NeilBrown252ac522010-06-01 19:37:29 +10006833 md_unplug(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834 cond_resched();
6835
NeilBrown57afd892005-06-21 17:17:13 -07006836 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6837 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838
NeilBrown88202a02006-01-06 00:21:36 -08006839 if (currspeed > speed_min(mddev)) {
6840 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006841 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006842 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843 goto repeat;
6844 }
6845 }
6846 }
NeilBrown61df9d92006-10-03 01:15:57 -07006847 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848 /*
6849 * this also signals 'finished resyncing' to md_stop
6850 */
6851 out:
NeilBrown252ac522010-06-01 19:37:29 +10006852 md_unplug(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006853
6854 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6855
6856 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006857 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858
NeilBrowndfc70642008-05-23 13:04:39 -07006859 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006860 mddev->curr_resync > 2) {
6861 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6862 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6863 if (mddev->curr_resync >= mddev->recovery_cp) {
6864 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006865 "md: checkpointing %s of %s.\n",
6866 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006867 mddev->recovery_cp = mddev->curr_resync;
6868 }
6869 } else
6870 mddev->recovery_cp = MaxSector;
6871 } else {
6872 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6873 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006874 rcu_read_lock();
6875 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006876 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10006877 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006878 !test_bit(Faulty, &rdev->flags) &&
6879 !test_bit(In_sync, &rdev->flags) &&
6880 rdev->recovery_offset < mddev->curr_resync)
6881 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07006882 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884 }
NeilBrown17571282006-12-10 02:20:52 -08006885 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006888 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6889 /* We completed so min/max setting can be forgotten if used. */
6890 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6891 mddev->resync_min = 0;
6892 mddev->resync_max = MaxSector;
6893 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6894 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006896 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6897 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006898 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899 wake_up(&resync_wait);
6900 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6901 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006902 return;
6903
6904 interrupted:
6905 /*
6906 * got a signal, exit.
6907 */
6908 printk(KERN_INFO
6909 "md: md_do_sync() got signal ... exiting\n");
6910 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6911 goto out;
6912
Linus Torvalds1da177e2005-04-16 15:20:36 -07006913}
NeilBrown29269552006-03-27 01:18:10 -08006914EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915
6916
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006917static int remove_and_add_spares(mddev_t *mddev)
6918{
6919 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006920 int spares = 0;
6921
NeilBrown97e4f422009-03-31 14:33:13 +11006922 mddev->curr_resync_completed = 0;
6923
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006924 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006925 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006926 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006927 (test_bit(Faulty, &rdev->flags) ||
6928 ! test_bit(In_sync, &rdev->flags)) &&
6929 atomic_read(&rdev->nr_pending)==0) {
6930 if (mddev->pers->hot_remove_disk(
6931 mddev, rdev->raid_disk)==0) {
6932 char nm[20];
6933 sprintf(nm,"rd%d", rdev->raid_disk);
6934 sysfs_remove_link(&mddev->kobj, nm);
6935 rdev->raid_disk = -1;
6936 }
6937 }
6938
NeilBrown4044ba52009-01-09 08:31:11 +11006939 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006940 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006941 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006942 !test_bit(In_sync, &rdev->flags) &&
6943 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006944 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006945 if (rdev->raid_disk < 0
6946 && !test_bit(Faulty, &rdev->flags)) {
6947 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006948 if (mddev->pers->
6949 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006950 char nm[20];
6951 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006952 if (sysfs_create_link(&mddev->kobj,
6953 &rdev->kobj, nm))
NeilBrown00bcb4a2010-06-01 19:37:23 +10006954 /* failure here is OK */;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006955 spares++;
6956 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11006957 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006958 } else
6959 break;
6960 }
NeilBrowndfc70642008-05-23 13:04:39 -07006961 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006962 }
6963 return spares;
6964}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965/*
6966 * This routine is regularly called by all per-raid-array threads to
6967 * deal with generic issues like resync and super-block update.
6968 * Raid personalities that don't have a thread (linear/raid0) do not
6969 * need this as they never do any recovery or update the superblock.
6970 *
6971 * It does not do any resync itself, but rather "forks" off other threads
6972 * to do that as needed.
6973 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6974 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006975 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006976 * and wakeups up this thread which will reap the thread and finish up.
6977 * This thread also removes any faulty devices (with nr_pending == 0).
6978 *
6979 * The overall approach is:
6980 * 1/ if the superblock needs updating, update it.
6981 * 2/ If a recovery thread is running, don't do anything else.
6982 * 3/ If recovery has finished, clean up, possibly marking spares active.
6983 * 4/ If there are any faulty devices, remove them.
6984 * 5/ If array is degraded, try to add spares devices
6985 * 6/ If array has spares or is not in-sync, start a resync thread.
6986 */
6987void md_check_recovery(mddev_t *mddev)
6988{
6989 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006990
6991
NeilBrown5f404022005-06-21 17:17:16 -07006992 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11006993 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994
6995 if (mddev->ro)
6996 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006997
6998 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006999 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007000 printk(KERN_INFO "md: %s in immediate safe mode\n",
7001 mdname(mddev));
7002 mddev->safemode = 2;
7003 }
7004 flush_signals(current);
7005 }
7006
NeilBrownc89a8ee2008-08-05 15:54:13 +10007007 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7008 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007009 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007010 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007012 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007013 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007014 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7015 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016 ))
7017 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007018
NeilBrowndf5b89b2006-03-27 01:18:20 -08007019 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007020 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007021
NeilBrownc89a8ee2008-08-05 15:54:13 +10007022 if (mddev->ro) {
7023 /* Only thing we do on a ro array is remove
7024 * failed devices.
7025 */
7026 remove_and_add_spares(mddev);
7027 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7028 goto unlock;
7029 }
7030
NeilBrown31a59e32008-04-30 00:52:30 -07007031 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007032 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007033 spin_lock_irq(&mddev->write_lock);
7034 if (mddev->safemode &&
7035 !atomic_read(&mddev->writes_pending) &&
7036 !mddev->in_sync &&
7037 mddev->recovery_cp == MaxSector) {
7038 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007039 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007040 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007041 }
7042 if (mddev->safemode == 1)
7043 mddev->safemode = 0;
7044 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007045 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007046 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007047 }
NeilBrownfca4d842005-06-21 17:17:11 -07007048
NeilBrown850b2b422006-10-03 01:15:46 -07007049 if (mddev->flags)
7050 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007051
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7053 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7054 /* resync/recovery still happening */
7055 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7056 goto unlock;
7057 }
7058 if (mddev->sync_thread) {
7059 /* resync has finished, collect result */
7060 md_unregister_thread(mddev->sync_thread);
7061 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07007062 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7063 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064 /* success...*/
7065 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10007066 if (mddev->pers->spare_active(mddev))
7067 sysfs_notify(&mddev->kobj, NULL,
7068 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069 }
NeilBrowncea9c222009-03-31 15:15:05 +11007070 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7071 mddev->pers->finish_reshape)
7072 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07007073 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07007074
7075 /* if array is no-longer degraded, then any saved_raid_disk
7076 * information must be scrapped
7077 */
7078 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007079 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07007080 rdev->saved_raid_disk = -1;
7081
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082 mddev->recovery = 0;
7083 /* flag recovery needed just to double check */
7084 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007085 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007086 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087 goto unlock;
7088 }
Neil Brown72a23c22008-06-28 08:31:41 +10007089 /* Set RUNNING before clearing NEEDED to avoid
7090 * any transients in the value of "sync_action".
7091 */
7092 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7093 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007094 /* Clear some bits that don't mean anything, but
7095 * might be left set
7096 */
NeilBrown24dd4692005-11-08 21:39:26 -08007097 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7098 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007100 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7101 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102 /* no recovery is running.
7103 * remove any failed drives, then
7104 * add spares if possible.
7105 * Spare are also removed and re-added, to allow
7106 * the personality to fail the re-add.
7107 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007108
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007109 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007110 if (mddev->pers->check_reshape == NULL ||
7111 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007112 /* Cannot proceed */
7113 goto unlock;
7114 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007115 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007116 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007117 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7118 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007119 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007120 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007121 } else if (mddev->recovery_cp < MaxSector) {
7122 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007123 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007124 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7125 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007127
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007129 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7130 /* We are adding a device or devices to an array
7131 * which has the bitmap stored on all devices.
7132 * So make sure all bitmap pages get written
7133 */
7134 bitmap_write_all(mddev->bitmap);
7135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007136 mddev->sync_thread = md_register_thread(md_do_sync,
7137 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007138 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139 if (!mddev->sync_thread) {
7140 printk(KERN_ERR "%s: could not start resync"
7141 " thread...\n",
7142 mdname(mddev));
7143 /* leave the spares where they are, it shouldn't hurt */
7144 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08007145 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007147 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007148 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149 }
7150 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007151 if (!mddev->sync_thread) {
7152 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7153 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7154 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007155 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007156 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 mddev_unlock(mddev);
7159 }
7160}
7161
Dan Williams6bfe0b42008-04-30 00:52:32 -07007162void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7163{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007164 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007165 wait_event_timeout(rdev->blocked_wait,
7166 !test_bit(Blocked, &rdev->flags),
7167 msecs_to_jiffies(5000));
7168 rdev_dec_pending(rdev, mddev);
7169}
7170EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7171
Adrian Bunk75c96f82005-05-05 16:16:09 -07007172static int md_notify_reboot(struct notifier_block *this,
7173 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174{
7175 struct list_head *tmp;
7176 mddev_t *mddev;
7177
7178 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7179
7180 printk(KERN_INFO "md: stopping all md devices.\n");
7181
NeilBrown29ac4aa2008-02-06 01:39:58 -08007182 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007183 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007184 /* Force a switch to readonly even array
7185 * appears to still be in use. Hence
7186 * the '100'.
7187 */
NeilBrowna4bd82d2010-03-29 13:23:10 +11007188 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007189 mddev_unlock(mddev);
7190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191 /*
7192 * certain more exotic SCSI devices are known to be
7193 * volatile wrt too early system reboots. While the
7194 * right place to handle this issue is the given
7195 * driver, we do want to have a safe RAID driver ...
7196 */
7197 mdelay(1000*1);
7198 }
7199 return NOTIFY_DONE;
7200}
7201
Adrian Bunk75c96f82005-05-05 16:16:09 -07007202static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203 .notifier_call = md_notify_reboot,
7204 .next = NULL,
7205 .priority = INT_MAX, /* before any real devices */
7206};
7207
7208static void md_geninit(void)
7209{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7211
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07007212 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213}
7214
Adrian Bunk75c96f82005-05-05 16:16:09 -07007215static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007217 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218 return -1;
7219 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007220 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221 return -1;
7222 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007223 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007224 md_probe, NULL, NULL);
7225 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226 md_probe, NULL, NULL);
7227
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007229 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230
7231 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007232 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233}
7234
7235
7236#ifndef MODULE
7237
7238/*
7239 * Searches all registered partitions for autorun RAID arrays
7240 * at boot time.
7241 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007242
7243static LIST_HEAD(all_detected_devices);
7244struct detected_devices_node {
7245 struct list_head list;
7246 dev_t dev;
7247};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248
7249void md_autodetect_dev(dev_t dev)
7250{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007251 struct detected_devices_node *node_detected_dev;
7252
7253 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7254 if (node_detected_dev) {
7255 node_detected_dev->dev = dev;
7256 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7257 } else {
7258 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7259 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007261}
7262
7263
7264static void autostart_arrays(int part)
7265{
7266 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007267 struct detected_devices_node *node_detected_dev;
7268 dev_t dev;
7269 int i_scanned, i_passed;
7270
7271 i_scanned = 0;
7272 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007273
7274 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7275
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007276 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7277 i_scanned++;
7278 node_detected_dev = list_entry(all_detected_devices.next,
7279 struct detected_devices_node, list);
7280 list_del(&node_detected_dev->list);
7281 dev = node_detected_dev->dev;
7282 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007283 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007284 if (IS_ERR(rdev))
7285 continue;
7286
NeilBrownb2d444d2005-11-08 21:39:31 -08007287 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288 MD_BUG();
7289 continue;
7290 }
NeilBrownd0fae182008-03-04 14:29:31 -08007291 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007293 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007295
7296 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7297 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007298
7299 autorun_devices(part);
7300}
7301
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007302#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007303
7304static __exit void md_exit(void)
7305{
7306 mddev_t *mddev;
7307 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007308
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007309 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007310 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007311
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007312 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007313 unregister_blkdev(mdp_major, "mdp");
7314 unregister_reboot_notifier(&md_notifier);
7315 unregister_sysctl_table(raid_table_header);
7316 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007317 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007318 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007319 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007320 }
7321}
7322
Dan Williams685784a2007-07-09 11:56:42 -07007323subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007324module_exit(md_exit)
7325
NeilBrownf91de922005-11-08 21:39:36 -08007326static int get_ro(char *buffer, struct kernel_param *kp)
7327{
7328 return sprintf(buffer, "%d", start_readonly);
7329}
7330static int set_ro(const char *val, struct kernel_param *kp)
7331{
7332 char *e;
7333 int num = simple_strtoul(val, &e, 10);
7334 if (*val && (*e == '\0' || *e == '\n')) {
7335 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007336 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007337 }
7338 return -EINVAL;
7339}
7340
NeilBrown80ca3a42006-07-10 04:44:18 -07007341module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7342module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007343
NeilBrownefeb53c2009-01-09 08:31:10 +11007344module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007345
Linus Torvalds1da177e2005-04-16 15:20:36 -07007346EXPORT_SYMBOL(register_md_personality);
7347EXPORT_SYMBOL(unregister_md_personality);
7348EXPORT_SYMBOL(md_error);
7349EXPORT_SYMBOL(md_done_sync);
7350EXPORT_SYMBOL(md_write_start);
7351EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007352EXPORT_SYMBOL(md_register_thread);
7353EXPORT_SYMBOL(md_unregister_thread);
7354EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007355EXPORT_SYMBOL(md_check_recovery);
7356MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007357MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007358MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007359MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);