blob: d97a6253479809bc99dba19425530caa8620b49b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +020039#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/buffer_head.h> /* for invalidate_bdev */
NeilBrownd7603b72006-01-06 00:20:30 -080041#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070042#include <linux/ctype.h>
André Goddard Rosae7d28602009-12-14 18:01:06 -080043#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110044#include <linux/hdreg.h>
45#include <linux/proc_fs.h>
46#include <linux/random.h>
47#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070048#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110049#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110050#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110051#include <linux/raid/md_p.h>
52#include <linux/raid/md_u.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090053#include <linux/slab.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110054#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110055#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#define DEBUG 0
58#define dprintk(x...) ((void)(DEBUG && printk(x)))
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110061static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#endif
63
NeilBrown2604b702006-01-06 00:20:36 -080064static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static DEFINE_SPINLOCK(pers_lock);
66
Adrian Bunk5e563412006-06-26 00:27:42 -070067static void md_print_devices(void);
68
Bernd Schubert90b08712008-05-23 13:04:38 -070069static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020070static struct workqueue_struct *md_wq;
71static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070072
Adrian Bunk5e563412006-06-26 00:27:42 -070073#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/*
Robert Becker1e509152009-12-14 12:49:58 +110076 * Default number of read corrections we'll attempt on an rdev
77 * before ejecting it from the array. We divide the read error
78 * count by 2 for every hour elapsed between read errors.
79 */
80#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
81/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
83 * is 1000 KB/sec, so the extra system load does not show up that much.
84 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070085 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * subsystem is idle. There is also an 'absolute maximum' reconstruction
87 * speed limit - in case reconstruction slows down your system despite
88 * idle IO detection.
89 *
90 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080091 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
93
94static int sysctl_speed_limit_min = 1000;
95static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080096static inline int speed_min(mddev_t *mddev)
97{
98 return mddev->sync_speed_min ?
99 mddev->sync_speed_min : sysctl_speed_limit_min;
100}
101
102static inline int speed_max(mddev_t *mddev)
103{
104 return mddev->sync_speed_max ?
105 mddev->sync_speed_max : sysctl_speed_limit_max;
106}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108static struct ctl_table_header *raid_table_header;
109
110static ctl_table raid_table[] = {
111 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 .procname = "speed_limit_min",
113 .data = &sysctl_speed_limit_min,
114 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700115 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800116 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 },
118 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .procname = "speed_limit_max",
120 .data = &sysctl_speed_limit_max,
121 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700122 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800123 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800125 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126};
127
128static ctl_table raid_dir_table[] = {
129 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 .procname = "raid",
131 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700132 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .child = raid_table,
134 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800135 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
138static ctl_table raid_root_table[] = {
139 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 .procname = "dev",
141 .maxlen = 0,
142 .mode = 0555,
143 .child = raid_dir_table,
144 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800145 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700148static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
NeilBrownf91de922005-11-08 21:39:36 -0800150static int start_readonly;
151
NeilBrowna167f662010-10-26 18:31:13 +1100152/* bio_clone_mddev
153 * like bio_clone, but with a local bio set
154 */
155
156static void mddev_bio_destructor(struct bio *bio)
157{
158 mddev_t *mddev, **mddevp;
159
160 mddevp = (void*)bio;
161 mddev = mddevp[-1];
162
163 bio_free(bio, mddev->bio_set);
164}
165
166struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
167 mddev_t *mddev)
168{
169 struct bio *b;
170 mddev_t **mddevp;
171
172 if (!mddev || !mddev->bio_set)
173 return bio_alloc(gfp_mask, nr_iovecs);
174
175 b = bio_alloc_bioset(gfp_mask, nr_iovecs,
176 mddev->bio_set);
177 if (!b)
178 return NULL;
179 mddevp = (void*)b;
180 mddevp[-1] = mddev;
181 b->bi_destructor = mddev_bio_destructor;
182 return b;
183}
184EXPORT_SYMBOL_GPL(bio_alloc_mddev);
185
186struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
187 mddev_t *mddev)
188{
189 struct bio *b;
190 mddev_t **mddevp;
191
192 if (!mddev || !mddev->bio_set)
193 return bio_clone(bio, gfp_mask);
194
195 b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs,
196 mddev->bio_set);
197 if (!b)
198 return NULL;
199 mddevp = (void*)b;
200 mddevp[-1] = mddev;
201 b->bi_destructor = mddev_bio_destructor;
202 __bio_clone(b, bio);
203 if (bio_integrity(bio)) {
204 int ret;
205
206 ret = bio_integrity_clone(b, bio, gfp_mask, mddev->bio_set);
207
208 if (ret < 0) {
209 bio_put(b);
210 return NULL;
211 }
212 }
213
214 return b;
215}
216EXPORT_SYMBOL_GPL(bio_clone_mddev);
217
NeilBrownd2eb35a2011-07-28 11:31:48 +1000218void md_trim_bio(struct bio *bio, int offset, int size)
219{
220 /* 'bio' is a cloned bio which we need to trim to match
221 * the given offset and size.
222 * This requires adjusting bi_sector, bi_size, and bi_io_vec
223 */
224 int i;
225 struct bio_vec *bvec;
226 int sofar = 0;
227
228 size <<= 9;
229 if (offset == 0 && size == bio->bi_size)
230 return;
231
232 bio->bi_sector += offset;
233 bio->bi_size = size;
234 offset <<= 9;
235 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
236
237 while (bio->bi_idx < bio->bi_vcnt &&
238 bio->bi_io_vec[bio->bi_idx].bv_len <= offset) {
239 /* remove this whole bio_vec */
240 offset -= bio->bi_io_vec[bio->bi_idx].bv_len;
241 bio->bi_idx++;
242 }
243 if (bio->bi_idx < bio->bi_vcnt) {
244 bio->bi_io_vec[bio->bi_idx].bv_offset += offset;
245 bio->bi_io_vec[bio->bi_idx].bv_len -= offset;
246 }
247 /* avoid any complications with bi_idx being non-zero*/
248 if (bio->bi_idx) {
249 memmove(bio->bi_io_vec, bio->bi_io_vec+bio->bi_idx,
250 (bio->bi_vcnt - bio->bi_idx) * sizeof(struct bio_vec));
251 bio->bi_vcnt -= bio->bi_idx;
252 bio->bi_idx = 0;
253 }
254 /* Make sure vcnt and last bv are not too big */
255 bio_for_each_segment(bvec, bio, i) {
256 if (sofar + bvec->bv_len > size)
257 bvec->bv_len = size - sofar;
258 if (bvec->bv_len == 0) {
259 bio->bi_vcnt = i;
260 break;
261 }
262 sofar += bvec->bv_len;
263 }
264}
265EXPORT_SYMBOL_GPL(md_trim_bio);
266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267/*
NeilBrownd7603b72006-01-06 00:20:30 -0800268 * We have a system wide 'event count' that is incremented
269 * on any 'interesting' event, and readers of /proc/mdstat
270 * can use 'poll' or 'select' to find out when the event
271 * count increases.
272 *
273 * Events are:
274 * start array, stop array, error, add device, remove device,
275 * start build, activate spare
276 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800277static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800278static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800279void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800280{
281 atomic_inc(&md_event_count);
282 wake_up(&md_event_waiters);
283}
NeilBrown29269552006-03-27 01:18:10 -0800284EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800285
NeilBrownc331eb02006-05-30 21:27:13 -0700286/* Alternate version that can be called from interrupts
287 * when calling sysfs_notify isn't needed.
288 */
Adrian Bunk05381952006-06-26 00:28:01 -0700289static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700290{
291 atomic_inc(&md_event_count);
292 wake_up(&md_event_waiters);
293}
294
NeilBrownd7603b72006-01-06 00:20:30 -0800295/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 * Enables to iterate over all existing md arrays
297 * all_mddevs_lock protects this list.
298 */
299static LIST_HEAD(all_mddevs);
300static DEFINE_SPINLOCK(all_mddevs_lock);
301
302
303/*
304 * iterates through all used mddevs in the system.
305 * We take care to grab the all_mddevs_lock whenever navigating
306 * the list, and to always hold a refcount when unlocked.
307 * Any code which breaks out of this loop while own
308 * a reference to the current mddev and must mddev_put it.
309 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800310#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 \
312 for (({ spin_lock(&all_mddevs_lock); \
313 tmp = all_mddevs.next; \
314 mddev = NULL;}); \
315 ({ if (tmp != &all_mddevs) \
316 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
317 spin_unlock(&all_mddevs_lock); \
318 if (mddev) mddev_put(mddev); \
319 mddev = list_entry(tmp, mddev_t, all_mddevs); \
320 tmp != &all_mddevs;}); \
321 ({ spin_lock(&all_mddevs_lock); \
322 tmp = tmp->next;}) \
323 )
324
325
NeilBrown409c57f2009-03-31 14:39:39 +1100326/* Rather than calling directly into the personality make_request function,
327 * IO requests come here first so that we can check if the device is
328 * being suspended pending a reconfiguration.
329 * We hold a refcount over the call to ->make_request. By the time that
330 * call has finished, the bio has been linked into some internal structure
331 * and so is visible to ->quiesce(), so we don't need the refcount any more.
332 */
333static int md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
NeilBrown49077322010-03-25 16:20:56 +1100335 const int rw = bio_data_dir(bio);
NeilBrown409c57f2009-03-31 14:39:39 +1100336 mddev_t *mddev = q->queuedata;
337 int rv;
NeilBrown49077322010-03-25 16:20:56 +1100338 int cpu;
Chris Masone91ece52011-02-07 19:21:48 -0500339 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100340
NeilBrown0ca69882011-01-14 09:14:33 +1100341 if (mddev == NULL || mddev->pers == NULL
342 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100343 bio_io_error(bio);
344 return 0;
345 }
NeilBrown0ca69882011-01-14 09:14:33 +1100346 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100347 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200348 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100349 DEFINE_WAIT(__wait);
350 for (;;) {
351 prepare_to_wait(&mddev->sb_wait, &__wait,
352 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200353 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100354 break;
355 rcu_read_unlock();
356 schedule();
357 rcu_read_lock();
358 }
359 finish_wait(&mddev->sb_wait, &__wait);
360 }
361 atomic_inc(&mddev->active_io);
362 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100363
Chris Masone91ece52011-02-07 19:21:48 -0500364 /*
365 * save the sectors now since our bio can
366 * go away inside make_request
367 */
368 sectors = bio_sectors(bio);
NeilBrown21a52c62010-04-01 15:02:13 +1100369 rv = mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100370
371 cpu = part_stat_lock();
372 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
Chris Masone91ece52011-02-07 19:21:48 -0500373 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
NeilBrown49077322010-03-25 16:20:56 +1100374 part_stat_unlock();
375
NeilBrown409c57f2009-03-31 14:39:39 +1100376 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
377 wake_up(&mddev->sb_wait);
378
379 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
NeilBrown9e35b992010-04-06 14:23:02 +1000382/* mddev_suspend makes sure no new requests are submitted
383 * to the device, and that any requests that have been submitted
384 * are completely handled.
385 * Once ->stop is called and completes, the module will be completely
386 * unused.
387 */
NeilBrown390ee602010-06-01 19:37:27 +1000388void mddev_suspend(mddev_t *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100389{
390 BUG_ON(mddev->suspended);
391 mddev->suspended = 1;
392 synchronize_rcu();
393 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
394 mddev->pers->quiesce(mddev, 1);
NeilBrown409c57f2009-03-31 14:39:39 +1100395}
NeilBrown390ee602010-06-01 19:37:27 +1000396EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100397
NeilBrown390ee602010-06-01 19:37:27 +1000398void mddev_resume(mddev_t *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100399{
400 mddev->suspended = 0;
401 wake_up(&mddev->sb_wait);
402 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500403
404 md_wakeup_thread(mddev->thread);
405 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100406}
NeilBrown390ee602010-06-01 19:37:27 +1000407EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100408
NeilBrown3fa841d2009-09-23 18:10:29 +1000409int mddev_congested(mddev_t *mddev, int bits)
410{
411 return mddev->suspended;
412}
413EXPORT_SYMBOL(mddev_congested);
414
NeilBrowna2826aa2009-12-14 12:49:49 +1100415/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200416 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100417 */
418
Tejun Heoe9c74692010-09-03 11:56:18 +0200419static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100420{
421 mdk_rdev_t *rdev = bio->bi_private;
422 mddev_t *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100423
424 rdev_dec_pending(rdev, mddev);
425
426 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200427 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200428 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100429 }
430 bio_put(bio);
431}
432
NeilBrowna7a07e62010-12-09 16:04:25 +1100433static void md_submit_flush_data(struct work_struct *ws);
434
NeilBrowna035fc32010-12-09 16:17:51 +1100435static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100436{
NeilBrowna035fc32010-12-09 16:17:51 +1100437 mddev_t *mddev = container_of(ws, mddev_t, flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100438 mdk_rdev_t *rdev;
439
NeilBrowna7a07e62010-12-09 16:04:25 +1100440 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
441 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100442 rcu_read_lock();
443 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
444 if (rdev->raid_disk >= 0 &&
445 !test_bit(Faulty, &rdev->flags)) {
446 /* Take two references, one is dropped
447 * when request finishes, one after
448 * we reclaim rcu_read_lock
449 */
450 struct bio *bi;
451 atomic_inc(&rdev->nr_pending);
452 atomic_inc(&rdev->nr_pending);
453 rcu_read_unlock();
NeilBrowna167f662010-10-26 18:31:13 +1100454 bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200455 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100456 bi->bi_private = rdev;
457 bi->bi_bdev = rdev->bdev;
458 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200459 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100460 rcu_read_lock();
461 rdev_dec_pending(rdev, mddev);
462 }
463 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100464 if (atomic_dec_and_test(&mddev->flush_pending))
465 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100466}
467
Tejun Heoe9c74692010-09-03 11:56:18 +0200468static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100469{
Tejun Heoe9c74692010-09-03 11:56:18 +0200470 mddev_t *mddev = container_of(ws, mddev_t, flush_work);
471 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100472
Tejun Heoe9c74692010-09-03 11:56:18 +0200473 if (bio->bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100474 /* an empty barrier - all done */
475 bio_endio(bio, 0);
476 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200477 bio->bi_rw &= ~REQ_FLUSH;
NeilBrown21a52c62010-04-01 15:02:13 +1100478 if (mddev->pers->make_request(mddev, bio))
NeilBrowna2826aa2009-12-14 12:49:49 +1100479 generic_make_request(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100480 }
NeilBrown2b74e122010-12-09 15:59:01 +1100481
482 mddev->flush_bio = NULL;
483 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100484}
485
Tejun Heoe9c74692010-09-03 11:56:18 +0200486void md_flush_request(mddev_t *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100487{
488 spin_lock_irq(&mddev->write_lock);
489 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200490 !mddev->flush_bio,
NeilBrowna2826aa2009-12-14 12:49:49 +1100491 mddev->write_lock, /*nothing*/);
Tejun Heoe9c74692010-09-03 11:56:18 +0200492 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100493 spin_unlock_irq(&mddev->write_lock);
494
NeilBrowna035fc32010-12-09 16:17:51 +1100495 INIT_WORK(&mddev->flush_work, submit_flushes);
496 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100497}
Tejun Heoe9c74692010-09-03 11:56:18 +0200498EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100499
NeilBrown97658cd2011-04-18 18:25:42 +1000500/* Support for plugging.
501 * This mirrors the plugging support in request_queue, but does not
502 * require having a whole queue or request structures.
503 * We allocate an md_plug_cb for each md device and each thread it gets
504 * plugged on. This links tot the private plug_handle structure in the
505 * personality data where we keep a count of the number of outstanding
506 * plugs so other code can see if a plug is active.
507 */
508struct md_plug_cb {
509 struct blk_plug_cb cb;
510 mddev_t *mddev;
511};
512
513static void plugger_unplug(struct blk_plug_cb *cb)
514{
515 struct md_plug_cb *mdcb = container_of(cb, struct md_plug_cb, cb);
516 if (atomic_dec_and_test(&mdcb->mddev->plug_cnt))
517 md_wakeup_thread(mdcb->mddev->thread);
518 kfree(mdcb);
519}
520
521/* Check that an unplug wakeup will come shortly.
522 * If not, wakeup the md thread immediately
523 */
524int mddev_check_plugged(mddev_t *mddev)
525{
526 struct blk_plug *plug = current->plug;
527 struct md_plug_cb *mdcb;
528
529 if (!plug)
530 return 0;
531
532 list_for_each_entry(mdcb, &plug->cb_list, cb.list) {
533 if (mdcb->cb.callback == plugger_unplug &&
534 mdcb->mddev == mddev) {
535 /* Already on the list, move to top */
536 if (mdcb != list_first_entry(&plug->cb_list,
537 struct md_plug_cb,
538 cb.list))
539 list_move(&mdcb->cb.list, &plug->cb_list);
540 return 1;
541 }
542 }
543 /* Not currently on the callback list */
544 mdcb = kmalloc(sizeof(*mdcb), GFP_ATOMIC);
545 if (!mdcb)
546 return 0;
547
548 mdcb->mddev = mddev;
549 mdcb->cb.callback = plugger_unplug;
550 atomic_inc(&mddev->plug_cnt);
551 list_add(&mdcb->cb.list, &plug->cb_list);
552 return 1;
553}
554EXPORT_SYMBOL_GPL(mddev_check_plugged);
NeilBrown2ac87402010-06-01 19:37:29 +1000555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556static inline mddev_t *mddev_get(mddev_t *mddev)
557{
558 atomic_inc(&mddev->active);
559 return mddev;
560}
561
Dan Williams5fd3a172009-03-04 00:57:25 -0700562static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564static void mddev_put(mddev_t *mddev)
565{
NeilBrowna167f662010-10-26 18:31:13 +1100566 struct bio_set *bs = NULL;
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
569 return;
NeilBrownd3374822009-01-09 08:31:10 +1100570 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100571 mddev->ctime == 0 && !mddev->hold_active) {
572 /* Array is not configured at all, and not held active,
573 * so destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 list_del(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100575 bs = mddev->bio_set;
576 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100577 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200578 /* We did a probe so need to clean up. Call
579 * queue_work inside the spinlock so that
580 * flush_workqueue() after mddev_find will
581 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100582 */
583 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200584 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100585 } else
586 kfree(mddev);
587 }
588 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100589 if (bs)
590 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
NeilBrown390ee602010-06-01 19:37:27 +1000593void mddev_init(mddev_t *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100594{
595 mutex_init(&mddev->open_mutex);
596 mutex_init(&mddev->reconfig_mutex);
597 mutex_init(&mddev->bitmap_info.mutex);
598 INIT_LIST_HEAD(&mddev->disks);
599 INIT_LIST_HEAD(&mddev->all_mddevs);
600 init_timer(&mddev->safemode_timer);
601 atomic_set(&mddev->active, 1);
602 atomic_set(&mddev->openers, 0);
603 atomic_set(&mddev->active_io, 0);
NeilBrown97658cd2011-04-18 18:25:42 +1000604 atomic_set(&mddev->plug_cnt, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100605 spin_lock_init(&mddev->write_lock);
606 atomic_set(&mddev->flush_pending, 0);
607 init_waitqueue_head(&mddev->sb_wait);
608 init_waitqueue_head(&mddev->recovery_wait);
609 mddev->reshape_position = MaxSector;
610 mddev->resync_min = 0;
611 mddev->resync_max = MaxSector;
612 mddev->level = LEVEL_NONE;
613}
NeilBrown390ee602010-06-01 19:37:27 +1000614EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616static mddev_t * mddev_find(dev_t unit)
617{
618 mddev_t *mddev, *new = NULL;
619
NeilBrown8f5f02c2011-02-16 13:58:51 +1100620 if (unit && MAJOR(unit) != MD_MAJOR)
621 unit &= ~((1<<MdpMinorShift)-1);
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 retry:
624 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
NeilBrownefeb53c2009-01-09 08:31:10 +1100626 if (unit) {
627 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
628 if (mddev->unit == unit) {
629 mddev_get(mddev);
630 spin_unlock(&all_mddevs_lock);
631 kfree(new);
632 return mddev;
633 }
634
635 if (new) {
636 list_add(&new->all_mddevs, &all_mddevs);
637 spin_unlock(&all_mddevs_lock);
638 new->hold_active = UNTIL_IOCTL;
639 return new;
640 }
641 } else if (new) {
642 /* find an unused unit number */
643 static int next_minor = 512;
644 int start = next_minor;
645 int is_free = 0;
646 int dev = 0;
647 while (!is_free) {
648 dev = MKDEV(MD_MAJOR, next_minor);
649 next_minor++;
650 if (next_minor > MINORMASK)
651 next_minor = 0;
652 if (next_minor == start) {
653 /* Oh dear, all in use. */
654 spin_unlock(&all_mddevs_lock);
655 kfree(new);
656 return NULL;
657 }
658
659 is_free = 1;
660 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
661 if (mddev->unit == dev) {
662 is_free = 0;
663 break;
664 }
665 }
666 new->unit = dev;
667 new->md_minor = MINOR(dev);
668 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 list_add(&new->all_mddevs, &all_mddevs);
670 spin_unlock(&all_mddevs_lock);
671 return new;
672 }
673 spin_unlock(&all_mddevs_lock);
674
NeilBrown9ffae0c2006-01-06 00:20:32 -0800675 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (!new)
677 return NULL;
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 new->unit = unit;
680 if (MAJOR(unit) == MD_MAJOR)
681 new->md_minor = MINOR(unit);
682 else
683 new->md_minor = MINOR(unit) >> MdpMinorShift;
684
NeilBrownfafd7fb2010-04-01 15:55:30 +1100685 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 goto retry;
688}
689
690static inline int mddev_lock(mddev_t * mddev)
691{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800692 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
Dan Williamsb522adc2009-03-31 15:00:31 +1100695static inline int mddev_is_locked(mddev_t *mddev)
696{
697 return mutex_is_locked(&mddev->reconfig_mutex);
698}
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700static inline int mddev_trylock(mddev_t * mddev)
701{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800702 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
NeilBrownb6eb1272010-04-15 10:13:47 +1000705static struct attribute_group md_redundancy_group;
706
NeilBrowna64c8762010-04-14 17:15:37 +1000707static void mddev_unlock(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
NeilBrowna64c8762010-04-14 17:15:37 +1000709 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000710 /* These cannot be removed under reconfig_mutex as
711 * an access to the files will try to take reconfig_mutex
712 * while holding the file unremovable, which leads to
713 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000714 * So hold set sysfs_active while the remove in happeing,
715 * and anything else which might set ->to_remove or my
716 * otherwise change the sysfs namespace will fail with
717 * -EBUSY if sysfs_active is still set.
718 * We set sysfs_active under reconfig_mutex and elsewhere
719 * test it under the same mutex to ensure its correct value
720 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000721 */
NeilBrowna64c8762010-04-14 17:15:37 +1000722 struct attribute_group *to_remove = mddev->to_remove;
723 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000724 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000725 mutex_unlock(&mddev->reconfig_mutex);
726
NeilBrown00bcb4a2010-06-01 19:37:23 +1000727 if (mddev->kobj.sd) {
728 if (to_remove != &md_redundancy_group)
729 sysfs_remove_group(&mddev->kobj, to_remove);
730 if (mddev->pers == NULL ||
731 mddev->pers->sync_request == NULL) {
732 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
733 if (mddev->sysfs_action)
734 sysfs_put(mddev->sysfs_action);
735 mddev->sysfs_action = NULL;
736 }
NeilBrowna64c8762010-04-14 17:15:37 +1000737 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000738 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000739 } else
740 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
NeilBrown005eca52005-08-22 13:11:08 -0700742 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
NeilBrown2989ddb2006-01-06 00:20:43 -0800745static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100747 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100749 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (rdev->desc_nr == nr)
751 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return NULL;
754}
755
756static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
757{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 mdk_rdev_t *rdev;
759
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100760 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (rdev->bdev->bd_dev == dev)
762 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return NULL;
765}
766
NeilBrownd9d166c2006-01-06 00:20:51 -0800767static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800768{
769 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800770 list_for_each_entry(pers, &pers_list, list) {
771 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800772 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800773 if (strcmp(pers->name, clevel)==0)
774 return pers;
775 }
NeilBrown2604b702006-01-06 00:20:36 -0800776 return NULL;
777}
778
Andre Nollb73df2d2008-07-11 22:02:23 +1000779/* return the offset of the super block in 512byte sectors */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100780static inline sector_t calc_dev_sboffset(mdk_rdev_t *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100782 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000783 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786static int alloc_disk_sb(mdk_rdev_t * rdev)
787{
788 if (rdev->sb_page)
789 MD_BUG();
790
791 rdev->sb_page = alloc_page(GFP_KERNEL);
792 if (!rdev->sb_page) {
793 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000794 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796
797 return 0;
798}
799
800static void free_disk_sb(mdk_rdev_t * rdev)
801{
802 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800803 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 rdev->sb_loaded = 0;
805 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000806 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100807 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
NeilBrown2699b672011-07-28 11:31:47 +1000809 if (rdev->bb_page) {
810 put_page(rdev->bb_page);
811 rdev->bb_page = NULL;
812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
815
NeilBrown6712ecf2007-09-27 12:47:43 +0200816static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700817{
818 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800819 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700820
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700821 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
822 printk("md: super_written gets error=%d, uptodate=%d\n",
823 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
824 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800825 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700826 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700827
NeilBrowna9701a32005-11-08 21:39:34 -0800828 if (atomic_dec_and_test(&mddev->pending_writes))
829 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700830 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700831}
832
833void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
834 sector_t sector, int size, struct page *page)
835{
836 /* write first size bytes of page to sector of rdev
837 * Increment mddev->pending_writes before returning
838 * and decrement it on completion, waking up sb_wait
839 * if zero is reached.
840 * If an error occurred, call md_error
841 */
NeilBrowna167f662010-10-26 18:31:13 +1100842 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700843
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100844 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700845 bio->bi_sector = sector;
846 bio_add_page(bio, page, size, 0);
847 bio->bi_private = rdev;
848 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800849
NeilBrown7bfa19f2005-06-21 17:17:28 -0700850 atomic_inc(&mddev->pending_writes);
Jens Axboe721a9602011-03-09 11:56:30 +0100851 submit_bio(REQ_WRITE | REQ_SYNC | REQ_FLUSH | REQ_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800852}
853
854void md_super_wait(mddev_t *mddev)
855{
Tejun Heoe9c74692010-09-03 11:56:18 +0200856 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800857 DEFINE_WAIT(wq);
858 for(;;) {
859 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
860 if (atomic_read(&mddev->pending_writes)==0)
861 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800862 schedule();
863 }
864 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700865}
866
NeilBrown6712ecf2007-09-27 12:47:43 +0200867static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
NeilBrown2b193362010-10-27 15:16:40 +1100872int sync_page_io(mdk_rdev_t *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100873 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
NeilBrowna167f662010-10-26 18:31:13 +1100875 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 struct completion event;
877 int ret;
878
Jens Axboe721a9602011-03-09 11:56:30 +0100879 rw |= REQ_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100881 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
882 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100883 if (metadata_op)
884 bio->bi_sector = sector + rdev->sb_start;
885 else
886 bio->bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 bio_add_page(bio, page, size, 0);
888 init_completion(&event);
889 bio->bi_private = &event;
890 bio->bi_end_io = bi_complete;
891 submit_bio(rw, bio);
892 wait_for_completion(&event);
893
894 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
895 bio_put(bio);
896 return ret;
897}
NeilBrowna8745db2006-01-06 00:20:34 -0800898EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
NeilBrown0002b272005-09-09 16:23:53 -0700900static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901{
902 char b[BDEVNAME_SIZE];
903 if (!rdev->sb_page) {
904 MD_BUG();
905 return -EINVAL;
906 }
907 if (rdev->sb_loaded)
908 return 0;
909
910
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100911 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 goto fail;
913 rdev->sb_loaded = 1;
914 return 0;
915
916fail:
917 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
918 bdevname(rdev->bdev,b));
919 return -EINVAL;
920}
921
922static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
923{
Andre Noll05710462008-07-11 22:02:20 +1000924 return sb1->set_uuid0 == sb2->set_uuid0 &&
925 sb1->set_uuid1 == sb2->set_uuid1 &&
926 sb1->set_uuid2 == sb2->set_uuid2 &&
927 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
931{
932 int ret;
933 mdp_super_t *tmp1, *tmp2;
934
935 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
936 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
937
938 if (!tmp1 || !tmp2) {
939 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100940 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 goto abort;
942 }
943
944 *tmp1 = *sb1;
945 *tmp2 = *sb2;
946
947 /*
948 * nr_disks is not constant
949 */
950 tmp1->nr_disks = 0;
951 tmp2->nr_disks = 0;
952
Andre Nollce0c8e02008-07-11 22:02:20 +1000953 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700955 kfree(tmp1);
956 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return ret;
958}
959
NeilBrown4d167f02007-05-09 02:35:37 -0700960
961static u32 md_csum_fold(u32 csum)
962{
963 csum = (csum & 0xffff) + (csum >> 16);
964 return (csum & 0xffff) + (csum >> 16);
965}
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967static unsigned int calc_sb_csum(mdp_super_t * sb)
968{
NeilBrown4d167f02007-05-09 02:35:37 -0700969 u64 newcsum = 0;
970 u32 *sb32 = (u32*)sb;
971 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 unsigned int disk_csum, csum;
973
974 disk_csum = sb->sb_csum;
975 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700976
977 for (i = 0; i < MD_SB_BYTES/4 ; i++)
978 newcsum += sb32[i];
979 csum = (newcsum & 0xffffffff) + (newcsum>>32);
980
981
982#ifdef CONFIG_ALPHA
983 /* This used to use csum_partial, which was wrong for several
984 * reasons including that different results are returned on
985 * different architectures. It isn't critical that we get exactly
986 * the same return value as before (we always csum_fold before
987 * testing, and that removes any differences). However as we
988 * know that csum_partial always returned a 16bit value on
989 * alphas, do a fold to maximise conformity to previous behaviour.
990 */
991 sb->sb_csum = md_csum_fold(disk_csum);
992#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700994#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return csum;
996}
997
998
999/*
1000 * Handle superblock details.
1001 * We want to be able to handle multiple superblock formats
1002 * so we have a common interface to them all, and an array of
1003 * different handlers.
1004 * We rely on user-space to write the initial superblock, and support
1005 * reading and updating of superblocks.
1006 * Interface methods are:
1007 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
1008 * loads and validates a superblock on dev.
1009 * if refdev != NULL, compare superblocks on both devices
1010 * Return:
1011 * 0 - dev has a superblock that is compatible with refdev
1012 * 1 - dev has a superblock that is compatible and newer than refdev
1013 * so dev should be used as the refdev in future
1014 * -EINVAL superblock incompatible or invalid
1015 * -othererror e.g. -EIO
1016 *
1017 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
1018 * Verify that dev is acceptable into mddev.
1019 * The first time, mddev->raid_disks will be 0, and data from
1020 * dev should be merged in. Subsequent calls check that dev
1021 * is new enough. Return 0 or -EINVAL
1022 *
1023 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
1024 * Update the superblock for rdev with data in mddev
1025 * This does not write to disc.
1026 *
1027 */
1028
1029struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001030 char *name;
1031 struct module *owner;
1032 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
1033 int minor_version);
1034 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
1035 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
1036 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +10001037 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038};
1039
1040/*
Andre Noll0894cc32009-06-18 08:49:23 +10001041 * Check that the given mddev has no bitmap.
1042 *
1043 * This function is called from the run method of all personalities that do not
1044 * support bitmaps. It prints an error message and returns non-zero if mddev
1045 * has a bitmap. Otherwise, it returns 0.
1046 *
1047 */
1048int md_check_no_bitmap(mddev_t *mddev)
1049{
NeilBrownc3d97142009-12-14 12:49:52 +11001050 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001051 return 0;
1052 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
1053 mdname(mddev), mddev->pers->name);
1054 return 1;
1055}
1056EXPORT_SYMBOL(md_check_no_bitmap);
1057
1058/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 * load_super for 0.90.0
1060 */
1061static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1062{
1063 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1064 mdp_super_t *sb;
1065 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 /*
Andre Noll0f420352008-07-11 22:02:23 +10001068 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 * it's at the end of the disk.
1070 *
1071 * It also happens to be a multiple of 4Kb.
1072 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001073 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
NeilBrown0002b272005-09-09 16:23:53 -07001075 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (ret) return ret;
1077
1078 ret = -EINVAL;
1079
1080 bdevname(rdev->bdev, b);
Namhyung Kim65a06f062011-07-27 11:00:36 +10001081 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 if (sb->md_magic != MD_SB_MAGIC) {
1084 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1085 b);
1086 goto abort;
1087 }
1088
1089 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001090 sb->minor_version < 90 ||
1091 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1093 sb->major_version, sb->minor_version,
1094 b);
1095 goto abort;
1096 }
1097
1098 if (sb->raid_disks <= 0)
1099 goto abort;
1100
NeilBrown4d167f02007-05-09 02:35:37 -07001101 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1103 b);
1104 goto abort;
1105 }
1106
1107 rdev->preferred_minor = sb->md_minor;
1108 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001109 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f3832011-07-28 11:31:47 +10001110 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 if (sb->level == LEVEL_MULTIPATH)
1113 rdev->desc_nr = -1;
1114 else
1115 rdev->desc_nr = sb->this_disk.number;
1116
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001117 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001119 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001121 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (!uuid_equal(refsb, sb)) {
1123 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1124 b, bdevname(refdev->bdev,b2));
1125 goto abort;
1126 }
1127 if (!sb_equal(refsb, sb)) {
1128 printk(KERN_WARNING "md: %s has same UUID"
1129 " but different superblock to %s\n",
1130 b, bdevname(refdev->bdev, b2));
1131 goto abort;
1132 }
1133 ev1 = md_event(sb);
1134 ev2 = md_event(refsb);
1135 if (ev1 > ev2)
1136 ret = 1;
1137 else
1138 ret = 0;
1139 }
NeilBrown8190e752009-06-18 08:48:58 +10001140 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Andre Nolldd8ac332009-03-31 14:33:13 +11001142 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001143 /* "this cannot possibly happen" ... */
1144 ret = -EINVAL;
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 abort:
1147 return ret;
1148}
1149
1150/*
1151 * validate_super for 0.90.0
1152 */
1153static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1154{
1155 mdp_disk_t *desc;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001156 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001157 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
NeilBrown41158c72005-06-21 17:17:25 -07001159 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001160 clear_bit(Faulty, &rdev->flags);
1161 clear_bit(In_sync, &rdev->flags);
1162 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (mddev->raid_disks == 0) {
1165 mddev->major_version = 0;
1166 mddev->minor_version = sb->minor_version;
1167 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001168 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001169 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 mddev->ctime = sb->ctime;
1171 mddev->utime = sb->utime;
1172 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001173 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 mddev->layout = sb->layout;
1175 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +11001176 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001177 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001178 mddev->bitmap_info.offset = 0;
1179 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
NeilBrownf6705572006-03-27 01:18:11 -08001181 if (mddev->minor_version >= 91) {
1182 mddev->reshape_position = sb->reshape_position;
1183 mddev->delta_disks = sb->delta_disks;
1184 mddev->new_level = sb->new_level;
1185 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001186 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001187 } else {
1188 mddev->reshape_position = MaxSector;
1189 mddev->delta_disks = 0;
1190 mddev->new_level = mddev->level;
1191 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001192 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001193 }
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if (sb->state & (1<<MD_SB_CLEAN))
1196 mddev->recovery_cp = MaxSector;
1197 else {
1198 if (sb->events_hi == sb->cp_events_hi &&
1199 sb->events_lo == sb->cp_events_lo) {
1200 mddev->recovery_cp = sb->recovery_cp;
1201 } else
1202 mddev->recovery_cp = 0;
1203 }
1204
1205 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1206 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1207 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1208 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1209
1210 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001211
1212 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001213 mddev->bitmap_info.file == NULL)
1214 mddev->bitmap_info.offset =
1215 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001216
NeilBrown41158c72005-06-21 17:17:25 -07001217 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001218 /* Insist on good event counter while assembling, except
1219 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001221 if (sb->disks[rdev->desc_nr].state & (
1222 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1223 if (ev1 < mddev->events)
1224 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001225 } else if (mddev->bitmap) {
1226 /* if adding to array with a bitmap, then we can accept an
1227 * older device ... but not too old.
1228 */
NeilBrown41158c72005-06-21 17:17:25 -07001229 if (ev1 < mddev->bitmap->events_cleared)
1230 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001231 } else {
1232 if (ev1 < mddev->events)
1233 /* just a hot-add of a new device, leave raid_disk at -1 */
1234 return 0;
1235 }
NeilBrown41158c72005-06-21 17:17:25 -07001236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 desc = sb->disks + rdev->desc_nr;
1239
1240 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001241 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001242 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1243 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001244 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001246 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1247 /* active but not in sync implies recovery up to
1248 * reshape position. We don't know exactly where
1249 * that is, so set to zero for now */
1250 if (mddev->minor_version >= 91) {
1251 rdev->recovery_offset = 0;
1252 rdev->raid_disk = desc->raid_disk;
1253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001255 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1256 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001257 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001258 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return 0;
1260}
1261
1262/*
1263 * sync_super for 0.90.0
1264 */
1265static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1266{
1267 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 mdk_rdev_t *rdev2;
1269 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 /* make rdev->sb match mddev data..
1273 *
1274 * 1/ zero out disks
1275 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1276 * 3/ any empty disks < next_spare become removed
1277 *
1278 * disks[0] gets initialised to REMOVED because
1279 * we cannot be sure from other fields if it has
1280 * been initialised or not.
1281 */
1282 int i;
1283 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1284
NeilBrown61181562005-09-09 16:24:02 -07001285 rdev->sb_size = MD_SB_BYTES;
1286
Namhyung Kim65a06f062011-07-27 11:00:36 +10001287 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 memset(sb, 0, sizeof(*sb));
1290
1291 sb->md_magic = MD_SB_MAGIC;
1292 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 sb->patch_version = mddev->patch_version;
1294 sb->gvalid_words = 0; /* ignored */
1295 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1296 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1297 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1298 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1299
1300 sb->ctime = mddev->ctime;
1301 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001302 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 sb->raid_disks = mddev->raid_disks;
1304 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001305 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 sb->utime = mddev->utime;
1307 sb->state = 0;
1308 sb->events_hi = (mddev->events>>32);
1309 sb->events_lo = (u32)mddev->events;
1310
NeilBrownf6705572006-03-27 01:18:11 -08001311 if (mddev->reshape_position == MaxSector)
1312 sb->minor_version = 90;
1313 else {
1314 sb->minor_version = 91;
1315 sb->reshape_position = mddev->reshape_position;
1316 sb->new_level = mddev->new_level;
1317 sb->delta_disks = mddev->delta_disks;
1318 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001319 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001320 }
1321 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 if (mddev->in_sync)
1323 {
1324 sb->recovery_cp = mddev->recovery_cp;
1325 sb->cp_events_hi = (mddev->events>>32);
1326 sb->cp_events_lo = (u32)mddev->events;
1327 if (mddev->recovery_cp == MaxSector)
1328 sb->state = (1<< MD_SB_CLEAN);
1329 } else
1330 sb->recovery_cp = 0;
1331
1332 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001333 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
NeilBrownc3d97142009-12-14 12:49:52 +11001335 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001336 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001339 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001341 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001342 int is_active = test_bit(In_sync, &rdev2->flags);
1343
1344 if (rdev2->raid_disk >= 0 &&
1345 sb->minor_version >= 91)
1346 /* we have nowhere to store the recovery_offset,
1347 * but if it is not below the reshape_position,
1348 * we can piggy-back on that.
1349 */
1350 is_active = 1;
1351 if (rdev2->raid_disk < 0 ||
1352 test_bit(Faulty, &rdev2->flags))
1353 is_active = 0;
1354 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001355 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001357 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001358 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 d = &sb->disks[rdev2->desc_nr];
1360 nr_disks++;
1361 d->number = rdev2->desc_nr;
1362 d->major = MAJOR(rdev2->bdev->bd_dev);
1363 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001364 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 d->raid_disk = rdev2->raid_disk;
1366 else
1367 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001368 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001370 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001372 if (test_bit(In_sync, &rdev2->flags))
1373 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 active++;
1375 working++;
1376 } else {
1377 d->state = 0;
1378 spare++;
1379 working++;
1380 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001381 if (test_bit(WriteMostly, &rdev2->flags))
1382 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 /* now set the "removed" and "faulty" bits on any missing devices */
1385 for (i=0 ; i < mddev->raid_disks ; i++) {
1386 mdp_disk_t *d = &sb->disks[i];
1387 if (d->state == 0 && d->number == 0) {
1388 d->number = i;
1389 d->raid_disk = i;
1390 d->state = (1<<MD_DISK_REMOVED);
1391 d->state |= (1<<MD_DISK_FAULTY);
1392 failed++;
1393 }
1394 }
1395 sb->nr_disks = nr_disks;
1396 sb->active_disks = active;
1397 sb->working_disks = working;
1398 sb->failed_disks = failed;
1399 sb->spare_disks = spare;
1400
1401 sb->this_disk = sb->disks[rdev->desc_nr];
1402 sb->sb_csum = calc_sb_csum(sb);
1403}
1404
1405/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001406 * rdev_size_change for 0.90.0
1407 */
1408static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001409super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001410{
Andre Noll58c0fed2009-03-31 14:33:13 +11001411 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001412 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001413 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001414 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001415 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001416 if (!num_sectors || num_sectors > rdev->sb_start)
1417 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001418 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001419 rdev->sb_page);
1420 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001421 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001422}
1423
1424
1425/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 * version 1 superblock
1427 */
1428
NeilBrown1c05b4b2006-10-21 10:24:08 -07001429static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001431 __le32 disk_csum;
1432 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 unsigned long long newcsum;
1434 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001435 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 int i;
1437
1438 disk_csum = sb->sb_csum;
1439 sb->sb_csum = 0;
1440 newcsum = 0;
1441 for (i=0; size>=4; size -= 4 )
1442 newcsum += le32_to_cpu(*isuper++);
1443
1444 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001445 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1448 sb->sb_csum = disk_csum;
1449 return cpu_to_le32(csum);
1450}
1451
NeilBrown2699b672011-07-28 11:31:47 +10001452static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1453 int acknowledged);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1455{
1456 struct mdp_superblock_1 *sb;
1457 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001458 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001460 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 /*
Andre Noll0f420352008-07-11 22:02:23 +10001463 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 * It is always aligned to a 4K boundary and
1465 * depeding on minor_version, it can be:
1466 * 0: At least 8K, but less than 12K, from end of device
1467 * 1: At start of device
1468 * 2: 4K from start of device.
1469 */
1470 switch(minor_version) {
1471 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001472 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001473 sb_start -= 8*2;
1474 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 break;
1476 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001477 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 break;
1479 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001480 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 break;
1482 default:
1483 return -EINVAL;
1484 }
Andre Noll0f420352008-07-11 22:02:23 +10001485 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
NeilBrown0002b272005-09-09 16:23:53 -07001487 /* superblock is rarely larger than 1K, but it can be larger,
1488 * and it is safe to read 4k, so we do that
1489 */
1490 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (ret) return ret;
1492
1493
Namhyung Kim65a06f062011-07-27 11:00:36 +10001494 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1497 sb->major_version != cpu_to_le32(1) ||
1498 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001499 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001500 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return -EINVAL;
1502
1503 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1504 printk("md: invalid superblock checksum on %s\n",
1505 bdevname(rdev->bdev,b));
1506 return -EINVAL;
1507 }
1508 if (le64_to_cpu(sb->data_size) < 10) {
1509 printk("md: data_size too small on %s\n",
1510 bdevname(rdev->bdev,b));
1511 return -EINVAL;
1512 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 rdev->preferred_minor = 0xffff;
1515 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001516 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
NeilBrown0002b272005-09-09 16:23:53 -07001518 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001519 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001520 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001521 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1522
1523 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001524 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001525 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001526
NeilBrown31b65a02006-07-10 04:44:14 -07001527 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1528 rdev->desc_nr = -1;
1529 else
1530 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1531
NeilBrown2699b672011-07-28 11:31:47 +10001532 if (!rdev->bb_page) {
1533 rdev->bb_page = alloc_page(GFP_KERNEL);
1534 if (!rdev->bb_page)
1535 return -ENOMEM;
1536 }
1537 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1538 rdev->badblocks.count == 0) {
1539 /* need to load the bad block list.
1540 * Currently we limit it to one page.
1541 */
1542 s32 offset;
1543 sector_t bb_sector;
1544 u64 *bbp;
1545 int i;
1546 int sectors = le16_to_cpu(sb->bblog_size);
1547 if (sectors > (PAGE_SIZE / 512))
1548 return -EINVAL;
1549 offset = le32_to_cpu(sb->bblog_offset);
1550 if (offset == 0)
1551 return -EINVAL;
1552 bb_sector = (long long)offset;
1553 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1554 rdev->bb_page, READ, true))
1555 return -EIO;
1556 bbp = (u64 *)page_address(rdev->bb_page);
1557 rdev->badblocks.shift = sb->bblog_shift;
1558 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1559 u64 bb = le64_to_cpu(*bbp);
1560 int count = bb & (0x3ff);
1561 u64 sector = bb >> 10;
1562 sector <<= sb->bblog_shift;
1563 count <<= sb->bblog_shift;
1564 if (bb + 1 == 0)
1565 break;
1566 if (md_set_badblocks(&rdev->badblocks,
1567 sector, count, 1) == 0)
1568 return -EINVAL;
1569 }
1570 } else if (sb->bblog_offset == 0)
1571 rdev->badblocks.shift = -1;
1572
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001573 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001574 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001575 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 __u64 ev1, ev2;
Namhyung Kim65a06f062011-07-27 11:00:36 +10001577 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
1579 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1580 sb->level != refsb->level ||
1581 sb->layout != refsb->layout ||
1582 sb->chunksize != refsb->chunksize) {
1583 printk(KERN_WARNING "md: %s has strangely different"
1584 " superblock to %s\n",
1585 bdevname(rdev->bdev,b),
1586 bdevname(refdev->bdev,b2));
1587 return -EINVAL;
1588 }
1589 ev1 = le64_to_cpu(sb->events);
1590 ev2 = le64_to_cpu(refsb->events);
1591
1592 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001593 ret = 1;
1594 else
1595 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 }
NeilBrowna1801f82008-03-04 14:29:31 -08001597 if (minor_version)
Mike Snitzer77304d22010-11-08 14:39:12 +01001598 rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11001599 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001601 rdev->sectors = rdev->sb_start;
1602 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001604 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001605 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001606 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001607 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609
1610static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1611{
Namhyung Kim65a06f062011-07-27 11:00:36 +10001612 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001613 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
NeilBrown41158c72005-06-21 17:17:25 -07001615 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001616 clear_bit(Faulty, &rdev->flags);
1617 clear_bit(In_sync, &rdev->flags);
1618 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 if (mddev->raid_disks == 0) {
1621 mddev->major_version = 1;
1622 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001623 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001624 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1626 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1627 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001628 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 mddev->layout = le32_to_cpu(sb->layout);
1630 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001631 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001632 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001633 mddev->bitmap_info.offset = 0;
1634 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1637 memcpy(mddev->uuid, sb->set_uuid, 16);
1638
1639 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001640
NeilBrown71c08052005-09-09 16:23:51 -07001641 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001642 mddev->bitmap_info.file == NULL )
1643 mddev->bitmap_info.offset =
1644 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001645
NeilBrownf6705572006-03-27 01:18:11 -08001646 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1647 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1648 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1649 mddev->new_level = le32_to_cpu(sb->new_level);
1650 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001651 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001652 } else {
1653 mddev->reshape_position = MaxSector;
1654 mddev->delta_disks = 0;
1655 mddev->new_level = mddev->level;
1656 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001657 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001658 }
1659
NeilBrown41158c72005-06-21 17:17:25 -07001660 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001661 /* Insist of good event counter while assembling, except for
1662 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001664 if (rdev->desc_nr >= 0 &&
1665 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1666 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1667 if (ev1 < mddev->events)
1668 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001669 } else if (mddev->bitmap) {
1670 /* If adding to array with a bitmap, then we can accept an
1671 * older device, but not too old.
1672 */
NeilBrown41158c72005-06-21 17:17:25 -07001673 if (ev1 < mddev->bitmap->events_cleared)
1674 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001675 } else {
1676 if (ev1 < mddev->events)
1677 /* just a hot-add of a new device, leave raid_disk at -1 */
1678 return 0;
1679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 if (mddev->level != LEVEL_MULTIPATH) {
1681 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001682 if (rdev->desc_nr < 0 ||
1683 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1684 role = 0xffff;
1685 rdev->desc_nr = -1;
1686 } else
1687 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 switch(role) {
1689 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 break;
1691 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001692 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 break;
1694 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001695 if ((le32_to_cpu(sb->feature_map) &
1696 MD_FEATURE_RECOVERY_OFFSET))
1697 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1698 else
1699 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 rdev->raid_disk = role;
1701 break;
1702 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001703 if (sb->devflags & WriteMostly1)
1704 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001705 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001706 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 return 0;
1709}
1710
1711static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1712{
1713 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 mdk_rdev_t *rdev2;
1715 int max_dev, i;
1716 /* make rdev->sb match mddev and rdev data. */
1717
Namhyung Kim65a06f062011-07-27 11:00:36 +10001718 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 sb->feature_map = 0;
1721 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001722 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 memset(sb->pad1, 0, sizeof(sb->pad1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 memset(sb->pad3, 0, sizeof(sb->pad3));
1725
1726 sb->utime = cpu_to_le64((__u64)mddev->utime);
1727 sb->events = cpu_to_le64(mddev->events);
1728 if (mddev->in_sync)
1729 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1730 else
1731 sb->resync_offset = cpu_to_le64(0);
1732
NeilBrown1c05b4b2006-10-21 10:24:08 -07001733 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001734
NeilBrownf0ca3402006-02-02 14:28:04 -08001735 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001736 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001737 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001738 sb->level = cpu_to_le32(mddev->level);
1739 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001740
NeilBrownc3d97142009-12-14 12:49:52 +11001741 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1742 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001743 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001744 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001745
1746 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001747 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001748 sb->feature_map |=
1749 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1750 sb->recovery_offset =
1751 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001752 }
1753
NeilBrownf6705572006-03-27 01:18:11 -08001754 if (mddev->reshape_position != MaxSector) {
1755 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1756 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1757 sb->new_layout = cpu_to_le32(mddev->new_layout);
1758 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1759 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001760 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001761 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001762
NeilBrown2699b672011-07-28 11:31:47 +10001763 if (rdev->badblocks.count == 0)
1764 /* Nothing to do for bad blocks*/ ;
1765 else if (sb->bblog_offset == 0)
1766 /* Cannot record bad blocks on this device */
1767 md_error(mddev, rdev);
1768 else {
1769 struct badblocks *bb = &rdev->badblocks;
1770 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1771 u64 *p = bb->page;
1772 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1773 if (bb->changed) {
1774 unsigned seq;
1775
1776retry:
1777 seq = read_seqbegin(&bb->lock);
1778
1779 memset(bbp, 0xff, PAGE_SIZE);
1780
1781 for (i = 0 ; i < bb->count ; i++) {
1782 u64 internal_bb = *p++;
1783 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1784 | BB_LEN(internal_bb));
1785 *bbp++ = cpu_to_le64(store_bb);
1786 }
1787 if (read_seqretry(&bb->lock, seq))
1788 goto retry;
1789
1790 bb->sector = (rdev->sb_start +
1791 (int)le32_to_cpu(sb->bblog_offset));
1792 bb->size = le16_to_cpu(sb->bblog_size);
1793 bb->changed = 0;
1794 }
1795 }
1796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001798 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 if (rdev2->desc_nr+1 > max_dev)
1800 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001801
NeilBrown70471da2009-08-03 10:59:57 +10001802 if (max_dev > le32_to_cpu(sb->max_dev)) {
1803 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001804 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001805 rdev->sb_size = max_dev * 2 + 256;
1806 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1807 if (rdev->sb_size & bmask)
1808 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001809 } else
1810 max_dev = le32_to_cpu(sb->max_dev);
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 for (i=0; i<max_dev;i++)
1813 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1814
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001815 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001817 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001819 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001821 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001822 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 else
1824 sb->dev_roles[i] = cpu_to_le16(0xffff);
1825 }
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 sb->sb_csum = calc_sb_1_csum(sb);
1828}
1829
Chris Webb0cd17fe2008-06-28 08:31:46 +10001830static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001831super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001832{
1833 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001834 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001835 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001836 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001837 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001838 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001839 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001840 max_sectors -= rdev->data_offset;
1841 if (!num_sectors || num_sectors > max_sectors)
1842 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001843 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001844 /* minor version 0 with bitmap we can't move */
1845 return 0;
1846 } else {
1847 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001848 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001849 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001850 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001851 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001852 if (!num_sectors || num_sectors > max_sectors)
1853 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001854 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001855 }
Namhyung Kim65a06f062011-07-27 11:00:36 +10001856 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001857 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001858 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001859 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001860 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001861 rdev->sb_page);
1862 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001863 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001864}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Adrian Bunk75c96f82005-05-05 16:16:09 -07001866static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 [0] = {
1868 .name = "0.90.0",
1869 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001870 .load_super = super_90_load,
1871 .validate_super = super_90_validate,
1872 .sync_super = super_90_sync,
1873 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 },
1875 [1] = {
1876 .name = "md-1",
1877 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001878 .load_super = super_1_load,
1879 .validate_super = super_1_validate,
1880 .sync_super = super_1_sync,
1881 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 },
1883};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Jonathan Brassow076f9682011-06-07 17:51:30 -05001885static void sync_super(mddev_t *mddev, mdk_rdev_t *rdev)
1886{
1887 if (mddev->sync_super) {
1888 mddev->sync_super(mddev, rdev);
1889 return;
1890 }
1891
1892 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1893
1894 super_types[mddev->major_version].sync_super(mddev, rdev);
1895}
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1898{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001899 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
NeilBrown4b809912008-07-21 17:05:25 +10001901 rcu_read_lock();
1902 rdev_for_each_rcu(rdev, mddev1)
1903 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001904 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001905 rdev2->bdev->bd_contains) {
1906 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001907 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001908 }
1909 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 return 0;
1911}
1912
1913static LIST_HEAD(pending_raid_disks);
1914
Andre Nollac5e7112009-08-03 10:59:47 +10001915/*
1916 * Try to register data integrity profile for an mddev
1917 *
1918 * This is called when an array is started and after a disk has been kicked
1919 * from the array. It only succeeds if all working and active component devices
1920 * are integrity capable with matching profiles.
1921 */
1922int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001923{
Andre Nollac5e7112009-08-03 10:59:47 +10001924 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001925
Andre Nollac5e7112009-08-03 10:59:47 +10001926 if (list_empty(&mddev->disks))
1927 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001928 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1929 return 0; /* shouldn't register, or already is */
Andre Nollac5e7112009-08-03 10:59:47 +10001930 list_for_each_entry(rdev, &mddev->disks, same_set) {
1931 /* skip spares and non-functional disks */
1932 if (test_bit(Faulty, &rdev->flags))
1933 continue;
1934 if (rdev->raid_disk < 0)
1935 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001936 if (!reference) {
1937 /* Use the first rdev as the reference */
1938 reference = rdev;
1939 continue;
1940 }
1941 /* does this rdev's profile match the reference profile? */
1942 if (blk_integrity_compare(reference->bdev->bd_disk,
1943 rdev->bdev->bd_disk) < 0)
1944 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001945 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001946 if (!reference || !bdev_get_integrity(reference->bdev))
1947 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001948 /*
1949 * All component devices are integrity capable and have matching
1950 * profiles, register the common profile for the md device.
1951 */
1952 if (blk_integrity_register(mddev->gendisk,
1953 bdev_get_integrity(reference->bdev)) != 0) {
1954 printk(KERN_ERR "md: failed to register integrity for %s\n",
1955 mdname(mddev));
1956 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001957 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001958 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1959 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1960 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1961 mdname(mddev));
1962 return -EINVAL;
1963 }
Andre Nollac5e7112009-08-03 10:59:47 +10001964 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001965}
Andre Nollac5e7112009-08-03 10:59:47 +10001966EXPORT_SYMBOL(md_integrity_register);
1967
1968/* Disable data integrity if non-capable/non-matching disk is being added */
1969void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1970{
1971 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1972 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1973
1974 if (!bi_mddev) /* nothing to do */
1975 return;
1976 if (rdev->raid_disk < 0) /* skip spares */
1977 return;
1978 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1979 rdev->bdev->bd_disk) >= 0)
1980 return;
1981 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1982 blk_integrity_unregister(mddev->gendisk);
1983}
1984EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1987{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001988 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001989 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001990 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001991 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 if (rdev->mddev) {
1994 MD_BUG();
1995 return -EINVAL;
1996 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001997
1998 /* prevent duplicates */
1999 if (find_rdev(mddev, rdev->bdev->bd_dev))
2000 return -EEXIST;
2001
Andre Nolldd8ac332009-03-31 14:33:13 +11002002 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2003 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2004 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002005 if (mddev->pers) {
2006 /* Cannot change size, so fail
2007 * If mddev->level <= 0, then we don't care
2008 * about aligning sizes (e.g. linear)
2009 */
2010 if (mddev->level > 0)
2011 return -ENOSPC;
2012 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002013 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 /* Verify rdev->desc_nr is unique.
2017 * If it is -1, assign a free number, else
2018 * check number is not in use
2019 */
2020 if (rdev->desc_nr < 0) {
2021 int choice = 0;
2022 if (mddev->pers) choice = mddev->raid_disks;
2023 while (find_rdev_nr(mddev, choice))
2024 choice++;
2025 rdev->desc_nr = choice;
2026 } else {
2027 if (find_rdev_nr(mddev, rdev->desc_nr))
2028 return -EBUSY;
2029 }
NeilBrownde01dfa2009-02-06 18:02:46 +11002030 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2031 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2032 mdname(mddev), mddev->max_disks);
2033 return -EBUSY;
2034 }
NeilBrown19133a42005-11-08 21:39:35 -08002035 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002036 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002037 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002040 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002041
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002042 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002043 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002044
Tejun Heo0762b8b2008-08-25 19:56:12 +09002045 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002046 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2047 /* failure here is OK */;
2048 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002049
NeilBrown4b809912008-07-21 17:05:25 +10002050 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002051 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002052
2053 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002054 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002057
2058 fail:
2059 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2060 b, mdname(mddev));
2061 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062}
2063
NeilBrown177a99b2008-02-06 01:39:56 -08002064static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002065{
2066 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
2067 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002068 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002069}
2070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071static void unbind_rdev_from_array(mdk_rdev_t * rdev)
2072{
2073 char b[BDEVNAME_SIZE];
2074 if (!rdev->mddev) {
2075 MD_BUG();
2076 return;
2077 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002078 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002079 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2081 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002082 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002083 sysfs_put(rdev->sysfs_state);
2084 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002085 kfree(rdev->badblocks.page);
2086 rdev->badblocks.count = 0;
2087 rdev->badblocks.page = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07002088 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002089 * writing to 'remove' to "dev/state". We also need
2090 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002091 */
NeilBrown4b809912008-07-21 17:05:25 +10002092 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002093 INIT_WORK(&rdev->del_work, md_delayed_delete);
2094 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002095 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096}
2097
2098/*
2099 * prevent the device from being mounted, repartitioned or
2100 * otherwise reused by a RAID array (or any other kernel
2101 * subsystem), by bd_claiming the device.
2102 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002103static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104{
2105 int err = 0;
2106 struct block_device *bdev;
2107 char b[BDEVNAME_SIZE];
2108
Tejun Heod4d77622010-11-13 11:55:18 +01002109 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
2110 shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 if (IS_ERR(bdev)) {
2112 printk(KERN_ERR "md: could not open %s.\n",
2113 __bdevname(dev, b));
2114 return PTR_ERR(bdev);
2115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 rdev->bdev = bdev;
2117 return err;
2118}
2119
2120static void unlock_rdev(mdk_rdev_t *rdev)
2121{
2122 struct block_device *bdev = rdev->bdev;
2123 rdev->bdev = NULL;
2124 if (!bdev)
2125 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002126 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
2128
2129void md_autodetect_dev(dev_t dev);
2130
2131static void export_rdev(mdk_rdev_t * rdev)
2132{
2133 char b[BDEVNAME_SIZE];
2134 printk(KERN_INFO "md: export_rdev(%s)\n",
2135 bdevname(rdev->bdev,b));
2136 if (rdev->mddev)
2137 MD_BUG();
2138 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002140 if (test_bit(AutoDetected, &rdev->flags))
2141 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142#endif
2143 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002144 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145}
2146
2147static void kick_rdev_from_array(mdk_rdev_t * rdev)
2148{
2149 unbind_rdev_from_array(rdev);
2150 export_rdev(rdev);
2151}
2152
2153static void export_array(mddev_t *mddev)
2154{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002155 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
NeilBrownd089c6a2008-02-06 01:39:59 -08002157 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 if (!rdev->mddev) {
2159 MD_BUG();
2160 continue;
2161 }
2162 kick_rdev_from_array(rdev);
2163 }
2164 if (!list_empty(&mddev->disks))
2165 MD_BUG();
2166 mddev->raid_disks = 0;
2167 mddev->major_version = 0;
2168}
2169
2170static void print_desc(mdp_disk_t *desc)
2171{
2172 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2173 desc->major,desc->minor,desc->raid_disk,desc->state);
2174}
2175
Cheng Renquancd2ac932009-01-09 08:31:08 +11002176static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
2178 int i;
2179
2180 printk(KERN_INFO
2181 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2182 sb->major_version, sb->minor_version, sb->patch_version,
2183 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2184 sb->ctime);
2185 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2186 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2187 sb->md_minor, sb->layout, sb->chunk_size);
2188 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2189 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2190 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2191 sb->failed_disks, sb->spare_disks,
2192 sb->sb_csum, (unsigned long)sb->events_lo);
2193
2194 printk(KERN_INFO);
2195 for (i = 0; i < MD_SB_DISKS; i++) {
2196 mdp_disk_t *desc;
2197
2198 desc = sb->disks + i;
2199 if (desc->number || desc->major || desc->minor ||
2200 desc->raid_disk || (desc->state && (desc->state != 4))) {
2201 printk(" D %2d: ", i);
2202 print_desc(desc);
2203 }
2204 }
2205 printk(KERN_INFO "md: THIS: ");
2206 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207}
2208
Cheng Renquancd2ac932009-01-09 08:31:08 +11002209static void print_sb_1(struct mdp_superblock_1 *sb)
2210{
2211 __u8 *uuid;
2212
2213 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002214 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002215 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002216 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002217 le32_to_cpu(sb->major_version),
2218 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002219 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002220 sb->set_name,
2221 (unsigned long long)le64_to_cpu(sb->ctime)
2222 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2223
2224 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002225 printk(KERN_INFO
2226 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002227 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002228 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002229 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2230 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002231 le32_to_cpu(sb->level),
2232 (unsigned long long)le64_to_cpu(sb->size),
2233 le32_to_cpu(sb->raid_disks),
2234 le32_to_cpu(sb->layout),
2235 le32_to_cpu(sb->chunksize),
2236 (unsigned long long)le64_to_cpu(sb->data_offset),
2237 (unsigned long long)le64_to_cpu(sb->data_size),
2238 (unsigned long long)le64_to_cpu(sb->super_offset),
2239 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2240 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002241 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002242 sb->devflags,
2243 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2244 (unsigned long long)le64_to_cpu(sb->events),
2245 (unsigned long long)le64_to_cpu(sb->resync_offset),
2246 le32_to_cpu(sb->sb_csum),
2247 le32_to_cpu(sb->max_dev)
2248 );
2249}
2250
2251static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
2253 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002254 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2255 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002256 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2257 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002259 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2260 switch (major_version) {
2261 case 0:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002262 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002263 break;
2264 case 1:
Namhyung Kim65a06f062011-07-27 11:00:36 +10002265 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002266 break;
2267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 } else
2269 printk(KERN_INFO "md: no rdev superblock!\n");
2270}
2271
Adrian Bunk5e563412006-06-26 00:27:42 -07002272static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002274 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 mdk_rdev_t *rdev;
2276 mddev_t *mddev;
2277 char b[BDEVNAME_SIZE];
2278
2279 printk("\n");
2280 printk("md: **********************************\n");
2281 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2282 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002283 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
NeilBrown32a76272005-06-21 17:17:14 -07002285 if (mddev->bitmap)
2286 bitmap_print_sb(mddev->bitmap);
2287 else
2288 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002289 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 printk("<%s>", bdevname(rdev->bdev,b));
2291 printk("\n");
2292
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002293 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002294 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 }
2296 printk("md: **********************************\n");
2297 printk("\n");
2298}
2299
2300
NeilBrown42543762006-06-26 00:27:57 -07002301static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302{
NeilBrown42543762006-06-26 00:27:57 -07002303 /* Update each superblock (in-memory image), but
2304 * if we are allowed to, skip spares which already
2305 * have the right event counter, or have one earlier
2306 * (which would mean they aren't being marked as dirty
2307 * with the rest of the array)
2308 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 mdk_rdev_t *rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002310 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002311 if (rdev->sb_events == mddev->events ||
2312 (nospares &&
2313 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002314 rdev->sb_events+1 == mddev->events)) {
2315 /* Don't update this superblock */
2316 rdev->sb_loaded = 2;
2317 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002318 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002319 rdev->sb_loaded = 1;
2320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 }
2322}
2323
NeilBrown850b2b42006-10-03 01:15:46 -07002324static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002327 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002328 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002329 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002332 /* First make sure individual recovery_offsets are correct */
2333 list_for_each_entry(rdev, &mddev->disks, same_set) {
2334 if (rdev->raid_disk >= 0 &&
2335 mddev->delta_disks >= 0 &&
2336 !test_bit(In_sync, &rdev->flags) &&
2337 mddev->curr_resync_completed > rdev->recovery_offset)
2338 rdev->recovery_offset = mddev->curr_resync_completed;
2339
2340 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002341 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002342 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002343 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002344 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002345 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002346 list_for_each_entry(rdev, &mddev->disks, same_set) {
2347 if (rdev->badblocks.changed) {
2348 md_ack_all_badblocks(&rdev->badblocks);
2349 md_error(mddev, rdev);
2350 }
2351 clear_bit(Blocked, &rdev->flags);
2352 clear_bit(BlockedBadBlocks, &rdev->flags);
2353 wake_up(&rdev->blocked_wait);
2354 }
2355 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002356 wake_up(&mddev->sb_wait);
2357 return;
2358 }
2359
NeilBrowna9701a32005-11-08 21:39:34 -08002360 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002361
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002362 mddev->utime = get_seconds();
2363
NeilBrown850b2b42006-10-03 01:15:46 -07002364 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2365 force_change = 1;
2366 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2367 /* just a clean<-> dirty transition, possibly leave spares alone,
2368 * though if events isn't the right even/odd, we will have to do
2369 * spares after all
2370 */
2371 nospares = 1;
2372 if (force_change)
2373 nospares = 0;
2374 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002375 /* If the array is degraded, then skipping spares is both
2376 * dangerous and fairly pointless.
2377 * Dangerous because a device that was removed from the array
2378 * might have a event_count that still looks up-to-date,
2379 * so it can be re-added without a resync.
2380 * Pointless because if there are any spares to skip,
2381 * then a recovery will happen and soon that array won't
2382 * be degraded any more and the spare can go back to sleep then.
2383 */
NeilBrown850b2b42006-10-03 01:15:46 -07002384 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002385
NeilBrown06d91a52005-06-21 17:17:12 -07002386 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002387
2388 /* If this is just a dirty<->clean transition, and the array is clean
2389 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002390 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002391 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002392 && mddev->can_decrease_events
2393 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002394 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002395 mddev->can_decrease_events = 0;
2396 } else {
NeilBrown42543762006-06-26 00:27:57 -07002397 /* otherwise we have to go forward and ... */
2398 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002399 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
2402 if (!mddev->events) {
2403 /*
2404 * oops, this 64-bit counter should never wrap.
2405 * Either we are in around ~1 trillion A.C., assuming
2406 * 1 reboot per second, or we have a bug:
2407 */
2408 MD_BUG();
2409 mddev->events --;
2410 }
NeilBrown2699b672011-07-28 11:31:47 +10002411
NeilBrownde393cd2011-07-28 11:31:48 +10002412 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown2699b672011-07-28 11:31:47 +10002413 if (rdev->badblocks.changed)
2414 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002415 if (test_bit(Faulty, &rdev->flags))
2416 set_bit(FaultRecorded, &rdev->flags);
2417 }
NeilBrown2699b672011-07-28 11:31:47 +10002418
NeilBrowne6910632008-02-06 01:39:51 -08002419 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002420 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
2422 dprintk(KERN_INFO
2423 "md: updating %s RAID superblock on device (in sync %d)\n",
2424 mdname(mddev),mddev->in_sync);
2425
NeilBrown4ad13662007-07-17 04:06:13 -07002426 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002427 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 char b[BDEVNAME_SIZE];
2429 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002430 if (rdev->sb_loaded != 1)
2431 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002432 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 dprintk("(skipping faulty ");
2434
2435 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002436 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002437 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002438 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002439 rdev->sb_page);
2440 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2441 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002442 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002443 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002444 if (rdev->badblocks.size) {
2445 md_super_write(mddev, rdev,
2446 rdev->badblocks.sector,
2447 rdev->badblocks.size << 9,
2448 rdev->bb_page);
2449 rdev->badblocks.size = 0;
2450 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002451
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 } else
2453 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002454 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 /* only need to write one superblock... */
2456 break;
2457 }
NeilBrowna9701a32005-11-08 21:39:34 -08002458 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002459 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002460
NeilBrowna9701a32005-11-08 21:39:34 -08002461 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002462 if (mddev->in_sync != sync_req ||
2463 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002464 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002465 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002466 goto repeat;
2467 }
NeilBrown850b2b42006-10-03 01:15:46 -07002468 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002469 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002470 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002471 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2472 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002473
NeilBrownde393cd2011-07-28 11:31:48 +10002474 list_for_each_entry(rdev, &mddev->disks, same_set) {
2475 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2476 clear_bit(Blocked, &rdev->flags);
2477
2478 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002479 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002480 clear_bit(BlockedBadBlocks, &rdev->flags);
2481 wake_up(&rdev->blocked_wait);
2482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483}
2484
Andre Noll7f6ce762008-03-23 18:34:54 +01002485/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002486 * We want to accept with case. For this we use cmd_match.
2487 */
2488static int cmd_match(const char *cmd, const char *str)
2489{
2490 /* See if cmd, written into a sysfs file, matches
2491 * str. They must either be the same, or cmd can
2492 * have a trailing newline
2493 */
2494 while (*cmd && *str && *cmd == *str) {
2495 cmd++;
2496 str++;
2497 }
2498 if (*cmd == '\n')
2499 cmd++;
2500 if (*str || *cmd)
2501 return 0;
2502 return 1;
2503}
2504
NeilBrown86e6ffd2005-11-08 21:39:24 -08002505struct rdev_sysfs_entry {
2506 struct attribute attr;
2507 ssize_t (*show)(mdk_rdev_t *, char *);
2508 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2509};
2510
2511static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002512state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002513{
2514 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002515 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002516
NeilBrownde393cd2011-07-28 11:31:48 +10002517 if (test_bit(Faulty, &rdev->flags) ||
2518 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002519 len+= sprintf(page+len, "%sfaulty",sep);
2520 sep = ",";
2521 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002522 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002523 len += sprintf(page+len, "%sin_sync",sep);
2524 sep = ",";
2525 }
NeilBrownf6556752006-06-26 00:28:01 -07002526 if (test_bit(WriteMostly, &rdev->flags)) {
2527 len += sprintf(page+len, "%swrite_mostly",sep);
2528 sep = ",";
2529 }
NeilBrownde393cd2011-07-28 11:31:48 +10002530 if (test_bit(Blocked, &rdev->flags) ||
2531 rdev->badblocks.unacked_exist) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002532 len += sprintf(page+len, "%sblocked", sep);
2533 sep = ",";
2534 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002535 if (!test_bit(Faulty, &rdev->flags) &&
2536 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002537 len += sprintf(page+len, "%sspare", sep);
2538 sep = ",";
2539 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002540 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2541 len += sprintf(page+len, "%swrite_error", sep);
2542 sep = ",";
2543 }
NeilBrown86e6ffd2005-11-08 21:39:24 -08002544 return len+sprintf(page+len, "\n");
2545}
2546
NeilBrown45dc2de2006-06-26 00:27:58 -07002547static ssize_t
2548state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2549{
2550 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002551 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002552 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002553 * writemostly - sets write_mostly
2554 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002555 * blocked - sets the Blocked flags
2556 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002557 * insync - sets Insync providing device isn't active
NeilBrownd7a9d442011-07-28 11:31:48 +10002558 * write_error - sets WriteErrorSeen
2559 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002560 */
2561 int err = -EINVAL;
2562 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2563 md_error(rdev->mddev, rdev);
2564 err = 0;
2565 } else if (cmd_match(buf, "remove")) {
2566 if (rdev->raid_disk >= 0)
2567 err = -EBUSY;
2568 else {
2569 mddev_t *mddev = rdev->mddev;
2570 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002571 if (mddev->pers)
2572 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002573 md_new_event(mddev);
2574 err = 0;
2575 }
NeilBrownf6556752006-06-26 00:28:01 -07002576 } else if (cmd_match(buf, "writemostly")) {
2577 set_bit(WriteMostly, &rdev->flags);
2578 err = 0;
2579 } else if (cmd_match(buf, "-writemostly")) {
2580 clear_bit(WriteMostly, &rdev->flags);
2581 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002582 } else if (cmd_match(buf, "blocked")) {
2583 set_bit(Blocked, &rdev->flags);
2584 err = 0;
2585 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002586 if (!test_bit(Faulty, &rdev->flags) &&
2587 test_bit(BlockedBadBlocks, &rdev->flags)) {
2588 /* metadata handler doesn't understand badblocks,
2589 * so we need to fail the device
2590 */
2591 md_error(rdev->mddev, rdev);
2592 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002593 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002594 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002595 wake_up(&rdev->blocked_wait);
2596 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2597 md_wakeup_thread(rdev->mddev->thread);
2598
2599 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002600 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2601 set_bit(In_sync, &rdev->flags);
2602 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002603 } else if (cmd_match(buf, "write_error")) {
2604 set_bit(WriteErrorSeen, &rdev->flags);
2605 err = 0;
2606 } else if (cmd_match(buf, "-write_error")) {
2607 clear_bit(WriteErrorSeen, &rdev->flags);
2608 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002609 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002610 if (!err)
2611 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002612 return err ? err : len;
2613}
NeilBrown80ca3a42006-07-10 04:44:18 -07002614static struct rdev_sysfs_entry rdev_state =
2615__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002616
2617static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002618errors_show(mdk_rdev_t *rdev, char *page)
2619{
2620 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2621}
2622
2623static ssize_t
2624errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2625{
2626 char *e;
2627 unsigned long n = simple_strtoul(buf, &e, 10);
2628 if (*buf && (*e == 0 || *e == '\n')) {
2629 atomic_set(&rdev->corrected_errors, n);
2630 return len;
2631 }
2632 return -EINVAL;
2633}
2634static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002635__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002636
NeilBrown014236d2006-01-06 00:20:55 -08002637static ssize_t
2638slot_show(mdk_rdev_t *rdev, char *page)
2639{
2640 if (rdev->raid_disk < 0)
2641 return sprintf(page, "none\n");
2642 else
2643 return sprintf(page, "%d\n", rdev->raid_disk);
2644}
2645
2646static ssize_t
2647slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2648{
2649 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002650 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002651 int slot = simple_strtoul(buf, &e, 10);
2652 if (strncmp(buf, "none", 4)==0)
2653 slot = -1;
2654 else if (e==buf || (*e && *e!= '\n'))
2655 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002656 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002657 /* Setting 'slot' on an active array requires also
2658 * updating the 'rd%d' link, and communicating
2659 * with the personality with ->hot_*_disk.
2660 * For now we only support removing
2661 * failed/spare devices. This normally happens automatically,
2662 * but not when the metadata is externally managed.
2663 */
NeilBrownc303da62008-02-06 01:39:51 -08002664 if (rdev->raid_disk == -1)
2665 return -EEXIST;
2666 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002667 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002668 return -EINVAL;
2669 err = rdev->mddev->pers->
2670 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2671 if (err)
2672 return err;
Namhyung Kim36fad852011-07-27 11:00:36 +10002673 sysfs_unlink_rdev(rdev->mddev, rdev);
Maciej Trelab7103102010-04-14 16:58:16 +10002674 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002675 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2676 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002677 } else if (rdev->mddev->pers) {
2678 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002679 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002680 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002681 */
2682
2683 if (rdev->raid_disk != -1)
2684 return -EBUSY;
2685
NeilBrownc6751b22011-02-02 11:57:13 +11002686 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2687 return -EBUSY;
2688
Neil Brown6c2fce22008-06-28 08:31:31 +10002689 if (rdev->mddev->pers->hot_add_disk == NULL)
2690 return -EINVAL;
2691
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002692 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002693 if (rdev2->raid_disk == slot)
2694 return -EEXIST;
2695
NeilBrownba1b41b2011-01-14 09:14:34 +11002696 if (slot >= rdev->mddev->raid_disks &&
2697 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2698 return -ENOSPC;
2699
Neil Brown6c2fce22008-06-28 08:31:31 +10002700 rdev->raid_disk = slot;
2701 if (test_bit(In_sync, &rdev->flags))
2702 rdev->saved_raid_disk = slot;
2703 else
2704 rdev->saved_raid_disk = -1;
2705 err = rdev->mddev->pers->
2706 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002707 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002708 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002709 return err;
Neil Brown52664732008-06-28 08:31:44 +10002710 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002711 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002712 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002713 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002714 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002715 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002716 if (slot >= rdev->mddev->raid_disks &&
2717 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002718 return -ENOSPC;
2719 rdev->raid_disk = slot;
2720 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002721 clear_bit(Faulty, &rdev->flags);
2722 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002723 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002724 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002725 }
NeilBrown014236d2006-01-06 00:20:55 -08002726 return len;
2727}
2728
2729
2730static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002731__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002732
NeilBrown93c8cad2006-01-06 00:20:56 -08002733static ssize_t
2734offset_show(mdk_rdev_t *rdev, char *page)
2735{
Andrew Morton6961ece2006-01-06 00:20:59 -08002736 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002737}
2738
2739static ssize_t
2740offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2741{
2742 char *e;
2743 unsigned long long offset = simple_strtoull(buf, &e, 10);
2744 if (e==buf || (*e && *e != '\n'))
2745 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002746 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002747 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002748 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002749 /* Must set offset before size, so overlap checks
2750 * can be sane */
2751 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002752 rdev->data_offset = offset;
2753 return len;
2754}
2755
2756static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002757__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002758
NeilBrown83303b62006-01-06 00:21:06 -08002759static ssize_t
2760rdev_size_show(mdk_rdev_t *rdev, char *page)
2761{
Andre Nolldd8ac332009-03-31 14:33:13 +11002762 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002763}
2764
NeilBrownc5d79ad2008-02-06 01:39:54 -08002765static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2766{
2767 /* check if two start/length pairs overlap */
2768 if (s1+l1 <= s2)
2769 return 0;
2770 if (s2+l2 <= s1)
2771 return 0;
2772 return 1;
2773}
2774
Dan Williamsb522adc2009-03-31 15:00:31 +11002775static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2776{
2777 unsigned long long blocks;
2778 sector_t new;
2779
2780 if (strict_strtoull(buf, 10, &blocks) < 0)
2781 return -EINVAL;
2782
2783 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2784 return -EINVAL; /* sector conversion overflow */
2785
2786 new = blocks * 2;
2787 if (new != blocks * 2)
2788 return -EINVAL; /* unsigned long long to sector_t overflow */
2789
2790 *sectors = new;
2791 return 0;
2792}
2793
NeilBrown83303b62006-01-06 00:21:06 -08002794static ssize_t
2795rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2796{
NeilBrown27c529b2008-03-04 14:29:33 -08002797 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002798 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002799 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002800
Dan Williamsb522adc2009-03-31 15:00:31 +11002801 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002802 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002803 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002804 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002805 sectors = super_types[my_mddev->major_version].
2806 rdev_size_change(rdev, sectors);
2807 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002808 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002809 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002810 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002811 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002812 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002813 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002814 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002815
Andre Nolldd8ac332009-03-31 14:33:13 +11002816 rdev->sectors = sectors;
2817 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002818 /* need to check that all other rdevs with the same ->bdev
2819 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002820 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002821 * we have to change it back, we will have the lock again.
2822 */
2823 mddev_t *mddev;
2824 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002825 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002826
NeilBrown27c529b2008-03-04 14:29:33 -08002827 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002828 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002829 mdk_rdev_t *rdev2;
2830
2831 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002832 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002833 if (rdev->bdev == rdev2->bdev &&
2834 rdev != rdev2 &&
2835 overlaps(rdev->data_offset, rdev->sectors,
2836 rdev2->data_offset,
2837 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002838 overlap = 1;
2839 break;
2840 }
2841 mddev_unlock(mddev);
2842 if (overlap) {
2843 mddev_put(mddev);
2844 break;
2845 }
2846 }
NeilBrown27c529b2008-03-04 14:29:33 -08002847 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002848 if (overlap) {
2849 /* Someone else could have slipped in a size
2850 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002851 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002852 * safe, and trust userspace not to race with
2853 * itself
2854 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002855 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002856 return -EBUSY;
2857 }
2858 }
NeilBrown83303b62006-01-06 00:21:06 -08002859 return len;
2860}
2861
2862static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002863__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002864
Dan Williams06e3c812009-12-12 21:17:12 -07002865
2866static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2867{
2868 unsigned long long recovery_start = rdev->recovery_offset;
2869
2870 if (test_bit(In_sync, &rdev->flags) ||
2871 recovery_start == MaxSector)
2872 return sprintf(page, "none\n");
2873
2874 return sprintf(page, "%llu\n", recovery_start);
2875}
2876
2877static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2878{
2879 unsigned long long recovery_start;
2880
2881 if (cmd_match(buf, "none"))
2882 recovery_start = MaxSector;
2883 else if (strict_strtoull(buf, 10, &recovery_start))
2884 return -EINVAL;
2885
2886 if (rdev->mddev->pers &&
2887 rdev->raid_disk >= 0)
2888 return -EBUSY;
2889
2890 rdev->recovery_offset = recovery_start;
2891 if (recovery_start == MaxSector)
2892 set_bit(In_sync, &rdev->flags);
2893 else
2894 clear_bit(In_sync, &rdev->flags);
2895 return len;
2896}
2897
2898static struct rdev_sysfs_entry rdev_recovery_start =
2899__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2900
NeilBrown16c791a2011-07-28 11:31:47 +10002901
2902static ssize_t
2903badblocks_show(struct badblocks *bb, char *page, int unack);
2904static ssize_t
2905badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
2906
2907static ssize_t bb_show(mdk_rdev_t *rdev, char *page)
2908{
2909 return badblocks_show(&rdev->badblocks, page, 0);
2910}
2911static ssize_t bb_store(mdk_rdev_t *rdev, const char *page, size_t len)
2912{
NeilBrownde393cd2011-07-28 11:31:48 +10002913 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
2914 /* Maybe that ack was all we needed */
2915 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
2916 wake_up(&rdev->blocked_wait);
2917 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10002918}
2919static struct rdev_sysfs_entry rdev_bad_blocks =
2920__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
2921
2922
2923static ssize_t ubb_show(mdk_rdev_t *rdev, char *page)
2924{
2925 return badblocks_show(&rdev->badblocks, page, 1);
2926}
2927static ssize_t ubb_store(mdk_rdev_t *rdev, const char *page, size_t len)
2928{
2929 return badblocks_store(&rdev->badblocks, page, len, 1);
2930}
2931static struct rdev_sysfs_entry rdev_unack_bad_blocks =
2932__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
2933
NeilBrown86e6ffd2005-11-08 21:39:24 -08002934static struct attribute *rdev_default_attrs[] = {
2935 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002936 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002937 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002938 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002939 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002940 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10002941 &rdev_bad_blocks.attr,
2942 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002943 NULL,
2944};
2945static ssize_t
2946rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2947{
2948 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2949 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002950 mddev_t *mddev = rdev->mddev;
2951 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002952
2953 if (!entry->show)
2954 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002955
2956 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2957 if (!rv) {
2958 if (rdev->mddev == NULL)
2959 rv = -EBUSY;
2960 else
2961 rv = entry->show(rdev, page);
2962 mddev_unlock(mddev);
2963 }
2964 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002965}
2966
2967static ssize_t
2968rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2969 const char *page, size_t length)
2970{
2971 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2972 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002973 ssize_t rv;
2974 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002975
2976 if (!entry->store)
2977 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002978 if (!capable(CAP_SYS_ADMIN))
2979 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002980 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002981 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002982 if (rdev->mddev == NULL)
2983 rv = -EBUSY;
2984 else
2985 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002986 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002987 }
2988 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002989}
2990
2991static void rdev_free(struct kobject *ko)
2992{
2993 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2994 kfree(rdev);
2995}
Emese Revfy52cf25d2010-01-19 02:58:23 +01002996static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002997 .show = rdev_attr_show,
2998 .store = rdev_attr_store,
2999};
3000static struct kobj_type rdev_ktype = {
3001 .release = rdev_free,
3002 .sysfs_ops = &rdev_sysfs_ops,
3003 .default_attrs = rdev_default_attrs,
3004};
3005
NeilBrown2230dfe2011-07-28 11:31:46 +10003006int md_rdev_init(mdk_rdev_t *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003007{
3008 rdev->desc_nr = -1;
3009 rdev->saved_raid_disk = -1;
3010 rdev->raid_disk = -1;
3011 rdev->flags = 0;
3012 rdev->data_offset = 0;
3013 rdev->sb_events = 0;
3014 rdev->last_read_error.tv_sec = 0;
3015 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003016 rdev->sb_loaded = 0;
3017 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003018 atomic_set(&rdev->nr_pending, 0);
3019 atomic_set(&rdev->read_errors, 0);
3020 atomic_set(&rdev->corrected_errors, 0);
3021
3022 INIT_LIST_HEAD(&rdev->same_set);
3023 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003024
3025 /* Add space to store bad block list.
3026 * This reserves the space even on arrays where it cannot
3027 * be used - I wonder if that matters
3028 */
3029 rdev->badblocks.count = 0;
3030 rdev->badblocks.shift = 0;
3031 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3032 seqlock_init(&rdev->badblocks.lock);
3033 if (rdev->badblocks.page == NULL)
3034 return -ENOMEM;
3035
3036 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003037}
3038EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039/*
3040 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3041 *
3042 * mark the device faulty if:
3043 *
3044 * - the device is nonexistent (zero size)
3045 * - the device has no valid superblock
3046 *
3047 * a faulty rdev _never_ has rdev->sb set.
3048 */
3049static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
3050{
3051 char b[BDEVNAME_SIZE];
3052 int err;
3053 mdk_rdev_t *rdev;
3054 sector_t size;
3055
NeilBrown9ffae0c2006-01-06 00:20:32 -08003056 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 if (!rdev) {
3058 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3059 return ERR_PTR(-ENOMEM);
3060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
NeilBrown2230dfe2011-07-28 11:31:46 +10003062 err = md_rdev_init(rdev);
3063 if (err)
3064 goto abort_free;
3065 err = alloc_disk_sb(rdev);
3066 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 goto abort_free;
3068
NeilBrownc5d79ad2008-02-06 01:39:54 -08003069 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 if (err)
3071 goto abort_free;
3072
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003073 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003074
Mike Snitzer77304d22010-11-08 14:39:12 +01003075 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 if (!size) {
3077 printk(KERN_WARNING
3078 "md: %s has zero or unknown size, marking faulty!\n",
3079 bdevname(rdev->bdev,b));
3080 err = -EINVAL;
3081 goto abort_free;
3082 }
3083
3084 if (super_format >= 0) {
3085 err = super_types[super_format].
3086 load_super(rdev, NULL, super_minor);
3087 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003088 printk(KERN_WARNING
3089 "md: %s does not have a valid v%d.%d "
3090 "superblock, not importing!\n",
3091 bdevname(rdev->bdev,b),
3092 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 goto abort_free;
3094 }
3095 if (err < 0) {
3096 printk(KERN_WARNING
3097 "md: could not read %s's sb, not importing!\n",
3098 bdevname(rdev->bdev,b));
3099 goto abort_free;
3100 }
3101 }
NeilBrown9f2f3832011-07-28 11:31:47 +10003102 if (super_format == -1)
3103 /* hot-add for 0.90, or non-persistent: so no badblocks */
3104 rdev->badblocks.shift = -1;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003105
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 return rdev;
3107
3108abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003109 if (rdev->bdev)
3110 unlock_rdev(rdev);
3111 free_disk_sb(rdev);
NeilBrown2230dfe2011-07-28 11:31:46 +10003112 kfree(rdev->badblocks.page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 kfree(rdev);
3114 return ERR_PTR(err);
3115}
3116
3117/*
3118 * Check a full RAID array for plausibility
3119 */
3120
3121
NeilBrowna757e642005-04-16 15:26:42 -07003122static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123{
3124 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003125 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 char b[BDEVNAME_SIZE];
3127
3128 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08003129 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 switch (super_types[mddev->major_version].
3131 load_super(rdev, freshest, mddev->minor_version)) {
3132 case 1:
3133 freshest = rdev;
3134 break;
3135 case 0:
3136 break;
3137 default:
3138 printk( KERN_ERR \
3139 "md: fatal superblock inconsistency in %s"
3140 " -- removing from array\n",
3141 bdevname(rdev->bdev,b));
3142 kick_rdev_from_array(rdev);
3143 }
3144
3145
3146 super_types[mddev->major_version].
3147 validate_super(mddev, freshest);
3148
3149 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003150 rdev_for_each(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003151 if (mddev->max_disks &&
3152 (rdev->desc_nr >= mddev->max_disks ||
3153 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003154 printk(KERN_WARNING
3155 "md: %s: %s: only %d devices permitted\n",
3156 mdname(mddev), bdevname(rdev->bdev, b),
3157 mddev->max_disks);
3158 kick_rdev_from_array(rdev);
3159 continue;
3160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 if (rdev != freshest)
3162 if (super_types[mddev->major_version].
3163 validate_super(mddev, rdev)) {
3164 printk(KERN_WARNING "md: kicking non-fresh %s"
3165 " from array!\n",
3166 bdevname(rdev->bdev,b));
3167 kick_rdev_from_array(rdev);
3168 continue;
3169 }
3170 if (mddev->level == LEVEL_MULTIPATH) {
3171 rdev->desc_nr = i++;
3172 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003173 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003174 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003175 rdev->raid_disk = -1;
3176 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 }
3178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179}
3180
NeilBrown72e02072009-12-14 12:49:55 +11003181/* Read a fixed-point number.
3182 * Numbers in sysfs attributes should be in "standard" units where
3183 * possible, so time should be in seconds.
3184 * However we internally use a a much smaller unit such as
3185 * milliseconds or jiffies.
3186 * This function takes a decimal number with a possible fractional
3187 * component, and produces an integer which is the result of
3188 * multiplying that number by 10^'scale'.
3189 * all without any floating-point arithmetic.
3190 */
3191int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3192{
3193 unsigned long result = 0;
3194 long decimals = -1;
3195 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3196 if (*cp == '.')
3197 decimals = 0;
3198 else if (decimals < scale) {
3199 unsigned int value;
3200 value = *cp - '0';
3201 result = result * 10 + value;
3202 if (decimals >= 0)
3203 decimals++;
3204 }
3205 cp++;
3206 }
3207 if (*cp == '\n')
3208 cp++;
3209 if (*cp)
3210 return -EINVAL;
3211 if (decimals < 0)
3212 decimals = 0;
3213 while (decimals < scale) {
3214 result *= 10;
3215 decimals ++;
3216 }
3217 *res = result;
3218 return 0;
3219}
3220
3221
NeilBrown19052c02008-08-05 15:54:13 +10003222static void md_safemode_timeout(unsigned long data);
3223
NeilBrowneae17012005-11-08 21:39:23 -08003224static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07003225safe_delay_show(mddev_t *mddev, char *page)
3226{
3227 int msec = (mddev->safemode_delay*1000)/HZ;
3228 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3229}
3230static ssize_t
3231safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
3232{
NeilBrown16f17b32006-06-26 00:27:37 -07003233 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003234
NeilBrown72e02072009-12-14 12:49:55 +11003235 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003236 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003237 if (msec == 0)
3238 mddev->safemode_delay = 0;
3239 else {
NeilBrown19052c02008-08-05 15:54:13 +10003240 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003241 mddev->safemode_delay = (msec*HZ)/1000;
3242 if (mddev->safemode_delay == 0)
3243 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10003244 if (mddev->safemode_delay < old_delay)
3245 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003246 }
3247 return len;
3248}
3249static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003250__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003251
3252static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003253level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003254{
NeilBrown2604b702006-01-06 00:20:36 -08003255 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003256 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003257 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003258 else if (mddev->clevel[0])
3259 return sprintf(page, "%s\n", mddev->clevel);
3260 else if (mddev->level != LEVEL_NONE)
3261 return sprintf(page, "%d\n", mddev->level);
3262 else
3263 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003264}
3265
NeilBrownd9d166c2006-01-06 00:20:51 -08003266static ssize_t
3267level_store(mddev_t *mddev, const char *buf, size_t len)
3268{
Dan Williamsf2859af2010-05-02 10:04:16 -07003269 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003270 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11003271 struct mdk_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003272 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003273 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10003274 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003275
3276 if (mddev->pers == NULL) {
3277 if (len == 0)
3278 return 0;
3279 if (len >= sizeof(mddev->clevel))
3280 return -ENOSPC;
3281 strncpy(mddev->clevel, buf, len);
3282 if (mddev->clevel[len-1] == '\n')
3283 len--;
3284 mddev->clevel[len] = 0;
3285 mddev->level = LEVEL_NONE;
3286 return rv;
3287 }
3288
3289 /* request to change the personality. Need to ensure:
3290 * - array is not engaged in resync/recovery/reshape
3291 * - old personality can be suspended
3292 * - new personality will access other array.
3293 */
3294
NeilBrownbb4f1e92010-08-08 21:18:03 +10003295 if (mddev->sync_thread ||
3296 mddev->reshape_position != MaxSector ||
3297 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003298 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003299
3300 if (!mddev->pers->quiesce) {
3301 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3302 mdname(mddev), mddev->pers->name);
3303 return -EINVAL;
3304 }
3305
3306 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003307 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003308 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003309 strncpy(clevel, buf, len);
3310 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003311 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003312 clevel[len] = 0;
3313 if (strict_strtol(clevel, 10, &level))
3314 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003315
Dan Williamsf2859af2010-05-02 10:04:16 -07003316 if (request_module("md-%s", clevel) != 0)
3317 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003318 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003319 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003320 if (!pers || !try_module_get(pers->owner)) {
3321 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003322 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003323 return -EINVAL;
3324 }
3325 spin_unlock(&pers_lock);
3326
3327 if (pers == mddev->pers) {
3328 /* Nothing to do! */
3329 module_put(pers->owner);
3330 return rv;
3331 }
3332 if (!pers->takeover) {
3333 module_put(pers->owner);
3334 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003335 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003336 return -EINVAL;
3337 }
3338
NeilBrowne93f68a2010-06-15 09:36:03 +01003339 list_for_each_entry(rdev, &mddev->disks, same_set)
3340 rdev->new_raid_disk = rdev->raid_disk;
3341
NeilBrown245f46c2009-03-31 14:39:39 +11003342 /* ->takeover must set new_* and/or delta_disks
3343 * if it succeeds, and may set them when it fails.
3344 */
3345 priv = pers->takeover(mddev);
3346 if (IS_ERR(priv)) {
3347 mddev->new_level = mddev->level;
3348 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003349 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003350 mddev->raid_disks -= mddev->delta_disks;
3351 mddev->delta_disks = 0;
3352 module_put(pers->owner);
3353 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003354 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003355 return PTR_ERR(priv);
3356 }
3357
3358 /* Looks like we have a winner */
3359 mddev_suspend(mddev);
3360 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003361
3362 if (mddev->pers->sync_request == NULL &&
3363 pers->sync_request != NULL) {
3364 /* need to add the md_redundancy_group */
3365 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3366 printk(KERN_WARNING
3367 "md: cannot register extra attributes for %s\n",
3368 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003369 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003370 }
3371 if (mddev->pers->sync_request != NULL &&
3372 pers->sync_request == NULL) {
3373 /* need to remove the md_redundancy_group */
3374 if (mddev->to_remove == NULL)
3375 mddev->to_remove = &md_redundancy_group;
3376 }
3377
Trela Maciej54071b32010-03-08 16:02:42 +11003378 if (mddev->pers->sync_request == NULL &&
3379 mddev->external) {
3380 /* We are converting from a no-redundancy array
3381 * to a redundancy array and metadata is managed
3382 * externally so we need to be sure that writes
3383 * won't block due to a need to transition
3384 * clean->dirty
3385 * until external management is started.
3386 */
3387 mddev->in_sync = 0;
3388 mddev->safemode_delay = 0;
3389 mddev->safemode = 0;
3390 }
3391
NeilBrowne93f68a2010-06-15 09:36:03 +01003392 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003393 if (rdev->raid_disk < 0)
3394 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003395 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003396 rdev->new_raid_disk = -1;
3397 if (rdev->new_raid_disk == rdev->raid_disk)
3398 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003399 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003400 }
3401 list_for_each_entry(rdev, &mddev->disks, same_set) {
3402 if (rdev->raid_disk < 0)
3403 continue;
3404 if (rdev->new_raid_disk == rdev->raid_disk)
3405 continue;
3406 rdev->raid_disk = rdev->new_raid_disk;
3407 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003408 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003409 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003410 if (sysfs_link_rdev(mddev, rdev))
3411 printk(KERN_WARNING "md: cannot register rd%d"
3412 " for %s after level change\n",
3413 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003414 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003415 }
3416
3417 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003418 mddev->pers = pers;
3419 mddev->private = priv;
3420 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3421 mddev->level = mddev->new_level;
3422 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003423 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003424 mddev->delta_disks = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003425 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003426 if (mddev->pers->sync_request == NULL) {
3427 /* this is now an array without redundancy, so
3428 * it must always be in_sync
3429 */
3430 mddev->in_sync = 1;
3431 del_timer_sync(&mddev->safemode_timer);
3432 }
NeilBrown245f46c2009-03-31 14:39:39 +11003433 pers->run(mddev);
3434 mddev_resume(mddev);
3435 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3436 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3437 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003438 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003439 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003440 return rv;
3441}
3442
3443static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003444__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003445
NeilBrownd4dbd022006-06-26 00:27:59 -07003446
3447static ssize_t
3448layout_show(mddev_t *mddev, char *page)
3449{
3450 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003451 if (mddev->reshape_position != MaxSector &&
3452 mddev->layout != mddev->new_layout)
3453 return sprintf(page, "%d (%d)\n",
3454 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003455 return sprintf(page, "%d\n", mddev->layout);
3456}
3457
3458static ssize_t
3459layout_store(mddev_t *mddev, const char *buf, size_t len)
3460{
3461 char *e;
3462 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003463
3464 if (!*buf || (*e && *e != '\n'))
3465 return -EINVAL;
3466
NeilBrownb3546032009-03-31 14:56:41 +11003467 if (mddev->pers) {
3468 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003469 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003470 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003471 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003472 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003473 if (err) {
3474 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003475 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003476 }
NeilBrownb3546032009-03-31 14:56:41 +11003477 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003478 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003479 if (mddev->reshape_position == MaxSector)
3480 mddev->layout = n;
3481 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003482 return len;
3483}
3484static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003485__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003486
3487
NeilBrowneae17012005-11-08 21:39:23 -08003488static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003489raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003490{
NeilBrownbb636542005-11-08 21:39:45 -08003491 if (mddev->raid_disks == 0)
3492 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003493 if (mddev->reshape_position != MaxSector &&
3494 mddev->delta_disks != 0)
3495 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3496 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003497 return sprintf(page, "%d\n", mddev->raid_disks);
3498}
3499
NeilBrownda943b992006-01-06 00:20:54 -08003500static int update_raid_disks(mddev_t *mddev, int raid_disks);
3501
3502static ssize_t
3503raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3504{
NeilBrownda943b992006-01-06 00:20:54 -08003505 char *e;
3506 int rv = 0;
3507 unsigned long n = simple_strtoul(buf, &e, 10);
3508
3509 if (!*buf || (*e && *e != '\n'))
3510 return -EINVAL;
3511
3512 if (mddev->pers)
3513 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003514 else if (mddev->reshape_position != MaxSector) {
3515 int olddisks = mddev->raid_disks - mddev->delta_disks;
3516 mddev->delta_disks = n - olddisks;
3517 mddev->raid_disks = n;
3518 } else
NeilBrownda943b992006-01-06 00:20:54 -08003519 mddev->raid_disks = n;
3520 return rv ? rv : len;
3521}
3522static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003523__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003524
NeilBrown24dd4692005-11-08 21:39:26 -08003525static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003526chunk_size_show(mddev_t *mddev, char *page)
3527{
NeilBrown08a02ec2007-05-09 02:35:38 -07003528 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003529 mddev->chunk_sectors != mddev->new_chunk_sectors)
3530 return sprintf(page, "%d (%d)\n",
3531 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003532 mddev->chunk_sectors << 9);
3533 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003534}
3535
3536static ssize_t
3537chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3538{
NeilBrown3b343802006-01-06 00:20:47 -08003539 char *e;
3540 unsigned long n = simple_strtoul(buf, &e, 10);
3541
NeilBrown3b343802006-01-06 00:20:47 -08003542 if (!*buf || (*e && *e != '\n'))
3543 return -EINVAL;
3544
NeilBrownb3546032009-03-31 14:56:41 +11003545 if (mddev->pers) {
3546 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003547 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003548 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003549 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003550 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003551 if (err) {
3552 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003553 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003554 }
NeilBrownb3546032009-03-31 14:56:41 +11003555 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003556 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003557 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003558 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003559 }
NeilBrown3b343802006-01-06 00:20:47 -08003560 return len;
3561}
3562static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003563__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003564
NeilBrowna94213b2006-06-26 00:28:00 -07003565static ssize_t
3566resync_start_show(mddev_t *mddev, char *page)
3567{
NeilBrownd1a7c502009-03-31 15:24:32 +11003568 if (mddev->recovery_cp == MaxSector)
3569 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003570 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3571}
3572
3573static ssize_t
3574resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3575{
NeilBrowna94213b2006-06-26 00:28:00 -07003576 char *e;
3577 unsigned long long n = simple_strtoull(buf, &e, 10);
3578
NeilBrownb0986362011-05-11 15:52:21 +10003579 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003580 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003581 if (cmd_match(buf, "none"))
3582 n = MaxSector;
3583 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003584 return -EINVAL;
3585
3586 mddev->recovery_cp = n;
3587 return len;
3588}
3589static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003590__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003591
NeilBrown9e653b62006-06-26 00:27:58 -07003592/*
3593 * The array state can be:
3594 *
3595 * clear
3596 * No devices, no size, no level
3597 * Equivalent to STOP_ARRAY ioctl
3598 * inactive
3599 * May have some settings, but array is not active
3600 * all IO results in error
3601 * When written, doesn't tear down array, but just stops it
3602 * suspended (not supported yet)
3603 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003604 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003605 * readonly
3606 * no resync can happen. no superblocks get written.
3607 * write requests fail
3608 * read-auto
3609 * like readonly, but behaves like 'clean' on a write request.
3610 *
3611 * clean - no pending writes, but otherwise active.
3612 * When written to inactive array, starts without resync
3613 * If a write request arrives then
3614 * if metadata is known, mark 'dirty' and switch to 'active'.
3615 * if not known, block and switch to write-pending
3616 * If written to an active array that has pending writes, then fails.
3617 * active
3618 * fully active: IO and resync can be happening.
3619 * When written to inactive array, starts with resync
3620 *
3621 * write-pending
3622 * clean, but writes are blocked waiting for 'active' to be written.
3623 *
3624 * active-idle
3625 * like active, but no writes have been seen for a while (100msec).
3626 *
3627 */
3628enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3629 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003630static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003631 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3632 "write-pending", "active-idle", NULL };
3633
3634static int match_word(const char *word, char **list)
3635{
3636 int n;
3637 for (n=0; list[n]; n++)
3638 if (cmd_match(word, list[n]))
3639 break;
3640 return n;
3641}
3642
3643static ssize_t
3644array_state_show(mddev_t *mddev, char *page)
3645{
3646 enum array_state st = inactive;
3647
3648 if (mddev->pers)
3649 switch(mddev->ro) {
3650 case 1:
3651 st = readonly;
3652 break;
3653 case 2:
3654 st = read_auto;
3655 break;
3656 case 0:
3657 if (mddev->in_sync)
3658 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003659 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003660 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003661 else if (mddev->safemode)
3662 st = active_idle;
3663 else
3664 st = active;
3665 }
3666 else {
3667 if (list_empty(&mddev->disks) &&
3668 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003669 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003670 st = clear;
3671 else
3672 st = inactive;
3673 }
3674 return sprintf(page, "%s\n", array_states[st]);
3675}
3676
Neil Browndf5b20c2008-07-11 22:02:22 +10003677static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrowna4bd82d02010-03-29 13:23:10 +11003678static int md_set_readonly(mddev_t * mddev, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003679static int do_md_run(mddev_t * mddev);
3680static int restart_array(mddev_t *mddev);
3681
3682static ssize_t
3683array_state_store(mddev_t *mddev, const char *buf, size_t len)
3684{
3685 int err = -EINVAL;
3686 enum array_state st = match_word(buf, array_states);
3687 switch(st) {
3688 case bad_word:
3689 break;
3690 case clear:
3691 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003692 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003693 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003694 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003695 break;
3696 case inactive:
3697 /* stopping an active array */
3698 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003699 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003700 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003701 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003702 } else
3703 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003704 break;
3705 case suspended:
3706 break; /* not supported yet */
3707 case readonly:
3708 if (mddev->pers)
NeilBrowna4bd82d02010-03-29 13:23:10 +11003709 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003710 else {
3711 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003712 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003713 err = do_md_run(mddev);
3714 }
3715 break;
3716 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003717 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003718 if (mddev->ro == 0)
NeilBrowna4bd82d02010-03-29 13:23:10 +11003719 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003720 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003721 err = restart_array(mddev);
3722 if (err == 0) {
3723 mddev->ro = 2;
3724 set_disk_ro(mddev->gendisk, 0);
3725 }
NeilBrown9e653b62006-06-26 00:27:58 -07003726 } else {
3727 mddev->ro = 2;
3728 err = do_md_run(mddev);
3729 }
3730 break;
3731 case clean:
3732 if (mddev->pers) {
3733 restart_array(mddev);
3734 spin_lock_irq(&mddev->write_lock);
3735 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003736 if (mddev->in_sync == 0) {
3737 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003738 if (mddev->safemode == 1)
3739 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003740 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003741 }
3742 err = 0;
3743 } else
3744 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003745 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003746 } else
3747 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003748 break;
3749 case active:
3750 if (mddev->pers) {
3751 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003752 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003753 wake_up(&mddev->sb_wait);
3754 err = 0;
3755 } else {
3756 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003757 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003758 err = do_md_run(mddev);
3759 }
3760 break;
3761 case write_pending:
3762 case active_idle:
3763 /* these cannot be set */
3764 break;
3765 }
3766 if (err)
3767 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003768 else {
NeilBrown00bcb4a2010-06-01 19:37:23 +10003769 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003770 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003771 }
NeilBrown9e653b62006-06-26 00:27:58 -07003772}
NeilBrown80ca3a42006-07-10 04:44:18 -07003773static struct md_sysfs_entry md_array_state =
3774__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003775
NeilBrown6d7ff7382006-01-06 00:21:16 -08003776static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003777max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3778 return sprintf(page, "%d\n",
3779 atomic_read(&mddev->max_corr_read_errors));
3780}
3781
3782static ssize_t
3783max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3784{
3785 char *e;
3786 unsigned long n = simple_strtoul(buf, &e, 10);
3787
3788 if (*buf && (*e == 0 || *e == '\n')) {
3789 atomic_set(&mddev->max_corr_read_errors, n);
3790 return len;
3791 }
3792 return -EINVAL;
3793}
3794
3795static struct md_sysfs_entry max_corr_read_errors =
3796__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3797 max_corrected_read_errors_store);
3798
3799static ssize_t
NeilBrown6d7ff7382006-01-06 00:21:16 -08003800null_show(mddev_t *mddev, char *page)
3801{
3802 return -EINVAL;
3803}
3804
3805static ssize_t
3806new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3807{
3808 /* buf must be %d:%d\n? giving major and minor numbers */
3809 /* The new device is added to the array.
3810 * If the array has a persistent superblock, we read the
3811 * superblock to initialise info and check validity.
3812 * Otherwise, only checking done is that in bind_rdev_to_array,
3813 * which mainly checks size.
3814 */
3815 char *e;
3816 int major = simple_strtoul(buf, &e, 10);
3817 int minor;
3818 dev_t dev;
3819 mdk_rdev_t *rdev;
3820 int err;
3821
3822 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3823 return -EINVAL;
3824 minor = simple_strtoul(e+1, &e, 10);
3825 if (*e && *e != '\n')
3826 return -EINVAL;
3827 dev = MKDEV(major, minor);
3828 if (major != MAJOR(dev) ||
3829 minor != MINOR(dev))
3830 return -EOVERFLOW;
3831
3832
3833 if (mddev->persistent) {
3834 rdev = md_import_device(dev, mddev->major_version,
3835 mddev->minor_version);
3836 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3837 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3838 mdk_rdev_t, same_set);
3839 err = super_types[mddev->major_version]
3840 .load_super(rdev, rdev0, mddev->minor_version);
3841 if (err < 0)
3842 goto out;
3843 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003844 } else if (mddev->external)
3845 rdev = md_import_device(dev, -2, -1);
3846 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003847 rdev = md_import_device(dev, -1, -1);
3848
3849 if (IS_ERR(rdev))
3850 return PTR_ERR(rdev);
3851 err = bind_rdev_to_array(rdev, mddev);
3852 out:
3853 if (err)
3854 export_rdev(rdev);
3855 return err ? err : len;
3856}
3857
3858static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003859__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003860
3861static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003862bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3863{
3864 char *end;
3865 unsigned long chunk, end_chunk;
3866
3867 if (!mddev->bitmap)
3868 goto out;
3869 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3870 while (*buf) {
3871 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3872 if (buf == end) break;
3873 if (*end == '-') { /* range */
3874 buf = end + 1;
3875 end_chunk = simple_strtoul(buf, &end, 0);
3876 if (buf == end) break;
3877 }
3878 if (*end && !isspace(*end)) break;
3879 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003880 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003881 }
3882 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3883out:
3884 return len;
3885}
3886
3887static struct md_sysfs_entry md_bitmap =
3888__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3889
3890static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003891size_show(mddev_t *mddev, char *page)
3892{
Andre Noll58c0fed2009-03-31 14:33:13 +11003893 return sprintf(page, "%llu\n",
3894 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003895}
3896
Andre Nolld71f9f82008-07-11 22:02:22 +10003897static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003898
3899static ssize_t
3900size_store(mddev_t *mddev, const char *buf, size_t len)
3901{
3902 /* If array is inactive, we can reduce the component size, but
3903 * not increase it (except from 0).
3904 * If array is active, we can try an on-line resize
3905 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003906 sector_t sectors;
3907 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003908
Andre Noll58c0fed2009-03-31 14:33:13 +11003909 if (err < 0)
3910 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003911 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003912 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003913 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003914 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003915 if (mddev->dev_sectors == 0 ||
3916 mddev->dev_sectors > sectors)
3917 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003918 else
3919 err = -ENOSPC;
3920 }
3921 return err ? err : len;
3922}
3923
3924static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003925__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003926
NeilBrown8bb93aa2006-01-06 00:20:50 -08003927
3928/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003929 * This is one of
3930 * 'none' for arrays with no metadata (good luck...)
3931 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003932 * or N.M for internally known formats
3933 */
3934static ssize_t
3935metadata_show(mddev_t *mddev, char *page)
3936{
3937 if (mddev->persistent)
3938 return sprintf(page, "%d.%d\n",
3939 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003940 else if (mddev->external)
3941 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003942 else
3943 return sprintf(page, "none\n");
3944}
3945
3946static ssize_t
3947metadata_store(mddev_t *mddev, const char *buf, size_t len)
3948{
3949 int major, minor;
3950 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003951 /* Changing the details of 'external' metadata is
3952 * always permitted. Otherwise there must be
3953 * no devices attached to the array.
3954 */
3955 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3956 ;
3957 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003958 return -EBUSY;
3959
3960 if (cmd_match(buf, "none")) {
3961 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003962 mddev->external = 0;
3963 mddev->major_version = 0;
3964 mddev->minor_version = 90;
3965 return len;
3966 }
3967 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003968 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003969 if (namelen >= sizeof(mddev->metadata_type))
3970 namelen = sizeof(mddev->metadata_type)-1;
3971 strncpy(mddev->metadata_type, buf+9, namelen);
3972 mddev->metadata_type[namelen] = 0;
3973 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3974 mddev->metadata_type[--namelen] = 0;
3975 mddev->persistent = 0;
3976 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003977 mddev->major_version = 0;
3978 mddev->minor_version = 90;
3979 return len;
3980 }
3981 major = simple_strtoul(buf, &e, 10);
3982 if (e==buf || *e != '.')
3983 return -EINVAL;
3984 buf = e+1;
3985 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003986 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003987 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003988 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003989 return -ENOENT;
3990 mddev->major_version = major;
3991 mddev->minor_version = minor;
3992 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003993 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003994 return len;
3995}
3996
3997static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003998__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003999
NeilBrowna35b0d62006-01-06 00:20:49 -08004000static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08004001action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004002{
NeilBrown7eec3142005-11-08 21:39:44 -08004003 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004004 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4005 type = "frozen";
4006 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004007 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004008 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4009 type = "reshape";
4010 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004011 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4012 type = "resync";
4013 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4014 type = "check";
4015 else
4016 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004017 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004018 type = "recover";
4019 }
4020 return sprintf(page, "%s\n", type);
4021}
4022
NeilBrown7ebc0be2011-01-14 09:14:33 +11004023static void reap_sync_thread(mddev_t *mddev);
4024
NeilBrown24dd4692005-11-08 21:39:26 -08004025static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08004026action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004027{
NeilBrown7eec3142005-11-08 21:39:44 -08004028 if (!mddev->pers || !mddev->pers->sync_request)
4029 return -EINVAL;
4030
NeilBrownb6a9ce62009-05-26 09:41:17 +10004031 if (cmd_match(page, "frozen"))
4032 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4033 else
4034 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4035
4036 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004037 if (mddev->sync_thread) {
4038 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004039 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004040 }
NeilBrown03c902e2006-01-06 00:20:46 -08004041 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4042 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004043 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004044 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004045 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004046 else if (cmd_match(page, "recover")) {
4047 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4048 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4049 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004050 int err;
4051 if (mddev->pers->start_reshape == NULL)
4052 return -EINVAL;
4053 err = mddev->pers->start_reshape(mddev);
4054 if (err)
4055 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004056 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004057 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004058 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004059 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004060 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004061 return -EINVAL;
4062 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4063 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004064 }
NeilBrown03c902e2006-01-06 00:20:46 -08004065 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004066 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004067 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004068 return len;
4069}
4070
NeilBrown9d888832005-11-08 21:39:26 -08004071static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08004072mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004073{
4074 return sprintf(page, "%llu\n",
4075 (unsigned long long) mddev->resync_mismatches);
4076}
4077
NeilBrown80ca3a42006-07-10 04:44:18 -07004078static struct md_sysfs_entry md_scan_mode =
4079__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08004080
NeilBrown96de1e62005-11-08 21:39:39 -08004081
NeilBrown80ca3a42006-07-10 04:44:18 -07004082static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004083
NeilBrown88202a02006-01-06 00:21:36 -08004084static ssize_t
4085sync_min_show(mddev_t *mddev, char *page)
4086{
4087 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4088 mddev->sync_speed_min ? "local": "system");
4089}
4090
4091static ssize_t
4092sync_min_store(mddev_t *mddev, const char *buf, size_t len)
4093{
4094 int min;
4095 char *e;
4096 if (strncmp(buf, "system", 6)==0) {
4097 mddev->sync_speed_min = 0;
4098 return len;
4099 }
4100 min = simple_strtoul(buf, &e, 10);
4101 if (buf == e || (*e && *e != '\n') || min <= 0)
4102 return -EINVAL;
4103 mddev->sync_speed_min = min;
4104 return len;
4105}
4106
4107static struct md_sysfs_entry md_sync_min =
4108__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4109
4110static ssize_t
4111sync_max_show(mddev_t *mddev, char *page)
4112{
4113 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4114 mddev->sync_speed_max ? "local": "system");
4115}
4116
4117static ssize_t
4118sync_max_store(mddev_t *mddev, const char *buf, size_t len)
4119{
4120 int max;
4121 char *e;
4122 if (strncmp(buf, "system", 6)==0) {
4123 mddev->sync_speed_max = 0;
4124 return len;
4125 }
4126 max = simple_strtoul(buf, &e, 10);
4127 if (buf == e || (*e && *e != '\n') || max <= 0)
4128 return -EINVAL;
4129 mddev->sync_speed_max = max;
4130 return len;
4131}
4132
4133static struct md_sysfs_entry md_sync_max =
4134__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4135
Iustin Popd7f3d292007-10-16 23:30:54 -07004136static ssize_t
4137degraded_show(mddev_t *mddev, char *page)
4138{
4139 return sprintf(page, "%d\n", mddev->degraded);
4140}
4141static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004142
4143static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07004144sync_force_parallel_show(mddev_t *mddev, char *page)
4145{
4146 return sprintf(page, "%d\n", mddev->parallel_resync);
4147}
4148
4149static ssize_t
4150sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
4151{
4152 long n;
4153
4154 if (strict_strtol(buf, 10, &n))
4155 return -EINVAL;
4156
4157 if (n != 0 && n != 1)
4158 return -EINVAL;
4159
4160 mddev->parallel_resync = n;
4161
4162 if (mddev->sync_thread)
4163 wake_up(&resync_wait);
4164
4165 return len;
4166}
4167
4168/* force parallel resync, even with shared block devices */
4169static struct md_sysfs_entry md_sync_force_parallel =
4170__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4171 sync_force_parallel_show, sync_force_parallel_store);
4172
4173static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08004174sync_speed_show(mddev_t *mddev, char *page)
4175{
4176 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004177 if (mddev->curr_resync == 0)
4178 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004179 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4180 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004181 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004182 db = resync - mddev->resync_mark_cnt;
4183 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004184}
4185
NeilBrown80ca3a42006-07-10 04:44:18 -07004186static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004187
4188static ssize_t
4189sync_completed_show(mddev_t *mddev, char *page)
4190{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004191 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004192
NeilBrownacb180b2009-04-14 16:28:34 +10004193 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4194 return sprintf(page, "none\n");
4195
NeilBrown88202a02006-01-06 00:21:36 -08004196 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004197 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004198 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004199 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004200
NeilBrownacb180b2009-04-14 16:28:34 +10004201 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004202 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004203}
4204
NeilBrown80ca3a42006-07-10 04:44:18 -07004205static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004206
NeilBrowne464eaf2006-03-27 01:18:14 -08004207static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10004208min_sync_show(mddev_t *mddev, char *page)
4209{
4210 return sprintf(page, "%llu\n",
4211 (unsigned long long)mddev->resync_min);
4212}
4213static ssize_t
4214min_sync_store(mddev_t *mddev, const char *buf, size_t len)
4215{
4216 unsigned long long min;
4217 if (strict_strtoull(buf, 10, &min))
4218 return -EINVAL;
4219 if (min > mddev->resync_max)
4220 return -EINVAL;
4221 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4222 return -EBUSY;
4223
4224 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004225 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004226 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004227 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004228 return -EINVAL;
4229 }
4230 mddev->resync_min = min;
4231
4232 return len;
4233}
4234
4235static struct md_sysfs_entry md_min_sync =
4236__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4237
4238static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08004239max_sync_show(mddev_t *mddev, char *page)
4240{
4241 if (mddev->resync_max == MaxSector)
4242 return sprintf(page, "max\n");
4243 else
4244 return sprintf(page, "%llu\n",
4245 (unsigned long long)mddev->resync_max);
4246}
4247static ssize_t
4248max_sync_store(mddev_t *mddev, const char *buf, size_t len)
4249{
4250 if (strncmp(buf, "max", 3) == 0)
4251 mddev->resync_max = MaxSector;
4252 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004253 unsigned long long max;
4254 if (strict_strtoull(buf, 10, &max))
4255 return -EINVAL;
4256 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004257 return -EINVAL;
4258 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004259 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004260 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4261 return -EBUSY;
4262
4263 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004264 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004265 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004266 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004267 return -EINVAL;
4268 }
4269 mddev->resync_max = max;
4270 }
4271 wake_up(&mddev->recovery_wait);
4272 return len;
4273}
4274
4275static struct md_sysfs_entry md_max_sync =
4276__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4277
4278static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08004279suspend_lo_show(mddev_t *mddev, char *page)
4280{
4281 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4282}
4283
4284static ssize_t
4285suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
4286{
4287 char *e;
4288 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004289 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004290
NeilBrownb8d966e2009-07-01 11:14:04 +10004291 if (mddev->pers == NULL ||
4292 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004293 return -EINVAL;
4294 if (buf == e || (*e && *e != '\n'))
4295 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004296
4297 mddev->suspend_lo = new;
4298 if (new >= old)
4299 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004300 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004301 else {
4302 /* Expanding suspended region - need to wait */
4303 mddev->pers->quiesce(mddev, 1);
4304 mddev->pers->quiesce(mddev, 0);
4305 }
4306 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004307}
4308static struct md_sysfs_entry md_suspend_lo =
4309__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4310
4311
4312static ssize_t
4313suspend_hi_show(mddev_t *mddev, char *page)
4314{
4315 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4316}
4317
4318static ssize_t
4319suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
4320{
4321 char *e;
4322 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004323 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004324
NeilBrownb8d966e2009-07-01 11:14:04 +10004325 if (mddev->pers == NULL ||
4326 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004327 return -EINVAL;
4328 if (buf == e || (*e && *e != '\n'))
4329 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004330
4331 mddev->suspend_hi = new;
4332 if (new <= old)
4333 /* Shrinking suspended region */
4334 mddev->pers->quiesce(mddev, 2);
4335 else {
4336 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004337 mddev->pers->quiesce(mddev, 1);
4338 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004339 }
4340 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004341}
4342static struct md_sysfs_entry md_suspend_hi =
4343__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4344
NeilBrown08a02ec2007-05-09 02:35:38 -07004345static ssize_t
4346reshape_position_show(mddev_t *mddev, char *page)
4347{
4348 if (mddev->reshape_position != MaxSector)
4349 return sprintf(page, "%llu\n",
4350 (unsigned long long)mddev->reshape_position);
4351 strcpy(page, "none\n");
4352 return 5;
4353}
4354
4355static ssize_t
4356reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
4357{
4358 char *e;
4359 unsigned long long new = simple_strtoull(buf, &e, 10);
4360 if (mddev->pers)
4361 return -EBUSY;
4362 if (buf == e || (*e && *e != '\n'))
4363 return -EINVAL;
4364 mddev->reshape_position = new;
4365 mddev->delta_disks = 0;
4366 mddev->new_level = mddev->level;
4367 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004368 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004369 return len;
4370}
4371
4372static struct md_sysfs_entry md_reshape_position =
4373__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4374 reshape_position_store);
4375
Dan Williamsb522adc2009-03-31 15:00:31 +11004376static ssize_t
4377array_size_show(mddev_t *mddev, char *page)
4378{
4379 if (mddev->external_size)
4380 return sprintf(page, "%llu\n",
4381 (unsigned long long)mddev->array_sectors/2);
4382 else
4383 return sprintf(page, "default\n");
4384}
4385
4386static ssize_t
4387array_size_store(mddev_t *mddev, const char *buf, size_t len)
4388{
4389 sector_t sectors;
4390
4391 if (strncmp(buf, "default", 7) == 0) {
4392 if (mddev->pers)
4393 sectors = mddev->pers->size(mddev, 0, 0);
4394 else
4395 sectors = mddev->array_sectors;
4396
4397 mddev->external_size = 0;
4398 } else {
4399 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4400 return -EINVAL;
4401 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004402 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004403
4404 mddev->external_size = 1;
4405 }
4406
4407 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004408 if (mddev->pers) {
4409 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004410 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004411 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004412 return len;
4413}
4414
4415static struct md_sysfs_entry md_array_size =
4416__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4417 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004418
NeilBrowneae17012005-11-08 21:39:23 -08004419static struct attribute *md_default_attrs[] = {
4420 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004421 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004422 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004423 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004424 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004425 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004426 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004427 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004428 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004429 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004430 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004431 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004432 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004433 NULL,
4434};
4435
4436static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004437 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004438 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004439 &md_sync_min.attr,
4440 &md_sync_max.attr,
4441 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004442 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004443 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004444 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004445 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004446 &md_suspend_lo.attr,
4447 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004448 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004449 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004450 NULL,
4451};
NeilBrown411036f2005-11-08 21:39:40 -08004452static struct attribute_group md_redundancy_group = {
4453 .name = NULL,
4454 .attrs = md_redundancy_attrs,
4455};
4456
NeilBrowneae17012005-11-08 21:39:23 -08004457
4458static ssize_t
4459md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4460{
4461 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4462 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004463 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004464
4465 if (!entry->show)
4466 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004467 rv = mddev_lock(mddev);
4468 if (!rv) {
4469 rv = entry->show(mddev, page);
4470 mddev_unlock(mddev);
4471 }
NeilBrown96de1e62005-11-08 21:39:39 -08004472 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004473}
4474
4475static ssize_t
4476md_attr_store(struct kobject *kobj, struct attribute *attr,
4477 const char *page, size_t length)
4478{
4479 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4480 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004481 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004482
4483 if (!entry->store)
4484 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004485 if (!capable(CAP_SYS_ADMIN))
4486 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004487 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004488 if (mddev->hold_active == UNTIL_IOCTL)
4489 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004490 if (!rv) {
4491 rv = entry->store(mddev, page, length);
4492 mddev_unlock(mddev);
4493 }
NeilBrown96de1e62005-11-08 21:39:39 -08004494 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004495}
4496
4497static void md_free(struct kobject *ko)
4498{
4499 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004500
4501 if (mddev->sysfs_state)
4502 sysfs_put(mddev->sysfs_state);
4503
4504 if (mddev->gendisk) {
4505 del_gendisk(mddev->gendisk);
4506 put_disk(mddev->gendisk);
4507 }
4508 if (mddev->queue)
4509 blk_cleanup_queue(mddev->queue);
4510
NeilBrowneae17012005-11-08 21:39:23 -08004511 kfree(mddev);
4512}
4513
Emese Revfy52cf25d2010-01-19 02:58:23 +01004514static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004515 .show = md_attr_show,
4516 .store = md_attr_store,
4517};
4518static struct kobj_type md_ktype = {
4519 .release = md_free,
4520 .sysfs_ops = &md_sysfs_ops,
4521 .default_attrs = md_default_attrs,
4522};
4523
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524int mdp_major = 0;
4525
Dan Williams5fd3a172009-03-04 00:57:25 -07004526static void mddev_delayed_delete(struct work_struct *ws)
4527{
4528 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4529
NeilBrown43a70502009-12-14 12:49:55 +11004530 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004531 kobject_del(&mddev->kobj);
4532 kobject_put(&mddev->kobj);
4533}
4534
NeilBrownefeb53c2009-01-09 08:31:10 +11004535static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004537 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 mddev_t *mddev = mddev_find(dev);
4539 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004540 int partitioned;
4541 int shift;
4542 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004543 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
4545 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004546 return -ENODEV;
4547
4548 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4549 shift = partitioned ? MdpMinorShift : 0;
4550 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551
Tejun Heoe804ac72010-10-15 15:36:08 +02004552 /* wait for any previous instance of this device to be
4553 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004554 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004555 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004556
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004557 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004558 error = -EEXIST;
4559 if (mddev->gendisk)
4560 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004561
4562 if (name) {
4563 /* Need to ensure that 'name' is not a duplicate.
4564 */
4565 mddev_t *mddev2;
4566 spin_lock(&all_mddevs_lock);
4567
4568 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4569 if (mddev2->gendisk &&
4570 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4571 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004572 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004573 }
4574 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 }
NeilBrown8b765392009-01-09 08:31:08 +11004576
NeilBrown0909dc42009-07-01 12:27:21 +10004577 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004578 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004579 if (!mddev->queue)
4580 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004581 mddev->queue->queuedata = mddev;
4582
NeilBrown409c57f2009-03-31 14:39:39 +11004583 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004584
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 disk = alloc_disk(1 << shift);
4586 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004587 blk_cleanup_queue(mddev->queue);
4588 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004589 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004591 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004593 if (name)
4594 strcpy(disk->disk_name, name);
4595 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004597 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 disk->fops = &md_fops;
4600 disk->private_data = mddev;
4601 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004602 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004603 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004604 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004605 * remove it now.
4606 */
4607 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004609 /* As soon as we call add_disk(), another thread could get
4610 * through to md_open, so make sure it doesn't get too far
4611 */
4612 mutex_lock(&mddev->open_mutex);
4613 add_disk(disk);
4614
Tejun Heoed9e1982008-08-25 19:56:05 +09004615 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4616 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004617 if (error) {
4618 /* This isn't possible, but as kobject_init_and_add is marked
4619 * __must_check, we must do something with the result
4620 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004621 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4622 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004623 error = 0;
4624 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004625 if (mddev->kobj.sd &&
4626 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004627 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004628 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004629 abort:
4630 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004631 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004632 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004633 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004634 }
NeilBrownd3374822009-01-09 08:31:10 +11004635 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004636 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004637}
4638
4639static struct kobject *md_probe(dev_t dev, int *part, void *data)
4640{
4641 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 return NULL;
4643}
4644
NeilBrownefeb53c2009-01-09 08:31:10 +11004645static int add_named_array(const char *val, struct kernel_param *kp)
4646{
4647 /* val must be "md_*" where * is not all digits.
4648 * We allocate an array with a large free minor number, and
4649 * set the name to val. val must not already be an active name.
4650 */
4651 int len = strlen(val);
4652 char buf[DISK_NAME_LEN];
4653
4654 while (len && val[len-1] == '\n')
4655 len--;
4656 if (len >= DISK_NAME_LEN)
4657 return -E2BIG;
4658 strlcpy(buf, val, len+1);
4659 if (strncmp(buf, "md_", 3) != 0)
4660 return -EINVAL;
4661 return md_alloc(0, buf);
4662}
4663
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664static void md_safemode_timeout(unsigned long data)
4665{
4666 mddev_t *mddev = (mddev_t *) data;
4667
Neil Brown0fd62b82008-06-28 08:31:36 +10004668 if (!atomic_read(&mddev->writes_pending)) {
4669 mddev->safemode = 1;
4670 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004671 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 md_wakeup_thread(mddev->thread);
4674}
4675
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004676static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
NeilBrown390ee602010-06-01 19:37:27 +10004678int md_run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679{
NeilBrown2604b702006-01-06 00:20:36 -08004680 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 mdk_rdev_t *rdev;
NeilBrown2604b702006-01-06 00:20:36 -08004682 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683
NeilBrowna757e642005-04-16 15:26:42 -07004684 if (list_empty(&mddev->disks))
4685 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
4688 if (mddev->pers)
4689 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004690 /* Cannot run until previous stop completes properly */
4691 if (mddev->sysfs_active)
4692 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004693
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 /*
4695 * Analyze all RAID superblock(s)
4696 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004697 if (!mddev->raid_disks) {
4698 if (!mddev->persistent)
4699 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004700 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702
NeilBrownd9d166c2006-01-06 00:20:51 -08004703 if (mddev->level != LEVEL_NONE)
4704 request_module("md-level-%d", mddev->level);
4705 else if (mddev->clevel[0])
4706 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707
4708 /*
4709 * Drop all container device buffers, from now on
4710 * the only valid external interface is through the md
4711 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004713 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004714 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 continue;
4716 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004717 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004718
4719 /* perform some consistency tests on the device.
4720 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004721 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004722 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004723 if (rdev->meta_bdev) {
4724 /* Nothing to check */;
4725 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004726 if (mddev->dev_sectors &&
4727 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004728 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004729 printk("md: %s: data overlaps metadata\n",
4730 mdname(mddev));
4731 return -EINVAL;
4732 }
4733 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004734 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004735 > rdev->data_offset) {
4736 printk("md: %s: metadata overlaps data\n",
4737 mdname(mddev));
4738 return -EINVAL;
4739 }
4740 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004741 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 }
4743
NeilBrowna167f662010-10-26 18:31:13 +11004744 if (mddev->bio_set == NULL)
NeilBrowna519b262011-07-28 07:56:24 +10004745 mddev->bio_set = bioset_create(BIO_POOL_SIZE,
4746 sizeof(mddev_t *));
NeilBrowna167f662010-10-26 18:31:13 +11004747
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004749 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004750 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004752 if (mddev->level != LEVEL_NONE)
4753 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4754 mddev->level);
4755 else
4756 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4757 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 return -EINVAL;
4759 }
NeilBrown2604b702006-01-06 00:20:36 -08004760 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004762 if (mddev->level != pers->level) {
4763 mddev->level = pers->level;
4764 mddev->new_level = pers->level;
4765 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004766 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767
NeilBrownf6705572006-03-27 01:18:11 -08004768 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004769 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004770 /* This personality cannot handle reshaping... */
4771 mddev->pers = NULL;
4772 module_put(pers->owner);
4773 return -EINVAL;
4774 }
4775
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004776 if (pers->sync_request) {
4777 /* Warn if this is a potentially silly
4778 * configuration.
4779 */
4780 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4781 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004782 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004783
4784 list_for_each_entry(rdev, &mddev->disks, same_set)
4785 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004786 if (rdev < rdev2 &&
4787 rdev->bdev->bd_contains ==
4788 rdev2->bdev->bd_contains) {
4789 printk(KERN_WARNING
4790 "%s: WARNING: %s appears to be"
4791 " on the same physical disk as"
4792 " %s.\n",
4793 mdname(mddev),
4794 bdevname(rdev->bdev,b),
4795 bdevname(rdev2->bdev,b2));
4796 warned = 1;
4797 }
4798 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004799
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004800 if (warned)
4801 printk(KERN_WARNING
4802 "True protection against single-disk"
4803 " failure might be compromised.\n");
4804 }
4805
NeilBrown657390d2005-08-26 18:34:16 -07004806 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004807 /* may be over-ridden by personality */
4808 mddev->resync_max_sectors = mddev->dev_sectors;
4809
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004810 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811
NeilBrown0f9552b52009-12-30 12:08:50 +11004812 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004813 mddev->ro = 2; /* read-only, but switch on first write */
4814
NeilBrownb15c2e52006-01-06 00:20:16 -08004815 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004816 if (err)
4817 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004818 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4819 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4820 " but 'external_size' not in effect?\n", __func__);
4821 printk(KERN_ERR
4822 "md: invalid array_size %llu > default size %llu\n",
4823 (unsigned long long)mddev->array_sectors / 2,
4824 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4825 err = -EINVAL;
4826 mddev->pers->stop(mddev);
4827 }
4828 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004829 err = bitmap_create(mddev);
4830 if (err) {
4831 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4832 mdname(mddev), err);
4833 mddev->pers->stop(mddev);
4834 }
4835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 module_put(mddev->pers->owner);
4838 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004839 bitmap_destroy(mddev);
4840 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004842 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004843 if (mddev->kobj.sd &&
4844 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004845 printk(KERN_WARNING
4846 "md: cannot register extra attributes for %s\n",
4847 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004848 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004849 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004850 mddev->ro = 0;
4851
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004853 atomic_set(&mddev->max_corr_read_errors,
4854 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 mddev->safemode = 0;
4856 mddev->safemode_timer.function = md_safemode_timeout;
4857 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004858 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11004860 smp_wmb();
4861 mddev->ready = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004862 list_for_each_entry(rdev, &mddev->disks, same_set)
Namhyung Kim36fad852011-07-27 11:00:36 +10004863 if (rdev->raid_disk >= 0)
4864 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004865 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
4867 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4868
NeilBrown850b2b42006-10-03 01:15:46 -07004869 if (mddev->flags)
4870 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871
NeilBrownd7603b72006-01-06 00:20:30 -08004872 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004873 sysfs_notify_dirent_safe(mddev->sysfs_state);
4874 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004875 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 return 0;
4877}
NeilBrown390ee602010-06-01 19:37:27 +10004878EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879
NeilBrownfe60b012010-03-29 11:10:42 +11004880static int do_md_run(mddev_t *mddev)
4881{
4882 int err;
4883
4884 err = md_run(mddev);
4885 if (err)
4886 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10004887 err = bitmap_load(mddev);
4888 if (err) {
4889 bitmap_destroy(mddev);
4890 goto out;
4891 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05004892
4893 md_wakeup_thread(mddev->thread);
4894 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4895
NeilBrownfe60b012010-03-29 11:10:42 +11004896 set_capacity(mddev->gendisk, mddev->array_sectors);
4897 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004898 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11004899 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4900out:
4901 return err;
4902}
4903
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904static int restart_array(mddev_t *mddev)
4905{
4906 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907
Andre Noll80fab1d2008-07-11 22:02:21 +10004908 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004910 return -ENXIO;
4911 if (!mddev->pers)
4912 return -EINVAL;
4913 if (!mddev->ro)
4914 return -EBUSY;
4915 mddev->safemode = 0;
4916 mddev->ro = 0;
4917 set_disk_ro(disk, 0);
4918 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4919 mdname(mddev));
4920 /* Kick recovery or resync if necessary */
4921 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4922 md_wakeup_thread(mddev->thread);
4923 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004924 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004925 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926}
4927
NeilBrownacc55e22006-06-26 00:27:47 -07004928/* similar to deny_write_access, but accounts for our holding a reference
4929 * to the file ourselves */
4930static int deny_bitmap_write_access(struct file * file)
4931{
4932 struct inode *inode = file->f_mapping->host;
4933
4934 spin_lock(&inode->i_lock);
4935 if (atomic_read(&inode->i_writecount) > 1) {
4936 spin_unlock(&inode->i_lock);
4937 return -ETXTBSY;
4938 }
4939 atomic_set(&inode->i_writecount, -1);
4940 spin_unlock(&inode->i_lock);
4941
4942 return 0;
4943}
4944
NeilBrown43a70502009-12-14 12:49:55 +11004945void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004946{
4947 struct inode *inode = file->f_mapping->host;
4948
4949 spin_lock(&inode->i_lock);
4950 atomic_set(&inode->i_writecount, 1);
4951 spin_unlock(&inode->i_lock);
4952}
4953
NeilBrown6177b472010-03-29 11:37:13 +11004954static void md_clean(mddev_t *mddev)
4955{
4956 mddev->array_sectors = 0;
4957 mddev->external_size = 0;
4958 mddev->dev_sectors = 0;
4959 mddev->raid_disks = 0;
4960 mddev->recovery_cp = 0;
4961 mddev->resync_min = 0;
4962 mddev->resync_max = MaxSector;
4963 mddev->reshape_position = MaxSector;
4964 mddev->external = 0;
4965 mddev->persistent = 0;
4966 mddev->level = LEVEL_NONE;
4967 mddev->clevel[0] = 0;
4968 mddev->flags = 0;
4969 mddev->ro = 0;
4970 mddev->metadata_type[0] = 0;
4971 mddev->chunk_sectors = 0;
4972 mddev->ctime = mddev->utime = 0;
4973 mddev->layout = 0;
4974 mddev->max_disks = 0;
4975 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10004976 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004977 mddev->delta_disks = 0;
4978 mddev->new_level = LEVEL_NONE;
4979 mddev->new_layout = 0;
4980 mddev->new_chunk_sectors = 0;
4981 mddev->curr_resync = 0;
4982 mddev->resync_mismatches = 0;
4983 mddev->suspend_lo = mddev->suspend_hi = 0;
4984 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4985 mddev->recovery = 0;
4986 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004987 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004988 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004989 mddev->safemode = 0;
4990 mddev->bitmap_info.offset = 0;
4991 mddev->bitmap_info.default_offset = 0;
4992 mddev->bitmap_info.chunksize = 0;
4993 mddev->bitmap_info.daemon_sleep = 0;
4994 mddev->bitmap_info.max_write_behind = 0;
4995}
4996
NeilBrowndefad612011-01-14 09:14:33 +11004997static void __md_stop_writes(mddev_t *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11004998{
4999 if (mddev->sync_thread) {
5000 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5001 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11005002 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005003 }
5004
5005 del_timer_sync(&mddev->safemode_timer);
5006
5007 bitmap_flush(mddev);
5008 md_super_wait(mddev);
5009
5010 if (!mddev->in_sync || mddev->flags) {
5011 /* mark array as shutdown cleanly */
5012 mddev->in_sync = 1;
5013 md_update_sb(mddev, 1);
5014 }
5015}
NeilBrowndefad612011-01-14 09:14:33 +11005016
5017void md_stop_writes(mddev_t *mddev)
5018{
5019 mddev_lock(mddev);
5020 __md_stop_writes(mddev);
5021 mddev_unlock(mddev);
5022}
NeilBrown390ee602010-06-01 19:37:27 +10005023EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005024
NeilBrown390ee602010-06-01 19:37:27 +10005025void md_stop(mddev_t *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005026{
NeilBrown0ca69882011-01-14 09:14:33 +11005027 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005028 mddev->pers->stop(mddev);
5029 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5030 mddev->to_remove = &md_redundancy_group;
5031 module_put(mddev->pers->owner);
5032 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005033 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005034}
NeilBrown390ee602010-06-01 19:37:27 +10005035EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005036
NeilBrowna4bd82d02010-03-29 13:23:10 +11005037static int md_set_readonly(mddev_t *mddev, int is_open)
5038{
5039 int err = 0;
5040 mutex_lock(&mddev->open_mutex);
5041 if (atomic_read(&mddev->openers) > is_open) {
5042 printk("md: %s still in use.\n",mdname(mddev));
5043 err = -EBUSY;
5044 goto out;
5045 }
5046 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005047 __md_stop_writes(mddev);
NeilBrowna4bd82d02010-03-29 13:23:10 +11005048
5049 err = -ENXIO;
5050 if (mddev->ro==1)
5051 goto out;
5052 mddev->ro = 1;
5053 set_disk_ro(mddev->gendisk, 1);
5054 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005055 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d02010-03-29 13:23:10 +11005056 err = 0;
5057 }
5058out:
5059 mutex_unlock(&mddev->open_mutex);
5060 return err;
5061}
5062
NeilBrown9e653b62006-06-26 00:27:58 -07005063/* mode:
5064 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005065 * 2 - stop but do not disassemble array
5066 */
Neil Browndf5b20c2008-07-11 22:02:22 +10005067static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10005070 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071
NeilBrownc8c00a62009-08-10 12:50:52 +10005072 mutex_lock(&mddev->open_mutex);
NeilBrownbb4f1e92010-08-08 21:18:03 +10005073 if (atomic_read(&mddev->openers) > is_open ||
5074 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005075 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005076 mutex_unlock(&mddev->open_mutex);
5077 return -EBUSY;
5078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079
NeilBrown6e17b022010-08-07 21:41:19 +10005080 if (mddev->pers) {
NeilBrowna4bd82d02010-03-29 13:23:10 +11005081 if (mddev->ro)
5082 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005083
NeilBrowndefad612011-01-14 09:14:33 +11005084 __md_stop_writes(mddev);
NeilBrowna4bd82d02010-03-29 13:23:10 +11005085 md_stop(mddev);
5086 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d02010-03-29 13:23:10 +11005087 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005088
NeilBrowna4bd82d02010-03-29 13:23:10 +11005089 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005090 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005091
NeilBrowna4bd82d02010-03-29 13:23:10 +11005092 list_for_each_entry(rdev, &mddev->disks, same_set)
Namhyung Kim36fad852011-07-27 11:00:36 +10005093 if (rdev->raid_disk >= 0)
5094 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005095
NeilBrowna4bd82d02010-03-29 13:23:10 +11005096 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005097 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005098 mddev->changed = 1;
NeilBrowna4bd82d02010-03-29 13:23:10 +11005099 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005100
NeilBrowna4bd82d02010-03-29 13:23:10 +11005101 if (mddev->ro)
5102 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005103 } else
5104 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 /*
5106 * Free resources if final stop
5107 */
NeilBrown9e653b62006-06-26 00:27:58 -07005108 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5110
NeilBrown978f9462006-02-02 14:28:05 -08005111 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005112 if (mddev->bitmap_info.file) {
5113 restore_bitmap_write_access(mddev->bitmap_info.file);
5114 fput(mddev->bitmap_info.file);
5115 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005116 }
NeilBrownc3d97142009-12-14 12:49:52 +11005117 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005118
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 export_array(mddev);
5120
NeilBrown6177b472010-03-29 11:37:13 +11005121 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005122 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005123 if (mddev->hold_active == UNTIL_STOP)
5124 mddev->hold_active = 0;
NeilBrowna4bd82d02010-03-29 13:23:10 +11005125 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005126 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005127 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005128 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130}
5131
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005132#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133static void autorun_array(mddev_t *mddev)
5134{
5135 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 int err;
5137
NeilBrowna757e642005-04-16 15:26:42 -07005138 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140
5141 printk(KERN_INFO "md: running: ");
5142
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005143 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 char b[BDEVNAME_SIZE];
5145 printk("<%s>", bdevname(rdev->bdev,b));
5146 }
5147 printk("\n");
5148
NeilBrownd710e132008-10-13 11:55:12 +11005149 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 if (err) {
5151 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11005152 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 }
5154}
5155
5156/*
5157 * lets try to run arrays based on all disks that have arrived
5158 * until now. (those are in pending_raid_disks)
5159 *
5160 * the method: pick the first pending disk, collect all disks with
5161 * the same UUID, remove all from the pending list and put them into
5162 * the 'same_array' list. Then order this list based on superblock
5163 * update time (freshest comes first), kick out 'old' disks and
5164 * compare superblocks. If everything's fine then run it.
5165 *
5166 * If "unit" is allocated, then bump its reference count
5167 */
5168static void autorun_devices(int part)
5169{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005170 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 mddev_t *mddev;
5172 char b[BDEVNAME_SIZE];
5173
5174 printk(KERN_INFO "md: autorun ...\n");
5175 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005176 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005178 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 rdev0 = list_entry(pending_raid_disks.next,
5180 mdk_rdev_t, same_set);
5181
5182 printk(KERN_INFO "md: considering %s ...\n",
5183 bdevname(rdev0->bdev,b));
5184 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005185 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 if (super_90_load(rdev, rdev0, 0) >= 0) {
5187 printk(KERN_INFO "md: adding %s ...\n",
5188 bdevname(rdev->bdev,b));
5189 list_move(&rdev->same_set, &candidates);
5190 }
5191 /*
5192 * now we have a set of devices, with all of them having
5193 * mostly sane superblocks. It's time to allocate the
5194 * mddev.
5195 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005196 if (part) {
5197 dev = MKDEV(mdp_major,
5198 rdev0->preferred_minor << MdpMinorShift);
5199 unit = MINOR(dev) >> MdpMinorShift;
5200 } else {
5201 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5202 unit = MINOR(dev);
5203 }
5204 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5206 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5207 break;
5208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209
5210 md_probe(dev, NULL, NULL);
5211 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005212 if (!mddev || !mddev->gendisk) {
5213 if (mddev)
5214 mddev_put(mddev);
5215 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 "md: cannot allocate memory for md drive.\n");
5217 break;
5218 }
5219 if (mddev_lock(mddev))
5220 printk(KERN_WARNING "md: %s locked, cannot run\n",
5221 mdname(mddev));
5222 else if (mddev->raid_disks || mddev->major_version
5223 || !list_empty(&mddev->disks)) {
5224 printk(KERN_WARNING
5225 "md: %s already running, cannot run %s\n",
5226 mdname(mddev), bdevname(rdev0->bdev,b));
5227 mddev_unlock(mddev);
5228 } else {
5229 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005230 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005231 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 list_del_init(&rdev->same_set);
5233 if (bind_rdev_to_array(rdev, mddev))
5234 export_rdev(rdev);
5235 }
5236 autorun_array(mddev);
5237 mddev_unlock(mddev);
5238 }
5239 /* on success, candidates will be empty, on error
5240 * it won't...
5241 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005242 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005243 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 mddev_put(mddev);
5247 }
5248 printk(KERN_INFO "md: ... autorun DONE.\n");
5249}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005250#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252static int get_version(void __user * arg)
5253{
5254 mdu_version_t ver;
5255
5256 ver.major = MD_MAJOR_VERSION;
5257 ver.minor = MD_MINOR_VERSION;
5258 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5259
5260 if (copy_to_user(arg, &ver, sizeof(ver)))
5261 return -EFAULT;
5262
5263 return 0;
5264}
5265
5266static int get_array_info(mddev_t * mddev, void __user * arg)
5267{
5268 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005269 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271
NeilBrowna9f326e2009-09-23 18:06:41 +10005272 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005273 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005275 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 failed++;
5277 else {
5278 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005279 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005280 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 else
5282 spare++;
5283 }
5284 }
5285
5286 info.major_version = mddev->major_version;
5287 info.minor_version = mddev->minor_version;
5288 info.patch_version = MD_PATCHLEVEL_VERSION;
5289 info.ctime = mddev->ctime;
5290 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005291 info.size = mddev->dev_sectors / 2;
5292 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005293 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 info.nr_disks = nr;
5295 info.raid_disks = mddev->raid_disks;
5296 info.md_minor = mddev->md_minor;
5297 info.not_persistent= !mddev->persistent;
5298
5299 info.utime = mddev->utime;
5300 info.state = 0;
5301 if (mddev->in_sync)
5302 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005303 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005304 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005305 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 info.working_disks = working;
5307 info.failed_disks = failed;
5308 info.spare_disks = spare;
5309
5310 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005311 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312
5313 if (copy_to_user(arg, &info, sizeof(info)))
5314 return -EFAULT;
5315
5316 return 0;
5317}
5318
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005319static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005320{
5321 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5322 char *ptr, *buf = NULL;
5323 int err = -ENOMEM;
5324
Dan Williamsb5470dc2008-06-27 21:44:04 -07005325 if (md_allow_write(mddev))
5326 file = kmalloc(sizeof(*file), GFP_NOIO);
5327 else
5328 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005329
NeilBrown32a76272005-06-21 17:17:14 -07005330 if (!file)
5331 goto out;
5332
5333 /* bitmap disabled, zero the first byte and copy out */
5334 if (!mddev->bitmap || !mddev->bitmap->file) {
5335 file->pathname[0] = '\0';
5336 goto copy_out;
5337 }
5338
5339 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5340 if (!buf)
5341 goto out;
5342
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005343 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5344 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005345 goto out;
5346
5347 strcpy(file->pathname, ptr);
5348
5349copy_out:
5350 err = 0;
5351 if (copy_to_user(arg, file, sizeof(*file)))
5352 err = -EFAULT;
5353out:
5354 kfree(buf);
5355 kfree(file);
5356 return err;
5357}
5358
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359static int get_disk_info(mddev_t * mddev, void __user * arg)
5360{
5361 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 mdk_rdev_t *rdev;
5363
5364 if (copy_from_user(&info, arg, sizeof(info)))
5365 return -EFAULT;
5366
Andre Noll26ef3792008-07-11 22:02:21 +10005367 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 if (rdev) {
5369 info.major = MAJOR(rdev->bdev->bd_dev);
5370 info.minor = MINOR(rdev->bdev->bd_dev);
5371 info.raid_disk = rdev->raid_disk;
5372 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005373 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005375 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 info.state |= (1<<MD_DISK_ACTIVE);
5377 info.state |= (1<<MD_DISK_SYNC);
5378 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005379 if (test_bit(WriteMostly, &rdev->flags))
5380 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 } else {
5382 info.major = info.minor = 0;
5383 info.raid_disk = -1;
5384 info.state = (1<<MD_DISK_REMOVED);
5385 }
5386
5387 if (copy_to_user(arg, &info, sizeof(info)))
5388 return -EFAULT;
5389
5390 return 0;
5391}
5392
5393static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5394{
5395 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5396 mdk_rdev_t *rdev;
5397 dev_t dev = MKDEV(info->major,info->minor);
5398
5399 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5400 return -EOVERFLOW;
5401
5402 if (!mddev->raid_disks) {
5403 int err;
5404 /* expecting a device which has a superblock */
5405 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5406 if (IS_ERR(rdev)) {
5407 printk(KERN_WARNING
5408 "md: md_import_device returned %ld\n",
5409 PTR_ERR(rdev));
5410 return PTR_ERR(rdev);
5411 }
5412 if (!list_empty(&mddev->disks)) {
5413 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5414 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005415 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 .load_super(rdev, rdev0, mddev->minor_version);
5417 if (err < 0) {
5418 printk(KERN_WARNING
5419 "md: %s has different UUID to %s\n",
5420 bdevname(rdev->bdev,b),
5421 bdevname(rdev0->bdev,b2));
5422 export_rdev(rdev);
5423 return -EINVAL;
5424 }
5425 }
5426 err = bind_rdev_to_array(rdev, mddev);
5427 if (err)
5428 export_rdev(rdev);
5429 return err;
5430 }
5431
5432 /*
5433 * add_new_disk can be used once the array is assembled
5434 * to add "hot spares". They must already have a superblock
5435 * written
5436 */
5437 if (mddev->pers) {
5438 int err;
5439 if (!mddev->pers->hot_add_disk) {
5440 printk(KERN_WARNING
5441 "%s: personality does not support diskops!\n",
5442 mdname(mddev));
5443 return -EINVAL;
5444 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005445 if (mddev->persistent)
5446 rdev = md_import_device(dev, mddev->major_version,
5447 mddev->minor_version);
5448 else
5449 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 if (IS_ERR(rdev)) {
5451 printk(KERN_WARNING
5452 "md: md_import_device returned %ld\n",
5453 PTR_ERR(rdev));
5454 return PTR_ERR(rdev);
5455 }
NeilBrown1a855a02010-12-09 16:36:28 +11005456 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005457 if (!mddev->persistent) {
5458 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005459 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005460 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005461 set_bit(In_sync, &rdev->flags);
5462 } else
NeilBrown41158c72005-06-21 17:17:25 -07005463 rdev->raid_disk = -1;
5464 } else
5465 super_types[mddev->major_version].
5466 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005467 if ((info->state & (1<<MD_DISK_SYNC)) &&
5468 (!test_bit(In_sync, &rdev->flags) ||
5469 rdev->raid_disk != info->raid_disk)) {
5470 /* This was a hot-add request, but events doesn't
5471 * match, so reject it.
5472 */
5473 export_rdev(rdev);
5474 return -EINVAL;
5475 }
5476
NeilBrown1a855a02010-12-09 16:36:28 +11005477 if (test_bit(In_sync, &rdev->flags))
5478 rdev->saved_raid_disk = rdev->raid_disk;
5479 else
5480 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005481
NeilBrownb2d444d2005-11-08 21:39:31 -08005482 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005483 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5484 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005485 else
5486 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005487
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 rdev->raid_disk = -1;
5489 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005490 if (!err && !mddev->pers->hot_remove_disk) {
5491 /* If there is hot_add_disk but no hot_remove_disk
5492 * then added disks for geometry changes,
5493 * and should be added immediately.
5494 */
5495 super_types[mddev->major_version].
5496 validate_super(mddev, rdev);
5497 err = mddev->pers->hot_add_disk(mddev, rdev);
5498 if (err)
5499 unbind_rdev_from_array(rdev);
5500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 if (err)
5502 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005503 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005504 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005505
NeilBrown17571282006-12-10 02:20:52 -08005506 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005507 if (mddev->degraded)
5508 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005509 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005510 if (!err)
5511 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005512 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 return err;
5514 }
5515
5516 /* otherwise, add_new_disk is only allowed
5517 * for major_version==0 superblocks
5518 */
5519 if (mddev->major_version != 0) {
5520 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5521 mdname(mddev));
5522 return -EINVAL;
5523 }
5524
5525 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5526 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005527 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 if (IS_ERR(rdev)) {
5529 printk(KERN_WARNING
5530 "md: error, md_import_device() returned %ld\n",
5531 PTR_ERR(rdev));
5532 return PTR_ERR(rdev);
5533 }
5534 rdev->desc_nr = info->number;
5535 if (info->raid_disk < mddev->raid_disks)
5536 rdev->raid_disk = info->raid_disk;
5537 else
5538 rdev->raid_disk = -1;
5539
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005541 if (info->state & (1<<MD_DISK_SYNC))
5542 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005544 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5545 set_bit(WriteMostly, &rdev->flags);
5546
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 if (!mddev->persistent) {
5548 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005549 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5550 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005551 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005552 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553
NeilBrown2bf071b2006-01-06 00:20:55 -08005554 err = bind_rdev_to_array(rdev, mddev);
5555 if (err) {
5556 export_rdev(rdev);
5557 return err;
5558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 }
5560
5561 return 0;
5562}
5563
5564static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5565{
5566 char b[BDEVNAME_SIZE];
5567 mdk_rdev_t *rdev;
5568
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 rdev = find_rdev(mddev, dev);
5570 if (!rdev)
5571 return -ENXIO;
5572
5573 if (rdev->raid_disk >= 0)
5574 goto busy;
5575
5576 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005577 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005578 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579
5580 return 0;
5581busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005582 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 bdevname(rdev->bdev,b), mdname(mddev));
5584 return -EBUSY;
5585}
5586
5587static int hot_add_disk(mddev_t * mddev, dev_t dev)
5588{
5589 char b[BDEVNAME_SIZE];
5590 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 mdk_rdev_t *rdev;
5592
5593 if (!mddev->pers)
5594 return -ENODEV;
5595
5596 if (mddev->major_version != 0) {
5597 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5598 " version-0 superblocks.\n",
5599 mdname(mddev));
5600 return -EINVAL;
5601 }
5602 if (!mddev->pers->hot_add_disk) {
5603 printk(KERN_WARNING
5604 "%s: personality does not support diskops!\n",
5605 mdname(mddev));
5606 return -EINVAL;
5607 }
5608
NeilBrownd710e132008-10-13 11:55:12 +11005609 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 if (IS_ERR(rdev)) {
5611 printk(KERN_WARNING
5612 "md: error, md_import_device() returned %ld\n",
5613 PTR_ERR(rdev));
5614 return -EINVAL;
5615 }
5616
5617 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005618 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005620 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621
NeilBrown8190e752009-06-18 08:48:58 +10005622 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623
NeilBrownb2d444d2005-11-08 21:39:31 -08005624 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 printk(KERN_WARNING
5626 "md: can not hot-add faulty %s disk to %s!\n",
5627 bdevname(rdev->bdev,b), mdname(mddev));
5628 err = -EINVAL;
5629 goto abort_export;
5630 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005631 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005633 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005634 err = bind_rdev_to_array(rdev, mddev);
5635 if (err)
5636 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637
5638 /*
5639 * The rest should better be atomic, we can have disk failures
5640 * noticed in interrupt contexts ...
5641 */
5642
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 rdev->raid_disk = -1;
5644
NeilBrown850b2b42006-10-03 01:15:46 -07005645 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646
5647 /*
5648 * Kick recovery, maybe this spare has to be added to the
5649 * array immediately.
5650 */
5651 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5652 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005653 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 return 0;
5655
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656abort_export:
5657 export_rdev(rdev);
5658 return err;
5659}
5660
NeilBrown32a76272005-06-21 17:17:14 -07005661static int set_bitmap_file(mddev_t *mddev, int fd)
5662{
5663 int err;
5664
NeilBrown36fa3062005-09-09 16:23:45 -07005665 if (mddev->pers) {
5666 if (!mddev->pers->quiesce)
5667 return -EBUSY;
5668 if (mddev->recovery || mddev->sync_thread)
5669 return -EBUSY;
5670 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005671 }
5672
NeilBrown36fa3062005-09-09 16:23:45 -07005673
5674 if (fd >= 0) {
5675 if (mddev->bitmap)
5676 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005677 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005678
NeilBrownc3d97142009-12-14 12:49:52 +11005679 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005680 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5681 mdname(mddev));
5682 return -EBADF;
5683 }
5684
NeilBrownc3d97142009-12-14 12:49:52 +11005685 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005686 if (err) {
5687 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5688 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005689 fput(mddev->bitmap_info.file);
5690 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005691 return err;
5692 }
NeilBrownc3d97142009-12-14 12:49:52 +11005693 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005694 } else if (mddev->bitmap == NULL)
5695 return -ENOENT; /* cannot remove what isn't there */
5696 err = 0;
5697 if (mddev->pers) {
5698 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005699 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005700 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005701 if (!err)
5702 err = bitmap_load(mddev);
5703 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005704 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005705 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005706 fd = -1; /* make sure to put the file */
5707 }
NeilBrown36fa3062005-09-09 16:23:45 -07005708 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005709 }
5710 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005711 if (mddev->bitmap_info.file) {
5712 restore_bitmap_write_access(mddev->bitmap_info.file);
5713 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005714 }
NeilBrownc3d97142009-12-14 12:49:52 +11005715 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005716 }
5717
NeilBrown32a76272005-06-21 17:17:14 -07005718 return err;
5719}
5720
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721/*
5722 * set_array_info is used two different ways
5723 * The original usage is when creating a new array.
5724 * In this usage, raid_disks is > 0 and it together with
5725 * level, size, not_persistent,layout,chunksize determine the
5726 * shape of the array.
5727 * This will always create an array with a type-0.90.0 superblock.
5728 * The newer usage is when assembling an array.
5729 * In this case raid_disks will be 0, and the major_version field is
5730 * use to determine which style super-blocks are to be found on the devices.
5731 * The minor and patch _version numbers are also kept incase the
5732 * super_block handler wishes to interpret them.
5733 */
5734static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5735{
5736
5737 if (info->raid_disks == 0) {
5738 /* just setting version number for superblock loading */
5739 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005740 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 super_types[info->major_version].name == NULL) {
5742 /* maybe try to auto-load a module? */
5743 printk(KERN_INFO
5744 "md: superblock version %d not known\n",
5745 info->major_version);
5746 return -EINVAL;
5747 }
5748 mddev->major_version = info->major_version;
5749 mddev->minor_version = info->minor_version;
5750 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005751 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005752 /* ensure mddev_put doesn't delete this now that there
5753 * is some minimal configuration.
5754 */
5755 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 return 0;
5757 }
5758 mddev->major_version = MD_MAJOR_VERSION;
5759 mddev->minor_version = MD_MINOR_VERSION;
5760 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5761 mddev->ctime = get_seconds();
5762
5763 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005764 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005765 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 mddev->raid_disks = info->raid_disks;
5767 /* don't set md_minor, it is determined by which /dev/md* was
5768 * openned
5769 */
5770 if (info->state & (1<<MD_SB_CLEAN))
5771 mddev->recovery_cp = MaxSector;
5772 else
5773 mddev->recovery_cp = 0;
5774 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005775 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776
5777 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005778 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779
5780 mddev->max_disks = MD_SB_DISKS;
5781
NeilBrowne6910632008-02-06 01:39:51 -08005782 if (mddev->persistent)
5783 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005784 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785
NeilBrownc3d97142009-12-14 12:49:52 +11005786 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5787 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005788
NeilBrownf6705572006-03-27 01:18:11 -08005789 mddev->reshape_position = MaxSector;
5790
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 /*
5792 * Generate a 128 bit UUID
5793 */
5794 get_random_bytes(mddev->uuid, 16);
5795
NeilBrownf6705572006-03-27 01:18:11 -08005796 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005797 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005798 mddev->new_layout = mddev->layout;
5799 mddev->delta_disks = 0;
5800
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 return 0;
5802}
5803
Dan Williams1f403622009-03-31 14:59:03 +11005804void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5805{
Dan Williamsb522adc2009-03-31 15:00:31 +11005806 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5807
5808 if (mddev->external_size)
5809 return;
5810
Dan Williams1f403622009-03-31 14:59:03 +11005811 mddev->array_sectors = array_sectors;
5812}
5813EXPORT_SYMBOL(md_set_array_sectors);
5814
Andre Nolld71f9f82008-07-11 22:02:22 +10005815static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005816{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005817 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005818 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005819 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005820
5821 if (mddev->pers->resize == NULL)
5822 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005823 /* The "num_sectors" is the number of sectors of each device that
5824 * is used. This can only make sense for arrays with redundancy.
5825 * linear and raid0 always use whatever space is available. We can only
5826 * consider changing this number if no resync or reconstruction is
5827 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005828 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005829 * of each device. If num_sectors is zero, we find the largest size
5830 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08005831 */
5832 if (mddev->sync_thread)
5833 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005834 if (mddev->bitmap)
5835 /* Sorry, cannot grow a bitmap yet, just remove it,
5836 * grow, and re-add.
5837 */
5838 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005839 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005840 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005841
Andre Nolld71f9f82008-07-11 22:02:22 +10005842 if (fit && (num_sectors == 0 || num_sectors > avail))
5843 num_sectors = avail;
5844 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005845 return -ENOSPC;
5846 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005847 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005848 if (!rv)
5849 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005850 return rv;
5851}
5852
NeilBrownda943b992006-01-06 00:20:54 -08005853static int update_raid_disks(mddev_t *mddev, int raid_disks)
5854{
5855 int rv;
5856 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005857 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005858 return -EINVAL;
5859 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005860 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08005861 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005862 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005863 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005864 mddev->delta_disks = raid_disks - mddev->raid_disks;
5865
5866 rv = mddev->pers->check_reshape(mddev);
NeilBrownde171cb2011-01-31 11:57:42 +11005867 if (rv < 0)
5868 mddev->delta_disks = 0;
NeilBrownda943b992006-01-06 00:20:54 -08005869 return rv;
5870}
5871
5872
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873/*
5874 * update_array_info is used to change the configuration of an
5875 * on-line array.
5876 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5877 * fields in the info are checked against the array.
5878 * Any differences that cannot be handled will cause an error.
5879 * Normally, only one change can be managed at a time.
5880 */
5881static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5882{
5883 int rv = 0;
5884 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005885 int state = 0;
5886
5887 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005888 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005889 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890
5891 if (mddev->major_version != info->major_version ||
5892 mddev->minor_version != info->minor_version ||
5893/* mddev->patch_version != info->patch_version || */
5894 mddev->ctime != info->ctime ||
5895 mddev->level != info->level ||
5896/* mddev->layout != info->layout || */
5897 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005898 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005899 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5900 ((state^info->state) & 0xfffffe00)
5901 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902 return -EINVAL;
5903 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005904 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5905 cnt++;
5906 if (mddev->raid_disks != info->raid_disks)
5907 cnt++;
5908 if (mddev->layout != info->layout)
5909 cnt++;
5910 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5911 cnt++;
5912 if (cnt == 0)
5913 return 0;
5914 if (cnt > 1)
5915 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916
5917 if (mddev->layout != info->layout) {
5918 /* Change layout
5919 * we don't need to do anything at the md level, the
5920 * personality will take care of it all.
5921 */
NeilBrown50ac1682009-06-18 08:47:55 +10005922 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005924 else {
5925 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005926 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005927 if (rv)
5928 mddev->new_layout = mddev->layout;
5929 return rv;
5930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005932 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005933 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934
NeilBrownda943b992006-01-06 00:20:54 -08005935 if (mddev->raid_disks != info->raid_disks)
5936 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937
NeilBrown36fa3062005-09-09 16:23:45 -07005938 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5939 if (mddev->pers->quiesce == NULL)
5940 return -EINVAL;
5941 if (mddev->recovery || mddev->sync_thread)
5942 return -EBUSY;
5943 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5944 /* add the bitmap */
5945 if (mddev->bitmap)
5946 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005947 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005948 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005949 mddev->bitmap_info.offset =
5950 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005951 mddev->pers->quiesce(mddev, 1);
5952 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005953 if (!rv)
5954 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07005955 if (rv)
5956 bitmap_destroy(mddev);
5957 mddev->pers->quiesce(mddev, 0);
5958 } else {
5959 /* remove the bitmap */
5960 if (!mddev->bitmap)
5961 return -ENOENT;
5962 if (mddev->bitmap->file)
5963 return -EINVAL;
5964 mddev->pers->quiesce(mddev, 1);
5965 bitmap_destroy(mddev);
5966 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005967 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005968 }
5969 }
NeilBrown850b2b42006-10-03 01:15:46 -07005970 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971 return rv;
5972}
5973
5974static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5975{
5976 mdk_rdev_t *rdev;
5977
5978 if (mddev->pers == NULL)
5979 return -ENODEV;
5980
5981 rdev = find_rdev(mddev, dev);
5982 if (!rdev)
5983 return -ENODEV;
5984
5985 md_error(mddev, rdev);
5986 return 0;
5987}
5988
Andre Noll2f9618c2008-04-25 18:57:58 +02005989/*
5990 * We have a problem here : there is no easy way to give a CHS
5991 * virtual geometry. We currently pretend that we have a 2 heads
5992 * 4 sectors (with a BIG number of cylinders...). This drives
5993 * dosfs just mad... ;-)
5994 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005995static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5996{
5997 mddev_t *mddev = bdev->bd_disk->private_data;
5998
5999 geo->heads = 2;
6000 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006001 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006002 return 0;
6003}
6004
Al Viroa39907f2008-03-02 10:31:15 -05006005static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 unsigned int cmd, unsigned long arg)
6007{
6008 int err = 0;
6009 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006011 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012
6013 if (!capable(CAP_SYS_ADMIN))
6014 return -EACCES;
6015
6016 /*
6017 * Commands dealing with the RAID driver but not any
6018 * particular array:
6019 */
6020 switch (cmd)
6021 {
6022 case RAID_VERSION:
6023 err = get_version(argp);
6024 goto done;
6025
6026 case PRINT_RAID_DEBUG:
6027 err = 0;
6028 md_print_devices();
6029 goto done;
6030
6031#ifndef MODULE
6032 case RAID_AUTORUN:
6033 err = 0;
6034 autostart_arrays(arg);
6035 goto done;
6036#endif
6037 default:;
6038 }
6039
6040 /*
6041 * Commands creating/starting a new array:
6042 */
6043
Al Viroa39907f2008-03-02 10:31:15 -05006044 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045
6046 if (!mddev) {
6047 BUG();
6048 goto abort;
6049 }
6050
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 err = mddev_lock(mddev);
6052 if (err) {
6053 printk(KERN_INFO
6054 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6055 err, cmd);
6056 goto abort;
6057 }
6058
6059 switch (cmd)
6060 {
6061 case SET_ARRAY_INFO:
6062 {
6063 mdu_array_info_t info;
6064 if (!arg)
6065 memset(&info, 0, sizeof(info));
6066 else if (copy_from_user(&info, argp, sizeof(info))) {
6067 err = -EFAULT;
6068 goto abort_unlock;
6069 }
6070 if (mddev->pers) {
6071 err = update_array_info(mddev, &info);
6072 if (err) {
6073 printk(KERN_WARNING "md: couldn't update"
6074 " array info. %d\n", err);
6075 goto abort_unlock;
6076 }
6077 goto done_unlock;
6078 }
6079 if (!list_empty(&mddev->disks)) {
6080 printk(KERN_WARNING
6081 "md: array %s already has disks!\n",
6082 mdname(mddev));
6083 err = -EBUSY;
6084 goto abort_unlock;
6085 }
6086 if (mddev->raid_disks) {
6087 printk(KERN_WARNING
6088 "md: array %s already initialised!\n",
6089 mdname(mddev));
6090 err = -EBUSY;
6091 goto abort_unlock;
6092 }
6093 err = set_array_info(mddev, &info);
6094 if (err) {
6095 printk(KERN_WARNING "md: couldn't set"
6096 " array info. %d\n", err);
6097 goto abort_unlock;
6098 }
6099 }
6100 goto done_unlock;
6101
6102 default:;
6103 }
6104
6105 /*
6106 * Commands querying/configuring an existing array:
6107 */
NeilBrown32a76272005-06-21 17:17:14 -07006108 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006109 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006110 if ((!mddev->raid_disks && !mddev->external)
6111 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6112 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6113 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114 err = -ENODEV;
6115 goto abort_unlock;
6116 }
6117
6118 /*
6119 * Commands even a read-only array can execute:
6120 */
6121 switch (cmd)
6122 {
6123 case GET_ARRAY_INFO:
6124 err = get_array_info(mddev, argp);
6125 goto done_unlock;
6126
NeilBrown32a76272005-06-21 17:17:14 -07006127 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01006128 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07006129 goto done_unlock;
6130
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 case GET_DISK_INFO:
6132 err = get_disk_info(mddev, argp);
6133 goto done_unlock;
6134
6135 case RESTART_ARRAY_RW:
6136 err = restart_array(mddev);
6137 goto done_unlock;
6138
6139 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006140 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141 goto done_unlock;
6142
6143 case STOP_ARRAY_RO:
NeilBrowna4bd82d02010-03-29 13:23:10 +11006144 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145 goto done_unlock;
6146
Dan Williamse2218352010-05-12 08:25:37 +10006147 case BLKROSET:
6148 if (get_user(ro, (int __user *)(arg))) {
6149 err = -EFAULT;
6150 goto done_unlock;
6151 }
6152 err = -EINVAL;
6153
6154 /* if the bdev is going readonly the value of mddev->ro
6155 * does not matter, no writes are coming
6156 */
6157 if (ro)
6158 goto done_unlock;
6159
6160 /* are we are already prepared for writes? */
6161 if (mddev->ro != 1)
6162 goto done_unlock;
6163
6164 /* transitioning to readauto need only happen for
6165 * arrays that call md_write_start
6166 */
6167 if (mddev->pers) {
6168 err = restart_array(mddev);
6169 if (err == 0) {
6170 mddev->ro = 2;
6171 set_disk_ro(mddev->gendisk, 0);
6172 }
6173 }
6174 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 }
6176
6177 /*
6178 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006179 * superblock, so we do not allow them on read-only arrays.
6180 * However non-MD ioctls (e.g. get-size) will still come through
6181 * here and hit the 'default' below, so only disallow
6182 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006184 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006185 if (mddev->ro == 2) {
6186 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006187 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006188 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6189 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08006190 } else {
6191 err = -EROFS;
6192 goto abort_unlock;
6193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194 }
6195
6196 switch (cmd)
6197 {
6198 case ADD_NEW_DISK:
6199 {
6200 mdu_disk_info_t info;
6201 if (copy_from_user(&info, argp, sizeof(info)))
6202 err = -EFAULT;
6203 else
6204 err = add_new_disk(mddev, &info);
6205 goto done_unlock;
6206 }
6207
6208 case HOT_REMOVE_DISK:
6209 err = hot_remove_disk(mddev, new_decode_dev(arg));
6210 goto done_unlock;
6211
6212 case HOT_ADD_DISK:
6213 err = hot_add_disk(mddev, new_decode_dev(arg));
6214 goto done_unlock;
6215
6216 case SET_DISK_FAULTY:
6217 err = set_disk_faulty(mddev, new_decode_dev(arg));
6218 goto done_unlock;
6219
6220 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006221 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 goto done_unlock;
6223
NeilBrown32a76272005-06-21 17:17:14 -07006224 case SET_BITMAP_FILE:
6225 err = set_bitmap_file(mddev, (int)arg);
6226 goto done_unlock;
6227
Linus Torvalds1da177e2005-04-16 15:20:36 -07006228 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 err = -EINVAL;
6230 goto abort_unlock;
6231 }
6232
6233done_unlock:
6234abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006235 if (mddev->hold_active == UNTIL_IOCTL &&
6236 err != -EINVAL)
6237 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 mddev_unlock(mddev);
6239
6240 return err;
6241done:
6242 if (err)
6243 MD_BUG();
6244abort:
6245 return err;
6246}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006247#ifdef CONFIG_COMPAT
6248static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6249 unsigned int cmd, unsigned long arg)
6250{
6251 switch (cmd) {
6252 case HOT_REMOVE_DISK:
6253 case HOT_ADD_DISK:
6254 case SET_DISK_FAULTY:
6255 case SET_BITMAP_FILE:
6256 /* These take in integer arg, do not convert */
6257 break;
6258 default:
6259 arg = (unsigned long)compat_ptr(arg);
6260 break;
6261 }
6262
6263 return md_ioctl(bdev, mode, cmd, arg);
6264}
6265#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266
Al Viroa39907f2008-03-02 10:31:15 -05006267static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268{
6269 /*
6270 * Succeed if we can lock the mddev, which confirms that
6271 * it isn't being stopped right now.
6272 */
NeilBrownd3374822009-01-09 08:31:10 +11006273 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274 int err;
6275
NeilBrownd3374822009-01-09 08:31:10 +11006276 if (mddev->gendisk != bdev->bd_disk) {
6277 /* we are racing with mddev_put which is discarding this
6278 * bd_disk.
6279 */
6280 mddev_put(mddev);
6281 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006282 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006283 /* Then retry the open from the top */
6284 return -ERESTARTSYS;
6285 }
6286 BUG_ON(mddev != bdev->bd_disk->private_data);
6287
NeilBrownc8c00a62009-08-10 12:50:52 +10006288 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289 goto out;
6290
6291 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006292 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006293 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006295 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 out:
6297 return err;
6298}
6299
Al Viroa39907f2008-03-02 10:31:15 -05006300static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301{
Al Viroa39907f2008-03-02 10:31:15 -05006302 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006304 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006305 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306 mddev_put(mddev);
6307
6308 return 0;
6309}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006310
6311static int md_media_changed(struct gendisk *disk)
6312{
6313 mddev_t *mddev = disk->private_data;
6314
6315 return mddev->changed;
6316}
6317
6318static int md_revalidate(struct gendisk *disk)
6319{
6320 mddev_t *mddev = disk->private_data;
6321
6322 mddev->changed = 0;
6323 return 0;
6324}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006325static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326{
6327 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006328 .open = md_open,
6329 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006330 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006331#ifdef CONFIG_COMPAT
6332 .compat_ioctl = md_compat_ioctl,
6333#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006334 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006335 .media_changed = md_media_changed,
6336 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337};
6338
Adrian Bunk75c96f82005-05-05 16:16:09 -07006339static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340{
6341 mdk_thread_t *thread = arg;
6342
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 /*
6344 * md_thread is a 'system-thread', it's priority should be very
6345 * high. We avoid resource deadlocks individually in each
6346 * raid personality. (RAID5 does preallocation) We also use RR and
6347 * the very same RT priority as kswapd, thus we will never get
6348 * into a priority inversion deadlock.
6349 *
6350 * we definitely have to have equal or higher priority than
6351 * bdflush, otherwise bdflush will deadlock if there are too
6352 * many dirty RAID5 blocks.
6353 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354
NeilBrown6985c432005-10-19 21:23:47 -07006355 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006356 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357
NeilBrown93588e22005-11-15 00:09:12 -08006358 /* We need to wait INTERRUPTIBLE so that
6359 * we don't add to the load-average.
6360 * That means we need to be sure no signals are
6361 * pending
6362 */
6363 if (signal_pending(current))
6364 flush_signals(current);
6365
6366 wait_event_interruptible_timeout
6367 (thread->wqueue,
6368 test_bit(THREAD_WAKEUP, &thread->flags)
6369 || kthread_should_stop(),
6370 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371
NeilBrown6c987912011-01-14 09:13:53 +11006372 clear_bit(THREAD_WAKEUP, &thread->flags);
6373 if (!kthread_should_stop())
NeilBrown589a5942010-12-09 17:02:14 +11006374 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006376
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 return 0;
6378}
6379
6380void md_wakeup_thread(mdk_thread_t *thread)
6381{
6382 if (thread) {
6383 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
6384 set_bit(THREAD_WAKEUP, &thread->flags);
6385 wake_up(&thread->wqueue);
6386 }
6387}
6388
6389mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
6390 const char *name)
6391{
6392 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393
NeilBrown9ffae0c2006-01-06 00:20:32 -08006394 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395 if (!thread)
6396 return NULL;
6397
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398 init_waitqueue_head(&thread->wqueue);
6399
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400 thread->run = run;
6401 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006402 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006403 thread->tsk = kthread_run(md_thread, thread,
6404 "%s_%s",
6405 mdname(thread->mddev),
6406 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006407 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408 kfree(thread);
6409 return NULL;
6410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411 return thread;
6412}
6413
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414void md_unregister_thread(mdk_thread_t *thread)
6415{
NeilBrowne0cf8f02009-03-31 14:39:39 +11006416 if (!thread)
6417 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07006418 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07006419
6420 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421 kfree(thread);
6422}
6423
6424void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6425{
6426 if (!mddev) {
6427 MD_BUG();
6428 return;
6429 }
6430
NeilBrownb2d444d2005-11-08 21:39:31 -08006431 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006433
NeilBrownde393cd2011-07-28 11:31:48 +10006434 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 return;
6436 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006437 if (mddev->degraded)
6438 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006439 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6441 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6442 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006443 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006444 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006445 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446}
6447
6448/* seq_file implementation /proc/mdstat */
6449
6450static void status_unused(struct seq_file *seq)
6451{
6452 int i = 0;
6453 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006454
6455 seq_printf(seq, "unused devices: ");
6456
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006457 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006458 char b[BDEVNAME_SIZE];
6459 i++;
6460 seq_printf(seq, "%s ",
6461 bdevname(rdev->bdev,b));
6462 }
6463 if (!i)
6464 seq_printf(seq, "<none>");
6465
6466 seq_printf(seq, "\n");
6467}
6468
6469
6470static void status_resync(struct seq_file *seq, mddev_t * mddev)
6471{
NeilBrowndd71cf62009-05-07 12:49:35 +10006472 sector_t max_sectors, resync, res;
6473 unsigned long dt, db;
6474 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006475 int scale;
6476 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477
NeilBrowndd71cf62009-05-07 12:49:35 +10006478 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479
6480 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006481 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006483 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484
6485 /*
6486 * Should not happen.
6487 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006488 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489 MD_BUG();
6490 return;
6491 }
NeilBrown4588b422006-03-27 01:18:04 -08006492 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006493 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006494 * u32, as those are the requirements for sector_div.
6495 * Thus 'scale' must be at least 10
6496 */
6497 scale = 10;
6498 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006499 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006500 scale++;
6501 }
6502 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006503 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006504
6505 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506 {
NeilBrown4588b422006-03-27 01:18:04 -08006507 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508 seq_printf(seq, "[");
6509 for (i = 0; i < x; i++)
6510 seq_printf(seq, "=");
6511 seq_printf(seq, ">");
6512 for (i = 0; i < y; i++)
6513 seq_printf(seq, ".");
6514 seq_printf(seq, "] ");
6515 }
NeilBrown4588b422006-03-27 01:18:04 -08006516 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006517 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6518 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006519 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6520 "check" :
6521 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6522 "resync" : "recovery"))),
6523 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006524 (unsigned long long) resync/2,
6525 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526
6527 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528 * dt: time from mark until now
6529 * db: blocks written from mark until now
6530 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006531 *
6532 * rt is a sector_t, so could be 32bit or 64bit.
6533 * So we divide before multiply in case it is 32bit and close
6534 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006535 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006536 * near the end of resync when the number of remaining sectors
6537 * is close to 'db'.
6538 * We then divide rt by 32 after multiplying by db to compensate.
6539 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540 */
6541 dt = ((jiffies - mddev->resync_mark) / HZ);
6542 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006543 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6544 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545
NeilBrowndd71cf62009-05-07 12:49:35 +10006546 rt = max_sectors - resync; /* number of remaining sectors */
6547 sector_div(rt, db/32+1);
6548 rt *= dt;
6549 rt >>= 5;
6550
6551 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6552 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006553
NeilBrownff4e8d92006-07-10 04:44:16 -07006554 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006555}
6556
6557static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6558{
6559 struct list_head *tmp;
6560 loff_t l = *pos;
6561 mddev_t *mddev;
6562
6563 if (l >= 0x10000)
6564 return NULL;
6565 if (!l--)
6566 /* header */
6567 return (void*)1;
6568
6569 spin_lock(&all_mddevs_lock);
6570 list_for_each(tmp,&all_mddevs)
6571 if (!l--) {
6572 mddev = list_entry(tmp, mddev_t, all_mddevs);
6573 mddev_get(mddev);
6574 spin_unlock(&all_mddevs_lock);
6575 return mddev;
6576 }
6577 spin_unlock(&all_mddevs_lock);
6578 if (!l--)
6579 return (void*)2;/* tail */
6580 return NULL;
6581}
6582
6583static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6584{
6585 struct list_head *tmp;
6586 mddev_t *next_mddev, *mddev = v;
6587
6588 ++*pos;
6589 if (v == (void*)2)
6590 return NULL;
6591
6592 spin_lock(&all_mddevs_lock);
6593 if (v == (void*)1)
6594 tmp = all_mddevs.next;
6595 else
6596 tmp = mddev->all_mddevs.next;
6597 if (tmp != &all_mddevs)
6598 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6599 else {
6600 next_mddev = (void*)2;
6601 *pos = 0x10000;
6602 }
6603 spin_unlock(&all_mddevs_lock);
6604
6605 if (v != (void*)1)
6606 mddev_put(mddev);
6607 return next_mddev;
6608
6609}
6610
6611static void md_seq_stop(struct seq_file *seq, void *v)
6612{
6613 mddev_t *mddev = v;
6614
6615 if (mddev && v != (void*)1 && v != (void*)2)
6616 mddev_put(mddev);
6617}
6618
NeilBrownd7603b72006-01-06 00:20:30 -08006619struct mdstat_info {
6620 int event;
6621};
6622
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623static int md_seq_show(struct seq_file *seq, void *v)
6624{
6625 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006626 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006628 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006629 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630
6631 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006632 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633 seq_printf(seq, "Personalities : ");
6634 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006635 list_for_each_entry(pers, &pers_list, list)
6636 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637
6638 spin_unlock(&pers_lock);
6639 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006640 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641 return 0;
6642 }
6643 if (v == (void*)2) {
6644 status_unused(seq);
6645 return 0;
6646 }
6647
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006648 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006649 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006650
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6652 seq_printf(seq, "%s : %sactive", mdname(mddev),
6653 mddev->pers ? "" : "in");
6654 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006655 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006657 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006658 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 seq_printf(seq, " %s", mddev->pers->name);
6660 }
6661
Andre Nolldd8ac332009-03-31 14:33:13 +11006662 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006663 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664 char b[BDEVNAME_SIZE];
6665 seq_printf(seq, " %s[%d]",
6666 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006667 if (test_bit(WriteMostly, &rdev->flags))
6668 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006669 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670 seq_printf(seq, "(F)");
6671 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006672 } else if (rdev->raid_disk < 0)
6673 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006674 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675 }
6676
6677 if (!list_empty(&mddev->disks)) {
6678 if (mddev->pers)
6679 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006680 (unsigned long long)
6681 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682 else
6683 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006684 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006686 if (mddev->persistent) {
6687 if (mddev->major_version != 0 ||
6688 mddev->minor_version != 90) {
6689 seq_printf(seq," super %d.%d",
6690 mddev->major_version,
6691 mddev->minor_version);
6692 }
NeilBrowne6910632008-02-06 01:39:51 -08006693 } else if (mddev->external)
6694 seq_printf(seq, " super external:%s",
6695 mddev->metadata_type);
6696 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006697 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698
6699 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006700 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006702 if (mddev->pers->sync_request) {
6703 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006704 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006705 seq_printf(seq, "\n ");
6706 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6707 seq_printf(seq, "\tresync=DELAYED\n ");
6708 else if (mddev->recovery_cp < MaxSector)
6709 seq_printf(seq, "\tresync=PENDING\n ");
6710 }
NeilBrown32a76272005-06-21 17:17:14 -07006711 } else
6712 seq_printf(seq, "\n ");
6713
6714 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006715 unsigned long chunk_kb;
6716 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006717 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006718 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006719 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6720 "%lu%s chunk",
6721 bitmap->pages - bitmap->missing_pages,
6722 bitmap->pages,
6723 (bitmap->pages - bitmap->missing_pages)
6724 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006725 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006726 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006727 if (bitmap->file) {
6728 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006729 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006730 }
NeilBrown78d742d2005-06-21 17:17:15 -07006731
NeilBrown32a76272005-06-21 17:17:14 -07006732 seq_printf(seq, "\n");
6733 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734 }
6735
6736 seq_printf(seq, "\n");
6737 }
6738 mddev_unlock(mddev);
6739
6740 return 0;
6741}
6742
Jan Engelhardt110518b2009-05-07 12:49:37 +10006743static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744 .start = md_seq_start,
6745 .next = md_seq_next,
6746 .stop = md_seq_stop,
6747 .show = md_seq_show,
6748};
6749
6750static int md_seq_open(struct inode *inode, struct file *file)
6751{
6752 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006753 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6754 if (mi == NULL)
6755 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006756
6757 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006758 if (error)
6759 kfree(mi);
6760 else {
6761 struct seq_file *p = file->private_data;
6762 p->private = mi;
6763 mi->event = atomic_read(&md_event_count);
6764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 return error;
6766}
6767
NeilBrownd7603b72006-01-06 00:20:30 -08006768static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6769{
6770 struct seq_file *m = filp->private_data;
6771 struct mdstat_info *mi = m->private;
6772 int mask;
6773
6774 poll_wait(filp, &md_event_waiters, wait);
6775
6776 /* always allow read */
6777 mask = POLLIN | POLLRDNORM;
6778
6779 if (mi->event != atomic_read(&md_event_count))
6780 mask |= POLLERR | POLLPRI;
6781 return mask;
6782}
6783
Arjan van de Venfa027c22007-02-12 00:55:33 -08006784static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006785 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786 .open = md_seq_open,
6787 .read = seq_read,
6788 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006789 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006790 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791};
6792
NeilBrown2604b702006-01-06 00:20:36 -08006793int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006796 list_add_tail(&p->list, &pers_list);
6797 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 spin_unlock(&pers_lock);
6799 return 0;
6800}
6801
NeilBrown2604b702006-01-06 00:20:36 -08006802int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803{
NeilBrown2604b702006-01-06 00:20:36 -08006804 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006805 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006806 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807 spin_unlock(&pers_lock);
6808 return 0;
6809}
6810
NeilBrowneea1bf32009-03-31 14:27:02 +11006811static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812{
6813 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006814 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006815 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816
6817 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006818 rcu_read_lock();
6819 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006821 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6822 (int)part_stat_read(&disk->part0, sectors[1]) -
6823 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006824 /* sync IO will cause sync_io to increase before the disk_stats
6825 * as sync_io is counted when a request starts, and
6826 * disk_stats is counted when it completes.
6827 * So resync activity will cause curr_events to be smaller than
6828 * when there was no such activity.
6829 * non-sync IO will cause disk_stat to increase without
6830 * increasing sync_io so curr_events will (eventually)
6831 * be larger than it was before. Once it becomes
6832 * substantially larger, the test below will cause
6833 * the array to appear non-idle, and resync will slow
6834 * down.
6835 * If there is a lot of outstanding resync activity when
6836 * we set last_event to curr_events, then all that activity
6837 * completing might cause the array to appear non-idle
6838 * and resync will be slowed down even though there might
6839 * not have been non-resync activity. This will only
6840 * happen once though. 'last_events' will soon reflect
6841 * the state where there is little or no outstanding
6842 * resync requests, and further resync activity will
6843 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006844 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006846 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847 rdev->last_events = curr_events;
6848 idle = 0;
6849 }
6850 }
NeilBrown4b809912008-07-21 17:05:25 +10006851 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852 return idle;
6853}
6854
6855void md_done_sync(mddev_t *mddev, int blocks, int ok)
6856{
6857 /* another "blocks" (512byte) blocks have been synced */
6858 atomic_sub(blocks, &mddev->recovery_active);
6859 wake_up(&mddev->recovery_wait);
6860 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006861 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862 md_wakeup_thread(mddev->thread);
6863 // stop recovery, signal do_sync ....
6864 }
6865}
6866
6867
NeilBrown06d91a52005-06-21 17:17:12 -07006868/* md_write_start(mddev, bi)
6869 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006870 * in superblock) before writing, schedule a superblock update
6871 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006872 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006873void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874{
Neil Brown0fd62b82008-06-28 08:31:36 +10006875 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006876 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006877 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006878
NeilBrownf91de922005-11-08 21:39:36 -08006879 BUG_ON(mddev->ro == 1);
6880 if (mddev->ro == 2) {
6881 /* need to switch to read/write */
6882 mddev->ro = 0;
6883 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6884 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006885 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006886 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006887 }
NeilBrown06d91a52005-06-21 17:17:12 -07006888 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006889 if (mddev->safemode == 1)
6890 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006891 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006892 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006893 if (mddev->in_sync) {
6894 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006895 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006896 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006897 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006898 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006899 }
NeilBrowna9701a32005-11-08 21:39:34 -08006900 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006901 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006902 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10006903 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006904 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07006905 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906}
6907
6908void md_write_end(mddev_t *mddev)
6909{
6910 if (atomic_dec_and_test(&mddev->writes_pending)) {
6911 if (mddev->safemode == 2)
6912 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006913 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6915 }
6916}
6917
NeilBrown2a2275d2007-01-26 00:57:11 -08006918/* md_allow_write(mddev)
6919 * Calling this ensures that the array is marked 'active' so that writes
6920 * may proceed without blocking. It is important to call this before
6921 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6922 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006923 *
6924 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6925 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006926 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006927int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006928{
6929 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006930 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006931 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006932 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006933 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006934 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006935
6936 spin_lock_irq(&mddev->write_lock);
6937 if (mddev->in_sync) {
6938 mddev->in_sync = 0;
6939 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006940 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08006941 if (mddev->safemode_delay &&
6942 mddev->safemode == 0)
6943 mddev->safemode = 1;
6944 spin_unlock_irq(&mddev->write_lock);
6945 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006946 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006947 } else
6948 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006949
NeilBrown070dc6d2010-08-30 17:33:34 +10006950 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07006951 return -EAGAIN;
6952 else
6953 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006954}
6955EXPORT_SYMBOL_GPL(md_allow_write);
6956
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957#define SYNC_MARKS 10
6958#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006959void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960{
6961 mddev_t *mddev2;
6962 unsigned int currspeed = 0,
6963 window;
NeilBrown57afd892005-06-21 17:17:13 -07006964 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965 unsigned long mark[SYNC_MARKS];
6966 sector_t mark_cnt[SYNC_MARKS];
6967 int last_mark,m;
6968 struct list_head *tmp;
6969 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006970 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006971 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006972 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006973
6974 /* just incase thread restarts... */
6975 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6976 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006977 if (mddev->ro) /* never try to sync a read-only array */
6978 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979
NeilBrown61df9d92006-10-03 01:15:57 -07006980 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6981 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6982 desc = "data-check";
6983 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6984 desc = "requested-resync";
6985 else
6986 desc = "resync";
6987 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6988 desc = "reshape";
6989 else
6990 desc = "recovery";
6991
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992 /* we overload curr_resync somewhat here.
6993 * 0 == not engaged in resync at all
6994 * 2 == checking that there is no conflict with another sync
6995 * 1 == like 2, but have yielded to allow conflicting resync to
6996 * commense
6997 * other == active in resync - this many blocks
6998 *
6999 * Before starting a resync we must have set curr_resync to
7000 * 2, and then checked that every "conflicting" array has curr_resync
7001 * less than ours. When we find one that is the same or higher
7002 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7003 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7004 * This will mean we have to start checking from the beginning again.
7005 *
7006 */
7007
7008 do {
7009 mddev->curr_resync = 2;
7010
7011 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007012 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07007013 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11007014
7015 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007016 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007017 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018 if (mddev2 == mddev)
7019 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007020 if (!mddev->parallel_resync
7021 && mddev2->curr_resync
7022 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023 DEFINE_WAIT(wq);
7024 if (mddev < mddev2 && mddev->curr_resync == 2) {
7025 /* arbitrarily yield */
7026 mddev->curr_resync = 1;
7027 wake_up(&resync_wait);
7028 }
7029 if (mddev > mddev2 && mddev->curr_resync == 1)
7030 /* no need to wait here, we can wait the next
7031 * time 'round when curr_resync == 2
7032 */
7033 continue;
NeilBrown97441972008-09-19 11:49:54 +10007034 /* We need to wait 'interruptible' so as not to
7035 * contribute to the load average, and not to
7036 * be caught by 'softlockup'
7037 */
7038 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08007039 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07007040 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007041 printk(KERN_INFO "md: delaying %s of %s"
7042 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007044 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007046 if (signal_pending(current))
7047 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007048 schedule();
7049 finish_wait(&resync_wait, &wq);
7050 goto try_again;
7051 }
7052 finish_wait(&resync_wait, &wq);
7053 }
7054 }
7055 } while (mddev->curr_resync < 2);
7056
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007057 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007058 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007060 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061 */
7062 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08007063 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007064 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007065 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7066 j = mddev->resync_min;
7067 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007068 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007069
NeilBrownccfcc3c2006-03-27 01:18:09 -08007070 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11007071 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007072 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007074 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007075 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007076 rcu_read_lock();
7077 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007078 if (rdev->raid_disk >= 0 &&
7079 !test_bit(Faulty, &rdev->flags) &&
7080 !test_bit(In_sync, &rdev->flags) &&
7081 rdev->recovery_offset < j)
7082 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007083 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085
NeilBrown61df9d92006-10-03 01:15:57 -07007086 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7087 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7088 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007089 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007090 "(but not more than %d KB/sec) for %s.\n",
7091 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007092
NeilBrowneea1bf32009-03-31 14:27:02 +11007093 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007094
NeilBrown57afd892005-06-21 17:17:13 -07007095 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096 for (m = 0; m < SYNC_MARKS; m++) {
7097 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007098 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099 }
7100 last_mark = 0;
7101 mddev->resync_mark = mark[last_mark];
7102 mddev->resync_mark_cnt = mark_cnt[last_mark];
7103
7104 /*
7105 * Tune reconstruction:
7106 */
7107 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007108 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7109 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110
7111 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007112 last_check = 0;
7113
7114 if (j>2) {
7115 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007116 "md: resuming %s of %s from checkpoint.\n",
7117 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118 mddev->curr_resync = j;
7119 }
NeilBrown75d3da42011-01-14 09:14:34 +11007120 mddev->curr_resync_completed = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121
7122 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007123 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124
NeilBrown57afd892005-06-21 17:17:13 -07007125 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007126
NeilBrown7a91ee12009-05-26 12:57:21 +10007127 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7128 ((mddev->curr_resync > mddev->curr_resync_completed &&
7129 (mddev->curr_resync - mddev->curr_resync_completed)
7130 > (max_sectors >> 4)) ||
7131 (j - mddev->curr_resync_completed)*2
7132 >= mddev->resync_max - mddev->curr_resync_completed
7133 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007134 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007135 wait_event(mddev->recovery_wait,
7136 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007137 mddev->curr_resync_completed = j;
NeilBrown070dc6d2010-08-30 17:33:34 +10007138 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007139 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007140 }
NeilBrownacb180b2009-04-14 16:28:34 +10007141
NeilBrowne62e58a2009-07-01 13:15:35 +10007142 while (j >= mddev->resync_max && !kthread_should_stop()) {
7143 /* As this condition is controlled by user-space,
7144 * we can block indefinitely, so use '_interruptible'
7145 * to avoid triggering warnings.
7146 */
7147 flush_signals(current); /* just in case */
7148 wait_event_interruptible(mddev->recovery_wait,
7149 mddev->resync_max > j
7150 || kthread_should_stop());
7151 }
NeilBrownacb180b2009-04-14 16:28:34 +10007152
7153 if (kthread_should_stop())
7154 goto interrupted;
7155
NeilBrown57afd892005-06-21 17:17:13 -07007156 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007157 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007158 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007159 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160 goto out;
7161 }
NeilBrown57afd892005-06-21 17:17:13 -07007162
7163 if (!skipped) { /* actual IO requested */
7164 io_sectors += sectors;
7165 atomic_add(sectors, &mddev->recovery_active);
7166 }
7167
NeilBrowne875ece2011-07-28 11:39:24 +10007168 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7169 break;
7170
Linus Torvalds1da177e2005-04-16 15:20:36 -07007171 j += sectors;
7172 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007173 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007174 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007175 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007176 * visible in /proc/mdstat
7177 */
7178 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007179
7180 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181 continue;
7182
NeilBrown57afd892005-06-21 17:17:13 -07007183 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184 repeat:
7185 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7186 /* step marks */
7187 int next = (last_mark+1) % SYNC_MARKS;
7188
7189 mddev->resync_mark = mark[next];
7190 mddev->resync_mark_cnt = mark_cnt[next];
7191 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007192 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193 last_mark = next;
7194 }
7195
7196
NeilBrownc6207272008-02-06 01:39:52 -08007197 if (kthread_should_stop())
7198 goto interrupted;
7199
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200
7201 /*
7202 * this loop exits only if either when we are slower than
7203 * the 'hard' speed limit, or the system was IO-idle for
7204 * a jiffy.
7205 * the system might be non-idle CPU-wise, but we only care
7206 * about not overloading the IO subsystem. (things like an
7207 * e2fsck being done on the RAID array should execute fast)
7208 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209 cond_resched();
7210
NeilBrown57afd892005-06-21 17:17:13 -07007211 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7212 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007213
NeilBrown88202a02006-01-06 00:21:36 -08007214 if (currspeed > speed_min(mddev)) {
7215 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007216 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007217 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007218 goto repeat;
7219 }
7220 }
7221 }
NeilBrown61df9d92006-10-03 01:15:57 -07007222 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007223 /*
7224 * this also signals 'finished resyncing' to md_stop
7225 */
7226 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007227 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7228
7229 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007230 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231
NeilBrowndfc70642008-05-23 13:04:39 -07007232 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007233 mddev->curr_resync > 2) {
7234 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7235 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7236 if (mddev->curr_resync >= mddev->recovery_cp) {
7237 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007238 "md: checkpointing %s of %s.\n",
7239 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007240 mddev->recovery_cp = mddev->curr_resync;
7241 }
7242 } else
7243 mddev->recovery_cp = MaxSector;
7244 } else {
7245 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7246 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007247 rcu_read_lock();
7248 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007249 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007250 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007251 !test_bit(Faulty, &rdev->flags) &&
7252 !test_bit(In_sync, &rdev->flags) &&
7253 rdev->recovery_offset < mddev->curr_resync)
7254 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007255 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007257 }
NeilBrown17571282006-12-10 02:20:52 -08007258 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007259
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11007261 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7262 /* We completed so min/max setting can be forgotten if used. */
7263 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7264 mddev->resync_min = 0;
7265 mddev->resync_max = MaxSector;
7266 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7267 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007268 mddev->curr_resync = 0;
7269 wake_up(&resync_wait);
7270 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7271 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007272 return;
7273
7274 interrupted:
7275 /*
7276 * got a signal, exit.
7277 */
7278 printk(KERN_INFO
7279 "md: md_do_sync() got signal ... exiting\n");
7280 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7281 goto out;
7282
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283}
NeilBrown29269552006-03-27 01:18:10 -08007284EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007286static int remove_and_add_spares(mddev_t *mddev)
7287{
7288 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007289 int spares = 0;
7290
NeilBrown97e4f422009-03-31 14:33:13 +11007291 mddev->curr_resync_completed = 0;
7292
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007293 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007294 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007295 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007296 (test_bit(Faulty, &rdev->flags) ||
7297 ! test_bit(In_sync, &rdev->flags)) &&
7298 atomic_read(&rdev->nr_pending)==0) {
7299 if (mddev->pers->hot_remove_disk(
7300 mddev, rdev->raid_disk)==0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007301 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007302 rdev->raid_disk = -1;
7303 }
7304 }
7305
NeilBrown53890422011-07-27 11:00:36 +10007306 if (mddev->degraded) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007307 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07007308 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07007309 !test_bit(In_sync, &rdev->flags) &&
NeilBrownde393cd2011-07-28 11:31:48 +10007310 !test_bit(Faulty, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07007311 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007312 if (rdev->raid_disk < 0
7313 && !test_bit(Faulty, &rdev->flags)) {
7314 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10007315 if (mddev->pers->
7316 hot_add_disk(mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007317 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10007318 /* failure here is OK */;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007319 spares++;
7320 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11007321 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007322 } else
7323 break;
7324 }
NeilBrowndfc70642008-05-23 13:04:39 -07007325 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007326 }
7327 return spares;
7328}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007329
7330static void reap_sync_thread(mddev_t *mddev)
7331{
7332 mdk_rdev_t *rdev;
7333
7334 /* resync has finished, collect result */
7335 md_unregister_thread(mddev->sync_thread);
7336 mddev->sync_thread = NULL;
7337 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7338 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7339 /* success...*/
7340 /* activate any spares */
7341 if (mddev->pers->spare_active(mddev))
7342 sysfs_notify(&mddev->kobj, NULL,
7343 "degraded");
7344 }
7345 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7346 mddev->pers->finish_reshape)
7347 mddev->pers->finish_reshape(mddev);
7348 md_update_sb(mddev, 1);
7349
7350 /* if array is no-longer degraded, then any saved_raid_disk
7351 * information must be scrapped
7352 */
7353 if (!mddev->degraded)
7354 list_for_each_entry(rdev, &mddev->disks, same_set)
7355 rdev->saved_raid_disk = -1;
7356
7357 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7358 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7359 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7360 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7361 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7362 /* flag recovery needed just to double check */
7363 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7364 sysfs_notify_dirent_safe(mddev->sysfs_action);
7365 md_new_event(mddev);
Jonathan Brassow768e587e2011-07-27 11:00:37 +10007366 if (mddev->event_work.func)
7367 queue_work(md_misc_wq, &mddev->event_work);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007368}
7369
Linus Torvalds1da177e2005-04-16 15:20:36 -07007370/*
7371 * This routine is regularly called by all per-raid-array threads to
7372 * deal with generic issues like resync and super-block update.
7373 * Raid personalities that don't have a thread (linear/raid0) do not
7374 * need this as they never do any recovery or update the superblock.
7375 *
7376 * It does not do any resync itself, but rather "forks" off other threads
7377 * to do that as needed.
7378 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7379 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007380 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007381 * and wakeups up this thread which will reap the thread and finish up.
7382 * This thread also removes any faulty devices (with nr_pending == 0).
7383 *
7384 * The overall approach is:
7385 * 1/ if the superblock needs updating, update it.
7386 * 2/ If a recovery thread is running, don't do anything else.
7387 * 3/ If recovery has finished, clean up, possibly marking spares active.
7388 * 4/ If there are any faulty devices, remove them.
7389 * 5/ If array is degraded, try to add spares devices
7390 * 6/ If array has spares or is not in-sync, start a resync thread.
7391 */
7392void md_check_recovery(mddev_t *mddev)
7393{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007394 if (mddev->suspended)
7395 return;
7396
NeilBrown5f404022005-06-21 17:17:16 -07007397 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007398 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007399
NeilBrownfca4d842005-06-21 17:17:11 -07007400 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007401 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007402 printk(KERN_INFO "md: %s in immediate safe mode\n",
7403 mdname(mddev));
7404 mddev->safemode = 2;
7405 }
7406 flush_signals(current);
7407 }
7408
NeilBrownc89a8ee2008-08-05 15:54:13 +10007409 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7410 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007411 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007412 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007414 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007415 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007416 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7417 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007418 ))
7419 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007420
NeilBrowndf5b89b2006-03-27 01:18:20 -08007421 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007422 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007423
NeilBrownc89a8ee2008-08-05 15:54:13 +10007424 if (mddev->ro) {
7425 /* Only thing we do on a ro array is remove
7426 * failed devices.
7427 */
NeilBrowna8c42c72011-01-31 13:47:13 +11007428 mdk_rdev_t *rdev;
7429 list_for_each_entry(rdev, &mddev->disks, same_set)
7430 if (rdev->raid_disk >= 0 &&
7431 !test_bit(Blocked, &rdev->flags) &&
7432 test_bit(Faulty, &rdev->flags) &&
7433 atomic_read(&rdev->nr_pending)==0) {
7434 if (mddev->pers->hot_remove_disk(
7435 mddev, rdev->raid_disk)==0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007436 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna8c42c72011-01-31 13:47:13 +11007437 rdev->raid_disk = -1;
7438 }
7439 }
NeilBrownc89a8ee2008-08-05 15:54:13 +10007440 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7441 goto unlock;
7442 }
7443
NeilBrown31a59e32008-04-30 00:52:30 -07007444 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007445 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007446 spin_lock_irq(&mddev->write_lock);
7447 if (mddev->safemode &&
7448 !atomic_read(&mddev->writes_pending) &&
7449 !mddev->in_sync &&
7450 mddev->recovery_cp == MaxSector) {
7451 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007452 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007453 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007454 }
7455 if (mddev->safemode == 1)
7456 mddev->safemode = 0;
7457 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007458 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007459 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007460 }
NeilBrownfca4d842005-06-21 17:17:11 -07007461
NeilBrown850b2b42006-10-03 01:15:46 -07007462 if (mddev->flags)
7463 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007464
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7466 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7467 /* resync/recovery still happening */
7468 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7469 goto unlock;
7470 }
7471 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007472 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007473 goto unlock;
7474 }
Neil Brown72a23c22008-06-28 08:31:41 +10007475 /* Set RUNNING before clearing NEEDED to avoid
7476 * any transients in the value of "sync_action".
7477 */
7478 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7479 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007480 /* Clear some bits that don't mean anything, but
7481 * might be left set
7482 */
NeilBrown24dd4692005-11-08 21:39:26 -08007483 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7484 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007486 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7487 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007488 /* no recovery is running.
7489 * remove any failed drives, then
7490 * add spares if possible.
7491 * Spare are also removed and re-added, to allow
7492 * the personality to fail the re-add.
7493 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007494
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007495 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007496 if (mddev->pers->check_reshape == NULL ||
7497 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007498 /* Cannot proceed */
7499 goto unlock;
7500 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007501 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007502 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007503 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7504 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007505 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007506 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007507 } else if (mddev->recovery_cp < MaxSector) {
7508 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007509 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007510 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7511 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007512 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007513
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007515 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7516 /* We are adding a device or devices to an array
7517 * which has the bitmap stored on all devices.
7518 * So make sure all bitmap pages get written
7519 */
7520 bitmap_write_all(mddev->bitmap);
7521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007522 mddev->sync_thread = md_register_thread(md_do_sync,
7523 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007524 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007525 if (!mddev->sync_thread) {
7526 printk(KERN_ERR "%s: could not start resync"
7527 " thread...\n",
7528 mdname(mddev));
7529 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007530 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7531 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7532 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7533 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7534 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007535 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007536 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007537 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007538 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007539 }
7540 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007541 if (!mddev->sync_thread) {
7542 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7543 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7544 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007545 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007546 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007548 mddev_unlock(mddev);
7549 }
7550}
7551
Dan Williams6bfe0b42008-04-30 00:52:32 -07007552void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7553{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007554 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007555 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007556 !test_bit(Blocked, &rdev->flags) &&
7557 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007558 msecs_to_jiffies(5000));
7559 rdev_dec_pending(rdev, mddev);
7560}
7561EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7562
NeilBrown2230dfe2011-07-28 11:31:46 +10007563
7564/* Bad block management.
7565 * We can record which blocks on each device are 'bad' and so just
7566 * fail those blocks, or that stripe, rather than the whole device.
7567 * Entries in the bad-block table are 64bits wide. This comprises:
7568 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7569 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7570 * A 'shift' can be set so that larger blocks are tracked and
7571 * consequently larger devices can be covered.
7572 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7573 *
7574 * Locking of the bad-block table uses a seqlock so md_is_badblock
7575 * might need to retry if it is very unlucky.
7576 * We will sometimes want to check for bad blocks in a bi_end_io function,
7577 * so we use the write_seqlock_irq variant.
7578 *
7579 * When looking for a bad block we specify a range and want to
7580 * know if any block in the range is bad. So we binary-search
7581 * to the last range that starts at-or-before the given endpoint,
7582 * (or "before the sector after the target range")
7583 * then see if it ends after the given start.
7584 * We return
7585 * 0 if there are no known bad blocks in the range
7586 * 1 if there are known bad block which are all acknowledged
7587 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7588 * plus the start/length of the first bad section we overlap.
7589 */
7590int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7591 sector_t *first_bad, int *bad_sectors)
7592{
7593 int hi;
7594 int lo = 0;
7595 u64 *p = bb->page;
7596 int rv = 0;
7597 sector_t target = s + sectors;
7598 unsigned seq;
7599
7600 if (bb->shift > 0) {
7601 /* round the start down, and the end up */
7602 s >>= bb->shift;
7603 target += (1<<bb->shift) - 1;
7604 target >>= bb->shift;
7605 sectors = target - s;
7606 }
7607 /* 'target' is now the first block after the bad range */
7608
7609retry:
7610 seq = read_seqbegin(&bb->lock);
7611
7612 hi = bb->count;
7613
7614 /* Binary search between lo and hi for 'target'
7615 * i.e. for the last range that starts before 'target'
7616 */
7617 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7618 * are known not to be the last range before target.
7619 * VARIANT: hi-lo is the number of possible
7620 * ranges, and decreases until it reaches 1
7621 */
7622 while (hi - lo > 1) {
7623 int mid = (lo + hi) / 2;
7624 sector_t a = BB_OFFSET(p[mid]);
7625 if (a < target)
7626 /* This could still be the one, earlier ranges
7627 * could not. */
7628 lo = mid;
7629 else
7630 /* This and later ranges are definitely out. */
7631 hi = mid;
7632 }
7633 /* 'lo' might be the last that started before target, but 'hi' isn't */
7634 if (hi > lo) {
7635 /* need to check all range that end after 's' to see if
7636 * any are unacknowledged.
7637 */
7638 while (lo >= 0 &&
7639 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7640 if (BB_OFFSET(p[lo]) < target) {
7641 /* starts before the end, and finishes after
7642 * the start, so they must overlap
7643 */
7644 if (rv != -1 && BB_ACK(p[lo]))
7645 rv = 1;
7646 else
7647 rv = -1;
7648 *first_bad = BB_OFFSET(p[lo]);
7649 *bad_sectors = BB_LEN(p[lo]);
7650 }
7651 lo--;
7652 }
7653 }
7654
7655 if (read_seqretry(&bb->lock, seq))
7656 goto retry;
7657
7658 return rv;
7659}
7660EXPORT_SYMBOL_GPL(md_is_badblock);
7661
7662/*
7663 * Add a range of bad blocks to the table.
7664 * This might extend the table, or might contract it
7665 * if two adjacent ranges can be merged.
7666 * We binary-search to find the 'insertion' point, then
7667 * decide how best to handle it.
7668 */
7669static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
7670 int acknowledged)
7671{
7672 u64 *p;
7673 int lo, hi;
7674 int rv = 1;
7675
7676 if (bb->shift < 0)
7677 /* badblocks are disabled */
7678 return 0;
7679
7680 if (bb->shift) {
7681 /* round the start down, and the end up */
7682 sector_t next = s + sectors;
7683 s >>= bb->shift;
7684 next += (1<<bb->shift) - 1;
7685 next >>= bb->shift;
7686 sectors = next - s;
7687 }
7688
7689 write_seqlock_irq(&bb->lock);
7690
7691 p = bb->page;
7692 lo = 0;
7693 hi = bb->count;
7694 /* Find the last range that starts at-or-before 's' */
7695 while (hi - lo > 1) {
7696 int mid = (lo + hi) / 2;
7697 sector_t a = BB_OFFSET(p[mid]);
7698 if (a <= s)
7699 lo = mid;
7700 else
7701 hi = mid;
7702 }
7703 if (hi > lo && BB_OFFSET(p[lo]) > s)
7704 hi = lo;
7705
7706 if (hi > lo) {
7707 /* we found a range that might merge with the start
7708 * of our new range
7709 */
7710 sector_t a = BB_OFFSET(p[lo]);
7711 sector_t e = a + BB_LEN(p[lo]);
7712 int ack = BB_ACK(p[lo]);
7713 if (e >= s) {
7714 /* Yes, we can merge with a previous range */
7715 if (s == a && s + sectors >= e)
7716 /* new range covers old */
7717 ack = acknowledged;
7718 else
7719 ack = ack && acknowledged;
7720
7721 if (e < s + sectors)
7722 e = s + sectors;
7723 if (e - a <= BB_MAX_LEN) {
7724 p[lo] = BB_MAKE(a, e-a, ack);
7725 s = e;
7726 } else {
7727 /* does not all fit in one range,
7728 * make p[lo] maximal
7729 */
7730 if (BB_LEN(p[lo]) != BB_MAX_LEN)
7731 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
7732 s = a + BB_MAX_LEN;
7733 }
7734 sectors = e - s;
7735 }
7736 }
7737 if (sectors && hi < bb->count) {
7738 /* 'hi' points to the first range that starts after 's'.
7739 * Maybe we can merge with the start of that range */
7740 sector_t a = BB_OFFSET(p[hi]);
7741 sector_t e = a + BB_LEN(p[hi]);
7742 int ack = BB_ACK(p[hi]);
7743 if (a <= s + sectors) {
7744 /* merging is possible */
7745 if (e <= s + sectors) {
7746 /* full overlap */
7747 e = s + sectors;
7748 ack = acknowledged;
7749 } else
7750 ack = ack && acknowledged;
7751
7752 a = s;
7753 if (e - a <= BB_MAX_LEN) {
7754 p[hi] = BB_MAKE(a, e-a, ack);
7755 s = e;
7756 } else {
7757 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
7758 s = a + BB_MAX_LEN;
7759 }
7760 sectors = e - s;
7761 lo = hi;
7762 hi++;
7763 }
7764 }
7765 if (sectors == 0 && hi < bb->count) {
7766 /* we might be able to combine lo and hi */
7767 /* Note: 's' is at the end of 'lo' */
7768 sector_t a = BB_OFFSET(p[hi]);
7769 int lolen = BB_LEN(p[lo]);
7770 int hilen = BB_LEN(p[hi]);
7771 int newlen = lolen + hilen - (s - a);
7772 if (s >= a && newlen < BB_MAX_LEN) {
7773 /* yes, we can combine them */
7774 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
7775 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
7776 memmove(p + hi, p + hi + 1,
7777 (bb->count - hi - 1) * 8);
7778 bb->count--;
7779 }
7780 }
7781 while (sectors) {
7782 /* didn't merge (it all).
7783 * Need to add a range just before 'hi' */
7784 if (bb->count >= MD_MAX_BADBLOCKS) {
7785 /* No room for more */
7786 rv = 0;
7787 break;
7788 } else {
7789 int this_sectors = sectors;
7790 memmove(p + hi + 1, p + hi,
7791 (bb->count - hi) * 8);
7792 bb->count++;
7793
7794 if (this_sectors > BB_MAX_LEN)
7795 this_sectors = BB_MAX_LEN;
7796 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
7797 sectors -= this_sectors;
7798 s += this_sectors;
7799 }
7800 }
7801
7802 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10007803 if (!acknowledged)
7804 bb->unacked_exist = 1;
NeilBrown2230dfe2011-07-28 11:31:46 +10007805 write_sequnlock_irq(&bb->lock);
7806
7807 return rv;
7808}
7809
7810int rdev_set_badblocks(mdk_rdev_t *rdev, sector_t s, int sectors,
7811 int acknowledged)
7812{
7813 int rv = md_set_badblocks(&rdev->badblocks,
7814 s + rdev->data_offset, sectors, acknowledged);
7815 if (rv) {
7816 /* Make sure they get written out promptly */
7817 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
7818 md_wakeup_thread(rdev->mddev->thread);
7819 }
7820 return rv;
7821}
7822EXPORT_SYMBOL_GPL(rdev_set_badblocks);
7823
7824/*
7825 * Remove a range of bad blocks from the table.
7826 * This may involve extending the table if we spilt a region,
7827 * but it must not fail. So if the table becomes full, we just
7828 * drop the remove request.
7829 */
7830static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
7831{
7832 u64 *p;
7833 int lo, hi;
7834 sector_t target = s + sectors;
7835 int rv = 0;
7836
7837 if (bb->shift > 0) {
7838 /* When clearing we round the start up and the end down.
7839 * This should not matter as the shift should align with
7840 * the block size and no rounding should ever be needed.
7841 * However it is better the think a block is bad when it
7842 * isn't than to think a block is not bad when it is.
7843 */
7844 s += (1<<bb->shift) - 1;
7845 s >>= bb->shift;
7846 target >>= bb->shift;
7847 sectors = target - s;
7848 }
7849
7850 write_seqlock_irq(&bb->lock);
7851
7852 p = bb->page;
7853 lo = 0;
7854 hi = bb->count;
7855 /* Find the last range that starts before 'target' */
7856 while (hi - lo > 1) {
7857 int mid = (lo + hi) / 2;
7858 sector_t a = BB_OFFSET(p[mid]);
7859 if (a < target)
7860 lo = mid;
7861 else
7862 hi = mid;
7863 }
7864 if (hi > lo) {
7865 /* p[lo] is the last range that could overlap the
7866 * current range. Earlier ranges could also overlap,
7867 * but only this one can overlap the end of the range.
7868 */
7869 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
7870 /* Partial overlap, leave the tail of this range */
7871 int ack = BB_ACK(p[lo]);
7872 sector_t a = BB_OFFSET(p[lo]);
7873 sector_t end = a + BB_LEN(p[lo]);
7874
7875 if (a < s) {
7876 /* we need to split this range */
7877 if (bb->count >= MD_MAX_BADBLOCKS) {
7878 rv = 0;
7879 goto out;
7880 }
7881 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
7882 bb->count++;
7883 p[lo] = BB_MAKE(a, s-a, ack);
7884 lo++;
7885 }
7886 p[lo] = BB_MAKE(target, end - target, ack);
7887 /* there is no longer an overlap */
7888 hi = lo;
7889 lo--;
7890 }
7891 while (lo >= 0 &&
7892 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7893 /* This range does overlap */
7894 if (BB_OFFSET(p[lo]) < s) {
7895 /* Keep the early parts of this range. */
7896 int ack = BB_ACK(p[lo]);
7897 sector_t start = BB_OFFSET(p[lo]);
7898 p[lo] = BB_MAKE(start, s - start, ack);
7899 /* now low doesn't overlap, so.. */
7900 break;
7901 }
7902 lo--;
7903 }
7904 /* 'lo' is strictly before, 'hi' is strictly after,
7905 * anything between needs to be discarded
7906 */
7907 if (hi - lo > 1) {
7908 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
7909 bb->count -= (hi - lo - 1);
7910 }
7911 }
7912
7913 bb->changed = 1;
7914out:
7915 write_sequnlock_irq(&bb->lock);
7916 return rv;
7917}
7918
7919int rdev_clear_badblocks(mdk_rdev_t *rdev, sector_t s, int sectors)
7920{
7921 return md_clear_badblocks(&rdev->badblocks,
7922 s + rdev->data_offset,
7923 sectors);
7924}
7925EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
7926
7927/*
7928 * Acknowledge all bad blocks in a list.
7929 * This only succeeds if ->changed is clear. It is used by
7930 * in-kernel metadata updates
7931 */
7932void md_ack_all_badblocks(struct badblocks *bb)
7933{
7934 if (bb->page == NULL || bb->changed)
7935 /* no point even trying */
7936 return;
7937 write_seqlock_irq(&bb->lock);
7938
7939 if (bb->changed == 0) {
7940 u64 *p = bb->page;
7941 int i;
7942 for (i = 0; i < bb->count ; i++) {
7943 if (!BB_ACK(p[i])) {
7944 sector_t start = BB_OFFSET(p[i]);
7945 int len = BB_LEN(p[i]);
7946 p[i] = BB_MAKE(start, len, 1);
7947 }
7948 }
NeilBrownde393cd2011-07-28 11:31:48 +10007949 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10007950 }
7951 write_sequnlock_irq(&bb->lock);
7952}
7953EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
7954
NeilBrown16c791a2011-07-28 11:31:47 +10007955/* sysfs access to bad-blocks list.
7956 * We present two files.
7957 * 'bad-blocks' lists sector numbers and lengths of ranges that
7958 * are recorded as bad. The list is truncated to fit within
7959 * the one-page limit of sysfs.
7960 * Writing "sector length" to this file adds an acknowledged
7961 * bad block list.
7962 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
7963 * been acknowledged. Writing to this file adds bad blocks
7964 * without acknowledging them. This is largely for testing.
7965 */
7966
7967static ssize_t
7968badblocks_show(struct badblocks *bb, char *page, int unack)
7969{
7970 size_t len;
7971 int i;
7972 u64 *p = bb->page;
7973 unsigned seq;
7974
7975 if (bb->shift < 0)
7976 return 0;
7977
7978retry:
7979 seq = read_seqbegin(&bb->lock);
7980
7981 len = 0;
7982 i = 0;
7983
7984 while (len < PAGE_SIZE && i < bb->count) {
7985 sector_t s = BB_OFFSET(p[i]);
7986 unsigned int length = BB_LEN(p[i]);
7987 int ack = BB_ACK(p[i]);
7988 i++;
7989
7990 if (unack && ack)
7991 continue;
7992
7993 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
7994 (unsigned long long)s << bb->shift,
7995 length << bb->shift);
7996 }
NeilBrownde393cd2011-07-28 11:31:48 +10007997 if (unack && len == 0)
7998 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10007999
8000 if (read_seqretry(&bb->lock, seq))
8001 goto retry;
8002
8003 return len;
8004}
8005
8006#define DO_DEBUG 1
8007
8008static ssize_t
8009badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8010{
8011 unsigned long long sector;
8012 int length;
8013 char newline;
8014#ifdef DO_DEBUG
8015 /* Allow clearing via sysfs *only* for testing/debugging.
8016 * Normally only a successful write may clear a badblock
8017 */
8018 int clear = 0;
8019 if (page[0] == '-') {
8020 clear = 1;
8021 page++;
8022 }
8023#endif /* DO_DEBUG */
8024
8025 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8026 case 3:
8027 if (newline != '\n')
8028 return -EINVAL;
8029 case 2:
8030 if (length <= 0)
8031 return -EINVAL;
8032 break;
8033 default:
8034 return -EINVAL;
8035 }
8036
8037#ifdef DO_DEBUG
8038 if (clear) {
8039 md_clear_badblocks(bb, sector, length);
8040 return len;
8041 }
8042#endif /* DO_DEBUG */
8043 if (md_set_badblocks(bb, sector, length, !unack))
8044 return len;
8045 else
8046 return -ENOSPC;
8047}
8048
Adrian Bunk75c96f82005-05-05 16:16:09 -07008049static int md_notify_reboot(struct notifier_block *this,
8050 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008051{
8052 struct list_head *tmp;
8053 mddev_t *mddev;
8054
8055 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
8056
8057 printk(KERN_INFO "md: stopping all md devices.\n");
8058
NeilBrown29ac4aa2008-02-06 01:39:58 -08008059 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10008060 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10008061 /* Force a switch to readonly even array
8062 * appears to still be in use. Hence
8063 * the '100'.
8064 */
NeilBrowna4bd82d02010-03-29 13:23:10 +11008065 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10008066 mddev_unlock(mddev);
8067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008068 /*
8069 * certain more exotic SCSI devices are known to be
8070 * volatile wrt too early system reboots. While the
8071 * right place to handle this issue is the given
8072 * driver, we do want to have a safe RAID driver ...
8073 */
8074 mdelay(1000*1);
8075 }
8076 return NOTIFY_DONE;
8077}
8078
Adrian Bunk75c96f82005-05-05 16:16:09 -07008079static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008080 .notifier_call = md_notify_reboot,
8081 .next = NULL,
8082 .priority = INT_MAX, /* before any real devices */
8083};
8084
8085static void md_geninit(void)
8086{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008087 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
8088
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07008089 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008090}
8091
Adrian Bunk75c96f82005-05-05 16:16:09 -07008092static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008093{
Tejun Heoe804ac72010-10-15 15:36:08 +02008094 int ret = -ENOMEM;
8095
Tejun Heoada609e2011-01-25 14:35:54 +01008096 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008097 if (!md_wq)
8098 goto err_wq;
8099
8100 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8101 if (!md_misc_wq)
8102 goto err_misc_wq;
8103
8104 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8105 goto err_md;
8106
8107 if ((ret = register_blkdev(0, "mdp")) < 0)
8108 goto err_mdp;
8109 mdp_major = ret;
8110
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008111 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008112 md_probe, NULL, NULL);
8113 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008114 md_probe, NULL, NULL);
8115
Linus Torvalds1da177e2005-04-16 15:20:36 -07008116 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008117 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008118
8119 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008121
Tejun Heoe804ac72010-10-15 15:36:08 +02008122err_mdp:
8123 unregister_blkdev(MD_MAJOR, "md");
8124err_md:
8125 destroy_workqueue(md_misc_wq);
8126err_misc_wq:
8127 destroy_workqueue(md_wq);
8128err_wq:
8129 return ret;
8130}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008131
8132#ifndef MODULE
8133
8134/*
8135 * Searches all registered partitions for autorun RAID arrays
8136 * at boot time.
8137 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008138
8139static LIST_HEAD(all_detected_devices);
8140struct detected_devices_node {
8141 struct list_head list;
8142 dev_t dev;
8143};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008144
8145void md_autodetect_dev(dev_t dev)
8146{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008147 struct detected_devices_node *node_detected_dev;
8148
8149 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8150 if (node_detected_dev) {
8151 node_detected_dev->dev = dev;
8152 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8153 } else {
8154 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8155 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008157}
8158
8159
8160static void autostart_arrays(int part)
8161{
8162 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008163 struct detected_devices_node *node_detected_dev;
8164 dev_t dev;
8165 int i_scanned, i_passed;
8166
8167 i_scanned = 0;
8168 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008169
8170 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8171
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008172 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8173 i_scanned++;
8174 node_detected_dev = list_entry(all_detected_devices.next,
8175 struct detected_devices_node, list);
8176 list_del(&node_detected_dev->list);
8177 dev = node_detected_dev->dev;
8178 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008179 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008180 if (IS_ERR(rdev))
8181 continue;
8182
NeilBrownb2d444d2005-11-08 21:39:31 -08008183 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008184 MD_BUG();
8185 continue;
8186 }
NeilBrownd0fae182008-03-04 14:29:31 -08008187 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008188 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008189 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008190 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008191
8192 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8193 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008194
8195 autorun_devices(part);
8196}
8197
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008198#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008199
8200static __exit void md_exit(void)
8201{
8202 mddev_t *mddev;
8203 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008204
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008205 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008206 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008207
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008208 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008209 unregister_blkdev(mdp_major, "mdp");
8210 unregister_reboot_notifier(&md_notifier);
8211 unregister_sysctl_table(raid_table_header);
8212 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008213 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008214 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008215 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008216 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008217 destroy_workqueue(md_misc_wq);
8218 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008219}
8220
Dan Williams685784a2007-07-09 11:56:42 -07008221subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008222module_exit(md_exit)
8223
NeilBrownf91de922005-11-08 21:39:36 -08008224static int get_ro(char *buffer, struct kernel_param *kp)
8225{
8226 return sprintf(buffer, "%d", start_readonly);
8227}
8228static int set_ro(const char *val, struct kernel_param *kp)
8229{
8230 char *e;
8231 int num = simple_strtoul(val, &e, 10);
8232 if (*val && (*e == '\0' || *e == '\n')) {
8233 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008234 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008235 }
8236 return -EINVAL;
8237}
8238
NeilBrown80ca3a42006-07-10 04:44:18 -07008239module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8240module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08008241
NeilBrownefeb53c2009-01-09 08:31:10 +11008242module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008243
Linus Torvalds1da177e2005-04-16 15:20:36 -07008244EXPORT_SYMBOL(register_md_personality);
8245EXPORT_SYMBOL(unregister_md_personality);
8246EXPORT_SYMBOL(md_error);
8247EXPORT_SYMBOL(md_done_sync);
8248EXPORT_SYMBOL(md_write_start);
8249EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008250EXPORT_SYMBOL(md_register_thread);
8251EXPORT_SYMBOL(md_unregister_thread);
8252EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008253EXPORT_SYMBOL(md_check_recovery);
8254MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008255MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008256MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008257MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);