blob: 1520d18c5af5437c4df706964307c91c47a834e2 [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 Kim65a06f02011-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;
NeilBrown9f2f383072011-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 Kim65a06f02011-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 Kim65a06f02011-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;
NeilBrown0261cd92009-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 Kim65a06f02011-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;
NeilBrown0261cd92009-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);
NeilBrown0261cd92009-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);
NeilBrown0261cd92009-11-13 17:40:48 +11001370 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd92009-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 Kim65a06f02011-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 Kim65a06f02011-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 Kim65a06f02011-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 Kim65a06f02011-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 Kim65a06f02011-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{
NeilBrown7dd5e7c2007-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)
NeilBrown7dd5e7c2007-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();
NeilBrown7dd5e7c2007-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{
NeilBrown7dd5e7c2007-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 Kim65a06f02011-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 Kim65a06f02011-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);
NeilBrownd97a41d2010-10-28 17:30:20 +11002344 if (!mddev->external)
2345 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002346 wake_up(&mddev->sb_wait);
2347 return;
2348 }
2349
NeilBrowna9701a32005-11-08 21:39:34 -08002350 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002351
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002352 mddev->utime = get_seconds();
2353
NeilBrown850b2b42006-10-03 01:15:46 -07002354 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2355 force_change = 1;
2356 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2357 /* just a clean<-> dirty transition, possibly leave spares alone,
2358 * though if events isn't the right even/odd, we will have to do
2359 * spares after all
2360 */
2361 nospares = 1;
2362 if (force_change)
2363 nospares = 0;
2364 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002365 /* If the array is degraded, then skipping spares is both
2366 * dangerous and fairly pointless.
2367 * Dangerous because a device that was removed from the array
2368 * might have a event_count that still looks up-to-date,
2369 * so it can be re-added without a resync.
2370 * Pointless because if there are any spares to skip,
2371 * then a recovery will happen and soon that array won't
2372 * be degraded any more and the spare can go back to sleep then.
2373 */
NeilBrown850b2b42006-10-03 01:15:46 -07002374 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002375
NeilBrown06d91a52005-06-21 17:17:12 -07002376 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002377
2378 /* If this is just a dirty<->clean transition, and the array is clean
2379 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002380 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002381 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002382 && mddev->can_decrease_events
2383 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002384 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002385 mddev->can_decrease_events = 0;
2386 } else {
NeilBrown42543762006-06-26 00:27:57 -07002387 /* otherwise we have to go forward and ... */
2388 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002389 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
2392 if (!mddev->events) {
2393 /*
2394 * oops, this 64-bit counter should never wrap.
2395 * Either we are in around ~1 trillion A.C., assuming
2396 * 1 reboot per second, or we have a bug:
2397 */
2398 MD_BUG();
2399 mddev->events --;
2400 }
NeilBrown2699b672011-07-28 11:31:47 +10002401
2402 list_for_each_entry(rdev, &mddev->disks, same_set)
2403 if (rdev->badblocks.changed)
2404 any_badblocks_changed++;
2405
NeilBrowne6910632008-02-06 01:39:51 -08002406 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002407 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
2409 dprintk(KERN_INFO
2410 "md: updating %s RAID superblock on device (in sync %d)\n",
2411 mdname(mddev),mddev->in_sync);
2412
NeilBrown4ad13662007-07-17 04:06:13 -07002413 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002414 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 char b[BDEVNAME_SIZE];
2416 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002417 if (rdev->sb_loaded != 1)
2418 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002419 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 dprintk("(skipping faulty ");
2421
2422 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002423 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002424 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002425 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002426 rdev->sb_page);
2427 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2428 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002429 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002430 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002431 if (rdev->badblocks.size) {
2432 md_super_write(mddev, rdev,
2433 rdev->badblocks.sector,
2434 rdev->badblocks.size << 9,
2435 rdev->bb_page);
2436 rdev->badblocks.size = 0;
2437 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 } else
2440 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002441 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 /* only need to write one superblock... */
2443 break;
2444 }
NeilBrowna9701a32005-11-08 21:39:34 -08002445 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002446 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002447
NeilBrowna9701a32005-11-08 21:39:34 -08002448 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002449 if (mddev->in_sync != sync_req ||
2450 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002451 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002452 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002453 goto repeat;
2454 }
NeilBrown850b2b42006-10-03 01:15:46 -07002455 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002456 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002457 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002458 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2459 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002460
NeilBrown2699b672011-07-28 11:31:47 +10002461 if (any_badblocks_changed)
2462 list_for_each_entry(rdev, &mddev->disks, same_set)
2463 md_ack_all_badblocks(&rdev->badblocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464}
2465
Andre Noll7f6ce762008-03-23 18:34:54 +01002466/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002467 * We want to accept with case. For this we use cmd_match.
2468 */
2469static int cmd_match(const char *cmd, const char *str)
2470{
2471 /* See if cmd, written into a sysfs file, matches
2472 * str. They must either be the same, or cmd can
2473 * have a trailing newline
2474 */
2475 while (*cmd && *str && *cmd == *str) {
2476 cmd++;
2477 str++;
2478 }
2479 if (*cmd == '\n')
2480 cmd++;
2481 if (*str || *cmd)
2482 return 0;
2483 return 1;
2484}
2485
NeilBrown86e6ffd2005-11-08 21:39:24 -08002486struct rdev_sysfs_entry {
2487 struct attribute attr;
2488 ssize_t (*show)(mdk_rdev_t *, char *);
2489 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2490};
2491
2492static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002493state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002494{
2495 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002496 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002497
NeilBrownb2d444d2005-11-08 21:39:31 -08002498 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002499 len+= sprintf(page+len, "%sfaulty",sep);
2500 sep = ",";
2501 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002502 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002503 len += sprintf(page+len, "%sin_sync",sep);
2504 sep = ",";
2505 }
NeilBrownf6556752006-06-26 00:28:01 -07002506 if (test_bit(WriteMostly, &rdev->flags)) {
2507 len += sprintf(page+len, "%swrite_mostly",sep);
2508 sep = ",";
2509 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002510 if (test_bit(Blocked, &rdev->flags)) {
2511 len += sprintf(page+len, "%sblocked", sep);
2512 sep = ",";
2513 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002514 if (!test_bit(Faulty, &rdev->flags) &&
2515 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002516 len += sprintf(page+len, "%sspare", sep);
2517 sep = ",";
2518 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002519 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2520 len += sprintf(page+len, "%swrite_error", sep);
2521 sep = ",";
2522 }
NeilBrown86e6ffd2005-11-08 21:39:24 -08002523 return len+sprintf(page+len, "\n");
2524}
2525
NeilBrown45dc2de2006-06-26 00:27:58 -07002526static ssize_t
2527state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2528{
2529 /* can write
2530 * faulty - simulates and error
2531 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002532 * writemostly - sets write_mostly
2533 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002534 * blocked - sets the Blocked flag
2535 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002536 * insync - sets Insync providing device isn't active
NeilBrownd7a9d442011-07-28 11:31:48 +10002537 * write_error - sets WriteErrorSeen
2538 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002539 */
2540 int err = -EINVAL;
2541 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2542 md_error(rdev->mddev, rdev);
2543 err = 0;
2544 } else if (cmd_match(buf, "remove")) {
2545 if (rdev->raid_disk >= 0)
2546 err = -EBUSY;
2547 else {
2548 mddev_t *mddev = rdev->mddev;
2549 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002550 if (mddev->pers)
2551 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002552 md_new_event(mddev);
2553 err = 0;
2554 }
NeilBrownf6556752006-06-26 00:28:01 -07002555 } else if (cmd_match(buf, "writemostly")) {
2556 set_bit(WriteMostly, &rdev->flags);
2557 err = 0;
2558 } else if (cmd_match(buf, "-writemostly")) {
2559 clear_bit(WriteMostly, &rdev->flags);
2560 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002561 } else if (cmd_match(buf, "blocked")) {
2562 set_bit(Blocked, &rdev->flags);
2563 err = 0;
2564 } else if (cmd_match(buf, "-blocked")) {
2565 clear_bit(Blocked, &rdev->flags);
2566 wake_up(&rdev->blocked_wait);
2567 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2568 md_wakeup_thread(rdev->mddev->thread);
2569
2570 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002571 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2572 set_bit(In_sync, &rdev->flags);
2573 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002574 } else if (cmd_match(buf, "write_error")) {
2575 set_bit(WriteErrorSeen, &rdev->flags);
2576 err = 0;
2577 } else if (cmd_match(buf, "-write_error")) {
2578 clear_bit(WriteErrorSeen, &rdev->flags);
2579 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002580 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002581 if (!err)
2582 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002583 return err ? err : len;
2584}
NeilBrown80ca3a42006-07-10 04:44:18 -07002585static struct rdev_sysfs_entry rdev_state =
2586__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002587
2588static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002589errors_show(mdk_rdev_t *rdev, char *page)
2590{
2591 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2592}
2593
2594static ssize_t
2595errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2596{
2597 char *e;
2598 unsigned long n = simple_strtoul(buf, &e, 10);
2599 if (*buf && (*e == 0 || *e == '\n')) {
2600 atomic_set(&rdev->corrected_errors, n);
2601 return len;
2602 }
2603 return -EINVAL;
2604}
2605static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002606__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002607
NeilBrown014236d2006-01-06 00:20:55 -08002608static ssize_t
2609slot_show(mdk_rdev_t *rdev, char *page)
2610{
2611 if (rdev->raid_disk < 0)
2612 return sprintf(page, "none\n");
2613 else
2614 return sprintf(page, "%d\n", rdev->raid_disk);
2615}
2616
2617static ssize_t
2618slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2619{
2620 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002621 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002622 int slot = simple_strtoul(buf, &e, 10);
2623 if (strncmp(buf, "none", 4)==0)
2624 slot = -1;
2625 else if (e==buf || (*e && *e!= '\n'))
2626 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002627 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002628 /* Setting 'slot' on an active array requires also
2629 * updating the 'rd%d' link, and communicating
2630 * with the personality with ->hot_*_disk.
2631 * For now we only support removing
2632 * failed/spare devices. This normally happens automatically,
2633 * but not when the metadata is externally managed.
2634 */
NeilBrownc303da62008-02-06 01:39:51 -08002635 if (rdev->raid_disk == -1)
2636 return -EEXIST;
2637 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002638 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002639 return -EINVAL;
2640 err = rdev->mddev->pers->
2641 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2642 if (err)
2643 return err;
Namhyung Kim36fad852011-07-27 11:00:36 +10002644 sysfs_unlink_rdev(rdev->mddev, rdev);
Maciej Trelab7103102010-04-14 16:58:16 +10002645 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002646 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2647 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002648 } else if (rdev->mddev->pers) {
2649 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002650 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002651 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002652 */
2653
2654 if (rdev->raid_disk != -1)
2655 return -EBUSY;
2656
NeilBrownc6751b22011-02-02 11:57:13 +11002657 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2658 return -EBUSY;
2659
Neil Brown6c2fce22008-06-28 08:31:31 +10002660 if (rdev->mddev->pers->hot_add_disk == NULL)
2661 return -EINVAL;
2662
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002663 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002664 if (rdev2->raid_disk == slot)
2665 return -EEXIST;
2666
NeilBrownba1b41b2011-01-14 09:14:34 +11002667 if (slot >= rdev->mddev->raid_disks &&
2668 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2669 return -ENOSPC;
2670
Neil Brown6c2fce22008-06-28 08:31:31 +10002671 rdev->raid_disk = slot;
2672 if (test_bit(In_sync, &rdev->flags))
2673 rdev->saved_raid_disk = slot;
2674 else
2675 rdev->saved_raid_disk = -1;
2676 err = rdev->mddev->pers->
2677 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002678 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002679 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002680 return err;
Neil Brown52664732008-06-28 08:31:44 +10002681 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002682 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002683 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002684 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002685 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002686 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002687 if (slot >= rdev->mddev->raid_disks &&
2688 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002689 return -ENOSPC;
2690 rdev->raid_disk = slot;
2691 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002692 clear_bit(Faulty, &rdev->flags);
2693 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002694 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002695 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002696 }
NeilBrown014236d2006-01-06 00:20:55 -08002697 return len;
2698}
2699
2700
2701static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002702__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002703
NeilBrown93c8cad2006-01-06 00:20:56 -08002704static ssize_t
2705offset_show(mdk_rdev_t *rdev, char *page)
2706{
Andrew Morton6961ece2006-01-06 00:20:59 -08002707 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002708}
2709
2710static ssize_t
2711offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2712{
2713 char *e;
2714 unsigned long long offset = simple_strtoull(buf, &e, 10);
2715 if (e==buf || (*e && *e != '\n'))
2716 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002717 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002718 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002719 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002720 /* Must set offset before size, so overlap checks
2721 * can be sane */
2722 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002723 rdev->data_offset = offset;
2724 return len;
2725}
2726
2727static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002728__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002729
NeilBrown83303b62006-01-06 00:21:06 -08002730static ssize_t
2731rdev_size_show(mdk_rdev_t *rdev, char *page)
2732{
Andre Nolldd8ac332009-03-31 14:33:13 +11002733 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002734}
2735
NeilBrownc5d79ad2008-02-06 01:39:54 -08002736static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2737{
2738 /* check if two start/length pairs overlap */
2739 if (s1+l1 <= s2)
2740 return 0;
2741 if (s2+l2 <= s1)
2742 return 0;
2743 return 1;
2744}
2745
Dan Williamsb522adc2009-03-31 15:00:31 +11002746static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2747{
2748 unsigned long long blocks;
2749 sector_t new;
2750
2751 if (strict_strtoull(buf, 10, &blocks) < 0)
2752 return -EINVAL;
2753
2754 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2755 return -EINVAL; /* sector conversion overflow */
2756
2757 new = blocks * 2;
2758 if (new != blocks * 2)
2759 return -EINVAL; /* unsigned long long to sector_t overflow */
2760
2761 *sectors = new;
2762 return 0;
2763}
2764
NeilBrown83303b62006-01-06 00:21:06 -08002765static ssize_t
2766rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2767{
NeilBrown27c529b2008-03-04 14:29:33 -08002768 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002769 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002770 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002771
Dan Williamsb522adc2009-03-31 15:00:31 +11002772 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002773 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002774 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002775 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002776 sectors = super_types[my_mddev->major_version].
2777 rdev_size_change(rdev, sectors);
2778 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002779 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002780 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002781 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002782 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002783 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002784 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002785 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002786
Andre Nolldd8ac332009-03-31 14:33:13 +11002787 rdev->sectors = sectors;
2788 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002789 /* need to check that all other rdevs with the same ->bdev
2790 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002791 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002792 * we have to change it back, we will have the lock again.
2793 */
2794 mddev_t *mddev;
2795 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002796 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002797
NeilBrown27c529b2008-03-04 14:29:33 -08002798 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002799 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002800 mdk_rdev_t *rdev2;
2801
2802 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002803 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002804 if (rdev->bdev == rdev2->bdev &&
2805 rdev != rdev2 &&
2806 overlaps(rdev->data_offset, rdev->sectors,
2807 rdev2->data_offset,
2808 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002809 overlap = 1;
2810 break;
2811 }
2812 mddev_unlock(mddev);
2813 if (overlap) {
2814 mddev_put(mddev);
2815 break;
2816 }
2817 }
NeilBrown27c529b2008-03-04 14:29:33 -08002818 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002819 if (overlap) {
2820 /* Someone else could have slipped in a size
2821 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002822 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002823 * safe, and trust userspace not to race with
2824 * itself
2825 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002826 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002827 return -EBUSY;
2828 }
2829 }
NeilBrown83303b62006-01-06 00:21:06 -08002830 return len;
2831}
2832
2833static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002834__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002835
Dan Williams06e3c812009-12-12 21:17:12 -07002836
2837static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2838{
2839 unsigned long long recovery_start = rdev->recovery_offset;
2840
2841 if (test_bit(In_sync, &rdev->flags) ||
2842 recovery_start == MaxSector)
2843 return sprintf(page, "none\n");
2844
2845 return sprintf(page, "%llu\n", recovery_start);
2846}
2847
2848static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2849{
2850 unsigned long long recovery_start;
2851
2852 if (cmd_match(buf, "none"))
2853 recovery_start = MaxSector;
2854 else if (strict_strtoull(buf, 10, &recovery_start))
2855 return -EINVAL;
2856
2857 if (rdev->mddev->pers &&
2858 rdev->raid_disk >= 0)
2859 return -EBUSY;
2860
2861 rdev->recovery_offset = recovery_start;
2862 if (recovery_start == MaxSector)
2863 set_bit(In_sync, &rdev->flags);
2864 else
2865 clear_bit(In_sync, &rdev->flags);
2866 return len;
2867}
2868
2869static struct rdev_sysfs_entry rdev_recovery_start =
2870__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2871
NeilBrown16c791a2011-07-28 11:31:47 +10002872
2873static ssize_t
2874badblocks_show(struct badblocks *bb, char *page, int unack);
2875static ssize_t
2876badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
2877
2878static ssize_t bb_show(mdk_rdev_t *rdev, char *page)
2879{
2880 return badblocks_show(&rdev->badblocks, page, 0);
2881}
2882static ssize_t bb_store(mdk_rdev_t *rdev, const char *page, size_t len)
2883{
2884 return badblocks_store(&rdev->badblocks, page, len, 0);
2885}
2886static struct rdev_sysfs_entry rdev_bad_blocks =
2887__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
2888
2889
2890static ssize_t ubb_show(mdk_rdev_t *rdev, char *page)
2891{
2892 return badblocks_show(&rdev->badblocks, page, 1);
2893}
2894static ssize_t ubb_store(mdk_rdev_t *rdev, const char *page, size_t len)
2895{
2896 return badblocks_store(&rdev->badblocks, page, len, 1);
2897}
2898static struct rdev_sysfs_entry rdev_unack_bad_blocks =
2899__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
2900
NeilBrown86e6ffd2005-11-08 21:39:24 -08002901static struct attribute *rdev_default_attrs[] = {
2902 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002903 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002904 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002905 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002906 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002907 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10002908 &rdev_bad_blocks.attr,
2909 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002910 NULL,
2911};
2912static ssize_t
2913rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2914{
2915 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2916 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002917 mddev_t *mddev = rdev->mddev;
2918 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002919
2920 if (!entry->show)
2921 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002922
2923 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2924 if (!rv) {
2925 if (rdev->mddev == NULL)
2926 rv = -EBUSY;
2927 else
2928 rv = entry->show(rdev, page);
2929 mddev_unlock(mddev);
2930 }
2931 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002932}
2933
2934static ssize_t
2935rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2936 const char *page, size_t length)
2937{
2938 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2939 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002940 ssize_t rv;
2941 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002942
2943 if (!entry->store)
2944 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002945 if (!capable(CAP_SYS_ADMIN))
2946 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002947 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002948 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002949 if (rdev->mddev == NULL)
2950 rv = -EBUSY;
2951 else
2952 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002953 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002954 }
2955 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002956}
2957
2958static void rdev_free(struct kobject *ko)
2959{
2960 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2961 kfree(rdev);
2962}
Emese Revfy52cf25d2010-01-19 02:58:23 +01002963static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002964 .show = rdev_attr_show,
2965 .store = rdev_attr_store,
2966};
2967static struct kobj_type rdev_ktype = {
2968 .release = rdev_free,
2969 .sysfs_ops = &rdev_sysfs_ops,
2970 .default_attrs = rdev_default_attrs,
2971};
2972
NeilBrown2230dfe2011-07-28 11:31:46 +10002973int md_rdev_init(mdk_rdev_t *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10002974{
2975 rdev->desc_nr = -1;
2976 rdev->saved_raid_disk = -1;
2977 rdev->raid_disk = -1;
2978 rdev->flags = 0;
2979 rdev->data_offset = 0;
2980 rdev->sb_events = 0;
2981 rdev->last_read_error.tv_sec = 0;
2982 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002983 rdev->sb_loaded = 0;
2984 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10002985 atomic_set(&rdev->nr_pending, 0);
2986 atomic_set(&rdev->read_errors, 0);
2987 atomic_set(&rdev->corrected_errors, 0);
2988
2989 INIT_LIST_HEAD(&rdev->same_set);
2990 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10002991
2992 /* Add space to store bad block list.
2993 * This reserves the space even on arrays where it cannot
2994 * be used - I wonder if that matters
2995 */
2996 rdev->badblocks.count = 0;
2997 rdev->badblocks.shift = 0;
2998 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
2999 seqlock_init(&rdev->badblocks.lock);
3000 if (rdev->badblocks.page == NULL)
3001 return -ENOMEM;
3002
3003 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003004}
3005EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006/*
3007 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3008 *
3009 * mark the device faulty if:
3010 *
3011 * - the device is nonexistent (zero size)
3012 * - the device has no valid superblock
3013 *
3014 * a faulty rdev _never_ has rdev->sb set.
3015 */
3016static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
3017{
3018 char b[BDEVNAME_SIZE];
3019 int err;
3020 mdk_rdev_t *rdev;
3021 sector_t size;
3022
NeilBrown9ffae0c2006-01-06 00:20:32 -08003023 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 if (!rdev) {
3025 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3026 return ERR_PTR(-ENOMEM);
3027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
NeilBrown2230dfe2011-07-28 11:31:46 +10003029 err = md_rdev_init(rdev);
3030 if (err)
3031 goto abort_free;
3032 err = alloc_disk_sb(rdev);
3033 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 goto abort_free;
3035
NeilBrownc5d79ad2008-02-06 01:39:54 -08003036 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 if (err)
3038 goto abort_free;
3039
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003040 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003041
Mike Snitzer77304d22010-11-08 14:39:12 +01003042 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 if (!size) {
3044 printk(KERN_WARNING
3045 "md: %s has zero or unknown size, marking faulty!\n",
3046 bdevname(rdev->bdev,b));
3047 err = -EINVAL;
3048 goto abort_free;
3049 }
3050
3051 if (super_format >= 0) {
3052 err = super_types[super_format].
3053 load_super(rdev, NULL, super_minor);
3054 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003055 printk(KERN_WARNING
3056 "md: %s does not have a valid v%d.%d "
3057 "superblock, not importing!\n",
3058 bdevname(rdev->bdev,b),
3059 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 goto abort_free;
3061 }
3062 if (err < 0) {
3063 printk(KERN_WARNING
3064 "md: could not read %s's sb, not importing!\n",
3065 bdevname(rdev->bdev,b));
3066 goto abort_free;
3067 }
3068 }
NeilBrown9f2f383072011-07-28 11:31:47 +10003069 if (super_format == -1)
3070 /* hot-add for 0.90, or non-persistent: so no badblocks */
3071 rdev->badblocks.shift = -1;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 return rdev;
3074
3075abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003076 if (rdev->bdev)
3077 unlock_rdev(rdev);
3078 free_disk_sb(rdev);
NeilBrown2230dfe2011-07-28 11:31:46 +10003079 kfree(rdev->badblocks.page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 kfree(rdev);
3081 return ERR_PTR(err);
3082}
3083
3084/*
3085 * Check a full RAID array for plausibility
3086 */
3087
3088
NeilBrowna757e642005-04-16 15:26:42 -07003089static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090{
3091 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003092 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 char b[BDEVNAME_SIZE];
3094
3095 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08003096 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 switch (super_types[mddev->major_version].
3098 load_super(rdev, freshest, mddev->minor_version)) {
3099 case 1:
3100 freshest = rdev;
3101 break;
3102 case 0:
3103 break;
3104 default:
3105 printk( KERN_ERR \
3106 "md: fatal superblock inconsistency in %s"
3107 " -- removing from array\n",
3108 bdevname(rdev->bdev,b));
3109 kick_rdev_from_array(rdev);
3110 }
3111
3112
3113 super_types[mddev->major_version].
3114 validate_super(mddev, freshest);
3115
3116 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003117 rdev_for_each(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003118 if (mddev->max_disks &&
3119 (rdev->desc_nr >= mddev->max_disks ||
3120 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003121 printk(KERN_WARNING
3122 "md: %s: %s: only %d devices permitted\n",
3123 mdname(mddev), bdevname(rdev->bdev, b),
3124 mddev->max_disks);
3125 kick_rdev_from_array(rdev);
3126 continue;
3127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 if (rdev != freshest)
3129 if (super_types[mddev->major_version].
3130 validate_super(mddev, rdev)) {
3131 printk(KERN_WARNING "md: kicking non-fresh %s"
3132 " from array!\n",
3133 bdevname(rdev->bdev,b));
3134 kick_rdev_from_array(rdev);
3135 continue;
3136 }
3137 if (mddev->level == LEVEL_MULTIPATH) {
3138 rdev->desc_nr = i++;
3139 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003140 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003141 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003142 rdev->raid_disk = -1;
3143 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 }
3145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146}
3147
NeilBrown72e02072009-12-14 12:49:55 +11003148/* Read a fixed-point number.
3149 * Numbers in sysfs attributes should be in "standard" units where
3150 * possible, so time should be in seconds.
3151 * However we internally use a a much smaller unit such as
3152 * milliseconds or jiffies.
3153 * This function takes a decimal number with a possible fractional
3154 * component, and produces an integer which is the result of
3155 * multiplying that number by 10^'scale'.
3156 * all without any floating-point arithmetic.
3157 */
3158int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3159{
3160 unsigned long result = 0;
3161 long decimals = -1;
3162 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3163 if (*cp == '.')
3164 decimals = 0;
3165 else if (decimals < scale) {
3166 unsigned int value;
3167 value = *cp - '0';
3168 result = result * 10 + value;
3169 if (decimals >= 0)
3170 decimals++;
3171 }
3172 cp++;
3173 }
3174 if (*cp == '\n')
3175 cp++;
3176 if (*cp)
3177 return -EINVAL;
3178 if (decimals < 0)
3179 decimals = 0;
3180 while (decimals < scale) {
3181 result *= 10;
3182 decimals ++;
3183 }
3184 *res = result;
3185 return 0;
3186}
3187
3188
NeilBrown19052c02008-08-05 15:54:13 +10003189static void md_safemode_timeout(unsigned long data);
3190
NeilBrowneae17012005-11-08 21:39:23 -08003191static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07003192safe_delay_show(mddev_t *mddev, char *page)
3193{
3194 int msec = (mddev->safemode_delay*1000)/HZ;
3195 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3196}
3197static ssize_t
3198safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
3199{
NeilBrown16f17b32006-06-26 00:27:37 -07003200 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003201
NeilBrown72e02072009-12-14 12:49:55 +11003202 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003203 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003204 if (msec == 0)
3205 mddev->safemode_delay = 0;
3206 else {
NeilBrown19052c02008-08-05 15:54:13 +10003207 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003208 mddev->safemode_delay = (msec*HZ)/1000;
3209 if (mddev->safemode_delay == 0)
3210 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10003211 if (mddev->safemode_delay < old_delay)
3212 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003213 }
3214 return len;
3215}
3216static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003217__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003218
3219static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003220level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003221{
NeilBrown2604b702006-01-06 00:20:36 -08003222 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003223 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003224 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003225 else if (mddev->clevel[0])
3226 return sprintf(page, "%s\n", mddev->clevel);
3227 else if (mddev->level != LEVEL_NONE)
3228 return sprintf(page, "%d\n", mddev->level);
3229 else
3230 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003231}
3232
NeilBrownd9d166c2006-01-06 00:20:51 -08003233static ssize_t
3234level_store(mddev_t *mddev, const char *buf, size_t len)
3235{
Dan Williamsf2859af2010-05-02 10:04:16 -07003236 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003237 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11003238 struct mdk_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003239 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003240 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10003241 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003242
3243 if (mddev->pers == NULL) {
3244 if (len == 0)
3245 return 0;
3246 if (len >= sizeof(mddev->clevel))
3247 return -ENOSPC;
3248 strncpy(mddev->clevel, buf, len);
3249 if (mddev->clevel[len-1] == '\n')
3250 len--;
3251 mddev->clevel[len] = 0;
3252 mddev->level = LEVEL_NONE;
3253 return rv;
3254 }
3255
3256 /* request to change the personality. Need to ensure:
3257 * - array is not engaged in resync/recovery/reshape
3258 * - old personality can be suspended
3259 * - new personality will access other array.
3260 */
3261
NeilBrownbb4f1e92010-08-08 21:18:03 +10003262 if (mddev->sync_thread ||
3263 mddev->reshape_position != MaxSector ||
3264 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003265 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003266
3267 if (!mddev->pers->quiesce) {
3268 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3269 mdname(mddev), mddev->pers->name);
3270 return -EINVAL;
3271 }
3272
3273 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003274 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003275 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003276 strncpy(clevel, buf, len);
3277 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003278 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003279 clevel[len] = 0;
3280 if (strict_strtol(clevel, 10, &level))
3281 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003282
Dan Williamsf2859af2010-05-02 10:04:16 -07003283 if (request_module("md-%s", clevel) != 0)
3284 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003285 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003286 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003287 if (!pers || !try_module_get(pers->owner)) {
3288 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003289 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003290 return -EINVAL;
3291 }
3292 spin_unlock(&pers_lock);
3293
3294 if (pers == mddev->pers) {
3295 /* Nothing to do! */
3296 module_put(pers->owner);
3297 return rv;
3298 }
3299 if (!pers->takeover) {
3300 module_put(pers->owner);
3301 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003302 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003303 return -EINVAL;
3304 }
3305
NeilBrowne93f68a2010-06-15 09:36:03 +01003306 list_for_each_entry(rdev, &mddev->disks, same_set)
3307 rdev->new_raid_disk = rdev->raid_disk;
3308
NeilBrown245f46c2009-03-31 14:39:39 +11003309 /* ->takeover must set new_* and/or delta_disks
3310 * if it succeeds, and may set them when it fails.
3311 */
3312 priv = pers->takeover(mddev);
3313 if (IS_ERR(priv)) {
3314 mddev->new_level = mddev->level;
3315 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003316 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003317 mddev->raid_disks -= mddev->delta_disks;
3318 mddev->delta_disks = 0;
3319 module_put(pers->owner);
3320 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003321 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003322 return PTR_ERR(priv);
3323 }
3324
3325 /* Looks like we have a winner */
3326 mddev_suspend(mddev);
3327 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003328
3329 if (mddev->pers->sync_request == NULL &&
3330 pers->sync_request != NULL) {
3331 /* need to add the md_redundancy_group */
3332 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3333 printk(KERN_WARNING
3334 "md: cannot register extra attributes for %s\n",
3335 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003336 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003337 }
3338 if (mddev->pers->sync_request != NULL &&
3339 pers->sync_request == NULL) {
3340 /* need to remove the md_redundancy_group */
3341 if (mddev->to_remove == NULL)
3342 mddev->to_remove = &md_redundancy_group;
3343 }
3344
Trela Maciej54071b32010-03-08 16:02:42 +11003345 if (mddev->pers->sync_request == NULL &&
3346 mddev->external) {
3347 /* We are converting from a no-redundancy array
3348 * to a redundancy array and metadata is managed
3349 * externally so we need to be sure that writes
3350 * won't block due to a need to transition
3351 * clean->dirty
3352 * until external management is started.
3353 */
3354 mddev->in_sync = 0;
3355 mddev->safemode_delay = 0;
3356 mddev->safemode = 0;
3357 }
3358
NeilBrowne93f68a2010-06-15 09:36:03 +01003359 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003360 if (rdev->raid_disk < 0)
3361 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003362 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003363 rdev->new_raid_disk = -1;
3364 if (rdev->new_raid_disk == rdev->raid_disk)
3365 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003366 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003367 }
3368 list_for_each_entry(rdev, &mddev->disks, same_set) {
3369 if (rdev->raid_disk < 0)
3370 continue;
3371 if (rdev->new_raid_disk == rdev->raid_disk)
3372 continue;
3373 rdev->raid_disk = rdev->new_raid_disk;
3374 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003375 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003376 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003377 if (sysfs_link_rdev(mddev, rdev))
3378 printk(KERN_WARNING "md: cannot register rd%d"
3379 " for %s after level change\n",
3380 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003381 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003382 }
3383
3384 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003385 mddev->pers = pers;
3386 mddev->private = priv;
3387 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3388 mddev->level = mddev->new_level;
3389 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003390 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003391 mddev->delta_disks = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003392 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003393 if (mddev->pers->sync_request == NULL) {
3394 /* this is now an array without redundancy, so
3395 * it must always be in_sync
3396 */
3397 mddev->in_sync = 1;
3398 del_timer_sync(&mddev->safemode_timer);
3399 }
NeilBrown245f46c2009-03-31 14:39:39 +11003400 pers->run(mddev);
3401 mddev_resume(mddev);
3402 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3403 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3404 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003405 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003406 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003407 return rv;
3408}
3409
3410static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003411__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003412
NeilBrownd4dbd022006-06-26 00:27:59 -07003413
3414static ssize_t
3415layout_show(mddev_t *mddev, char *page)
3416{
3417 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003418 if (mddev->reshape_position != MaxSector &&
3419 mddev->layout != mddev->new_layout)
3420 return sprintf(page, "%d (%d)\n",
3421 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003422 return sprintf(page, "%d\n", mddev->layout);
3423}
3424
3425static ssize_t
3426layout_store(mddev_t *mddev, const char *buf, size_t len)
3427{
3428 char *e;
3429 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003430
3431 if (!*buf || (*e && *e != '\n'))
3432 return -EINVAL;
3433
NeilBrownb3546032009-03-31 14:56:41 +11003434 if (mddev->pers) {
3435 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003436 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003437 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003438 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003439 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003440 if (err) {
3441 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003442 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003443 }
NeilBrownb3546032009-03-31 14:56:41 +11003444 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003445 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003446 if (mddev->reshape_position == MaxSector)
3447 mddev->layout = n;
3448 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003449 return len;
3450}
3451static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003452__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003453
3454
NeilBrowneae17012005-11-08 21:39:23 -08003455static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003456raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003457{
NeilBrownbb636542005-11-08 21:39:45 -08003458 if (mddev->raid_disks == 0)
3459 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003460 if (mddev->reshape_position != MaxSector &&
3461 mddev->delta_disks != 0)
3462 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3463 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003464 return sprintf(page, "%d\n", mddev->raid_disks);
3465}
3466
NeilBrownda943b92006-01-06 00:20:54 -08003467static int update_raid_disks(mddev_t *mddev, int raid_disks);
3468
3469static ssize_t
3470raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3471{
NeilBrownda943b92006-01-06 00:20:54 -08003472 char *e;
3473 int rv = 0;
3474 unsigned long n = simple_strtoul(buf, &e, 10);
3475
3476 if (!*buf || (*e && *e != '\n'))
3477 return -EINVAL;
3478
3479 if (mddev->pers)
3480 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003481 else if (mddev->reshape_position != MaxSector) {
3482 int olddisks = mddev->raid_disks - mddev->delta_disks;
3483 mddev->delta_disks = n - olddisks;
3484 mddev->raid_disks = n;
3485 } else
NeilBrownda943b92006-01-06 00:20:54 -08003486 mddev->raid_disks = n;
3487 return rv ? rv : len;
3488}
3489static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003490__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003491
NeilBrown24dd4692005-11-08 21:39:26 -08003492static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003493chunk_size_show(mddev_t *mddev, char *page)
3494{
NeilBrown08a02ec2007-05-09 02:35:38 -07003495 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003496 mddev->chunk_sectors != mddev->new_chunk_sectors)
3497 return sprintf(page, "%d (%d)\n",
3498 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003499 mddev->chunk_sectors << 9);
3500 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003501}
3502
3503static ssize_t
3504chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3505{
NeilBrown3b343802006-01-06 00:20:47 -08003506 char *e;
3507 unsigned long n = simple_strtoul(buf, &e, 10);
3508
NeilBrown3b343802006-01-06 00:20:47 -08003509 if (!*buf || (*e && *e != '\n'))
3510 return -EINVAL;
3511
NeilBrownb3546032009-03-31 14:56:41 +11003512 if (mddev->pers) {
3513 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003514 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003515 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003516 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003517 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003518 if (err) {
3519 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003520 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003521 }
NeilBrownb3546032009-03-31 14:56:41 +11003522 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003523 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003524 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003525 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003526 }
NeilBrown3b343802006-01-06 00:20:47 -08003527 return len;
3528}
3529static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003530__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003531
NeilBrowna94213b2006-06-26 00:28:00 -07003532static ssize_t
3533resync_start_show(mddev_t *mddev, char *page)
3534{
NeilBrownd1a7c502009-03-31 15:24:32 +11003535 if (mddev->recovery_cp == MaxSector)
3536 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003537 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3538}
3539
3540static ssize_t
3541resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3542{
NeilBrowna94213b2006-06-26 00:28:00 -07003543 char *e;
3544 unsigned long long n = simple_strtoull(buf, &e, 10);
3545
NeilBrownb0986362011-05-11 15:52:21 +10003546 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003547 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003548 if (cmd_match(buf, "none"))
3549 n = MaxSector;
3550 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003551 return -EINVAL;
3552
3553 mddev->recovery_cp = n;
3554 return len;
3555}
3556static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003557__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003558
NeilBrown9e653b62006-06-26 00:27:58 -07003559/*
3560 * The array state can be:
3561 *
3562 * clear
3563 * No devices, no size, no level
3564 * Equivalent to STOP_ARRAY ioctl
3565 * inactive
3566 * May have some settings, but array is not active
3567 * all IO results in error
3568 * When written, doesn't tear down array, but just stops it
3569 * suspended (not supported yet)
3570 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003571 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003572 * readonly
3573 * no resync can happen. no superblocks get written.
3574 * write requests fail
3575 * read-auto
3576 * like readonly, but behaves like 'clean' on a write request.
3577 *
3578 * clean - no pending writes, but otherwise active.
3579 * When written to inactive array, starts without resync
3580 * If a write request arrives then
3581 * if metadata is known, mark 'dirty' and switch to 'active'.
3582 * if not known, block and switch to write-pending
3583 * If written to an active array that has pending writes, then fails.
3584 * active
3585 * fully active: IO and resync can be happening.
3586 * When written to inactive array, starts with resync
3587 *
3588 * write-pending
3589 * clean, but writes are blocked waiting for 'active' to be written.
3590 *
3591 * active-idle
3592 * like active, but no writes have been seen for a while (100msec).
3593 *
3594 */
3595enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3596 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003597static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003598 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3599 "write-pending", "active-idle", NULL };
3600
3601static int match_word(const char *word, char **list)
3602{
3603 int n;
3604 for (n=0; list[n]; n++)
3605 if (cmd_match(word, list[n]))
3606 break;
3607 return n;
3608}
3609
3610static ssize_t
3611array_state_show(mddev_t *mddev, char *page)
3612{
3613 enum array_state st = inactive;
3614
3615 if (mddev->pers)
3616 switch(mddev->ro) {
3617 case 1:
3618 st = readonly;
3619 break;
3620 case 2:
3621 st = read_auto;
3622 break;
3623 case 0:
3624 if (mddev->in_sync)
3625 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003626 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003627 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003628 else if (mddev->safemode)
3629 st = active_idle;
3630 else
3631 st = active;
3632 }
3633 else {
3634 if (list_empty(&mddev->disks) &&
3635 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003636 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003637 st = clear;
3638 else
3639 st = inactive;
3640 }
3641 return sprintf(page, "%s\n", array_states[st]);
3642}
3643
Neil Browndf5b20c2008-07-11 22:02:22 +10003644static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrowna4bd82d2010-03-29 13:23:10 +11003645static int md_set_readonly(mddev_t * mddev, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003646static int do_md_run(mddev_t * mddev);
3647static int restart_array(mddev_t *mddev);
3648
3649static ssize_t
3650array_state_store(mddev_t *mddev, const char *buf, size_t len)
3651{
3652 int err = -EINVAL;
3653 enum array_state st = match_word(buf, array_states);
3654 switch(st) {
3655 case bad_word:
3656 break;
3657 case clear:
3658 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003659 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003660 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003661 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003662 break;
3663 case inactive:
3664 /* stopping an active array */
3665 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003666 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003667 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003668 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003669 } else
3670 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003671 break;
3672 case suspended:
3673 break; /* not supported yet */
3674 case readonly:
3675 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003676 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003677 else {
3678 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003679 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003680 err = do_md_run(mddev);
3681 }
3682 break;
3683 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003684 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003685 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003686 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003687 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003688 err = restart_array(mddev);
3689 if (err == 0) {
3690 mddev->ro = 2;
3691 set_disk_ro(mddev->gendisk, 0);
3692 }
NeilBrown9e653b62006-06-26 00:27:58 -07003693 } else {
3694 mddev->ro = 2;
3695 err = do_md_run(mddev);
3696 }
3697 break;
3698 case clean:
3699 if (mddev->pers) {
3700 restart_array(mddev);
3701 spin_lock_irq(&mddev->write_lock);
3702 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003703 if (mddev->in_sync == 0) {
3704 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003705 if (mddev->safemode == 1)
3706 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003707 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003708 }
3709 err = 0;
3710 } else
3711 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003712 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003713 } else
3714 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003715 break;
3716 case active:
3717 if (mddev->pers) {
3718 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003719 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003720 wake_up(&mddev->sb_wait);
3721 err = 0;
3722 } else {
3723 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003724 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003725 err = do_md_run(mddev);
3726 }
3727 break;
3728 case write_pending:
3729 case active_idle:
3730 /* these cannot be set */
3731 break;
3732 }
3733 if (err)
3734 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003735 else {
NeilBrown00bcb4a2010-06-01 19:37:23 +10003736 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003737 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003738 }
NeilBrown9e653b62006-06-26 00:27:58 -07003739}
NeilBrown80ca3a42006-07-10 04:44:18 -07003740static struct md_sysfs_entry md_array_state =
3741__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003742
NeilBrown6d7ff732006-01-06 00:21:16 -08003743static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003744max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3745 return sprintf(page, "%d\n",
3746 atomic_read(&mddev->max_corr_read_errors));
3747}
3748
3749static ssize_t
3750max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3751{
3752 char *e;
3753 unsigned long n = simple_strtoul(buf, &e, 10);
3754
3755 if (*buf && (*e == 0 || *e == '\n')) {
3756 atomic_set(&mddev->max_corr_read_errors, n);
3757 return len;
3758 }
3759 return -EINVAL;
3760}
3761
3762static struct md_sysfs_entry max_corr_read_errors =
3763__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3764 max_corrected_read_errors_store);
3765
3766static ssize_t
NeilBrown6d7ff732006-01-06 00:21:16 -08003767null_show(mddev_t *mddev, char *page)
3768{
3769 return -EINVAL;
3770}
3771
3772static ssize_t
3773new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3774{
3775 /* buf must be %d:%d\n? giving major and minor numbers */
3776 /* The new device is added to the array.
3777 * If the array has a persistent superblock, we read the
3778 * superblock to initialise info and check validity.
3779 * Otherwise, only checking done is that in bind_rdev_to_array,
3780 * which mainly checks size.
3781 */
3782 char *e;
3783 int major = simple_strtoul(buf, &e, 10);
3784 int minor;
3785 dev_t dev;
3786 mdk_rdev_t *rdev;
3787 int err;
3788
3789 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3790 return -EINVAL;
3791 minor = simple_strtoul(e+1, &e, 10);
3792 if (*e && *e != '\n')
3793 return -EINVAL;
3794 dev = MKDEV(major, minor);
3795 if (major != MAJOR(dev) ||
3796 minor != MINOR(dev))
3797 return -EOVERFLOW;
3798
3799
3800 if (mddev->persistent) {
3801 rdev = md_import_device(dev, mddev->major_version,
3802 mddev->minor_version);
3803 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3804 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3805 mdk_rdev_t, same_set);
3806 err = super_types[mddev->major_version]
3807 .load_super(rdev, rdev0, mddev->minor_version);
3808 if (err < 0)
3809 goto out;
3810 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003811 } else if (mddev->external)
3812 rdev = md_import_device(dev, -2, -1);
3813 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003814 rdev = md_import_device(dev, -1, -1);
3815
3816 if (IS_ERR(rdev))
3817 return PTR_ERR(rdev);
3818 err = bind_rdev_to_array(rdev, mddev);
3819 out:
3820 if (err)
3821 export_rdev(rdev);
3822 return err ? err : len;
3823}
3824
3825static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003826__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003827
3828static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003829bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3830{
3831 char *end;
3832 unsigned long chunk, end_chunk;
3833
3834 if (!mddev->bitmap)
3835 goto out;
3836 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3837 while (*buf) {
3838 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3839 if (buf == end) break;
3840 if (*end == '-') { /* range */
3841 buf = end + 1;
3842 end_chunk = simple_strtoul(buf, &end, 0);
3843 if (buf == end) break;
3844 }
3845 if (*end && !isspace(*end)) break;
3846 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003847 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003848 }
3849 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3850out:
3851 return len;
3852}
3853
3854static struct md_sysfs_entry md_bitmap =
3855__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3856
3857static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003858size_show(mddev_t *mddev, char *page)
3859{
Andre Noll58c0fed2009-03-31 14:33:13 +11003860 return sprintf(page, "%llu\n",
3861 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003862}
3863
Andre Nolld71f9f82008-07-11 22:02:22 +10003864static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003865
3866static ssize_t
3867size_store(mddev_t *mddev, const char *buf, size_t len)
3868{
3869 /* If array is inactive, we can reduce the component size, but
3870 * not increase it (except from 0).
3871 * If array is active, we can try an on-line resize
3872 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003873 sector_t sectors;
3874 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003875
Andre Noll58c0fed2009-03-31 14:33:13 +11003876 if (err < 0)
3877 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003878 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003879 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003880 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003881 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003882 if (mddev->dev_sectors == 0 ||
3883 mddev->dev_sectors > sectors)
3884 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003885 else
3886 err = -ENOSPC;
3887 }
3888 return err ? err : len;
3889}
3890
3891static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003892__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003893
NeilBrown8bb93aa2006-01-06 00:20:50 -08003894
3895/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003896 * This is one of
3897 * 'none' for arrays with no metadata (good luck...)
3898 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003899 * or N.M for internally known formats
3900 */
3901static ssize_t
3902metadata_show(mddev_t *mddev, char *page)
3903{
3904 if (mddev->persistent)
3905 return sprintf(page, "%d.%d\n",
3906 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003907 else if (mddev->external)
3908 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003909 else
3910 return sprintf(page, "none\n");
3911}
3912
3913static ssize_t
3914metadata_store(mddev_t *mddev, const char *buf, size_t len)
3915{
3916 int major, minor;
3917 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003918 /* Changing the details of 'external' metadata is
3919 * always permitted. Otherwise there must be
3920 * no devices attached to the array.
3921 */
3922 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3923 ;
3924 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003925 return -EBUSY;
3926
3927 if (cmd_match(buf, "none")) {
3928 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003929 mddev->external = 0;
3930 mddev->major_version = 0;
3931 mddev->minor_version = 90;
3932 return len;
3933 }
3934 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003935 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003936 if (namelen >= sizeof(mddev->metadata_type))
3937 namelen = sizeof(mddev->metadata_type)-1;
3938 strncpy(mddev->metadata_type, buf+9, namelen);
3939 mddev->metadata_type[namelen] = 0;
3940 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3941 mddev->metadata_type[--namelen] = 0;
3942 mddev->persistent = 0;
3943 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003944 mddev->major_version = 0;
3945 mddev->minor_version = 90;
3946 return len;
3947 }
3948 major = simple_strtoul(buf, &e, 10);
3949 if (e==buf || *e != '.')
3950 return -EINVAL;
3951 buf = e+1;
3952 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003953 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003954 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003955 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003956 return -ENOENT;
3957 mddev->major_version = major;
3958 mddev->minor_version = minor;
3959 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003960 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003961 return len;
3962}
3963
3964static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003965__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003966
NeilBrowna35b0d62006-01-06 00:20:49 -08003967static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003968action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003969{
NeilBrown7eec3142005-11-08 21:39:44 -08003970 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003971 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3972 type = "frozen";
3973 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003974 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003975 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3976 type = "reshape";
3977 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003978 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3979 type = "resync";
3980 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3981 type = "check";
3982 else
3983 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003984 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003985 type = "recover";
3986 }
3987 return sprintf(page, "%s\n", type);
3988}
3989
NeilBrown7ebc0be2011-01-14 09:14:33 +11003990static void reap_sync_thread(mddev_t *mddev);
3991
NeilBrown24dd4692005-11-08 21:39:26 -08003992static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003993action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003994{
NeilBrown7eec3142005-11-08 21:39:44 -08003995 if (!mddev->pers || !mddev->pers->sync_request)
3996 return -EINVAL;
3997
NeilBrownb6a9ce62009-05-26 09:41:17 +10003998 if (cmd_match(page, "frozen"))
3999 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4000 else
4001 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4002
4003 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004004 if (mddev->sync_thread) {
4005 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004006 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004007 }
NeilBrown03c902e2006-01-06 00:20:46 -08004008 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4009 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004010 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004011 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004012 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004013 else if (cmd_match(page, "recover")) {
4014 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4015 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4016 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004017 int err;
4018 if (mddev->pers->start_reshape == NULL)
4019 return -EINVAL;
4020 err = mddev->pers->start_reshape(mddev);
4021 if (err)
4022 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004023 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004024 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004025 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004026 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004027 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004028 return -EINVAL;
4029 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4030 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004031 }
NeilBrown03c902e2006-01-06 00:20:46 -08004032 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004033 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004034 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004035 return len;
4036}
4037
NeilBrown9d888832005-11-08 21:39:26 -08004038static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08004039mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004040{
4041 return sprintf(page, "%llu\n",
4042 (unsigned long long) mddev->resync_mismatches);
4043}
4044
NeilBrown80ca3a42006-07-10 04:44:18 -07004045static struct md_sysfs_entry md_scan_mode =
4046__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08004047
NeilBrown96de1e62005-11-08 21:39:39 -08004048
NeilBrown80ca3a42006-07-10 04:44:18 -07004049static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004050
NeilBrown88202a02006-01-06 00:21:36 -08004051static ssize_t
4052sync_min_show(mddev_t *mddev, char *page)
4053{
4054 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4055 mddev->sync_speed_min ? "local": "system");
4056}
4057
4058static ssize_t
4059sync_min_store(mddev_t *mddev, const char *buf, size_t len)
4060{
4061 int min;
4062 char *e;
4063 if (strncmp(buf, "system", 6)==0) {
4064 mddev->sync_speed_min = 0;
4065 return len;
4066 }
4067 min = simple_strtoul(buf, &e, 10);
4068 if (buf == e || (*e && *e != '\n') || min <= 0)
4069 return -EINVAL;
4070 mddev->sync_speed_min = min;
4071 return len;
4072}
4073
4074static struct md_sysfs_entry md_sync_min =
4075__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4076
4077static ssize_t
4078sync_max_show(mddev_t *mddev, char *page)
4079{
4080 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4081 mddev->sync_speed_max ? "local": "system");
4082}
4083
4084static ssize_t
4085sync_max_store(mddev_t *mddev, const char *buf, size_t len)
4086{
4087 int max;
4088 char *e;
4089 if (strncmp(buf, "system", 6)==0) {
4090 mddev->sync_speed_max = 0;
4091 return len;
4092 }
4093 max = simple_strtoul(buf, &e, 10);
4094 if (buf == e || (*e && *e != '\n') || max <= 0)
4095 return -EINVAL;
4096 mddev->sync_speed_max = max;
4097 return len;
4098}
4099
4100static struct md_sysfs_entry md_sync_max =
4101__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4102
Iustin Popd7f3d292007-10-16 23:30:54 -07004103static ssize_t
4104degraded_show(mddev_t *mddev, char *page)
4105{
4106 return sprintf(page, "%d\n", mddev->degraded);
4107}
4108static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004109
4110static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07004111sync_force_parallel_show(mddev_t *mddev, char *page)
4112{
4113 return sprintf(page, "%d\n", mddev->parallel_resync);
4114}
4115
4116static ssize_t
4117sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
4118{
4119 long n;
4120
4121 if (strict_strtol(buf, 10, &n))
4122 return -EINVAL;
4123
4124 if (n != 0 && n != 1)
4125 return -EINVAL;
4126
4127 mddev->parallel_resync = n;
4128
4129 if (mddev->sync_thread)
4130 wake_up(&resync_wait);
4131
4132 return len;
4133}
4134
4135/* force parallel resync, even with shared block devices */
4136static struct md_sysfs_entry md_sync_force_parallel =
4137__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4138 sync_force_parallel_show, sync_force_parallel_store);
4139
4140static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08004141sync_speed_show(mddev_t *mddev, char *page)
4142{
4143 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004144 if (mddev->curr_resync == 0)
4145 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004146 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4147 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004148 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004149 db = resync - mddev->resync_mark_cnt;
4150 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004151}
4152
NeilBrown80ca3a42006-07-10 04:44:18 -07004153static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004154
4155static ssize_t
4156sync_completed_show(mddev_t *mddev, char *page)
4157{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004158 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004159
NeilBrownacb180b2009-04-14 16:28:34 +10004160 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4161 return sprintf(page, "none\n");
4162
NeilBrown88202a02006-01-06 00:21:36 -08004163 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004164 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004165 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004166 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004167
NeilBrownacb180b2009-04-14 16:28:34 +10004168 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004169 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004170}
4171
NeilBrown80ca3a42006-07-10 04:44:18 -07004172static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004173
NeilBrowne464eaf2006-03-27 01:18:14 -08004174static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10004175min_sync_show(mddev_t *mddev, char *page)
4176{
4177 return sprintf(page, "%llu\n",
4178 (unsigned long long)mddev->resync_min);
4179}
4180static ssize_t
4181min_sync_store(mddev_t *mddev, const char *buf, size_t len)
4182{
4183 unsigned long long min;
4184 if (strict_strtoull(buf, 10, &min))
4185 return -EINVAL;
4186 if (min > mddev->resync_max)
4187 return -EINVAL;
4188 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4189 return -EBUSY;
4190
4191 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004192 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004193 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004194 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004195 return -EINVAL;
4196 }
4197 mddev->resync_min = min;
4198
4199 return len;
4200}
4201
4202static struct md_sysfs_entry md_min_sync =
4203__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4204
4205static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08004206max_sync_show(mddev_t *mddev, char *page)
4207{
4208 if (mddev->resync_max == MaxSector)
4209 return sprintf(page, "max\n");
4210 else
4211 return sprintf(page, "%llu\n",
4212 (unsigned long long)mddev->resync_max);
4213}
4214static ssize_t
4215max_sync_store(mddev_t *mddev, const char *buf, size_t len)
4216{
4217 if (strncmp(buf, "max", 3) == 0)
4218 mddev->resync_max = MaxSector;
4219 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004220 unsigned long long max;
4221 if (strict_strtoull(buf, 10, &max))
4222 return -EINVAL;
4223 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004224 return -EINVAL;
4225 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004226 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004227 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4228 return -EBUSY;
4229
4230 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004231 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004232 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004233 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004234 return -EINVAL;
4235 }
4236 mddev->resync_max = max;
4237 }
4238 wake_up(&mddev->recovery_wait);
4239 return len;
4240}
4241
4242static struct md_sysfs_entry md_max_sync =
4243__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4244
4245static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08004246suspend_lo_show(mddev_t *mddev, char *page)
4247{
4248 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4249}
4250
4251static ssize_t
4252suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
4253{
4254 char *e;
4255 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004256 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004257
NeilBrownb8d966e2009-07-01 11:14:04 +10004258 if (mddev->pers == NULL ||
4259 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004260 return -EINVAL;
4261 if (buf == e || (*e && *e != '\n'))
4262 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004263
4264 mddev->suspend_lo = new;
4265 if (new >= old)
4266 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004267 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004268 else {
4269 /* Expanding suspended region - need to wait */
4270 mddev->pers->quiesce(mddev, 1);
4271 mddev->pers->quiesce(mddev, 0);
4272 }
4273 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004274}
4275static struct md_sysfs_entry md_suspend_lo =
4276__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4277
4278
4279static ssize_t
4280suspend_hi_show(mddev_t *mddev, char *page)
4281{
4282 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4283}
4284
4285static ssize_t
4286suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
4287{
4288 char *e;
4289 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004290 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004291
NeilBrownb8d966e2009-07-01 11:14:04 +10004292 if (mddev->pers == NULL ||
4293 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004294 return -EINVAL;
4295 if (buf == e || (*e && *e != '\n'))
4296 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004297
4298 mddev->suspend_hi = new;
4299 if (new <= old)
4300 /* Shrinking suspended region */
4301 mddev->pers->quiesce(mddev, 2);
4302 else {
4303 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004304 mddev->pers->quiesce(mddev, 1);
4305 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004306 }
4307 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004308}
4309static struct md_sysfs_entry md_suspend_hi =
4310__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4311
NeilBrown08a02ec2007-05-09 02:35:38 -07004312static ssize_t
4313reshape_position_show(mddev_t *mddev, char *page)
4314{
4315 if (mddev->reshape_position != MaxSector)
4316 return sprintf(page, "%llu\n",
4317 (unsigned long long)mddev->reshape_position);
4318 strcpy(page, "none\n");
4319 return 5;
4320}
4321
4322static ssize_t
4323reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
4324{
4325 char *e;
4326 unsigned long long new = simple_strtoull(buf, &e, 10);
4327 if (mddev->pers)
4328 return -EBUSY;
4329 if (buf == e || (*e && *e != '\n'))
4330 return -EINVAL;
4331 mddev->reshape_position = new;
4332 mddev->delta_disks = 0;
4333 mddev->new_level = mddev->level;
4334 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004335 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004336 return len;
4337}
4338
4339static struct md_sysfs_entry md_reshape_position =
4340__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4341 reshape_position_store);
4342
Dan Williamsb522adc2009-03-31 15:00:31 +11004343static ssize_t
4344array_size_show(mddev_t *mddev, char *page)
4345{
4346 if (mddev->external_size)
4347 return sprintf(page, "%llu\n",
4348 (unsigned long long)mddev->array_sectors/2);
4349 else
4350 return sprintf(page, "default\n");
4351}
4352
4353static ssize_t
4354array_size_store(mddev_t *mddev, const char *buf, size_t len)
4355{
4356 sector_t sectors;
4357
4358 if (strncmp(buf, "default", 7) == 0) {
4359 if (mddev->pers)
4360 sectors = mddev->pers->size(mddev, 0, 0);
4361 else
4362 sectors = mddev->array_sectors;
4363
4364 mddev->external_size = 0;
4365 } else {
4366 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4367 return -EINVAL;
4368 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004369 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004370
4371 mddev->external_size = 1;
4372 }
4373
4374 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004375 if (mddev->pers) {
4376 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004377 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004378 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004379 return len;
4380}
4381
4382static struct md_sysfs_entry md_array_size =
4383__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4384 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004385
NeilBrowneae17012005-11-08 21:39:23 -08004386static struct attribute *md_default_attrs[] = {
4387 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004388 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004389 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004390 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004391 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004392 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004393 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004394 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004395 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004396 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004397 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004398 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004399 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004400 NULL,
4401};
4402
4403static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004404 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004405 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004406 &md_sync_min.attr,
4407 &md_sync_max.attr,
4408 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004409 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004410 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004411 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004412 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004413 &md_suspend_lo.attr,
4414 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004415 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004416 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004417 NULL,
4418};
NeilBrown411036f2005-11-08 21:39:40 -08004419static struct attribute_group md_redundancy_group = {
4420 .name = NULL,
4421 .attrs = md_redundancy_attrs,
4422};
4423
NeilBrowneae17012005-11-08 21:39:23 -08004424
4425static ssize_t
4426md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4427{
4428 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4429 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004430 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004431
4432 if (!entry->show)
4433 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004434 rv = mddev_lock(mddev);
4435 if (!rv) {
4436 rv = entry->show(mddev, page);
4437 mddev_unlock(mddev);
4438 }
NeilBrown96de1e62005-11-08 21:39:39 -08004439 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004440}
4441
4442static ssize_t
4443md_attr_store(struct kobject *kobj, struct attribute *attr,
4444 const char *page, size_t length)
4445{
4446 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4447 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004448 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004449
4450 if (!entry->store)
4451 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004452 if (!capable(CAP_SYS_ADMIN))
4453 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004454 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004455 if (mddev->hold_active == UNTIL_IOCTL)
4456 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004457 if (!rv) {
4458 rv = entry->store(mddev, page, length);
4459 mddev_unlock(mddev);
4460 }
NeilBrown96de1e62005-11-08 21:39:39 -08004461 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004462}
4463
4464static void md_free(struct kobject *ko)
4465{
4466 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004467
4468 if (mddev->sysfs_state)
4469 sysfs_put(mddev->sysfs_state);
4470
4471 if (mddev->gendisk) {
4472 del_gendisk(mddev->gendisk);
4473 put_disk(mddev->gendisk);
4474 }
4475 if (mddev->queue)
4476 blk_cleanup_queue(mddev->queue);
4477
NeilBrowneae17012005-11-08 21:39:23 -08004478 kfree(mddev);
4479}
4480
Emese Revfy52cf25d2010-01-19 02:58:23 +01004481static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004482 .show = md_attr_show,
4483 .store = md_attr_store,
4484};
4485static struct kobj_type md_ktype = {
4486 .release = md_free,
4487 .sysfs_ops = &md_sysfs_ops,
4488 .default_attrs = md_default_attrs,
4489};
4490
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491int mdp_major = 0;
4492
Dan Williams5fd3a172009-03-04 00:57:25 -07004493static void mddev_delayed_delete(struct work_struct *ws)
4494{
4495 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4496
NeilBrown43a70502009-12-14 12:49:55 +11004497 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004498 kobject_del(&mddev->kobj);
4499 kobject_put(&mddev->kobj);
4500}
4501
NeilBrownefeb53c2009-01-09 08:31:10 +11004502static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004504 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 mddev_t *mddev = mddev_find(dev);
4506 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004507 int partitioned;
4508 int shift;
4509 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004510 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511
4512 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004513 return -ENODEV;
4514
4515 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4516 shift = partitioned ? MdpMinorShift : 0;
4517 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518
Tejun Heoe804ac72010-10-15 15:36:08 +02004519 /* wait for any previous instance of this device to be
4520 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004521 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004522 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004523
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004524 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004525 error = -EEXIST;
4526 if (mddev->gendisk)
4527 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004528
4529 if (name) {
4530 /* Need to ensure that 'name' is not a duplicate.
4531 */
4532 mddev_t *mddev2;
4533 spin_lock(&all_mddevs_lock);
4534
4535 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4536 if (mddev2->gendisk &&
4537 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4538 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004539 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004540 }
4541 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 }
NeilBrown8b765392009-01-09 08:31:08 +11004543
NeilBrown0909dc42009-07-01 12:27:21 +10004544 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004545 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004546 if (!mddev->queue)
4547 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004548 mddev->queue->queuedata = mddev;
4549
NeilBrown409c57f2009-03-31 14:39:39 +11004550 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004551
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 disk = alloc_disk(1 << shift);
4553 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004554 blk_cleanup_queue(mddev->queue);
4555 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004556 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004558 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004560 if (name)
4561 strcpy(disk->disk_name, name);
4562 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004564 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 disk->fops = &md_fops;
4567 disk->private_data = mddev;
4568 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004569 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004570 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004571 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004572 * remove it now.
4573 */
4574 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004576 /* As soon as we call add_disk(), another thread could get
4577 * through to md_open, so make sure it doesn't get too far
4578 */
4579 mutex_lock(&mddev->open_mutex);
4580 add_disk(disk);
4581
Tejun Heoed9e1982008-08-25 19:56:05 +09004582 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4583 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004584 if (error) {
4585 /* This isn't possible, but as kobject_init_and_add is marked
4586 * __must_check, we must do something with the result
4587 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004588 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4589 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004590 error = 0;
4591 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004592 if (mddev->kobj.sd &&
4593 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004594 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004595 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004596 abort:
4597 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004598 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004599 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004600 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004601 }
NeilBrownd3374822009-01-09 08:31:10 +11004602 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004603 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004604}
4605
4606static struct kobject *md_probe(dev_t dev, int *part, void *data)
4607{
4608 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 return NULL;
4610}
4611
NeilBrownefeb53c2009-01-09 08:31:10 +11004612static int add_named_array(const char *val, struct kernel_param *kp)
4613{
4614 /* val must be "md_*" where * is not all digits.
4615 * We allocate an array with a large free minor number, and
4616 * set the name to val. val must not already be an active name.
4617 */
4618 int len = strlen(val);
4619 char buf[DISK_NAME_LEN];
4620
4621 while (len && val[len-1] == '\n')
4622 len--;
4623 if (len >= DISK_NAME_LEN)
4624 return -E2BIG;
4625 strlcpy(buf, val, len+1);
4626 if (strncmp(buf, "md_", 3) != 0)
4627 return -EINVAL;
4628 return md_alloc(0, buf);
4629}
4630
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631static void md_safemode_timeout(unsigned long data)
4632{
4633 mddev_t *mddev = (mddev_t *) data;
4634
Neil Brown0fd62b82008-06-28 08:31:36 +10004635 if (!atomic_read(&mddev->writes_pending)) {
4636 mddev->safemode = 1;
4637 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004638 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 md_wakeup_thread(mddev->thread);
4641}
4642
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004643static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644
NeilBrown390ee602010-06-01 19:37:27 +10004645int md_run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646{
NeilBrown2604b702006-01-06 00:20:36 -08004647 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 mdk_rdev_t *rdev;
NeilBrown2604b702006-01-06 00:20:36 -08004649 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650
NeilBrowna757e642005-04-16 15:26:42 -07004651 if (list_empty(&mddev->disks))
4652 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654
4655 if (mddev->pers)
4656 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004657 /* Cannot run until previous stop completes properly */
4658 if (mddev->sysfs_active)
4659 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004660
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 /*
4662 * Analyze all RAID superblock(s)
4663 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004664 if (!mddev->raid_disks) {
4665 if (!mddev->persistent)
4666 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004667 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669
NeilBrownd9d166c2006-01-06 00:20:51 -08004670 if (mddev->level != LEVEL_NONE)
4671 request_module("md-level-%d", mddev->level);
4672 else if (mddev->clevel[0])
4673 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674
4675 /*
4676 * Drop all container device buffers, from now on
4677 * the only valid external interface is through the md
4678 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004680 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004681 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 continue;
4683 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004684 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004685
4686 /* perform some consistency tests on the device.
4687 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004688 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004689 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004690 if (rdev->meta_bdev) {
4691 /* Nothing to check */;
4692 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004693 if (mddev->dev_sectors &&
4694 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004695 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004696 printk("md: %s: data overlaps metadata\n",
4697 mdname(mddev));
4698 return -EINVAL;
4699 }
4700 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004701 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004702 > rdev->data_offset) {
4703 printk("md: %s: metadata overlaps data\n",
4704 mdname(mddev));
4705 return -EINVAL;
4706 }
4707 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004708 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 }
4710
NeilBrowna167f662010-10-26 18:31:13 +11004711 if (mddev->bio_set == NULL)
NeilBrowna519b262011-07-28 07:56:24 +10004712 mddev->bio_set = bioset_create(BIO_POOL_SIZE,
4713 sizeof(mddev_t *));
NeilBrowna167f662010-10-26 18:31:13 +11004714
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004716 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004717 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004719 if (mddev->level != LEVEL_NONE)
4720 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4721 mddev->level);
4722 else
4723 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4724 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 return -EINVAL;
4726 }
NeilBrown2604b702006-01-06 00:20:36 -08004727 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004729 if (mddev->level != pers->level) {
4730 mddev->level = pers->level;
4731 mddev->new_level = pers->level;
4732 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004733 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734
NeilBrownf6705572006-03-27 01:18:11 -08004735 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004736 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004737 /* This personality cannot handle reshaping... */
4738 mddev->pers = NULL;
4739 module_put(pers->owner);
4740 return -EINVAL;
4741 }
4742
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004743 if (pers->sync_request) {
4744 /* Warn if this is a potentially silly
4745 * configuration.
4746 */
4747 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4748 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004749 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004750
4751 list_for_each_entry(rdev, &mddev->disks, same_set)
4752 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004753 if (rdev < rdev2 &&
4754 rdev->bdev->bd_contains ==
4755 rdev2->bdev->bd_contains) {
4756 printk(KERN_WARNING
4757 "%s: WARNING: %s appears to be"
4758 " on the same physical disk as"
4759 " %s.\n",
4760 mdname(mddev),
4761 bdevname(rdev->bdev,b),
4762 bdevname(rdev2->bdev,b2));
4763 warned = 1;
4764 }
4765 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004766
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004767 if (warned)
4768 printk(KERN_WARNING
4769 "True protection against single-disk"
4770 " failure might be compromised.\n");
4771 }
4772
NeilBrown657390d2005-08-26 18:34:16 -07004773 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004774 /* may be over-ridden by personality */
4775 mddev->resync_max_sectors = mddev->dev_sectors;
4776
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004777 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778
NeilBrown0f9552b2009-12-30 12:08:50 +11004779 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004780 mddev->ro = 2; /* read-only, but switch on first write */
4781
NeilBrownb15c2e52006-01-06 00:20:16 -08004782 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004783 if (err)
4784 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004785 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4786 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4787 " but 'external_size' not in effect?\n", __func__);
4788 printk(KERN_ERR
4789 "md: invalid array_size %llu > default size %llu\n",
4790 (unsigned long long)mddev->array_sectors / 2,
4791 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4792 err = -EINVAL;
4793 mddev->pers->stop(mddev);
4794 }
4795 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004796 err = bitmap_create(mddev);
4797 if (err) {
4798 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4799 mdname(mddev), err);
4800 mddev->pers->stop(mddev);
4801 }
4802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 module_put(mddev->pers->owner);
4805 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004806 bitmap_destroy(mddev);
4807 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004809 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004810 if (mddev->kobj.sd &&
4811 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004812 printk(KERN_WARNING
4813 "md: cannot register extra attributes for %s\n",
4814 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004815 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004816 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004817 mddev->ro = 0;
4818
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004820 atomic_set(&mddev->max_corr_read_errors,
4821 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 mddev->safemode = 0;
4823 mddev->safemode_timer.function = md_safemode_timeout;
4824 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004825 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11004827 smp_wmb();
4828 mddev->ready = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004829 list_for_each_entry(rdev, &mddev->disks, same_set)
Namhyung Kim36fad852011-07-27 11:00:36 +10004830 if (rdev->raid_disk >= 0)
4831 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004832 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
4834 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4835
NeilBrown850b2b42006-10-03 01:15:46 -07004836 if (mddev->flags)
4837 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838
NeilBrownd7603b72006-01-06 00:20:30 -08004839 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004840 sysfs_notify_dirent_safe(mddev->sysfs_state);
4841 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004842 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 return 0;
4844}
NeilBrown390ee602010-06-01 19:37:27 +10004845EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846
NeilBrownfe60b012010-03-29 11:10:42 +11004847static int do_md_run(mddev_t *mddev)
4848{
4849 int err;
4850
4851 err = md_run(mddev);
4852 if (err)
4853 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10004854 err = bitmap_load(mddev);
4855 if (err) {
4856 bitmap_destroy(mddev);
4857 goto out;
4858 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05004859
4860 md_wakeup_thread(mddev->thread);
4861 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4862
NeilBrownfe60b012010-03-29 11:10:42 +11004863 set_capacity(mddev->gendisk, mddev->array_sectors);
4864 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004865 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11004866 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4867out:
4868 return err;
4869}
4870
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871static int restart_array(mddev_t *mddev)
4872{
4873 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874
Andre Noll80fab1d2008-07-11 22:02:21 +10004875 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004877 return -ENXIO;
4878 if (!mddev->pers)
4879 return -EINVAL;
4880 if (!mddev->ro)
4881 return -EBUSY;
4882 mddev->safemode = 0;
4883 mddev->ro = 0;
4884 set_disk_ro(disk, 0);
4885 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4886 mdname(mddev));
4887 /* Kick recovery or resync if necessary */
4888 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4889 md_wakeup_thread(mddev->thread);
4890 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004891 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004892 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893}
4894
NeilBrownacc55e22006-06-26 00:27:47 -07004895/* similar to deny_write_access, but accounts for our holding a reference
4896 * to the file ourselves */
4897static int deny_bitmap_write_access(struct file * file)
4898{
4899 struct inode *inode = file->f_mapping->host;
4900
4901 spin_lock(&inode->i_lock);
4902 if (atomic_read(&inode->i_writecount) > 1) {
4903 spin_unlock(&inode->i_lock);
4904 return -ETXTBSY;
4905 }
4906 atomic_set(&inode->i_writecount, -1);
4907 spin_unlock(&inode->i_lock);
4908
4909 return 0;
4910}
4911
NeilBrown43a70502009-12-14 12:49:55 +11004912void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004913{
4914 struct inode *inode = file->f_mapping->host;
4915
4916 spin_lock(&inode->i_lock);
4917 atomic_set(&inode->i_writecount, 1);
4918 spin_unlock(&inode->i_lock);
4919}
4920
NeilBrown6177b472010-03-29 11:37:13 +11004921static void md_clean(mddev_t *mddev)
4922{
4923 mddev->array_sectors = 0;
4924 mddev->external_size = 0;
4925 mddev->dev_sectors = 0;
4926 mddev->raid_disks = 0;
4927 mddev->recovery_cp = 0;
4928 mddev->resync_min = 0;
4929 mddev->resync_max = MaxSector;
4930 mddev->reshape_position = MaxSector;
4931 mddev->external = 0;
4932 mddev->persistent = 0;
4933 mddev->level = LEVEL_NONE;
4934 mddev->clevel[0] = 0;
4935 mddev->flags = 0;
4936 mddev->ro = 0;
4937 mddev->metadata_type[0] = 0;
4938 mddev->chunk_sectors = 0;
4939 mddev->ctime = mddev->utime = 0;
4940 mddev->layout = 0;
4941 mddev->max_disks = 0;
4942 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10004943 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004944 mddev->delta_disks = 0;
4945 mddev->new_level = LEVEL_NONE;
4946 mddev->new_layout = 0;
4947 mddev->new_chunk_sectors = 0;
4948 mddev->curr_resync = 0;
4949 mddev->resync_mismatches = 0;
4950 mddev->suspend_lo = mddev->suspend_hi = 0;
4951 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4952 mddev->recovery = 0;
4953 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004954 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004955 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004956 mddev->safemode = 0;
4957 mddev->bitmap_info.offset = 0;
4958 mddev->bitmap_info.default_offset = 0;
4959 mddev->bitmap_info.chunksize = 0;
4960 mddev->bitmap_info.daemon_sleep = 0;
4961 mddev->bitmap_info.max_write_behind = 0;
4962}
4963
NeilBrowndefad612011-01-14 09:14:33 +11004964static void __md_stop_writes(mddev_t *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11004965{
4966 if (mddev->sync_thread) {
4967 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4968 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004969 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11004970 }
4971
4972 del_timer_sync(&mddev->safemode_timer);
4973
4974 bitmap_flush(mddev);
4975 md_super_wait(mddev);
4976
4977 if (!mddev->in_sync || mddev->flags) {
4978 /* mark array as shutdown cleanly */
4979 mddev->in_sync = 1;
4980 md_update_sb(mddev, 1);
4981 }
4982}
NeilBrowndefad612011-01-14 09:14:33 +11004983
4984void md_stop_writes(mddev_t *mddev)
4985{
4986 mddev_lock(mddev);
4987 __md_stop_writes(mddev);
4988 mddev_unlock(mddev);
4989}
NeilBrown390ee602010-06-01 19:37:27 +10004990EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11004991
NeilBrown390ee602010-06-01 19:37:27 +10004992void md_stop(mddev_t *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11004993{
NeilBrown0ca69882011-01-14 09:14:33 +11004994 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004995 mddev->pers->stop(mddev);
4996 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4997 mddev->to_remove = &md_redundancy_group;
4998 module_put(mddev->pers->owner);
4999 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005000 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005001}
NeilBrown390ee602010-06-01 19:37:27 +10005002EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005003
NeilBrowna4bd82d2010-03-29 13:23:10 +11005004static int md_set_readonly(mddev_t *mddev, int is_open)
5005{
5006 int err = 0;
5007 mutex_lock(&mddev->open_mutex);
5008 if (atomic_read(&mddev->openers) > is_open) {
5009 printk("md: %s still in use.\n",mdname(mddev));
5010 err = -EBUSY;
5011 goto out;
5012 }
5013 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005014 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005015
5016 err = -ENXIO;
5017 if (mddev->ro==1)
5018 goto out;
5019 mddev->ro = 1;
5020 set_disk_ro(mddev->gendisk, 1);
5021 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005022 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005023 err = 0;
5024 }
5025out:
5026 mutex_unlock(&mddev->open_mutex);
5027 return err;
5028}
5029
NeilBrown9e653b62006-06-26 00:27:58 -07005030/* mode:
5031 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005032 * 2 - stop but do not disassemble array
5033 */
Neil Browndf5b20c2008-07-11 22:02:22 +10005034static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10005037 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038
NeilBrownc8c00a62009-08-10 12:50:52 +10005039 mutex_lock(&mddev->open_mutex);
NeilBrownbb4f1e92010-08-08 21:18:03 +10005040 if (atomic_read(&mddev->openers) > is_open ||
5041 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005042 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005043 mutex_unlock(&mddev->open_mutex);
5044 return -EBUSY;
5045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046
NeilBrown6e17b022010-08-07 21:41:19 +10005047 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005048 if (mddev->ro)
5049 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005050
NeilBrowndefad612011-01-14 09:14:33 +11005051 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005052 md_stop(mddev);
5053 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005054 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005055
NeilBrowna4bd82d2010-03-29 13:23:10 +11005056 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005057 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005058
NeilBrowna4bd82d2010-03-29 13:23:10 +11005059 list_for_each_entry(rdev, &mddev->disks, same_set)
Namhyung Kim36fad852011-07-27 11:00:36 +10005060 if (rdev->raid_disk >= 0)
5061 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005062
NeilBrowna4bd82d2010-03-29 13:23:10 +11005063 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005064 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005065 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005066 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005067
NeilBrowna4bd82d2010-03-29 13:23:10 +11005068 if (mddev->ro)
5069 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005070 } else
5071 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 /*
5073 * Free resources if final stop
5074 */
NeilBrown9e653b62006-06-26 00:27:58 -07005075 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5077
NeilBrown978f9462006-02-02 14:28:05 -08005078 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005079 if (mddev->bitmap_info.file) {
5080 restore_bitmap_write_access(mddev->bitmap_info.file);
5081 fput(mddev->bitmap_info.file);
5082 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005083 }
NeilBrownc3d97142009-12-14 12:49:52 +11005084 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005085
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 export_array(mddev);
5087
NeilBrown6177b472010-03-29 11:37:13 +11005088 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005089 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005090 if (mddev->hold_active == UNTIL_STOP)
5091 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005092 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005093 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005094 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005095 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005096 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097}
5098
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005099#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100static void autorun_array(mddev_t *mddev)
5101{
5102 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 int err;
5104
NeilBrowna757e642005-04-16 15:26:42 -07005105 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
5108 printk(KERN_INFO "md: running: ");
5109
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005110 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 char b[BDEVNAME_SIZE];
5112 printk("<%s>", bdevname(rdev->bdev,b));
5113 }
5114 printk("\n");
5115
NeilBrownd710e132008-10-13 11:55:12 +11005116 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 if (err) {
5118 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11005119 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 }
5121}
5122
5123/*
5124 * lets try to run arrays based on all disks that have arrived
5125 * until now. (those are in pending_raid_disks)
5126 *
5127 * the method: pick the first pending disk, collect all disks with
5128 * the same UUID, remove all from the pending list and put them into
5129 * the 'same_array' list. Then order this list based on superblock
5130 * update time (freshest comes first), kick out 'old' disks and
5131 * compare superblocks. If everything's fine then run it.
5132 *
5133 * If "unit" is allocated, then bump its reference count
5134 */
5135static void autorun_devices(int part)
5136{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005137 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 mddev_t *mddev;
5139 char b[BDEVNAME_SIZE];
5140
5141 printk(KERN_INFO "md: autorun ...\n");
5142 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005143 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005145 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 rdev0 = list_entry(pending_raid_disks.next,
5147 mdk_rdev_t, same_set);
5148
5149 printk(KERN_INFO "md: considering %s ...\n",
5150 bdevname(rdev0->bdev,b));
5151 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005152 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 if (super_90_load(rdev, rdev0, 0) >= 0) {
5154 printk(KERN_INFO "md: adding %s ...\n",
5155 bdevname(rdev->bdev,b));
5156 list_move(&rdev->same_set, &candidates);
5157 }
5158 /*
5159 * now we have a set of devices, with all of them having
5160 * mostly sane superblocks. It's time to allocate the
5161 * mddev.
5162 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005163 if (part) {
5164 dev = MKDEV(mdp_major,
5165 rdev0->preferred_minor << MdpMinorShift);
5166 unit = MINOR(dev) >> MdpMinorShift;
5167 } else {
5168 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5169 unit = MINOR(dev);
5170 }
5171 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5173 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5174 break;
5175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176
5177 md_probe(dev, NULL, NULL);
5178 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005179 if (!mddev || !mddev->gendisk) {
5180 if (mddev)
5181 mddev_put(mddev);
5182 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 "md: cannot allocate memory for md drive.\n");
5184 break;
5185 }
5186 if (mddev_lock(mddev))
5187 printk(KERN_WARNING "md: %s locked, cannot run\n",
5188 mdname(mddev));
5189 else if (mddev->raid_disks || mddev->major_version
5190 || !list_empty(&mddev->disks)) {
5191 printk(KERN_WARNING
5192 "md: %s already running, cannot run %s\n",
5193 mdname(mddev), bdevname(rdev0->bdev,b));
5194 mddev_unlock(mddev);
5195 } else {
5196 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005197 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005198 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 list_del_init(&rdev->same_set);
5200 if (bind_rdev_to_array(rdev, mddev))
5201 export_rdev(rdev);
5202 }
5203 autorun_array(mddev);
5204 mddev_unlock(mddev);
5205 }
5206 /* on success, candidates will be empty, on error
5207 * it won't...
5208 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005209 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005210 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 mddev_put(mddev);
5214 }
5215 printk(KERN_INFO "md: ... autorun DONE.\n");
5216}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005217#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219static int get_version(void __user * arg)
5220{
5221 mdu_version_t ver;
5222
5223 ver.major = MD_MAJOR_VERSION;
5224 ver.minor = MD_MINOR_VERSION;
5225 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5226
5227 if (copy_to_user(arg, &ver, sizeof(ver)))
5228 return -EFAULT;
5229
5230 return 0;
5231}
5232
5233static int get_array_info(mddev_t * mddev, void __user * arg)
5234{
5235 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005236 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238
NeilBrowna9f326e2009-09-23 18:06:41 +10005239 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005240 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005242 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 failed++;
5244 else {
5245 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005246 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005247 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 else
5249 spare++;
5250 }
5251 }
5252
5253 info.major_version = mddev->major_version;
5254 info.minor_version = mddev->minor_version;
5255 info.patch_version = MD_PATCHLEVEL_VERSION;
5256 info.ctime = mddev->ctime;
5257 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005258 info.size = mddev->dev_sectors / 2;
5259 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005260 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 info.nr_disks = nr;
5262 info.raid_disks = mddev->raid_disks;
5263 info.md_minor = mddev->md_minor;
5264 info.not_persistent= !mddev->persistent;
5265
5266 info.utime = mddev->utime;
5267 info.state = 0;
5268 if (mddev->in_sync)
5269 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005270 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005271 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005272 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 info.working_disks = working;
5274 info.failed_disks = failed;
5275 info.spare_disks = spare;
5276
5277 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005278 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279
5280 if (copy_to_user(arg, &info, sizeof(info)))
5281 return -EFAULT;
5282
5283 return 0;
5284}
5285
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005286static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005287{
5288 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5289 char *ptr, *buf = NULL;
5290 int err = -ENOMEM;
5291
Dan Williamsb5470dc2008-06-27 21:44:04 -07005292 if (md_allow_write(mddev))
5293 file = kmalloc(sizeof(*file), GFP_NOIO);
5294 else
5295 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005296
NeilBrown32a76272005-06-21 17:17:14 -07005297 if (!file)
5298 goto out;
5299
5300 /* bitmap disabled, zero the first byte and copy out */
5301 if (!mddev->bitmap || !mddev->bitmap->file) {
5302 file->pathname[0] = '\0';
5303 goto copy_out;
5304 }
5305
5306 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5307 if (!buf)
5308 goto out;
5309
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005310 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5311 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005312 goto out;
5313
5314 strcpy(file->pathname, ptr);
5315
5316copy_out:
5317 err = 0;
5318 if (copy_to_user(arg, file, sizeof(*file)))
5319 err = -EFAULT;
5320out:
5321 kfree(buf);
5322 kfree(file);
5323 return err;
5324}
5325
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326static int get_disk_info(mddev_t * mddev, void __user * arg)
5327{
5328 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 mdk_rdev_t *rdev;
5330
5331 if (copy_from_user(&info, arg, sizeof(info)))
5332 return -EFAULT;
5333
Andre Noll26ef3792008-07-11 22:02:21 +10005334 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 if (rdev) {
5336 info.major = MAJOR(rdev->bdev->bd_dev);
5337 info.minor = MINOR(rdev->bdev->bd_dev);
5338 info.raid_disk = rdev->raid_disk;
5339 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005340 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005342 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 info.state |= (1<<MD_DISK_ACTIVE);
5344 info.state |= (1<<MD_DISK_SYNC);
5345 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005346 if (test_bit(WriteMostly, &rdev->flags))
5347 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 } else {
5349 info.major = info.minor = 0;
5350 info.raid_disk = -1;
5351 info.state = (1<<MD_DISK_REMOVED);
5352 }
5353
5354 if (copy_to_user(arg, &info, sizeof(info)))
5355 return -EFAULT;
5356
5357 return 0;
5358}
5359
5360static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5361{
5362 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5363 mdk_rdev_t *rdev;
5364 dev_t dev = MKDEV(info->major,info->minor);
5365
5366 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5367 return -EOVERFLOW;
5368
5369 if (!mddev->raid_disks) {
5370 int err;
5371 /* expecting a device which has a superblock */
5372 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5373 if (IS_ERR(rdev)) {
5374 printk(KERN_WARNING
5375 "md: md_import_device returned %ld\n",
5376 PTR_ERR(rdev));
5377 return PTR_ERR(rdev);
5378 }
5379 if (!list_empty(&mddev->disks)) {
5380 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5381 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005382 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 .load_super(rdev, rdev0, mddev->minor_version);
5384 if (err < 0) {
5385 printk(KERN_WARNING
5386 "md: %s has different UUID to %s\n",
5387 bdevname(rdev->bdev,b),
5388 bdevname(rdev0->bdev,b2));
5389 export_rdev(rdev);
5390 return -EINVAL;
5391 }
5392 }
5393 err = bind_rdev_to_array(rdev, mddev);
5394 if (err)
5395 export_rdev(rdev);
5396 return err;
5397 }
5398
5399 /*
5400 * add_new_disk can be used once the array is assembled
5401 * to add "hot spares". They must already have a superblock
5402 * written
5403 */
5404 if (mddev->pers) {
5405 int err;
5406 if (!mddev->pers->hot_add_disk) {
5407 printk(KERN_WARNING
5408 "%s: personality does not support diskops!\n",
5409 mdname(mddev));
5410 return -EINVAL;
5411 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005412 if (mddev->persistent)
5413 rdev = md_import_device(dev, mddev->major_version,
5414 mddev->minor_version);
5415 else
5416 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 if (IS_ERR(rdev)) {
5418 printk(KERN_WARNING
5419 "md: md_import_device returned %ld\n",
5420 PTR_ERR(rdev));
5421 return PTR_ERR(rdev);
5422 }
NeilBrown1a855a02010-12-09 16:36:28 +11005423 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005424 if (!mddev->persistent) {
5425 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005426 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005427 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005428 set_bit(In_sync, &rdev->flags);
5429 } else
NeilBrown41158c72005-06-21 17:17:25 -07005430 rdev->raid_disk = -1;
5431 } else
5432 super_types[mddev->major_version].
5433 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005434 if ((info->state & (1<<MD_DISK_SYNC)) &&
5435 (!test_bit(In_sync, &rdev->flags) ||
5436 rdev->raid_disk != info->raid_disk)) {
5437 /* This was a hot-add request, but events doesn't
5438 * match, so reject it.
5439 */
5440 export_rdev(rdev);
5441 return -EINVAL;
5442 }
5443
NeilBrown1a855a02010-12-09 16:36:28 +11005444 if (test_bit(In_sync, &rdev->flags))
5445 rdev->saved_raid_disk = rdev->raid_disk;
5446 else
5447 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005448
NeilBrownb2d444d2005-11-08 21:39:31 -08005449 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005450 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5451 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005452 else
5453 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005454
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 rdev->raid_disk = -1;
5456 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005457 if (!err && !mddev->pers->hot_remove_disk) {
5458 /* If there is hot_add_disk but no hot_remove_disk
5459 * then added disks for geometry changes,
5460 * and should be added immediately.
5461 */
5462 super_types[mddev->major_version].
5463 validate_super(mddev, rdev);
5464 err = mddev->pers->hot_add_disk(mddev, rdev);
5465 if (err)
5466 unbind_rdev_from_array(rdev);
5467 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 if (err)
5469 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005470 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005471 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005472
NeilBrown17571282006-12-10 02:20:52 -08005473 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005474 if (mddev->degraded)
5475 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005476 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005477 if (!err)
5478 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005479 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 return err;
5481 }
5482
5483 /* otherwise, add_new_disk is only allowed
5484 * for major_version==0 superblocks
5485 */
5486 if (mddev->major_version != 0) {
5487 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5488 mdname(mddev));
5489 return -EINVAL;
5490 }
5491
5492 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5493 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005494 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 if (IS_ERR(rdev)) {
5496 printk(KERN_WARNING
5497 "md: error, md_import_device() returned %ld\n",
5498 PTR_ERR(rdev));
5499 return PTR_ERR(rdev);
5500 }
5501 rdev->desc_nr = info->number;
5502 if (info->raid_disk < mddev->raid_disks)
5503 rdev->raid_disk = info->raid_disk;
5504 else
5505 rdev->raid_disk = -1;
5506
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005508 if (info->state & (1<<MD_DISK_SYNC))
5509 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005511 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5512 set_bit(WriteMostly, &rdev->flags);
5513
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 if (!mddev->persistent) {
5515 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005516 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5517 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005518 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005519 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520
NeilBrown2bf071b2006-01-06 00:20:55 -08005521 err = bind_rdev_to_array(rdev, mddev);
5522 if (err) {
5523 export_rdev(rdev);
5524 return err;
5525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 }
5527
5528 return 0;
5529}
5530
5531static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5532{
5533 char b[BDEVNAME_SIZE];
5534 mdk_rdev_t *rdev;
5535
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 rdev = find_rdev(mddev, dev);
5537 if (!rdev)
5538 return -ENXIO;
5539
5540 if (rdev->raid_disk >= 0)
5541 goto busy;
5542
5543 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005544 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005545 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546
5547 return 0;
5548busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005549 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 bdevname(rdev->bdev,b), mdname(mddev));
5551 return -EBUSY;
5552}
5553
5554static int hot_add_disk(mddev_t * mddev, dev_t dev)
5555{
5556 char b[BDEVNAME_SIZE];
5557 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 mdk_rdev_t *rdev;
5559
5560 if (!mddev->pers)
5561 return -ENODEV;
5562
5563 if (mddev->major_version != 0) {
5564 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5565 " version-0 superblocks.\n",
5566 mdname(mddev));
5567 return -EINVAL;
5568 }
5569 if (!mddev->pers->hot_add_disk) {
5570 printk(KERN_WARNING
5571 "%s: personality does not support diskops!\n",
5572 mdname(mddev));
5573 return -EINVAL;
5574 }
5575
NeilBrownd710e132008-10-13 11:55:12 +11005576 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 if (IS_ERR(rdev)) {
5578 printk(KERN_WARNING
5579 "md: error, md_import_device() returned %ld\n",
5580 PTR_ERR(rdev));
5581 return -EINVAL;
5582 }
5583
5584 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005585 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005587 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588
NeilBrown8190e752009-06-18 08:48:58 +10005589 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590
NeilBrownb2d444d2005-11-08 21:39:31 -08005591 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 printk(KERN_WARNING
5593 "md: can not hot-add faulty %s disk to %s!\n",
5594 bdevname(rdev->bdev,b), mdname(mddev));
5595 err = -EINVAL;
5596 goto abort_export;
5597 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005598 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005600 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005601 err = bind_rdev_to_array(rdev, mddev);
5602 if (err)
5603 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604
5605 /*
5606 * The rest should better be atomic, we can have disk failures
5607 * noticed in interrupt contexts ...
5608 */
5609
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 rdev->raid_disk = -1;
5611
NeilBrown850b2b42006-10-03 01:15:46 -07005612 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613
5614 /*
5615 * Kick recovery, maybe this spare has to be added to the
5616 * array immediately.
5617 */
5618 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5619 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005620 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 return 0;
5622
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623abort_export:
5624 export_rdev(rdev);
5625 return err;
5626}
5627
NeilBrown32a76272005-06-21 17:17:14 -07005628static int set_bitmap_file(mddev_t *mddev, int fd)
5629{
5630 int err;
5631
NeilBrown36fa3062005-09-09 16:23:45 -07005632 if (mddev->pers) {
5633 if (!mddev->pers->quiesce)
5634 return -EBUSY;
5635 if (mddev->recovery || mddev->sync_thread)
5636 return -EBUSY;
5637 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005638 }
5639
NeilBrown36fa3062005-09-09 16:23:45 -07005640
5641 if (fd >= 0) {
5642 if (mddev->bitmap)
5643 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005644 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005645
NeilBrownc3d97142009-12-14 12:49:52 +11005646 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005647 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5648 mdname(mddev));
5649 return -EBADF;
5650 }
5651
NeilBrownc3d97142009-12-14 12:49:52 +11005652 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005653 if (err) {
5654 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5655 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005656 fput(mddev->bitmap_info.file);
5657 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005658 return err;
5659 }
NeilBrownc3d97142009-12-14 12:49:52 +11005660 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005661 } else if (mddev->bitmap == NULL)
5662 return -ENOENT; /* cannot remove what isn't there */
5663 err = 0;
5664 if (mddev->pers) {
5665 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005666 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005667 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005668 if (!err)
5669 err = bitmap_load(mddev);
5670 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005671 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005672 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005673 fd = -1; /* make sure to put the file */
5674 }
NeilBrown36fa3062005-09-09 16:23:45 -07005675 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005676 }
5677 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005678 if (mddev->bitmap_info.file) {
5679 restore_bitmap_write_access(mddev->bitmap_info.file);
5680 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005681 }
NeilBrownc3d97142009-12-14 12:49:52 +11005682 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005683 }
5684
NeilBrown32a76272005-06-21 17:17:14 -07005685 return err;
5686}
5687
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688/*
5689 * set_array_info is used two different ways
5690 * The original usage is when creating a new array.
5691 * In this usage, raid_disks is > 0 and it together with
5692 * level, size, not_persistent,layout,chunksize determine the
5693 * shape of the array.
5694 * This will always create an array with a type-0.90.0 superblock.
5695 * The newer usage is when assembling an array.
5696 * In this case raid_disks will be 0, and the major_version field is
5697 * use to determine which style super-blocks are to be found on the devices.
5698 * The minor and patch _version numbers are also kept incase the
5699 * super_block handler wishes to interpret them.
5700 */
5701static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5702{
5703
5704 if (info->raid_disks == 0) {
5705 /* just setting version number for superblock loading */
5706 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005707 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 super_types[info->major_version].name == NULL) {
5709 /* maybe try to auto-load a module? */
5710 printk(KERN_INFO
5711 "md: superblock version %d not known\n",
5712 info->major_version);
5713 return -EINVAL;
5714 }
5715 mddev->major_version = info->major_version;
5716 mddev->minor_version = info->minor_version;
5717 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005718 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005719 /* ensure mddev_put doesn't delete this now that there
5720 * is some minimal configuration.
5721 */
5722 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 return 0;
5724 }
5725 mddev->major_version = MD_MAJOR_VERSION;
5726 mddev->minor_version = MD_MINOR_VERSION;
5727 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5728 mddev->ctime = get_seconds();
5729
5730 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005731 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005732 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 mddev->raid_disks = info->raid_disks;
5734 /* don't set md_minor, it is determined by which /dev/md* was
5735 * openned
5736 */
5737 if (info->state & (1<<MD_SB_CLEAN))
5738 mddev->recovery_cp = MaxSector;
5739 else
5740 mddev->recovery_cp = 0;
5741 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005742 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743
5744 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005745 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746
5747 mddev->max_disks = MD_SB_DISKS;
5748
NeilBrowne6910632008-02-06 01:39:51 -08005749 if (mddev->persistent)
5750 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005751 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752
NeilBrownc3d97142009-12-14 12:49:52 +11005753 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5754 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005755
NeilBrownf6705572006-03-27 01:18:11 -08005756 mddev->reshape_position = MaxSector;
5757
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 /*
5759 * Generate a 128 bit UUID
5760 */
5761 get_random_bytes(mddev->uuid, 16);
5762
NeilBrownf6705572006-03-27 01:18:11 -08005763 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005764 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005765 mddev->new_layout = mddev->layout;
5766 mddev->delta_disks = 0;
5767
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 return 0;
5769}
5770
Dan Williams1f403622009-03-31 14:59:03 +11005771void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5772{
Dan Williamsb522adc2009-03-31 15:00:31 +11005773 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5774
5775 if (mddev->external_size)
5776 return;
5777
Dan Williams1f403622009-03-31 14:59:03 +11005778 mddev->array_sectors = array_sectors;
5779}
5780EXPORT_SYMBOL(md_set_array_sectors);
5781
Andre Nolld71f9f82008-07-11 22:02:22 +10005782static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005783{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005784 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005785 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005786 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005787
5788 if (mddev->pers->resize == NULL)
5789 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005790 /* The "num_sectors" is the number of sectors of each device that
5791 * is used. This can only make sense for arrays with redundancy.
5792 * linear and raid0 always use whatever space is available. We can only
5793 * consider changing this number if no resync or reconstruction is
5794 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005795 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005796 * of each device. If num_sectors is zero, we find the largest size
5797 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08005798 */
5799 if (mddev->sync_thread)
5800 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005801 if (mddev->bitmap)
5802 /* Sorry, cannot grow a bitmap yet, just remove it,
5803 * grow, and re-add.
5804 */
5805 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005806 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005807 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005808
Andre Nolld71f9f82008-07-11 22:02:22 +10005809 if (fit && (num_sectors == 0 || num_sectors > avail))
5810 num_sectors = avail;
5811 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005812 return -ENOSPC;
5813 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005814 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005815 if (!rv)
5816 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005817 return rv;
5818}
5819
NeilBrownda943b92006-01-06 00:20:54 -08005820static int update_raid_disks(mddev_t *mddev, int raid_disks)
5821{
5822 int rv;
5823 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005824 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b92006-01-06 00:20:54 -08005825 return -EINVAL;
5826 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005827 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b92006-01-06 00:20:54 -08005828 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005829 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b92006-01-06 00:20:54 -08005830 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005831 mddev->delta_disks = raid_disks - mddev->raid_disks;
5832
5833 rv = mddev->pers->check_reshape(mddev);
NeilBrownde171cb2011-01-31 11:57:42 +11005834 if (rv < 0)
5835 mddev->delta_disks = 0;
NeilBrownda943b92006-01-06 00:20:54 -08005836 return rv;
5837}
5838
5839
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840/*
5841 * update_array_info is used to change the configuration of an
5842 * on-line array.
5843 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5844 * fields in the info are checked against the array.
5845 * Any differences that cannot be handled will cause an error.
5846 * Normally, only one change can be managed at a time.
5847 */
5848static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5849{
5850 int rv = 0;
5851 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005852 int state = 0;
5853
5854 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005855 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005856 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857
5858 if (mddev->major_version != info->major_version ||
5859 mddev->minor_version != info->minor_version ||
5860/* mddev->patch_version != info->patch_version || */
5861 mddev->ctime != info->ctime ||
5862 mddev->level != info->level ||
5863/* mddev->layout != info->layout || */
5864 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005865 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005866 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5867 ((state^info->state) & 0xfffffe00)
5868 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 return -EINVAL;
5870 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005871 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5872 cnt++;
5873 if (mddev->raid_disks != info->raid_disks)
5874 cnt++;
5875 if (mddev->layout != info->layout)
5876 cnt++;
5877 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5878 cnt++;
5879 if (cnt == 0)
5880 return 0;
5881 if (cnt > 1)
5882 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883
5884 if (mddev->layout != info->layout) {
5885 /* Change layout
5886 * we don't need to do anything at the md level, the
5887 * personality will take care of it all.
5888 */
NeilBrown50ac1682009-06-18 08:47:55 +10005889 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005891 else {
5892 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005893 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005894 if (rv)
5895 mddev->new_layout = mddev->layout;
5896 return rv;
5897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005899 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005900 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901
NeilBrownda943b92006-01-06 00:20:54 -08005902 if (mddev->raid_disks != info->raid_disks)
5903 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904
NeilBrown36fa3062005-09-09 16:23:45 -07005905 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5906 if (mddev->pers->quiesce == NULL)
5907 return -EINVAL;
5908 if (mddev->recovery || mddev->sync_thread)
5909 return -EBUSY;
5910 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5911 /* add the bitmap */
5912 if (mddev->bitmap)
5913 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005914 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005915 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005916 mddev->bitmap_info.offset =
5917 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005918 mddev->pers->quiesce(mddev, 1);
5919 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005920 if (!rv)
5921 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07005922 if (rv)
5923 bitmap_destroy(mddev);
5924 mddev->pers->quiesce(mddev, 0);
5925 } else {
5926 /* remove the bitmap */
5927 if (!mddev->bitmap)
5928 return -ENOENT;
5929 if (mddev->bitmap->file)
5930 return -EINVAL;
5931 mddev->pers->quiesce(mddev, 1);
5932 bitmap_destroy(mddev);
5933 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005934 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005935 }
5936 }
NeilBrown850b2b42006-10-03 01:15:46 -07005937 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 return rv;
5939}
5940
5941static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5942{
5943 mdk_rdev_t *rdev;
5944
5945 if (mddev->pers == NULL)
5946 return -ENODEV;
5947
5948 rdev = find_rdev(mddev, dev);
5949 if (!rdev)
5950 return -ENODEV;
5951
5952 md_error(mddev, rdev);
5953 return 0;
5954}
5955
Andre Noll2f9618c2008-04-25 18:57:58 +02005956/*
5957 * We have a problem here : there is no easy way to give a CHS
5958 * virtual geometry. We currently pretend that we have a 2 heads
5959 * 4 sectors (with a BIG number of cylinders...). This drives
5960 * dosfs just mad... ;-)
5961 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005962static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5963{
5964 mddev_t *mddev = bdev->bd_disk->private_data;
5965
5966 geo->heads = 2;
5967 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11005968 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005969 return 0;
5970}
5971
Al Viroa39907f2008-03-02 10:31:15 -05005972static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973 unsigned int cmd, unsigned long arg)
5974{
5975 int err = 0;
5976 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10005978 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979
5980 if (!capable(CAP_SYS_ADMIN))
5981 return -EACCES;
5982
5983 /*
5984 * Commands dealing with the RAID driver but not any
5985 * particular array:
5986 */
5987 switch (cmd)
5988 {
5989 case RAID_VERSION:
5990 err = get_version(argp);
5991 goto done;
5992
5993 case PRINT_RAID_DEBUG:
5994 err = 0;
5995 md_print_devices();
5996 goto done;
5997
5998#ifndef MODULE
5999 case RAID_AUTORUN:
6000 err = 0;
6001 autostart_arrays(arg);
6002 goto done;
6003#endif
6004 default:;
6005 }
6006
6007 /*
6008 * Commands creating/starting a new array:
6009 */
6010
Al Viroa39907f2008-03-02 10:31:15 -05006011 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012
6013 if (!mddev) {
6014 BUG();
6015 goto abort;
6016 }
6017
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 err = mddev_lock(mddev);
6019 if (err) {
6020 printk(KERN_INFO
6021 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6022 err, cmd);
6023 goto abort;
6024 }
6025
6026 switch (cmd)
6027 {
6028 case SET_ARRAY_INFO:
6029 {
6030 mdu_array_info_t info;
6031 if (!arg)
6032 memset(&info, 0, sizeof(info));
6033 else if (copy_from_user(&info, argp, sizeof(info))) {
6034 err = -EFAULT;
6035 goto abort_unlock;
6036 }
6037 if (mddev->pers) {
6038 err = update_array_info(mddev, &info);
6039 if (err) {
6040 printk(KERN_WARNING "md: couldn't update"
6041 " array info. %d\n", err);
6042 goto abort_unlock;
6043 }
6044 goto done_unlock;
6045 }
6046 if (!list_empty(&mddev->disks)) {
6047 printk(KERN_WARNING
6048 "md: array %s already has disks!\n",
6049 mdname(mddev));
6050 err = -EBUSY;
6051 goto abort_unlock;
6052 }
6053 if (mddev->raid_disks) {
6054 printk(KERN_WARNING
6055 "md: array %s already initialised!\n",
6056 mdname(mddev));
6057 err = -EBUSY;
6058 goto abort_unlock;
6059 }
6060 err = set_array_info(mddev, &info);
6061 if (err) {
6062 printk(KERN_WARNING "md: couldn't set"
6063 " array info. %d\n", err);
6064 goto abort_unlock;
6065 }
6066 }
6067 goto done_unlock;
6068
6069 default:;
6070 }
6071
6072 /*
6073 * Commands querying/configuring an existing array:
6074 */
NeilBrown32a76272005-06-21 17:17:14 -07006075 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006076 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006077 if ((!mddev->raid_disks && !mddev->external)
6078 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6079 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6080 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 err = -ENODEV;
6082 goto abort_unlock;
6083 }
6084
6085 /*
6086 * Commands even a read-only array can execute:
6087 */
6088 switch (cmd)
6089 {
6090 case GET_ARRAY_INFO:
6091 err = get_array_info(mddev, argp);
6092 goto done_unlock;
6093
NeilBrown32a76272005-06-21 17:17:14 -07006094 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01006095 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07006096 goto done_unlock;
6097
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098 case GET_DISK_INFO:
6099 err = get_disk_info(mddev, argp);
6100 goto done_unlock;
6101
6102 case RESTART_ARRAY_RW:
6103 err = restart_array(mddev);
6104 goto done_unlock;
6105
6106 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006107 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108 goto done_unlock;
6109
6110 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11006111 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112 goto done_unlock;
6113
Dan Williamse2218352010-05-12 08:25:37 +10006114 case BLKROSET:
6115 if (get_user(ro, (int __user *)(arg))) {
6116 err = -EFAULT;
6117 goto done_unlock;
6118 }
6119 err = -EINVAL;
6120
6121 /* if the bdev is going readonly the value of mddev->ro
6122 * does not matter, no writes are coming
6123 */
6124 if (ro)
6125 goto done_unlock;
6126
6127 /* are we are already prepared for writes? */
6128 if (mddev->ro != 1)
6129 goto done_unlock;
6130
6131 /* transitioning to readauto need only happen for
6132 * arrays that call md_write_start
6133 */
6134 if (mddev->pers) {
6135 err = restart_array(mddev);
6136 if (err == 0) {
6137 mddev->ro = 2;
6138 set_disk_ro(mddev->gendisk, 0);
6139 }
6140 }
6141 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 }
6143
6144 /*
6145 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006146 * superblock, so we do not allow them on read-only arrays.
6147 * However non-MD ioctls (e.g. get-size) will still come through
6148 * here and hit the 'default' below, so only disallow
6149 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006151 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006152 if (mddev->ro == 2) {
6153 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006154 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006155 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6156 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08006157 } else {
6158 err = -EROFS;
6159 goto abort_unlock;
6160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 }
6162
6163 switch (cmd)
6164 {
6165 case ADD_NEW_DISK:
6166 {
6167 mdu_disk_info_t info;
6168 if (copy_from_user(&info, argp, sizeof(info)))
6169 err = -EFAULT;
6170 else
6171 err = add_new_disk(mddev, &info);
6172 goto done_unlock;
6173 }
6174
6175 case HOT_REMOVE_DISK:
6176 err = hot_remove_disk(mddev, new_decode_dev(arg));
6177 goto done_unlock;
6178
6179 case HOT_ADD_DISK:
6180 err = hot_add_disk(mddev, new_decode_dev(arg));
6181 goto done_unlock;
6182
6183 case SET_DISK_FAULTY:
6184 err = set_disk_faulty(mddev, new_decode_dev(arg));
6185 goto done_unlock;
6186
6187 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006188 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 goto done_unlock;
6190
NeilBrown32a76272005-06-21 17:17:14 -07006191 case SET_BITMAP_FILE:
6192 err = set_bitmap_file(mddev, (int)arg);
6193 goto done_unlock;
6194
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 err = -EINVAL;
6197 goto abort_unlock;
6198 }
6199
6200done_unlock:
6201abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006202 if (mddev->hold_active == UNTIL_IOCTL &&
6203 err != -EINVAL)
6204 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205 mddev_unlock(mddev);
6206
6207 return err;
6208done:
6209 if (err)
6210 MD_BUG();
6211abort:
6212 return err;
6213}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006214#ifdef CONFIG_COMPAT
6215static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6216 unsigned int cmd, unsigned long arg)
6217{
6218 switch (cmd) {
6219 case HOT_REMOVE_DISK:
6220 case HOT_ADD_DISK:
6221 case SET_DISK_FAULTY:
6222 case SET_BITMAP_FILE:
6223 /* These take in integer arg, do not convert */
6224 break;
6225 default:
6226 arg = (unsigned long)compat_ptr(arg);
6227 break;
6228 }
6229
6230 return md_ioctl(bdev, mode, cmd, arg);
6231}
6232#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233
Al Viroa39907f2008-03-02 10:31:15 -05006234static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235{
6236 /*
6237 * Succeed if we can lock the mddev, which confirms that
6238 * it isn't being stopped right now.
6239 */
NeilBrownd3374822009-01-09 08:31:10 +11006240 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 int err;
6242
NeilBrownd3374822009-01-09 08:31:10 +11006243 if (mddev->gendisk != bdev->bd_disk) {
6244 /* we are racing with mddev_put which is discarding this
6245 * bd_disk.
6246 */
6247 mddev_put(mddev);
6248 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006249 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006250 /* Then retry the open from the top */
6251 return -ERESTARTSYS;
6252 }
6253 BUG_ON(mddev != bdev->bd_disk->private_data);
6254
NeilBrownc8c00a62009-08-10 12:50:52 +10006255 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256 goto out;
6257
6258 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006259 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006260 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006262 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 out:
6264 return err;
6265}
6266
Al Viroa39907f2008-03-02 10:31:15 -05006267static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268{
Al Viroa39907f2008-03-02 10:31:15 -05006269 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006271 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006272 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 mddev_put(mddev);
6274
6275 return 0;
6276}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006277
6278static int md_media_changed(struct gendisk *disk)
6279{
6280 mddev_t *mddev = disk->private_data;
6281
6282 return mddev->changed;
6283}
6284
6285static int md_revalidate(struct gendisk *disk)
6286{
6287 mddev_t *mddev = disk->private_data;
6288
6289 mddev->changed = 0;
6290 return 0;
6291}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006292static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293{
6294 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006295 .open = md_open,
6296 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006297 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006298#ifdef CONFIG_COMPAT
6299 .compat_ioctl = md_compat_ioctl,
6300#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006301 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006302 .media_changed = md_media_changed,
6303 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304};
6305
Adrian Bunk75c96f82005-05-05 16:16:09 -07006306static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307{
6308 mdk_thread_t *thread = arg;
6309
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310 /*
6311 * md_thread is a 'system-thread', it's priority should be very
6312 * high. We avoid resource deadlocks individually in each
6313 * raid personality. (RAID5 does preallocation) We also use RR and
6314 * the very same RT priority as kswapd, thus we will never get
6315 * into a priority inversion deadlock.
6316 *
6317 * we definitely have to have equal or higher priority than
6318 * bdflush, otherwise bdflush will deadlock if there are too
6319 * many dirty RAID5 blocks.
6320 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321
NeilBrown6985c432005-10-19 21:23:47 -07006322 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006323 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324
NeilBrown93588e22005-11-15 00:09:12 -08006325 /* We need to wait INTERRUPTIBLE so that
6326 * we don't add to the load-average.
6327 * That means we need to be sure no signals are
6328 * pending
6329 */
6330 if (signal_pending(current))
6331 flush_signals(current);
6332
6333 wait_event_interruptible_timeout
6334 (thread->wqueue,
6335 test_bit(THREAD_WAKEUP, &thread->flags)
6336 || kthread_should_stop(),
6337 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338
NeilBrown6c987912011-01-14 09:13:53 +11006339 clear_bit(THREAD_WAKEUP, &thread->flags);
6340 if (!kthread_should_stop())
NeilBrown589a5942010-12-09 17:02:14 +11006341 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006343
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344 return 0;
6345}
6346
6347void md_wakeup_thread(mdk_thread_t *thread)
6348{
6349 if (thread) {
6350 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
6351 set_bit(THREAD_WAKEUP, &thread->flags);
6352 wake_up(&thread->wqueue);
6353 }
6354}
6355
6356mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
6357 const char *name)
6358{
6359 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360
NeilBrown9ffae0c2006-01-06 00:20:32 -08006361 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 if (!thread)
6363 return NULL;
6364
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365 init_waitqueue_head(&thread->wqueue);
6366
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 thread->run = run;
6368 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006369 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006370 thread->tsk = kthread_run(md_thread, thread,
6371 "%s_%s",
6372 mdname(thread->mddev),
6373 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006374 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 kfree(thread);
6376 return NULL;
6377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378 return thread;
6379}
6380
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381void md_unregister_thread(mdk_thread_t *thread)
6382{
NeilBrowne0cf8f02009-03-31 14:39:39 +11006383 if (!thread)
6384 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07006385 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07006386
6387 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006388 kfree(thread);
6389}
6390
6391void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6392{
6393 if (!mddev) {
6394 MD_BUG();
6395 return;
6396 }
6397
NeilBrownb2d444d2005-11-08 21:39:31 -08006398 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006400
6401 if (mddev->external)
6402 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07006403/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6405 mdname(mddev),
6406 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6407 __builtin_return_address(0),__builtin_return_address(1),
6408 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07006409*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07006410 if (!mddev->pers)
6411 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412 if (!mddev->pers->error_handler)
6413 return;
6414 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006415 if (mddev->degraded)
6416 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006417 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6419 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6420 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006421 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006422 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006423 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424}
6425
6426/* seq_file implementation /proc/mdstat */
6427
6428static void status_unused(struct seq_file *seq)
6429{
6430 int i = 0;
6431 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432
6433 seq_printf(seq, "unused devices: ");
6434
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006435 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436 char b[BDEVNAME_SIZE];
6437 i++;
6438 seq_printf(seq, "%s ",
6439 bdevname(rdev->bdev,b));
6440 }
6441 if (!i)
6442 seq_printf(seq, "<none>");
6443
6444 seq_printf(seq, "\n");
6445}
6446
6447
6448static void status_resync(struct seq_file *seq, mddev_t * mddev)
6449{
NeilBrowndd71cf62009-05-07 12:49:35 +10006450 sector_t max_sectors, resync, res;
6451 unsigned long dt, db;
6452 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006453 int scale;
6454 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006455
NeilBrowndd71cf62009-05-07 12:49:35 +10006456 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457
6458 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006459 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006461 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462
6463 /*
6464 * Should not happen.
6465 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006466 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467 MD_BUG();
6468 return;
6469 }
NeilBrown4588b422006-03-27 01:18:04 -08006470 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006471 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006472 * u32, as those are the requirements for sector_div.
6473 * Thus 'scale' must be at least 10
6474 */
6475 scale = 10;
6476 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006477 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006478 scale++;
6479 }
6480 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006481 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006482
6483 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484 {
NeilBrown4588b422006-03-27 01:18:04 -08006485 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486 seq_printf(seq, "[");
6487 for (i = 0; i < x; i++)
6488 seq_printf(seq, "=");
6489 seq_printf(seq, ">");
6490 for (i = 0; i < y; i++)
6491 seq_printf(seq, ".");
6492 seq_printf(seq, "] ");
6493 }
NeilBrown4588b422006-03-27 01:18:04 -08006494 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006495 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6496 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006497 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6498 "check" :
6499 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6500 "resync" : "recovery"))),
6501 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006502 (unsigned long long) resync/2,
6503 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504
6505 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506 * dt: time from mark until now
6507 * db: blocks written from mark until now
6508 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006509 *
6510 * rt is a sector_t, so could be 32bit or 64bit.
6511 * So we divide before multiply in case it is 32bit and close
6512 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006513 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006514 * near the end of resync when the number of remaining sectors
6515 * is close to 'db'.
6516 * We then divide rt by 32 after multiplying by db to compensate.
6517 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006518 */
6519 dt = ((jiffies - mddev->resync_mark) / HZ);
6520 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006521 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6522 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523
NeilBrowndd71cf62009-05-07 12:49:35 +10006524 rt = max_sectors - resync; /* number of remaining sectors */
6525 sector_div(rt, db/32+1);
6526 rt *= dt;
6527 rt >>= 5;
6528
6529 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6530 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531
NeilBrownff4e8d92006-07-10 04:44:16 -07006532 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533}
6534
6535static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6536{
6537 struct list_head *tmp;
6538 loff_t l = *pos;
6539 mddev_t *mddev;
6540
6541 if (l >= 0x10000)
6542 return NULL;
6543 if (!l--)
6544 /* header */
6545 return (void*)1;
6546
6547 spin_lock(&all_mddevs_lock);
6548 list_for_each(tmp,&all_mddevs)
6549 if (!l--) {
6550 mddev = list_entry(tmp, mddev_t, all_mddevs);
6551 mddev_get(mddev);
6552 spin_unlock(&all_mddevs_lock);
6553 return mddev;
6554 }
6555 spin_unlock(&all_mddevs_lock);
6556 if (!l--)
6557 return (void*)2;/* tail */
6558 return NULL;
6559}
6560
6561static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6562{
6563 struct list_head *tmp;
6564 mddev_t *next_mddev, *mddev = v;
6565
6566 ++*pos;
6567 if (v == (void*)2)
6568 return NULL;
6569
6570 spin_lock(&all_mddevs_lock);
6571 if (v == (void*)1)
6572 tmp = all_mddevs.next;
6573 else
6574 tmp = mddev->all_mddevs.next;
6575 if (tmp != &all_mddevs)
6576 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6577 else {
6578 next_mddev = (void*)2;
6579 *pos = 0x10000;
6580 }
6581 spin_unlock(&all_mddevs_lock);
6582
6583 if (v != (void*)1)
6584 mddev_put(mddev);
6585 return next_mddev;
6586
6587}
6588
6589static void md_seq_stop(struct seq_file *seq, void *v)
6590{
6591 mddev_t *mddev = v;
6592
6593 if (mddev && v != (void*)1 && v != (void*)2)
6594 mddev_put(mddev);
6595}
6596
NeilBrownd7603b72006-01-06 00:20:30 -08006597struct mdstat_info {
6598 int event;
6599};
6600
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601static int md_seq_show(struct seq_file *seq, void *v)
6602{
6603 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006604 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006606 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006607 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608
6609 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006610 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611 seq_printf(seq, "Personalities : ");
6612 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006613 list_for_each_entry(pers, &pers_list, list)
6614 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615
6616 spin_unlock(&pers_lock);
6617 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006618 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619 return 0;
6620 }
6621 if (v == (void*)2) {
6622 status_unused(seq);
6623 return 0;
6624 }
6625
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006626 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006628
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6630 seq_printf(seq, "%s : %sactive", mdname(mddev),
6631 mddev->pers ? "" : "in");
6632 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006633 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006634 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006635 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006636 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637 seq_printf(seq, " %s", mddev->pers->name);
6638 }
6639
Andre Nolldd8ac332009-03-31 14:33:13 +11006640 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006641 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642 char b[BDEVNAME_SIZE];
6643 seq_printf(seq, " %s[%d]",
6644 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006645 if (test_bit(WriteMostly, &rdev->flags))
6646 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006647 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648 seq_printf(seq, "(F)");
6649 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006650 } else if (rdev->raid_disk < 0)
6651 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006652 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653 }
6654
6655 if (!list_empty(&mddev->disks)) {
6656 if (mddev->pers)
6657 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006658 (unsigned long long)
6659 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660 else
6661 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006662 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006664 if (mddev->persistent) {
6665 if (mddev->major_version != 0 ||
6666 mddev->minor_version != 90) {
6667 seq_printf(seq," super %d.%d",
6668 mddev->major_version,
6669 mddev->minor_version);
6670 }
NeilBrowne6910632008-02-06 01:39:51 -08006671 } else if (mddev->external)
6672 seq_printf(seq, " super external:%s",
6673 mddev->metadata_type);
6674 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006675 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676
6677 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006678 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006680 if (mddev->pers->sync_request) {
6681 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006682 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006683 seq_printf(seq, "\n ");
6684 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6685 seq_printf(seq, "\tresync=DELAYED\n ");
6686 else if (mddev->recovery_cp < MaxSector)
6687 seq_printf(seq, "\tresync=PENDING\n ");
6688 }
NeilBrown32a76272005-06-21 17:17:14 -07006689 } else
6690 seq_printf(seq, "\n ");
6691
6692 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006693 unsigned long chunk_kb;
6694 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006695 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006696 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006697 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6698 "%lu%s chunk",
6699 bitmap->pages - bitmap->missing_pages,
6700 bitmap->pages,
6701 (bitmap->pages - bitmap->missing_pages)
6702 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006703 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006704 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006705 if (bitmap->file) {
6706 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006707 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006708 }
NeilBrown78d742d2005-06-21 17:17:15 -07006709
NeilBrown32a76272005-06-21 17:17:14 -07006710 seq_printf(seq, "\n");
6711 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712 }
6713
6714 seq_printf(seq, "\n");
6715 }
6716 mddev_unlock(mddev);
6717
6718 return 0;
6719}
6720
Jan Engelhardt110518b2009-05-07 12:49:37 +10006721static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006722 .start = md_seq_start,
6723 .next = md_seq_next,
6724 .stop = md_seq_stop,
6725 .show = md_seq_show,
6726};
6727
6728static int md_seq_open(struct inode *inode, struct file *file)
6729{
6730 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006731 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6732 if (mi == NULL)
6733 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734
6735 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006736 if (error)
6737 kfree(mi);
6738 else {
6739 struct seq_file *p = file->private_data;
6740 p->private = mi;
6741 mi->event = atomic_read(&md_event_count);
6742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006743 return error;
6744}
6745
NeilBrownd7603b72006-01-06 00:20:30 -08006746static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6747{
6748 struct seq_file *m = filp->private_data;
6749 struct mdstat_info *mi = m->private;
6750 int mask;
6751
6752 poll_wait(filp, &md_event_waiters, wait);
6753
6754 /* always allow read */
6755 mask = POLLIN | POLLRDNORM;
6756
6757 if (mi->event != atomic_read(&md_event_count))
6758 mask |= POLLERR | POLLPRI;
6759 return mask;
6760}
6761
Arjan van de Venfa027c22007-02-12 00:55:33 -08006762static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006763 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764 .open = md_seq_open,
6765 .read = seq_read,
6766 .llseek = seq_lseek,
Martin Peschkec3f94b42007-05-09 02:35:35 -07006767 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006768 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769};
6770
NeilBrown2604b702006-01-06 00:20:36 -08006771int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006774 list_add_tail(&p->list, &pers_list);
6775 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776 spin_unlock(&pers_lock);
6777 return 0;
6778}
6779
NeilBrown2604b702006-01-06 00:20:36 -08006780int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781{
NeilBrown2604b702006-01-06 00:20:36 -08006782 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006784 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785 spin_unlock(&pers_lock);
6786 return 0;
6787}
6788
NeilBrowneea1bf32009-03-31 14:27:02 +11006789static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790{
6791 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006793 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794
6795 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006796 rcu_read_lock();
6797 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006799 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6800 (int)part_stat_read(&disk->part0, sectors[1]) -
6801 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006802 /* sync IO will cause sync_io to increase before the disk_stats
6803 * as sync_io is counted when a request starts, and
6804 * disk_stats is counted when it completes.
6805 * So resync activity will cause curr_events to be smaller than
6806 * when there was no such activity.
6807 * non-sync IO will cause disk_stat to increase without
6808 * increasing sync_io so curr_events will (eventually)
6809 * be larger than it was before. Once it becomes
6810 * substantially larger, the test below will cause
6811 * the array to appear non-idle, and resync will slow
6812 * down.
6813 * If there is a lot of outstanding resync activity when
6814 * we set last_event to curr_events, then all that activity
6815 * completing might cause the array to appear non-idle
6816 * and resync will be slowed down even though there might
6817 * not have been non-resync activity. This will only
6818 * happen once though. 'last_events' will soon reflect
6819 * the state where there is little or no outstanding
6820 * resync requests, and further resync activity will
6821 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006822 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006824 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825 rdev->last_events = curr_events;
6826 idle = 0;
6827 }
6828 }
NeilBrown4b809912008-07-21 17:05:25 +10006829 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830 return idle;
6831}
6832
6833void md_done_sync(mddev_t *mddev, int blocks, int ok)
6834{
6835 /* another "blocks" (512byte) blocks have been synced */
6836 atomic_sub(blocks, &mddev->recovery_active);
6837 wake_up(&mddev->recovery_wait);
6838 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006839 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006840 md_wakeup_thread(mddev->thread);
6841 // stop recovery, signal do_sync ....
6842 }
6843}
6844
6845
NeilBrown06d91a52005-06-21 17:17:12 -07006846/* md_write_start(mddev, bi)
6847 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006848 * in superblock) before writing, schedule a superblock update
6849 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006850 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006851void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852{
Neil Brown0fd62b82008-06-28 08:31:36 +10006853 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006854 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006855 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006856
NeilBrownf91de922005-11-08 21:39:36 -08006857 BUG_ON(mddev->ro == 1);
6858 if (mddev->ro == 2) {
6859 /* need to switch to read/write */
6860 mddev->ro = 0;
6861 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6862 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006863 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006864 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006865 }
NeilBrown06d91a52005-06-21 17:17:12 -07006866 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006867 if (mddev->safemode == 1)
6868 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006869 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006870 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006871 if (mddev->in_sync) {
6872 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006873 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006874 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006875 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006876 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006877 }
NeilBrowna9701a32005-11-08 21:39:34 -08006878 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006879 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006880 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10006881 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006882 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07006883 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884}
6885
6886void md_write_end(mddev_t *mddev)
6887{
6888 if (atomic_dec_and_test(&mddev->writes_pending)) {
6889 if (mddev->safemode == 2)
6890 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006891 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6893 }
6894}
6895
NeilBrown2a2275d2007-01-26 00:57:11 -08006896/* md_allow_write(mddev)
6897 * Calling this ensures that the array is marked 'active' so that writes
6898 * may proceed without blocking. It is important to call this before
6899 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6900 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006901 *
6902 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6903 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006904 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006905int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006906{
6907 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006908 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006909 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006910 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006911 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006912 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006913
6914 spin_lock_irq(&mddev->write_lock);
6915 if (mddev->in_sync) {
6916 mddev->in_sync = 0;
6917 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006918 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08006919 if (mddev->safemode_delay &&
6920 mddev->safemode == 0)
6921 mddev->safemode = 1;
6922 spin_unlock_irq(&mddev->write_lock);
6923 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006924 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006925 } else
6926 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006927
NeilBrown070dc6d2010-08-30 17:33:34 +10006928 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07006929 return -EAGAIN;
6930 else
6931 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006932}
6933EXPORT_SYMBOL_GPL(md_allow_write);
6934
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935#define SYNC_MARKS 10
6936#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006937void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938{
6939 mddev_t *mddev2;
6940 unsigned int currspeed = 0,
6941 window;
NeilBrown57afd892005-06-21 17:17:13 -07006942 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943 unsigned long mark[SYNC_MARKS];
6944 sector_t mark_cnt[SYNC_MARKS];
6945 int last_mark,m;
6946 struct list_head *tmp;
6947 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006948 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006949 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006950 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951
6952 /* just incase thread restarts... */
6953 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6954 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006955 if (mddev->ro) /* never try to sync a read-only array */
6956 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006957
NeilBrown61df9d92006-10-03 01:15:57 -07006958 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6959 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6960 desc = "data-check";
6961 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6962 desc = "requested-resync";
6963 else
6964 desc = "resync";
6965 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6966 desc = "reshape";
6967 else
6968 desc = "recovery";
6969
Linus Torvalds1da177e2005-04-16 15:20:36 -07006970 /* we overload curr_resync somewhat here.
6971 * 0 == not engaged in resync at all
6972 * 2 == checking that there is no conflict with another sync
6973 * 1 == like 2, but have yielded to allow conflicting resync to
6974 * commense
6975 * other == active in resync - this many blocks
6976 *
6977 * Before starting a resync we must have set curr_resync to
6978 * 2, and then checked that every "conflicting" array has curr_resync
6979 * less than ours. When we find one that is the same or higher
6980 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6981 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6982 * This will mean we have to start checking from the beginning again.
6983 *
6984 */
6985
6986 do {
6987 mddev->curr_resync = 2;
6988
6989 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006990 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006991 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006992
6993 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006995 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996 if (mddev2 == mddev)
6997 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006998 if (!mddev->parallel_resync
6999 && mddev2->curr_resync
7000 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001 DEFINE_WAIT(wq);
7002 if (mddev < mddev2 && mddev->curr_resync == 2) {
7003 /* arbitrarily yield */
7004 mddev->curr_resync = 1;
7005 wake_up(&resync_wait);
7006 }
7007 if (mddev > mddev2 && mddev->curr_resync == 1)
7008 /* no need to wait here, we can wait the next
7009 * time 'round when curr_resync == 2
7010 */
7011 continue;
NeilBrown97441972008-09-19 11:49:54 +10007012 /* We need to wait 'interruptible' so as not to
7013 * contribute to the load average, and not to
7014 * be caught by 'softlockup'
7015 */
7016 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08007017 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07007018 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007019 printk(KERN_INFO "md: delaying %s of %s"
7020 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007022 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007024 if (signal_pending(current))
7025 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026 schedule();
7027 finish_wait(&resync_wait, &wq);
7028 goto try_again;
7029 }
7030 finish_wait(&resync_wait, &wq);
7031 }
7032 }
7033 } while (mddev->curr_resync < 2);
7034
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007035 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007036 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007038 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039 */
7040 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08007041 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007042 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007043 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7044 j = mddev->resync_min;
7045 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007046 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007047
NeilBrownccfcc3c2006-03-27 01:18:09 -08007048 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11007049 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007050 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007051 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007052 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007053 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007054 rcu_read_lock();
7055 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007056 if (rdev->raid_disk >= 0 &&
7057 !test_bit(Faulty, &rdev->flags) &&
7058 !test_bit(In_sync, &rdev->flags) &&
7059 rdev->recovery_offset < j)
7060 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007061 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063
NeilBrown61df9d92006-10-03 01:15:57 -07007064 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7065 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7066 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007067 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007068 "(but not more than %d KB/sec) for %s.\n",
7069 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070
NeilBrowneea1bf32009-03-31 14:27:02 +11007071 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007072
NeilBrown57afd892005-06-21 17:17:13 -07007073 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074 for (m = 0; m < SYNC_MARKS; m++) {
7075 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007076 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007077 }
7078 last_mark = 0;
7079 mddev->resync_mark = mark[last_mark];
7080 mddev->resync_mark_cnt = mark_cnt[last_mark];
7081
7082 /*
7083 * Tune reconstruction:
7084 */
7085 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007086 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7087 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088
7089 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007090 last_check = 0;
7091
7092 if (j>2) {
7093 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007094 "md: resuming %s of %s from checkpoint.\n",
7095 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096 mddev->curr_resync = j;
7097 }
NeilBrown75d3da42011-01-14 09:14:34 +11007098 mddev->curr_resync_completed = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007099
7100 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007101 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102
NeilBrown57afd892005-06-21 17:17:13 -07007103 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007104
NeilBrown7a91ee12009-05-26 12:57:21 +10007105 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7106 ((mddev->curr_resync > mddev->curr_resync_completed &&
7107 (mddev->curr_resync - mddev->curr_resync_completed)
7108 > (max_sectors >> 4)) ||
7109 (j - mddev->curr_resync_completed)*2
7110 >= mddev->resync_max - mddev->curr_resync_completed
7111 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007112 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007113 wait_event(mddev->recovery_wait,
7114 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007115 mddev->curr_resync_completed = j;
NeilBrown070dc6d2010-08-30 17:33:34 +10007116 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007117 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007118 }
NeilBrownacb180b2009-04-14 16:28:34 +10007119
NeilBrowne62e58a2009-07-01 13:15:35 +10007120 while (j >= mddev->resync_max && !kthread_should_stop()) {
7121 /* As this condition is controlled by user-space,
7122 * we can block indefinitely, so use '_interruptible'
7123 * to avoid triggering warnings.
7124 */
7125 flush_signals(current); /* just in case */
7126 wait_event_interruptible(mddev->recovery_wait,
7127 mddev->resync_max > j
7128 || kthread_should_stop());
7129 }
NeilBrownacb180b2009-04-14 16:28:34 +10007130
7131 if (kthread_should_stop())
7132 goto interrupted;
7133
NeilBrown57afd892005-06-21 17:17:13 -07007134 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007135 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007136 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007137 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138 goto out;
7139 }
NeilBrown57afd892005-06-21 17:17:13 -07007140
7141 if (!skipped) { /* actual IO requested */
7142 io_sectors += sectors;
7143 atomic_add(sectors, &mddev->recovery_active);
7144 }
7145
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146 j += sectors;
7147 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007148 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007149 if (last_check == 0)
7150 /* this is the earliers that rebuilt will be
7151 * visible in /proc/mdstat
7152 */
7153 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007154
7155 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156 continue;
7157
NeilBrown57afd892005-06-21 17:17:13 -07007158 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159
NeilBrowndfc70642008-05-23 13:04:39 -07007160 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161 break;
7162
7163 repeat:
7164 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7165 /* step marks */
7166 int next = (last_mark+1) % SYNC_MARKS;
7167
7168 mddev->resync_mark = mark[next];
7169 mddev->resync_mark_cnt = mark_cnt[next];
7170 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007171 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007172 last_mark = next;
7173 }
7174
7175
NeilBrownc6207272008-02-06 01:39:52 -08007176 if (kthread_should_stop())
7177 goto interrupted;
7178
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179
7180 /*
7181 * this loop exits only if either when we are slower than
7182 * the 'hard' speed limit, or the system was IO-idle for
7183 * a jiffy.
7184 * the system might be non-idle CPU-wise, but we only care
7185 * about not overloading the IO subsystem. (things like an
7186 * e2fsck being done on the RAID array should execute fast)
7187 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188 cond_resched();
7189
NeilBrown57afd892005-06-21 17:17:13 -07007190 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7191 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192
NeilBrown88202a02006-01-06 00:21:36 -08007193 if (currspeed > speed_min(mddev)) {
7194 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007195 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007196 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197 goto repeat;
7198 }
7199 }
7200 }
NeilBrown61df9d92006-10-03 01:15:57 -07007201 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202 /*
7203 * this also signals 'finished resyncing' to md_stop
7204 */
7205 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7207
7208 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007209 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210
NeilBrowndfc70642008-05-23 13:04:39 -07007211 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007212 mddev->curr_resync > 2) {
7213 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7214 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7215 if (mddev->curr_resync >= mddev->recovery_cp) {
7216 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007217 "md: checkpointing %s of %s.\n",
7218 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007219 mddev->recovery_cp = mddev->curr_resync;
7220 }
7221 } else
7222 mddev->recovery_cp = MaxSector;
7223 } else {
7224 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7225 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007226 rcu_read_lock();
7227 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007228 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007229 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007230 !test_bit(Faulty, &rdev->flags) &&
7231 !test_bit(In_sync, &rdev->flags) &&
7232 rdev->recovery_offset < mddev->curr_resync)
7233 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007234 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236 }
NeilBrown17571282006-12-10 02:20:52 -08007237 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007238
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11007240 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7241 /* We completed so min/max setting can be forgotten if used. */
7242 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7243 mddev->resync_min = 0;
7244 mddev->resync_max = MaxSector;
7245 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7246 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007247 mddev->curr_resync = 0;
7248 wake_up(&resync_wait);
7249 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7250 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007251 return;
7252
7253 interrupted:
7254 /*
7255 * got a signal, exit.
7256 */
7257 printk(KERN_INFO
7258 "md: md_do_sync() got signal ... exiting\n");
7259 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7260 goto out;
7261
Linus Torvalds1da177e2005-04-16 15:20:36 -07007262}
NeilBrown29269552006-03-27 01:18:10 -08007263EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007264
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007265static int remove_and_add_spares(mddev_t *mddev)
7266{
7267 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007268 int spares = 0;
7269
NeilBrown97e4f422009-03-31 14:33:13 +11007270 mddev->curr_resync_completed = 0;
7271
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007272 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007273 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007274 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007275 (test_bit(Faulty, &rdev->flags) ||
7276 ! test_bit(In_sync, &rdev->flags)) &&
7277 atomic_read(&rdev->nr_pending)==0) {
7278 if (mddev->pers->hot_remove_disk(
7279 mddev, rdev->raid_disk)==0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007280 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007281 rdev->raid_disk = -1;
7282 }
7283 }
7284
NeilBrown53890422011-07-27 11:00:36 +10007285 if (mddev->degraded) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007286 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07007287 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07007288 !test_bit(In_sync, &rdev->flags) &&
NeilBrown42742152011-06-28 16:59:42 +10007289 !test_bit(Faulty, &rdev->flags) &&
Dan Williamse5427132008-07-28 23:28:06 -07007290 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07007291 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007292 if (rdev->raid_disk < 0
7293 && !test_bit(Faulty, &rdev->flags)) {
7294 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10007295 if (mddev->pers->
7296 hot_add_disk(mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007297 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10007298 /* failure here is OK */;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007299 spares++;
7300 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11007301 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007302 } else
7303 break;
7304 }
NeilBrowndfc70642008-05-23 13:04:39 -07007305 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007306 }
7307 return spares;
7308}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007309
7310static void reap_sync_thread(mddev_t *mddev)
7311{
7312 mdk_rdev_t *rdev;
7313
7314 /* resync has finished, collect result */
7315 md_unregister_thread(mddev->sync_thread);
7316 mddev->sync_thread = NULL;
7317 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7318 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7319 /* success...*/
7320 /* activate any spares */
7321 if (mddev->pers->spare_active(mddev))
7322 sysfs_notify(&mddev->kobj, NULL,
7323 "degraded");
7324 }
7325 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7326 mddev->pers->finish_reshape)
7327 mddev->pers->finish_reshape(mddev);
7328 md_update_sb(mddev, 1);
7329
7330 /* if array is no-longer degraded, then any saved_raid_disk
7331 * information must be scrapped
7332 */
7333 if (!mddev->degraded)
7334 list_for_each_entry(rdev, &mddev->disks, same_set)
7335 rdev->saved_raid_disk = -1;
7336
7337 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7338 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7339 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7340 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7341 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7342 /* flag recovery needed just to double check */
7343 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7344 sysfs_notify_dirent_safe(mddev->sysfs_action);
7345 md_new_event(mddev);
Jonathan Brassow768e5872011-07-27 11:00:37 +10007346 if (mddev->event_work.func)
7347 queue_work(md_misc_wq, &mddev->event_work);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007348}
7349
Linus Torvalds1da177e2005-04-16 15:20:36 -07007350/*
7351 * This routine is regularly called by all per-raid-array threads to
7352 * deal with generic issues like resync and super-block update.
7353 * Raid personalities that don't have a thread (linear/raid0) do not
7354 * need this as they never do any recovery or update the superblock.
7355 *
7356 * It does not do any resync itself, but rather "forks" off other threads
7357 * to do that as needed.
7358 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7359 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007360 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007361 * and wakeups up this thread which will reap the thread and finish up.
7362 * This thread also removes any faulty devices (with nr_pending == 0).
7363 *
7364 * The overall approach is:
7365 * 1/ if the superblock needs updating, update it.
7366 * 2/ If a recovery thread is running, don't do anything else.
7367 * 3/ If recovery has finished, clean up, possibly marking spares active.
7368 * 4/ If there are any faulty devices, remove them.
7369 * 5/ If array is degraded, try to add spares devices
7370 * 6/ If array has spares or is not in-sync, start a resync thread.
7371 */
7372void md_check_recovery(mddev_t *mddev)
7373{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007374 if (mddev->suspended)
7375 return;
7376
NeilBrown5f404022005-06-21 17:17:16 -07007377 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007378 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007379
NeilBrownfca4d842005-06-21 17:17:11 -07007380 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007381 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007382 printk(KERN_INFO "md: %s in immediate safe mode\n",
7383 mdname(mddev));
7384 mddev->safemode = 2;
7385 }
7386 flush_signals(current);
7387 }
7388
NeilBrownc89a8ee2008-08-05 15:54:13 +10007389 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7390 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007392 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007393 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007394 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007395 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007396 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7397 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007398 ))
7399 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007400
NeilBrowndf5b89b2006-03-27 01:18:20 -08007401 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007402 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007403
NeilBrownc89a8ee2008-08-05 15:54:13 +10007404 if (mddev->ro) {
7405 /* Only thing we do on a ro array is remove
7406 * failed devices.
7407 */
NeilBrowna8c42c72011-01-31 13:47:13 +11007408 mdk_rdev_t *rdev;
7409 list_for_each_entry(rdev, &mddev->disks, same_set)
7410 if (rdev->raid_disk >= 0 &&
7411 !test_bit(Blocked, &rdev->flags) &&
7412 test_bit(Faulty, &rdev->flags) &&
7413 atomic_read(&rdev->nr_pending)==0) {
7414 if (mddev->pers->hot_remove_disk(
7415 mddev, rdev->raid_disk)==0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007416 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna8c42c72011-01-31 13:47:13 +11007417 rdev->raid_disk = -1;
7418 }
7419 }
NeilBrownc89a8ee2008-08-05 15:54:13 +10007420 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7421 goto unlock;
7422 }
7423
NeilBrown31a59e32008-04-30 00:52:30 -07007424 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007425 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007426 spin_lock_irq(&mddev->write_lock);
7427 if (mddev->safemode &&
7428 !atomic_read(&mddev->writes_pending) &&
7429 !mddev->in_sync &&
7430 mddev->recovery_cp == MaxSector) {
7431 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007432 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007433 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007434 }
7435 if (mddev->safemode == 1)
7436 mddev->safemode = 0;
7437 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007438 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007439 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007440 }
NeilBrownfca4d842005-06-21 17:17:11 -07007441
NeilBrown850b2b42006-10-03 01:15:46 -07007442 if (mddev->flags)
7443 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007444
Linus Torvalds1da177e2005-04-16 15:20:36 -07007445 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7446 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7447 /* resync/recovery still happening */
7448 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7449 goto unlock;
7450 }
7451 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007452 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007453 goto unlock;
7454 }
Neil Brown72a23c22008-06-28 08:31:41 +10007455 /* Set RUNNING before clearing NEEDED to avoid
7456 * any transients in the value of "sync_action".
7457 */
7458 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7459 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007460 /* Clear some bits that don't mean anything, but
7461 * might be left set
7462 */
NeilBrown24dd4692005-11-08 21:39:26 -08007463 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7464 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007465
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007466 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7467 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007468 /* no recovery is running.
7469 * remove any failed drives, then
7470 * add spares if possible.
7471 * Spare are also removed and re-added, to allow
7472 * the personality to fail the re-add.
7473 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007474
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007475 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007476 if (mddev->pers->check_reshape == NULL ||
7477 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007478 /* Cannot proceed */
7479 goto unlock;
7480 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007481 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007482 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007483 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7484 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007485 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007486 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007487 } else if (mddev->recovery_cp < MaxSector) {
7488 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007489 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007490 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7491 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007492 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007493
Linus Torvalds1da177e2005-04-16 15:20:36 -07007494 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007495 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7496 /* We are adding a device or devices to an array
7497 * which has the bitmap stored on all devices.
7498 * So make sure all bitmap pages get written
7499 */
7500 bitmap_write_all(mddev->bitmap);
7501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007502 mddev->sync_thread = md_register_thread(md_do_sync,
7503 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007504 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007505 if (!mddev->sync_thread) {
7506 printk(KERN_ERR "%s: could not start resync"
7507 " thread...\n",
7508 mdname(mddev));
7509 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007510 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7511 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7512 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7513 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7514 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007515 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007516 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007517 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007518 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007519 }
7520 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007521 if (!mddev->sync_thread) {
7522 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7523 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7524 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007525 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007526 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007528 mddev_unlock(mddev);
7529 }
7530}
7531
Dan Williams6bfe0b42008-04-30 00:52:32 -07007532void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7533{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007534 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007535 wait_event_timeout(rdev->blocked_wait,
7536 !test_bit(Blocked, &rdev->flags),
7537 msecs_to_jiffies(5000));
7538 rdev_dec_pending(rdev, mddev);
7539}
7540EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7541
NeilBrown2230dfe2011-07-28 11:31:46 +10007542
7543/* Bad block management.
7544 * We can record which blocks on each device are 'bad' and so just
7545 * fail those blocks, or that stripe, rather than the whole device.
7546 * Entries in the bad-block table are 64bits wide. This comprises:
7547 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7548 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7549 * A 'shift' can be set so that larger blocks are tracked and
7550 * consequently larger devices can be covered.
7551 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7552 *
7553 * Locking of the bad-block table uses a seqlock so md_is_badblock
7554 * might need to retry if it is very unlucky.
7555 * We will sometimes want to check for bad blocks in a bi_end_io function,
7556 * so we use the write_seqlock_irq variant.
7557 *
7558 * When looking for a bad block we specify a range and want to
7559 * know if any block in the range is bad. So we binary-search
7560 * to the last range that starts at-or-before the given endpoint,
7561 * (or "before the sector after the target range")
7562 * then see if it ends after the given start.
7563 * We return
7564 * 0 if there are no known bad blocks in the range
7565 * 1 if there are known bad block which are all acknowledged
7566 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7567 * plus the start/length of the first bad section we overlap.
7568 */
7569int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7570 sector_t *first_bad, int *bad_sectors)
7571{
7572 int hi;
7573 int lo = 0;
7574 u64 *p = bb->page;
7575 int rv = 0;
7576 sector_t target = s + sectors;
7577 unsigned seq;
7578
7579 if (bb->shift > 0) {
7580 /* round the start down, and the end up */
7581 s >>= bb->shift;
7582 target += (1<<bb->shift) - 1;
7583 target >>= bb->shift;
7584 sectors = target - s;
7585 }
7586 /* 'target' is now the first block after the bad range */
7587
7588retry:
7589 seq = read_seqbegin(&bb->lock);
7590
7591 hi = bb->count;
7592
7593 /* Binary search between lo and hi for 'target'
7594 * i.e. for the last range that starts before 'target'
7595 */
7596 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7597 * are known not to be the last range before target.
7598 * VARIANT: hi-lo is the number of possible
7599 * ranges, and decreases until it reaches 1
7600 */
7601 while (hi - lo > 1) {
7602 int mid = (lo + hi) / 2;
7603 sector_t a = BB_OFFSET(p[mid]);
7604 if (a < target)
7605 /* This could still be the one, earlier ranges
7606 * could not. */
7607 lo = mid;
7608 else
7609 /* This and later ranges are definitely out. */
7610 hi = mid;
7611 }
7612 /* 'lo' might be the last that started before target, but 'hi' isn't */
7613 if (hi > lo) {
7614 /* need to check all range that end after 's' to see if
7615 * any are unacknowledged.
7616 */
7617 while (lo >= 0 &&
7618 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7619 if (BB_OFFSET(p[lo]) < target) {
7620 /* starts before the end, and finishes after
7621 * the start, so they must overlap
7622 */
7623 if (rv != -1 && BB_ACK(p[lo]))
7624 rv = 1;
7625 else
7626 rv = -1;
7627 *first_bad = BB_OFFSET(p[lo]);
7628 *bad_sectors = BB_LEN(p[lo]);
7629 }
7630 lo--;
7631 }
7632 }
7633
7634 if (read_seqretry(&bb->lock, seq))
7635 goto retry;
7636
7637 return rv;
7638}
7639EXPORT_SYMBOL_GPL(md_is_badblock);
7640
7641/*
7642 * Add a range of bad blocks to the table.
7643 * This might extend the table, or might contract it
7644 * if two adjacent ranges can be merged.
7645 * We binary-search to find the 'insertion' point, then
7646 * decide how best to handle it.
7647 */
7648static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
7649 int acknowledged)
7650{
7651 u64 *p;
7652 int lo, hi;
7653 int rv = 1;
7654
7655 if (bb->shift < 0)
7656 /* badblocks are disabled */
7657 return 0;
7658
7659 if (bb->shift) {
7660 /* round the start down, and the end up */
7661 sector_t next = s + sectors;
7662 s >>= bb->shift;
7663 next += (1<<bb->shift) - 1;
7664 next >>= bb->shift;
7665 sectors = next - s;
7666 }
7667
7668 write_seqlock_irq(&bb->lock);
7669
7670 p = bb->page;
7671 lo = 0;
7672 hi = bb->count;
7673 /* Find the last range that starts at-or-before 's' */
7674 while (hi - lo > 1) {
7675 int mid = (lo + hi) / 2;
7676 sector_t a = BB_OFFSET(p[mid]);
7677 if (a <= s)
7678 lo = mid;
7679 else
7680 hi = mid;
7681 }
7682 if (hi > lo && BB_OFFSET(p[lo]) > s)
7683 hi = lo;
7684
7685 if (hi > lo) {
7686 /* we found a range that might merge with the start
7687 * of our new range
7688 */
7689 sector_t a = BB_OFFSET(p[lo]);
7690 sector_t e = a + BB_LEN(p[lo]);
7691 int ack = BB_ACK(p[lo]);
7692 if (e >= s) {
7693 /* Yes, we can merge with a previous range */
7694 if (s == a && s + sectors >= e)
7695 /* new range covers old */
7696 ack = acknowledged;
7697 else
7698 ack = ack && acknowledged;
7699
7700 if (e < s + sectors)
7701 e = s + sectors;
7702 if (e - a <= BB_MAX_LEN) {
7703 p[lo] = BB_MAKE(a, e-a, ack);
7704 s = e;
7705 } else {
7706 /* does not all fit in one range,
7707 * make p[lo] maximal
7708 */
7709 if (BB_LEN(p[lo]) != BB_MAX_LEN)
7710 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
7711 s = a + BB_MAX_LEN;
7712 }
7713 sectors = e - s;
7714 }
7715 }
7716 if (sectors && hi < bb->count) {
7717 /* 'hi' points to the first range that starts after 's'.
7718 * Maybe we can merge with the start of that range */
7719 sector_t a = BB_OFFSET(p[hi]);
7720 sector_t e = a + BB_LEN(p[hi]);
7721 int ack = BB_ACK(p[hi]);
7722 if (a <= s + sectors) {
7723 /* merging is possible */
7724 if (e <= s + sectors) {
7725 /* full overlap */
7726 e = s + sectors;
7727 ack = acknowledged;
7728 } else
7729 ack = ack && acknowledged;
7730
7731 a = s;
7732 if (e - a <= BB_MAX_LEN) {
7733 p[hi] = BB_MAKE(a, e-a, ack);
7734 s = e;
7735 } else {
7736 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
7737 s = a + BB_MAX_LEN;
7738 }
7739 sectors = e - s;
7740 lo = hi;
7741 hi++;
7742 }
7743 }
7744 if (sectors == 0 && hi < bb->count) {
7745 /* we might be able to combine lo and hi */
7746 /* Note: 's' is at the end of 'lo' */
7747 sector_t a = BB_OFFSET(p[hi]);
7748 int lolen = BB_LEN(p[lo]);
7749 int hilen = BB_LEN(p[hi]);
7750 int newlen = lolen + hilen - (s - a);
7751 if (s >= a && newlen < BB_MAX_LEN) {
7752 /* yes, we can combine them */
7753 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
7754 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
7755 memmove(p + hi, p + hi + 1,
7756 (bb->count - hi - 1) * 8);
7757 bb->count--;
7758 }
7759 }
7760 while (sectors) {
7761 /* didn't merge (it all).
7762 * Need to add a range just before 'hi' */
7763 if (bb->count >= MD_MAX_BADBLOCKS) {
7764 /* No room for more */
7765 rv = 0;
7766 break;
7767 } else {
7768 int this_sectors = sectors;
7769 memmove(p + hi + 1, p + hi,
7770 (bb->count - hi) * 8);
7771 bb->count++;
7772
7773 if (this_sectors > BB_MAX_LEN)
7774 this_sectors = BB_MAX_LEN;
7775 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
7776 sectors -= this_sectors;
7777 s += this_sectors;
7778 }
7779 }
7780
7781 bb->changed = 1;
7782 write_sequnlock_irq(&bb->lock);
7783
7784 return rv;
7785}
7786
7787int rdev_set_badblocks(mdk_rdev_t *rdev, sector_t s, int sectors,
7788 int acknowledged)
7789{
7790 int rv = md_set_badblocks(&rdev->badblocks,
7791 s + rdev->data_offset, sectors, acknowledged);
7792 if (rv) {
7793 /* Make sure they get written out promptly */
7794 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
7795 md_wakeup_thread(rdev->mddev->thread);
7796 }
7797 return rv;
7798}
7799EXPORT_SYMBOL_GPL(rdev_set_badblocks);
7800
7801/*
7802 * Remove a range of bad blocks from the table.
7803 * This may involve extending the table if we spilt a region,
7804 * but it must not fail. So if the table becomes full, we just
7805 * drop the remove request.
7806 */
7807static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
7808{
7809 u64 *p;
7810 int lo, hi;
7811 sector_t target = s + sectors;
7812 int rv = 0;
7813
7814 if (bb->shift > 0) {
7815 /* When clearing we round the start up and the end down.
7816 * This should not matter as the shift should align with
7817 * the block size and no rounding should ever be needed.
7818 * However it is better the think a block is bad when it
7819 * isn't than to think a block is not bad when it is.
7820 */
7821 s += (1<<bb->shift) - 1;
7822 s >>= bb->shift;
7823 target >>= bb->shift;
7824 sectors = target - s;
7825 }
7826
7827 write_seqlock_irq(&bb->lock);
7828
7829 p = bb->page;
7830 lo = 0;
7831 hi = bb->count;
7832 /* Find the last range that starts before 'target' */
7833 while (hi - lo > 1) {
7834 int mid = (lo + hi) / 2;
7835 sector_t a = BB_OFFSET(p[mid]);
7836 if (a < target)
7837 lo = mid;
7838 else
7839 hi = mid;
7840 }
7841 if (hi > lo) {
7842 /* p[lo] is the last range that could overlap the
7843 * current range. Earlier ranges could also overlap,
7844 * but only this one can overlap the end of the range.
7845 */
7846 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
7847 /* Partial overlap, leave the tail of this range */
7848 int ack = BB_ACK(p[lo]);
7849 sector_t a = BB_OFFSET(p[lo]);
7850 sector_t end = a + BB_LEN(p[lo]);
7851
7852 if (a < s) {
7853 /* we need to split this range */
7854 if (bb->count >= MD_MAX_BADBLOCKS) {
7855 rv = 0;
7856 goto out;
7857 }
7858 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
7859 bb->count++;
7860 p[lo] = BB_MAKE(a, s-a, ack);
7861 lo++;
7862 }
7863 p[lo] = BB_MAKE(target, end - target, ack);
7864 /* there is no longer an overlap */
7865 hi = lo;
7866 lo--;
7867 }
7868 while (lo >= 0 &&
7869 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7870 /* This range does overlap */
7871 if (BB_OFFSET(p[lo]) < s) {
7872 /* Keep the early parts of this range. */
7873 int ack = BB_ACK(p[lo]);
7874 sector_t start = BB_OFFSET(p[lo]);
7875 p[lo] = BB_MAKE(start, s - start, ack);
7876 /* now low doesn't overlap, so.. */
7877 break;
7878 }
7879 lo--;
7880 }
7881 /* 'lo' is strictly before, 'hi' is strictly after,
7882 * anything between needs to be discarded
7883 */
7884 if (hi - lo > 1) {
7885 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
7886 bb->count -= (hi - lo - 1);
7887 }
7888 }
7889
7890 bb->changed = 1;
7891out:
7892 write_sequnlock_irq(&bb->lock);
7893 return rv;
7894}
7895
7896int rdev_clear_badblocks(mdk_rdev_t *rdev, sector_t s, int sectors)
7897{
7898 return md_clear_badblocks(&rdev->badblocks,
7899 s + rdev->data_offset,
7900 sectors);
7901}
7902EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
7903
7904/*
7905 * Acknowledge all bad blocks in a list.
7906 * This only succeeds if ->changed is clear. It is used by
7907 * in-kernel metadata updates
7908 */
7909void md_ack_all_badblocks(struct badblocks *bb)
7910{
7911 if (bb->page == NULL || bb->changed)
7912 /* no point even trying */
7913 return;
7914 write_seqlock_irq(&bb->lock);
7915
7916 if (bb->changed == 0) {
7917 u64 *p = bb->page;
7918 int i;
7919 for (i = 0; i < bb->count ; i++) {
7920 if (!BB_ACK(p[i])) {
7921 sector_t start = BB_OFFSET(p[i]);
7922 int len = BB_LEN(p[i]);
7923 p[i] = BB_MAKE(start, len, 1);
7924 }
7925 }
7926 }
7927 write_sequnlock_irq(&bb->lock);
7928}
7929EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
7930
NeilBrown16c791a2011-07-28 11:31:47 +10007931/* sysfs access to bad-blocks list.
7932 * We present two files.
7933 * 'bad-blocks' lists sector numbers and lengths of ranges that
7934 * are recorded as bad. The list is truncated to fit within
7935 * the one-page limit of sysfs.
7936 * Writing "sector length" to this file adds an acknowledged
7937 * bad block list.
7938 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
7939 * been acknowledged. Writing to this file adds bad blocks
7940 * without acknowledging them. This is largely for testing.
7941 */
7942
7943static ssize_t
7944badblocks_show(struct badblocks *bb, char *page, int unack)
7945{
7946 size_t len;
7947 int i;
7948 u64 *p = bb->page;
7949 unsigned seq;
7950
7951 if (bb->shift < 0)
7952 return 0;
7953
7954retry:
7955 seq = read_seqbegin(&bb->lock);
7956
7957 len = 0;
7958 i = 0;
7959
7960 while (len < PAGE_SIZE && i < bb->count) {
7961 sector_t s = BB_OFFSET(p[i]);
7962 unsigned int length = BB_LEN(p[i]);
7963 int ack = BB_ACK(p[i]);
7964 i++;
7965
7966 if (unack && ack)
7967 continue;
7968
7969 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
7970 (unsigned long long)s << bb->shift,
7971 length << bb->shift);
7972 }
7973
7974 if (read_seqretry(&bb->lock, seq))
7975 goto retry;
7976
7977 return len;
7978}
7979
7980#define DO_DEBUG 1
7981
7982static ssize_t
7983badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
7984{
7985 unsigned long long sector;
7986 int length;
7987 char newline;
7988#ifdef DO_DEBUG
7989 /* Allow clearing via sysfs *only* for testing/debugging.
7990 * Normally only a successful write may clear a badblock
7991 */
7992 int clear = 0;
7993 if (page[0] == '-') {
7994 clear = 1;
7995 page++;
7996 }
7997#endif /* DO_DEBUG */
7998
7999 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8000 case 3:
8001 if (newline != '\n')
8002 return -EINVAL;
8003 case 2:
8004 if (length <= 0)
8005 return -EINVAL;
8006 break;
8007 default:
8008 return -EINVAL;
8009 }
8010
8011#ifdef DO_DEBUG
8012 if (clear) {
8013 md_clear_badblocks(bb, sector, length);
8014 return len;
8015 }
8016#endif /* DO_DEBUG */
8017 if (md_set_badblocks(bb, sector, length, !unack))
8018 return len;
8019 else
8020 return -ENOSPC;
8021}
8022
Adrian Bunk75c96f82005-05-05 16:16:09 -07008023static int md_notify_reboot(struct notifier_block *this,
8024 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008025{
8026 struct list_head *tmp;
8027 mddev_t *mddev;
8028
8029 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
8030
8031 printk(KERN_INFO "md: stopping all md devices.\n");
8032
NeilBrown29ac4aa2008-02-06 01:39:58 -08008033 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10008034 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10008035 /* Force a switch to readonly even array
8036 * appears to still be in use. Hence
8037 * the '100'.
8038 */
NeilBrowna4bd82d2010-03-29 13:23:10 +11008039 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10008040 mddev_unlock(mddev);
8041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008042 /*
8043 * certain more exotic SCSI devices are known to be
8044 * volatile wrt too early system reboots. While the
8045 * right place to handle this issue is the given
8046 * driver, we do want to have a safe RAID driver ...
8047 */
8048 mdelay(1000*1);
8049 }
8050 return NOTIFY_DONE;
8051}
8052
Adrian Bunk75c96f82005-05-05 16:16:09 -07008053static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008054 .notifier_call = md_notify_reboot,
8055 .next = NULL,
8056 .priority = INT_MAX, /* before any real devices */
8057};
8058
8059static void md_geninit(void)
8060{
Linus Torvalds1da177e2005-04-16 15:20:36 -07008061 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
8062
Denis V. Lunevc7705f32008-04-29 01:02:35 -07008063 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008064}
8065
Adrian Bunk75c96f82005-05-05 16:16:09 -07008066static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008067{
Tejun Heoe804ac72010-10-15 15:36:08 +02008068 int ret = -ENOMEM;
8069
Tejun Heoada609e2011-01-25 14:35:54 +01008070 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008071 if (!md_wq)
8072 goto err_wq;
8073
8074 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8075 if (!md_misc_wq)
8076 goto err_misc_wq;
8077
8078 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8079 goto err_md;
8080
8081 if ((ret = register_blkdev(0, "mdp")) < 0)
8082 goto err_mdp;
8083 mdp_major = ret;
8084
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008085 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008086 md_probe, NULL, NULL);
8087 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008088 md_probe, NULL, NULL);
8089
Linus Torvalds1da177e2005-04-16 15:20:36 -07008090 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008091 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008092
8093 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008094 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008095
Tejun Heoe804ac72010-10-15 15:36:08 +02008096err_mdp:
8097 unregister_blkdev(MD_MAJOR, "md");
8098err_md:
8099 destroy_workqueue(md_misc_wq);
8100err_misc_wq:
8101 destroy_workqueue(md_wq);
8102err_wq:
8103 return ret;
8104}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008105
8106#ifndef MODULE
8107
8108/*
8109 * Searches all registered partitions for autorun RAID arrays
8110 * at boot time.
8111 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008112
8113static LIST_HEAD(all_detected_devices);
8114struct detected_devices_node {
8115 struct list_head list;
8116 dev_t dev;
8117};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008118
8119void md_autodetect_dev(dev_t dev)
8120{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008121 struct detected_devices_node *node_detected_dev;
8122
8123 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8124 if (node_detected_dev) {
8125 node_detected_dev->dev = dev;
8126 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8127 } else {
8128 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8129 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008131}
8132
8133
8134static void autostart_arrays(int part)
8135{
8136 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008137 struct detected_devices_node *node_detected_dev;
8138 dev_t dev;
8139 int i_scanned, i_passed;
8140
8141 i_scanned = 0;
8142 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008143
8144 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8145
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008146 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8147 i_scanned++;
8148 node_detected_dev = list_entry(all_detected_devices.next,
8149 struct detected_devices_node, list);
8150 list_del(&node_detected_dev->list);
8151 dev = node_detected_dev->dev;
8152 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008153 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008154 if (IS_ERR(rdev))
8155 continue;
8156
NeilBrownb2d444d2005-11-08 21:39:31 -08008157 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008158 MD_BUG();
8159 continue;
8160 }
NeilBrownd0fae182008-03-04 14:29:31 -08008161 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008162 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008163 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008164 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008165
8166 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8167 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008168
8169 autorun_devices(part);
8170}
8171
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008172#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008173
8174static __exit void md_exit(void)
8175{
8176 mddev_t *mddev;
8177 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008178
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008179 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008180 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008181
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008182 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008183 unregister_blkdev(mdp_major, "mdp");
8184 unregister_reboot_notifier(&md_notifier);
8185 unregister_sysctl_table(raid_table_header);
8186 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008187 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008188 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008189 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008190 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008191 destroy_workqueue(md_misc_wq);
8192 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008193}
8194
Dan Williams685784a2007-07-09 11:56:42 -07008195subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008196module_exit(md_exit)
8197
NeilBrownf91de922005-11-08 21:39:36 -08008198static int get_ro(char *buffer, struct kernel_param *kp)
8199{
8200 return sprintf(buffer, "%d", start_readonly);
8201}
8202static int set_ro(const char *val, struct kernel_param *kp)
8203{
8204 char *e;
8205 int num = simple_strtoul(val, &e, 10);
8206 if (*val && (*e == '\0' || *e == '\n')) {
8207 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008208 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008209 }
8210 return -EINVAL;
8211}
8212
NeilBrown80ca3a42006-07-10 04:44:18 -07008213module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8214module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8e2006-01-06 00:20:15 -08008215
NeilBrownefeb53c2009-01-09 08:31:10 +11008216module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008217
Linus Torvalds1da177e2005-04-16 15:20:36 -07008218EXPORT_SYMBOL(register_md_personality);
8219EXPORT_SYMBOL(unregister_md_personality);
8220EXPORT_SYMBOL(md_error);
8221EXPORT_SYMBOL(md_done_sync);
8222EXPORT_SYMBOL(md_write_start);
8223EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008224EXPORT_SYMBOL(md_register_thread);
8225EXPORT_SYMBOL(md_unregister_thread);
8226EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008227EXPORT_SYMBOL(md_check_recovery);
8228MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008229MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008230MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008231MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);