blob: 1f661bbcaefe2e90de5a3b276bfc8714dc521488 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110058static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
NeilBrown01f96c02011-09-21 15:30:20 +100061/* pers_list is a list of registered personalities protected
62 * by pers_lock.
63 * pers_lock does extra service to protect accesses to
64 * mddev->thread when the mutex cannot be held.
65 */
NeilBrown2604b702006-01-06 00:20:36 -080066static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static DEFINE_SPINLOCK(pers_lock);
68
Adrian Bunk5e563412006-06-26 00:27:42 -070069static void md_print_devices(void);
70
Bernd Schubert90b08712008-05-23 13:04:38 -070071static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Tejun Heoe804ac72010-10-15 15:36:08 +020072static struct workqueue_struct *md_wq;
73static struct workqueue_struct *md_misc_wq;
Bernd Schubert90b08712008-05-23 13:04:38 -070074
Adrian Bunk5e563412006-06-26 00:27:42 -070075#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/*
Robert Becker1e509152009-12-14 12:49:58 +110078 * Default number of read corrections we'll attempt on an rdev
79 * before ejecting it from the array. We divide the read error
80 * count by 2 for every hour elapsed between read errors.
81 */
82#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
83/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
85 * is 1000 KB/sec, so the extra system load does not show up that much.
86 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070087 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * subsystem is idle. There is also an 'absolute maximum' reconstruction
89 * speed limit - in case reconstruction slows down your system despite
90 * idle IO detection.
91 *
92 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080093 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 */
95
96static int sysctl_speed_limit_min = 1000;
97static int sysctl_speed_limit_max = 200000;
NeilBrownfd01b882011-10-11 16:47:53 +110098static inline int speed_min(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -080099{
100 return mddev->sync_speed_min ?
101 mddev->sync_speed_min : sysctl_speed_limit_min;
102}
103
NeilBrownfd01b882011-10-11 16:47:53 +1100104static inline int speed_max(struct mddev *mddev)
NeilBrown88202a02006-01-06 00:21:36 -0800105{
106 return mddev->sync_speed_max ?
107 mddev->sync_speed_max : sysctl_speed_limit_max;
108}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110static struct ctl_table_header *raid_table_header;
111
112static ctl_table raid_table[] = {
113 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .procname = "speed_limit_min",
115 .data = &sysctl_speed_limit_min,
116 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700117 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800118 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 },
120 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 .procname = "speed_limit_max",
122 .data = &sysctl_speed_limit_max,
123 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700124 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800125 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800127 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128};
129
130static ctl_table raid_dir_table[] = {
131 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 .procname = "raid",
133 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700134 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .child = raid_table,
136 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800137 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
140static ctl_table raid_root_table[] = {
141 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 .procname = "dev",
143 .maxlen = 0,
144 .mode = 0555,
145 .child = raid_dir_table,
146 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800147 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700150static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
NeilBrownf91de922005-11-08 21:39:36 -0800152static int start_readonly;
153
NeilBrowna167f662010-10-26 18:31:13 +1100154/* bio_clone_mddev
155 * like bio_clone, but with a local bio set
156 */
157
158static void mddev_bio_destructor(struct bio *bio)
159{
NeilBrownfd01b882011-10-11 16:47:53 +1100160 struct mddev *mddev, **mddevp;
NeilBrowna167f662010-10-26 18:31:13 +1100161
162 mddevp = (void*)bio;
163 mddev = mddevp[-1];
164
165 bio_free(bio, mddev->bio_set);
166}
167
168struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
NeilBrownfd01b882011-10-11 16:47:53 +1100169 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100170{
171 struct bio *b;
NeilBrownfd01b882011-10-11 16:47:53 +1100172 struct mddev **mddevp;
NeilBrowna167f662010-10-26 18:31:13 +1100173
174 if (!mddev || !mddev->bio_set)
175 return bio_alloc(gfp_mask, nr_iovecs);
176
177 b = bio_alloc_bioset(gfp_mask, nr_iovecs,
178 mddev->bio_set);
179 if (!b)
180 return NULL;
181 mddevp = (void*)b;
182 mddevp[-1] = mddev;
183 b->bi_destructor = mddev_bio_destructor;
184 return b;
185}
186EXPORT_SYMBOL_GPL(bio_alloc_mddev);
187
188struct bio *bio_clone_mddev(struct bio *bio, gfp_t gfp_mask,
NeilBrownfd01b882011-10-11 16:47:53 +1100189 struct mddev *mddev)
NeilBrowna167f662010-10-26 18:31:13 +1100190{
191 struct bio *b;
NeilBrownfd01b882011-10-11 16:47:53 +1100192 struct mddev **mddevp;
NeilBrowna167f662010-10-26 18:31:13 +1100193
194 if (!mddev || !mddev->bio_set)
195 return bio_clone(bio, gfp_mask);
196
197 b = bio_alloc_bioset(gfp_mask, bio->bi_max_vecs,
198 mddev->bio_set);
199 if (!b)
200 return NULL;
201 mddevp = (void*)b;
202 mddevp[-1] = mddev;
203 b->bi_destructor = mddev_bio_destructor;
204 __bio_clone(b, bio);
205 if (bio_integrity(bio)) {
206 int ret;
207
208 ret = bio_integrity_clone(b, bio, gfp_mask, mddev->bio_set);
209
210 if (ret < 0) {
211 bio_put(b);
212 return NULL;
213 }
214 }
215
216 return b;
217}
218EXPORT_SYMBOL_GPL(bio_clone_mddev);
219
NeilBrownd2eb35a2011-07-28 11:31:48 +1000220void md_trim_bio(struct bio *bio, int offset, int size)
221{
222 /* 'bio' is a cloned bio which we need to trim to match
223 * the given offset and size.
224 * This requires adjusting bi_sector, bi_size, and bi_io_vec
225 */
226 int i;
227 struct bio_vec *bvec;
228 int sofar = 0;
229
230 size <<= 9;
231 if (offset == 0 && size == bio->bi_size)
232 return;
233
234 bio->bi_sector += offset;
235 bio->bi_size = size;
236 offset <<= 9;
237 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
238
239 while (bio->bi_idx < bio->bi_vcnt &&
240 bio->bi_io_vec[bio->bi_idx].bv_len <= offset) {
241 /* remove this whole bio_vec */
242 offset -= bio->bi_io_vec[bio->bi_idx].bv_len;
243 bio->bi_idx++;
244 }
245 if (bio->bi_idx < bio->bi_vcnt) {
246 bio->bi_io_vec[bio->bi_idx].bv_offset += offset;
247 bio->bi_io_vec[bio->bi_idx].bv_len -= offset;
248 }
249 /* avoid any complications with bi_idx being non-zero*/
250 if (bio->bi_idx) {
251 memmove(bio->bi_io_vec, bio->bi_io_vec+bio->bi_idx,
252 (bio->bi_vcnt - bio->bi_idx) * sizeof(struct bio_vec));
253 bio->bi_vcnt -= bio->bi_idx;
254 bio->bi_idx = 0;
255 }
256 /* Make sure vcnt and last bv are not too big */
257 bio_for_each_segment(bvec, bio, i) {
258 if (sofar + bvec->bv_len > size)
259 bvec->bv_len = size - sofar;
260 if (bvec->bv_len == 0) {
261 bio->bi_vcnt = i;
262 break;
263 }
264 sofar += bvec->bv_len;
265 }
266}
267EXPORT_SYMBOL_GPL(md_trim_bio);
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269/*
NeilBrownd7603b72006-01-06 00:20:30 -0800270 * We have a system wide 'event count' that is incremented
271 * on any 'interesting' event, and readers of /proc/mdstat
272 * can use 'poll' or 'select' to find out when the event
273 * count increases.
274 *
275 * Events are:
276 * start array, stop array, error, add device, remove device,
277 * start build, activate spare
278 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800279static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800280static atomic_t md_event_count;
NeilBrownfd01b882011-10-11 16:47:53 +1100281void md_new_event(struct mddev *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800282{
283 atomic_inc(&md_event_count);
284 wake_up(&md_event_waiters);
285}
NeilBrown29269552006-03-27 01:18:10 -0800286EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800287
NeilBrownc331eb02006-05-30 21:27:13 -0700288/* Alternate version that can be called from interrupts
289 * when calling sysfs_notify isn't needed.
290 */
NeilBrownfd01b882011-10-11 16:47:53 +1100291static void md_new_event_inintr(struct mddev *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700292{
293 atomic_inc(&md_event_count);
294 wake_up(&md_event_waiters);
295}
296
NeilBrownd7603b72006-01-06 00:20:30 -0800297/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 * Enables to iterate over all existing md arrays
299 * all_mddevs_lock protects this list.
300 */
301static LIST_HEAD(all_mddevs);
302static DEFINE_SPINLOCK(all_mddevs_lock);
303
304
305/*
306 * iterates through all used mddevs in the system.
307 * We take care to grab the all_mddevs_lock whenever navigating
308 * the list, and to always hold a refcount when unlocked.
309 * Any code which breaks out of this loop while own
310 * a reference to the current mddev and must mddev_put it.
311 */
NeilBrownfd01b882011-10-11 16:47:53 +1100312#define for_each_mddev(_mddev,_tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 \
314 for (({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100315 _tmp = all_mddevs.next; \
316 _mddev = NULL;}); \
317 ({ if (_tmp != &all_mddevs) \
318 mddev_get(list_entry(_tmp, struct mddev, all_mddevs));\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 spin_unlock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100320 if (_mddev) mddev_put(_mddev); \
321 _mddev = list_entry(_tmp, struct mddev, all_mddevs); \
322 _tmp != &all_mddevs;}); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 ({ spin_lock(&all_mddevs_lock); \
NeilBrownfd01b882011-10-11 16:47:53 +1100324 _tmp = _tmp->next;}) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 )
326
327
NeilBrown409c57f2009-03-31 14:39:39 +1100328/* Rather than calling directly into the personality make_request function,
329 * IO requests come here first so that we can check if the device is
330 * being suspended pending a reconfiguration.
331 * We hold a refcount over the call to ->make_request. By the time that
332 * call has finished, the bio has been linked into some internal structure
333 * and so is visible to ->quiesce(), so we don't need the refcount any more.
334 */
335static int md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
NeilBrown49077322010-03-25 16:20:56 +1100337 const int rw = bio_data_dir(bio);
NeilBrownfd01b882011-10-11 16:47:53 +1100338 struct mddev *mddev = q->queuedata;
NeilBrown409c57f2009-03-31 14:39:39 +1100339 int rv;
NeilBrown49077322010-03-25 16:20:56 +1100340 int cpu;
Chris Masone91ece52011-02-07 19:21:48 -0500341 unsigned int sectors;
NeilBrown49077322010-03-25 16:20:56 +1100342
NeilBrown0ca69882011-01-14 09:14:33 +1100343 if (mddev == NULL || mddev->pers == NULL
344 || !mddev->ready) {
NeilBrown409c57f2009-03-31 14:39:39 +1100345 bio_io_error(bio);
346 return 0;
347 }
NeilBrown0ca69882011-01-14 09:14:33 +1100348 smp_rmb(); /* Ensure implications of 'active' are visible */
NeilBrown409c57f2009-03-31 14:39:39 +1100349 rcu_read_lock();
Tejun Heoe9c74692010-09-03 11:56:18 +0200350 if (mddev->suspended) {
NeilBrown409c57f2009-03-31 14:39:39 +1100351 DEFINE_WAIT(__wait);
352 for (;;) {
353 prepare_to_wait(&mddev->sb_wait, &__wait,
354 TASK_UNINTERRUPTIBLE);
Tejun Heoe9c74692010-09-03 11:56:18 +0200355 if (!mddev->suspended)
NeilBrown409c57f2009-03-31 14:39:39 +1100356 break;
357 rcu_read_unlock();
358 schedule();
359 rcu_read_lock();
360 }
361 finish_wait(&mddev->sb_wait, &__wait);
362 }
363 atomic_inc(&mddev->active_io);
364 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100365
Chris Masone91ece52011-02-07 19:21:48 -0500366 /*
367 * save the sectors now since our bio can
368 * go away inside make_request
369 */
370 sectors = bio_sectors(bio);
NeilBrown21a52c62010-04-01 15:02:13 +1100371 rv = mddev->pers->make_request(mddev, bio);
NeilBrown49077322010-03-25 16:20:56 +1100372
373 cpu = part_stat_lock();
374 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
Chris Masone91ece52011-02-07 19:21:48 -0500375 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw], sectors);
NeilBrown49077322010-03-25 16:20:56 +1100376 part_stat_unlock();
377
NeilBrown409c57f2009-03-31 14:39:39 +1100378 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
379 wake_up(&mddev->sb_wait);
380
381 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
NeilBrown9e35b992010-04-06 14:23:02 +1000384/* mddev_suspend makes sure no new requests are submitted
385 * to the device, and that any requests that have been submitted
386 * are completely handled.
387 * Once ->stop is called and completes, the module will be completely
388 * unused.
389 */
NeilBrownfd01b882011-10-11 16:47:53 +1100390void mddev_suspend(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100391{
392 BUG_ON(mddev->suspended);
393 mddev->suspended = 1;
394 synchronize_rcu();
395 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
396 mddev->pers->quiesce(mddev, 1);
NeilBrown409c57f2009-03-31 14:39:39 +1100397}
NeilBrown390ee602010-06-01 19:37:27 +1000398EXPORT_SYMBOL_GPL(mddev_suspend);
NeilBrown409c57f2009-03-31 14:39:39 +1100399
NeilBrownfd01b882011-10-11 16:47:53 +1100400void mddev_resume(struct mddev *mddev)
NeilBrown409c57f2009-03-31 14:39:39 +1100401{
402 mddev->suspended = 0;
403 wake_up(&mddev->sb_wait);
404 mddev->pers->quiesce(mddev, 0);
Jonathan Brassow0fd018a2011-06-07 17:49:36 -0500405
406 md_wakeup_thread(mddev->thread);
407 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown409c57f2009-03-31 14:39:39 +1100408}
NeilBrown390ee602010-06-01 19:37:27 +1000409EXPORT_SYMBOL_GPL(mddev_resume);
NeilBrown409c57f2009-03-31 14:39:39 +1100410
NeilBrownfd01b882011-10-11 16:47:53 +1100411int mddev_congested(struct mddev *mddev, int bits)
NeilBrown3fa841d2009-09-23 18:10:29 +1000412{
413 return mddev->suspended;
414}
415EXPORT_SYMBOL(mddev_congested);
416
NeilBrowna2826aa2009-12-14 12:49:49 +1100417/*
Tejun Heoe9c74692010-09-03 11:56:18 +0200418 * Generic flush handling for md
NeilBrowna2826aa2009-12-14 12:49:49 +1100419 */
420
Tejun Heoe9c74692010-09-03 11:56:18 +0200421static void md_end_flush(struct bio *bio, int err)
NeilBrowna2826aa2009-12-14 12:49:49 +1100422{
NeilBrown3cb03002011-10-11 16:45:26 +1100423 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100424 struct mddev *mddev = rdev->mddev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100425
426 rdev_dec_pending(rdev, mddev);
427
428 if (atomic_dec_and_test(&mddev->flush_pending)) {
Tejun Heoe9c74692010-09-03 11:56:18 +0200429 /* The pre-request flush has finished */
Tejun Heoe804ac72010-10-15 15:36:08 +0200430 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100431 }
432 bio_put(bio);
433}
434
NeilBrowna7a07e62010-12-09 16:04:25 +1100435static void md_submit_flush_data(struct work_struct *ws);
436
NeilBrowna035fc32010-12-09 16:17:51 +1100437static void submit_flushes(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100438{
NeilBrownfd01b882011-10-11 16:47:53 +1100439 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
NeilBrown3cb03002011-10-11 16:45:26 +1100440 struct md_rdev *rdev;
NeilBrowna2826aa2009-12-14 12:49:49 +1100441
NeilBrowna7a07e62010-12-09 16:04:25 +1100442 INIT_WORK(&mddev->flush_work, md_submit_flush_data);
443 atomic_set(&mddev->flush_pending, 1);
NeilBrowna2826aa2009-12-14 12:49:49 +1100444 rcu_read_lock();
445 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
446 if (rdev->raid_disk >= 0 &&
447 !test_bit(Faulty, &rdev->flags)) {
448 /* Take two references, one is dropped
449 * when request finishes, one after
450 * we reclaim rcu_read_lock
451 */
452 struct bio *bi;
453 atomic_inc(&rdev->nr_pending);
454 atomic_inc(&rdev->nr_pending);
455 rcu_read_unlock();
NeilBrowna167f662010-10-26 18:31:13 +1100456 bi = bio_alloc_mddev(GFP_KERNEL, 0, mddev);
Tejun Heoe9c74692010-09-03 11:56:18 +0200457 bi->bi_end_io = md_end_flush;
NeilBrowna2826aa2009-12-14 12:49:49 +1100458 bi->bi_private = rdev;
459 bi->bi_bdev = rdev->bdev;
460 atomic_inc(&mddev->flush_pending);
Tejun Heoe9c74692010-09-03 11:56:18 +0200461 submit_bio(WRITE_FLUSH, bi);
NeilBrowna2826aa2009-12-14 12:49:49 +1100462 rcu_read_lock();
463 rdev_dec_pending(rdev, mddev);
464 }
465 rcu_read_unlock();
NeilBrowna7a07e62010-12-09 16:04:25 +1100466 if (atomic_dec_and_test(&mddev->flush_pending))
467 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100468}
469
Tejun Heoe9c74692010-09-03 11:56:18 +0200470static void md_submit_flush_data(struct work_struct *ws)
NeilBrowna2826aa2009-12-14 12:49:49 +1100471{
NeilBrownfd01b882011-10-11 16:47:53 +1100472 struct mddev *mddev = container_of(ws, struct mddev, flush_work);
Tejun Heoe9c74692010-09-03 11:56:18 +0200473 struct bio *bio = mddev->flush_bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100474
Tejun Heoe9c74692010-09-03 11:56:18 +0200475 if (bio->bi_size == 0)
NeilBrowna2826aa2009-12-14 12:49:49 +1100476 /* an empty barrier - all done */
477 bio_endio(bio, 0);
478 else {
Tejun Heoe9c74692010-09-03 11:56:18 +0200479 bio->bi_rw &= ~REQ_FLUSH;
NeilBrown21a52c62010-04-01 15:02:13 +1100480 if (mddev->pers->make_request(mddev, bio))
NeilBrowna2826aa2009-12-14 12:49:49 +1100481 generic_make_request(bio);
NeilBrowna2826aa2009-12-14 12:49:49 +1100482 }
NeilBrown2b74e122010-12-09 15:59:01 +1100483
484 mddev->flush_bio = NULL;
485 wake_up(&mddev->sb_wait);
NeilBrowna2826aa2009-12-14 12:49:49 +1100486}
487
NeilBrownfd01b882011-10-11 16:47:53 +1100488void md_flush_request(struct mddev *mddev, struct bio *bio)
NeilBrowna2826aa2009-12-14 12:49:49 +1100489{
490 spin_lock_irq(&mddev->write_lock);
491 wait_event_lock_irq(mddev->sb_wait,
Tejun Heoe9c74692010-09-03 11:56:18 +0200492 !mddev->flush_bio,
NeilBrowna2826aa2009-12-14 12:49:49 +1100493 mddev->write_lock, /*nothing*/);
Tejun Heoe9c74692010-09-03 11:56:18 +0200494 mddev->flush_bio = bio;
NeilBrowna2826aa2009-12-14 12:49:49 +1100495 spin_unlock_irq(&mddev->write_lock);
496
NeilBrowna035fc32010-12-09 16:17:51 +1100497 INIT_WORK(&mddev->flush_work, submit_flushes);
498 queue_work(md_wq, &mddev->flush_work);
NeilBrowna2826aa2009-12-14 12:49:49 +1100499}
Tejun Heoe9c74692010-09-03 11:56:18 +0200500EXPORT_SYMBOL(md_flush_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100501
NeilBrown97658cd2011-04-18 18:25:42 +1000502/* Support for plugging.
503 * This mirrors the plugging support in request_queue, but does not
504 * require having a whole queue or request structures.
505 * We allocate an md_plug_cb for each md device and each thread it gets
506 * plugged on. This links tot the private plug_handle structure in the
507 * personality data where we keep a count of the number of outstanding
508 * plugs so other code can see if a plug is active.
509 */
510struct md_plug_cb {
511 struct blk_plug_cb cb;
NeilBrownfd01b882011-10-11 16:47:53 +1100512 struct mddev *mddev;
NeilBrown97658cd2011-04-18 18:25:42 +1000513};
514
515static void plugger_unplug(struct blk_plug_cb *cb)
516{
517 struct md_plug_cb *mdcb = container_of(cb, struct md_plug_cb, cb);
518 if (atomic_dec_and_test(&mdcb->mddev->plug_cnt))
519 md_wakeup_thread(mdcb->mddev->thread);
520 kfree(mdcb);
521}
522
523/* Check that an unplug wakeup will come shortly.
524 * If not, wakeup the md thread immediately
525 */
NeilBrownfd01b882011-10-11 16:47:53 +1100526int mddev_check_plugged(struct mddev *mddev)
NeilBrown97658cd2011-04-18 18:25:42 +1000527{
528 struct blk_plug *plug = current->plug;
529 struct md_plug_cb *mdcb;
530
531 if (!plug)
532 return 0;
533
534 list_for_each_entry(mdcb, &plug->cb_list, cb.list) {
535 if (mdcb->cb.callback == plugger_unplug &&
536 mdcb->mddev == mddev) {
537 /* Already on the list, move to top */
538 if (mdcb != list_first_entry(&plug->cb_list,
539 struct md_plug_cb,
540 cb.list))
541 list_move(&mdcb->cb.list, &plug->cb_list);
542 return 1;
543 }
544 }
545 /* Not currently on the callback list */
546 mdcb = kmalloc(sizeof(*mdcb), GFP_ATOMIC);
547 if (!mdcb)
548 return 0;
549
550 mdcb->mddev = mddev;
551 mdcb->cb.callback = plugger_unplug;
552 atomic_inc(&mddev->plug_cnt);
553 list_add(&mdcb->cb.list, &plug->cb_list);
554 return 1;
555}
556EXPORT_SYMBOL_GPL(mddev_check_plugged);
NeilBrown2ac87402010-06-01 19:37:29 +1000557
NeilBrownfd01b882011-10-11 16:47:53 +1100558static inline struct mddev *mddev_get(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 atomic_inc(&mddev->active);
561 return mddev;
562}
563
Dan Williams5fd3a172009-03-04 00:57:25 -0700564static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100565
NeilBrownfd01b882011-10-11 16:47:53 +1100566static void mddev_put(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
NeilBrowna167f662010-10-26 18:31:13 +1100568 struct bio_set *bs = NULL;
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
571 return;
NeilBrownd3374822009-01-09 08:31:10 +1100572 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100573 mddev->ctime == 0 && !mddev->hold_active) {
574 /* Array is not configured at all, and not held active,
575 * so destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 list_del(&mddev->all_mddevs);
NeilBrowna167f662010-10-26 18:31:13 +1100577 bs = mddev->bio_set;
578 mddev->bio_set = NULL;
NeilBrownd3374822009-01-09 08:31:10 +1100579 if (mddev->gendisk) {
Tejun Heoe804ac72010-10-15 15:36:08 +0200580 /* We did a probe so need to clean up. Call
581 * queue_work inside the spinlock so that
582 * flush_workqueue() after mddev_find will
583 * succeed in waiting for the work to be done.
NeilBrownd3374822009-01-09 08:31:10 +1100584 */
585 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
Tejun Heoe804ac72010-10-15 15:36:08 +0200586 queue_work(md_misc_wq, &mddev->del_work);
NeilBrownd3374822009-01-09 08:31:10 +1100587 } else
588 kfree(mddev);
589 }
590 spin_unlock(&all_mddevs_lock);
NeilBrowna167f662010-10-26 18:31:13 +1100591 if (bs)
592 bioset_free(bs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
NeilBrownfd01b882011-10-11 16:47:53 +1100595void mddev_init(struct mddev *mddev)
NeilBrownfafd7fb2010-04-01 15:55:30 +1100596{
597 mutex_init(&mddev->open_mutex);
598 mutex_init(&mddev->reconfig_mutex);
599 mutex_init(&mddev->bitmap_info.mutex);
600 INIT_LIST_HEAD(&mddev->disks);
601 INIT_LIST_HEAD(&mddev->all_mddevs);
602 init_timer(&mddev->safemode_timer);
603 atomic_set(&mddev->active, 1);
604 atomic_set(&mddev->openers, 0);
605 atomic_set(&mddev->active_io, 0);
NeilBrown97658cd2011-04-18 18:25:42 +1000606 atomic_set(&mddev->plug_cnt, 0);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100607 spin_lock_init(&mddev->write_lock);
608 atomic_set(&mddev->flush_pending, 0);
609 init_waitqueue_head(&mddev->sb_wait);
610 init_waitqueue_head(&mddev->recovery_wait);
611 mddev->reshape_position = MaxSector;
612 mddev->resync_min = 0;
613 mddev->resync_max = MaxSector;
614 mddev->level = LEVEL_NONE;
615}
NeilBrown390ee602010-06-01 19:37:27 +1000616EXPORT_SYMBOL_GPL(mddev_init);
NeilBrownfafd7fb2010-04-01 15:55:30 +1100617
NeilBrownfd01b882011-10-11 16:47:53 +1100618static struct mddev * mddev_find(dev_t unit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
NeilBrownfd01b882011-10-11 16:47:53 +1100620 struct mddev *mddev, *new = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
NeilBrown8f5f02c2011-02-16 13:58:51 +1100622 if (unit && MAJOR(unit) != MD_MAJOR)
623 unit &= ~((1<<MdpMinorShift)-1);
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 retry:
626 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
NeilBrownefeb53c2009-01-09 08:31:10 +1100628 if (unit) {
629 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
630 if (mddev->unit == unit) {
631 mddev_get(mddev);
632 spin_unlock(&all_mddevs_lock);
633 kfree(new);
634 return mddev;
635 }
636
637 if (new) {
638 list_add(&new->all_mddevs, &all_mddevs);
639 spin_unlock(&all_mddevs_lock);
640 new->hold_active = UNTIL_IOCTL;
641 return new;
642 }
643 } else if (new) {
644 /* find an unused unit number */
645 static int next_minor = 512;
646 int start = next_minor;
647 int is_free = 0;
648 int dev = 0;
649 while (!is_free) {
650 dev = MKDEV(MD_MAJOR, next_minor);
651 next_minor++;
652 if (next_minor > MINORMASK)
653 next_minor = 0;
654 if (next_minor == start) {
655 /* Oh dear, all in use. */
656 spin_unlock(&all_mddevs_lock);
657 kfree(new);
658 return NULL;
659 }
660
661 is_free = 1;
662 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
663 if (mddev->unit == dev) {
664 is_free = 0;
665 break;
666 }
667 }
668 new->unit = dev;
669 new->md_minor = MINOR(dev);
670 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 list_add(&new->all_mddevs, &all_mddevs);
672 spin_unlock(&all_mddevs_lock);
673 return new;
674 }
675 spin_unlock(&all_mddevs_lock);
676
NeilBrown9ffae0c2006-01-06 00:20:32 -0800677 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (!new)
679 return NULL;
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 new->unit = unit;
682 if (MAJOR(unit) == MD_MAJOR)
683 new->md_minor = MINOR(unit);
684 else
685 new->md_minor = MINOR(unit) >> MdpMinorShift;
686
NeilBrownfafd7fb2010-04-01 15:55:30 +1100687 mddev_init(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 goto retry;
690}
691
NeilBrownfd01b882011-10-11 16:47:53 +1100692static inline int mddev_lock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800694 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
NeilBrownfd01b882011-10-11 16:47:53 +1100697static inline int mddev_is_locked(struct mddev *mddev)
Dan Williamsb522adc2009-03-31 15:00:31 +1100698{
699 return mutex_is_locked(&mddev->reconfig_mutex);
700}
701
NeilBrownfd01b882011-10-11 16:47:53 +1100702static inline int mddev_trylock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800704 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
NeilBrownb6eb1272010-04-15 10:13:47 +1000707static struct attribute_group md_redundancy_group;
708
NeilBrownfd01b882011-10-11 16:47:53 +1100709static void mddev_unlock(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
NeilBrowna64c8762010-04-14 17:15:37 +1000711 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000712 /* These cannot be removed under reconfig_mutex as
713 * an access to the files will try to take reconfig_mutex
714 * while holding the file unremovable, which leads to
715 * a deadlock.
NeilBrownbb4f1e92010-08-08 21:18:03 +1000716 * So hold set sysfs_active while the remove in happeing,
717 * and anything else which might set ->to_remove or my
718 * otherwise change the sysfs namespace will fail with
719 * -EBUSY if sysfs_active is still set.
720 * We set sysfs_active under reconfig_mutex and elsewhere
721 * test it under the same mutex to ensure its correct value
722 * is seen.
NeilBrownb6eb1272010-04-15 10:13:47 +1000723 */
NeilBrowna64c8762010-04-14 17:15:37 +1000724 struct attribute_group *to_remove = mddev->to_remove;
725 mddev->to_remove = NULL;
NeilBrownbb4f1e92010-08-08 21:18:03 +1000726 mddev->sysfs_active = 1;
NeilBrownb6eb1272010-04-15 10:13:47 +1000727 mutex_unlock(&mddev->reconfig_mutex);
728
NeilBrown00bcb4a2010-06-01 19:37:23 +1000729 if (mddev->kobj.sd) {
730 if (to_remove != &md_redundancy_group)
731 sysfs_remove_group(&mddev->kobj, to_remove);
732 if (mddev->pers == NULL ||
733 mddev->pers->sync_request == NULL) {
734 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
735 if (mddev->sysfs_action)
736 sysfs_put(mddev->sysfs_action);
737 mddev->sysfs_action = NULL;
738 }
NeilBrowna64c8762010-04-14 17:15:37 +1000739 }
NeilBrownbb4f1e92010-08-08 21:18:03 +1000740 mddev->sysfs_active = 0;
NeilBrownb6eb1272010-04-15 10:13:47 +1000741 } else
742 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
NeilBrown01f96c02011-09-21 15:30:20 +1000744 /* was we've dropped the mutex we need a spinlock to
745 * make sur the thread doesn't disappear
746 */
747 spin_lock(&pers_lock);
NeilBrown005eca52005-08-22 13:11:08 -0700748 md_wakeup_thread(mddev->thread);
NeilBrown01f96c02011-09-21 15:30:20 +1000749 spin_unlock(&pers_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
NeilBrownfd01b882011-10-11 16:47:53 +1100752static struct md_rdev * find_rdev_nr(struct mddev *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
NeilBrown3cb03002011-10-11 16:45:26 +1100754 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100756 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (rdev->desc_nr == nr)
758 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return NULL;
761}
762
NeilBrownfd01b882011-10-11 16:47:53 +1100763static struct md_rdev * find_rdev(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
NeilBrown3cb03002011-10-11 16:45:26 +1100765 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100767 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (rdev->bdev->bd_dev == dev)
769 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return NULL;
772}
773
NeilBrownd9d166c2006-01-06 00:20:51 -0800774static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800775{
776 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800777 list_for_each_entry(pers, &pers_list, list) {
778 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800779 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800780 if (strcmp(pers->name, clevel)==0)
781 return pers;
782 }
NeilBrown2604b702006-01-06 00:20:36 -0800783 return NULL;
784}
785
Andre Nollb73df2d2008-07-11 22:02:23 +1000786/* return the offset of the super block in 512byte sectors */
NeilBrown3cb03002011-10-11 16:45:26 +1100787static inline sector_t calc_dev_sboffset(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Jonathan Brassow57b2caa2011-01-14 09:14:33 +1100789 sector_t num_sectors = i_size_read(rdev->bdev->bd_inode) / 512;
Andre Nollb73df2d2008-07-11 22:02:23 +1000790 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
NeilBrown3cb03002011-10-11 16:45:26 +1100793static int alloc_disk_sb(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 if (rdev->sb_page)
796 MD_BUG();
797
798 rdev->sb_page = alloc_page(GFP_KERNEL);
799 if (!rdev->sb_page) {
800 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000801 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803
804 return 0;
805}
806
NeilBrown3cb03002011-10-11 16:45:26 +1100807static void free_disk_sb(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
809 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800810 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 rdev->sb_loaded = 0;
812 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000813 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100814 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
NeilBrown2699b672011-07-28 11:31:47 +1000816 if (rdev->bb_page) {
817 put_page(rdev->bb_page);
818 rdev->bb_page = NULL;
819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
822
NeilBrown6712ecf2007-09-27 12:47:43 +0200823static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700824{
NeilBrown3cb03002011-10-11 16:45:26 +1100825 struct md_rdev *rdev = bio->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +1100826 struct mddev *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700827
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700828 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
829 printk("md: super_written gets error=%d, uptodate=%d\n",
830 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
831 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800832 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700833 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700834
NeilBrowna9701a32005-11-08 21:39:34 -0800835 if (atomic_dec_and_test(&mddev->pending_writes))
836 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700837 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700838}
839
NeilBrownfd01b882011-10-11 16:47:53 +1100840void md_super_write(struct mddev *mddev, struct md_rdev *rdev,
NeilBrown7bfa19f2005-06-21 17:17:28 -0700841 sector_t sector, int size, struct page *page)
842{
843 /* write first size bytes of page to sector of rdev
844 * Increment mddev->pending_writes before returning
845 * and decrement it on completion, waking up sb_wait
846 * if zero is reached.
847 * If an error occurred, call md_error
848 */
NeilBrowna167f662010-10-26 18:31:13 +1100849 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700850
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100851 bio->bi_bdev = rdev->meta_bdev ? rdev->meta_bdev : rdev->bdev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700852 bio->bi_sector = sector;
853 bio_add_page(bio, page, size, 0);
854 bio->bi_private = rdev;
855 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800856
NeilBrown7bfa19f2005-06-21 17:17:28 -0700857 atomic_inc(&mddev->pending_writes);
Namhyung Kima5bf4df2011-08-25 14:43:34 +1000858 submit_bio(WRITE_FLUSH_FUA, bio);
NeilBrowna9701a32005-11-08 21:39:34 -0800859}
860
NeilBrownfd01b882011-10-11 16:47:53 +1100861void md_super_wait(struct mddev *mddev)
NeilBrowna9701a32005-11-08 21:39:34 -0800862{
Tejun Heoe9c74692010-09-03 11:56:18 +0200863 /* wait for all superblock writes that were scheduled to complete */
NeilBrowna9701a32005-11-08 21:39:34 -0800864 DEFINE_WAIT(wq);
865 for(;;) {
866 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
867 if (atomic_read(&mddev->pending_writes)==0)
868 break;
NeilBrowna9701a32005-11-08 21:39:34 -0800869 schedule();
870 }
871 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700872}
873
NeilBrown6712ecf2007-09-27 12:47:43 +0200874static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
NeilBrown3cb03002011-10-11 16:45:26 +1100879int sync_page_io(struct md_rdev *rdev, sector_t sector, int size,
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100880 struct page *page, int rw, bool metadata_op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
NeilBrowna167f662010-10-26 18:31:13 +1100882 struct bio *bio = bio_alloc_mddev(GFP_NOIO, 1, rdev->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 struct completion event;
884 int ret;
885
Jens Axboe721a9602011-03-09 11:56:30 +0100886 rw |= REQ_SYNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +1100888 bio->bi_bdev = (metadata_op && rdev->meta_bdev) ?
889 rdev->meta_bdev : rdev->bdev;
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100890 if (metadata_op)
891 bio->bi_sector = sector + rdev->sb_start;
892 else
893 bio->bi_sector = sector + rdev->data_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 bio_add_page(bio, page, size, 0);
895 init_completion(&event);
896 bio->bi_private = &event;
897 bio->bi_end_io = bi_complete;
898 submit_bio(rw, bio);
899 wait_for_completion(&event);
900
901 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
902 bio_put(bio);
903 return ret;
904}
NeilBrowna8745db2006-01-06 00:20:34 -0800905EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
NeilBrown3cb03002011-10-11 16:45:26 +1100907static int read_disk_sb(struct md_rdev * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 char b[BDEVNAME_SIZE];
910 if (!rdev->sb_page) {
911 MD_BUG();
912 return -EINVAL;
913 }
914 if (rdev->sb_loaded)
915 return 0;
916
917
Jonathan Brassowccebd4c2011-01-14 09:14:33 +1100918 if (!sync_page_io(rdev, 0, size, rdev->sb_page, READ, true))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 goto fail;
920 rdev->sb_loaded = 1;
921 return 0;
922
923fail:
924 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
925 bdevname(rdev->bdev,b));
926 return -EINVAL;
927}
928
929static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
930{
Andre Noll05710462008-07-11 22:02:20 +1000931 return sb1->set_uuid0 == sb2->set_uuid0 &&
932 sb1->set_uuid1 == sb2->set_uuid1 &&
933 sb1->set_uuid2 == sb2->set_uuid2 &&
934 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
938{
939 int ret;
940 mdp_super_t *tmp1, *tmp2;
941
942 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
943 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
944
945 if (!tmp1 || !tmp2) {
946 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100947 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 goto abort;
949 }
950
951 *tmp1 = *sb1;
952 *tmp2 = *sb2;
953
954 /*
955 * nr_disks is not constant
956 */
957 tmp1->nr_disks = 0;
958 tmp2->nr_disks = 0;
959
Andre Nollce0c8e02008-07-11 22:02:20 +1000960 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700962 kfree(tmp1);
963 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return ret;
965}
966
NeilBrown4d167f02007-05-09 02:35:37 -0700967
968static u32 md_csum_fold(u32 csum)
969{
970 csum = (csum & 0xffff) + (csum >> 16);
971 return (csum & 0xffff) + (csum >> 16);
972}
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974static unsigned int calc_sb_csum(mdp_super_t * sb)
975{
NeilBrown4d167f02007-05-09 02:35:37 -0700976 u64 newcsum = 0;
977 u32 *sb32 = (u32*)sb;
978 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 unsigned int disk_csum, csum;
980
981 disk_csum = sb->sb_csum;
982 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700983
984 for (i = 0; i < MD_SB_BYTES/4 ; i++)
985 newcsum += sb32[i];
986 csum = (newcsum & 0xffffffff) + (newcsum>>32);
987
988
989#ifdef CONFIG_ALPHA
990 /* This used to use csum_partial, which was wrong for several
991 * reasons including that different results are returned on
992 * different architectures. It isn't critical that we get exactly
993 * the same return value as before (we always csum_fold before
994 * testing, and that removes any differences). However as we
995 * know that csum_partial always returned a 16bit value on
996 * alphas, do a fold to maximise conformity to previous behaviour.
997 */
998 sb->sb_csum = md_csum_fold(disk_csum);
999#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -07001001#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return csum;
1003}
1004
1005
1006/*
1007 * Handle superblock details.
1008 * We want to be able to handle multiple superblock formats
1009 * so we have a common interface to them all, and an array of
1010 * different handlers.
1011 * We rely on user-space to write the initial superblock, and support
1012 * reading and updating of superblocks.
1013 * Interface methods are:
NeilBrown3cb03002011-10-11 16:45:26 +11001014 * int load_super(struct md_rdev *dev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 * loads and validates a superblock on dev.
1016 * if refdev != NULL, compare superblocks on both devices
1017 * Return:
1018 * 0 - dev has a superblock that is compatible with refdev
1019 * 1 - dev has a superblock that is compatible and newer than refdev
1020 * so dev should be used as the refdev in future
1021 * -EINVAL superblock incompatible or invalid
1022 * -othererror e.g. -EIO
1023 *
NeilBrownfd01b882011-10-11 16:47:53 +11001024 * int validate_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 * Verify that dev is acceptable into mddev.
1026 * The first time, mddev->raid_disks will be 0, and data from
1027 * dev should be merged in. Subsequent calls check that dev
1028 * is new enough. Return 0 or -EINVAL
1029 *
NeilBrownfd01b882011-10-11 16:47:53 +11001030 * void sync_super(struct mddev *mddev, struct md_rdev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 * Update the superblock for rdev with data in mddev
1032 * This does not write to disc.
1033 *
1034 */
1035
1036struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001037 char *name;
1038 struct module *owner;
NeilBrown3cb03002011-10-11 16:45:26 +11001039 int (*load_super)(struct md_rdev *rdev, struct md_rdev *refdev,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001040 int minor_version);
NeilBrownfd01b882011-10-11 16:47:53 +11001041 int (*validate_super)(struct mddev *mddev, struct md_rdev *rdev);
1042 void (*sync_super)(struct mddev *mddev, struct md_rdev *rdev);
NeilBrown3cb03002011-10-11 16:45:26 +11001043 unsigned long long (*rdev_size_change)(struct md_rdev *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +10001044 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045};
1046
1047/*
Andre Noll0894cc32009-06-18 08:49:23 +10001048 * Check that the given mddev has no bitmap.
1049 *
1050 * This function is called from the run method of all personalities that do not
1051 * support bitmaps. It prints an error message and returns non-zero if mddev
1052 * has a bitmap. Otherwise, it returns 0.
1053 *
1054 */
NeilBrownfd01b882011-10-11 16:47:53 +11001055int md_check_no_bitmap(struct mddev *mddev)
Andre Noll0894cc32009-06-18 08:49:23 +10001056{
NeilBrownc3d97142009-12-14 12:49:52 +11001057 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +10001058 return 0;
1059 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
1060 mdname(mddev), mddev->pers->name);
1061 return 1;
1062}
1063EXPORT_SYMBOL(md_check_no_bitmap);
1064
1065/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 * load_super for 0.90.0
1067 */
NeilBrown3cb03002011-10-11 16:45:26 +11001068static int super_90_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
1070 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
1071 mdp_super_t *sb;
1072 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 /*
Andre Noll0f420352008-07-11 22:02:23 +10001075 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 * it's at the end of the disk.
1077 *
1078 * It also happens to be a multiple of 4Kb.
1079 */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001080 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
NeilBrown0002b272005-09-09 16:23:53 -07001082 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (ret) return ret;
1084
1085 ret = -EINVAL;
1086
1087 bdevname(rdev->bdev, b);
Namhyung Kim65a06f02011-07-27 11:00:36 +10001088 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 if (sb->md_magic != MD_SB_MAGIC) {
1091 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
1092 b);
1093 goto abort;
1094 }
1095
1096 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -08001097 sb->minor_version < 90 ||
1098 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
1100 sb->major_version, sb->minor_version,
1101 b);
1102 goto abort;
1103 }
1104
1105 if (sb->raid_disks <= 0)
1106 goto abort;
1107
NeilBrown4d167f02007-05-09 02:35:37 -07001108 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
1110 b);
1111 goto abort;
1112 }
1113
1114 rdev->preferred_minor = sb->md_minor;
1115 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -07001116 rdev->sb_size = MD_SB_BYTES;
NeilBrown9f2f383072011-07-28 11:31:47 +10001117 rdev->badblocks.shift = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 if (sb->level == LEVEL_MULTIPATH)
1120 rdev->desc_nr = -1;
1121 else
1122 rdev->desc_nr = sb->this_disk.number;
1123
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001124 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001126 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 __u64 ev1, ev2;
Namhyung Kim65a06f02011-07-27 11:00:36 +10001128 mdp_super_t *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (!uuid_equal(refsb, sb)) {
1130 printk(KERN_WARNING "md: %s has different UUID to %s\n",
1131 b, bdevname(refdev->bdev,b2));
1132 goto abort;
1133 }
1134 if (!sb_equal(refsb, sb)) {
1135 printk(KERN_WARNING "md: %s has same UUID"
1136 " but different superblock to %s\n",
1137 b, bdevname(refdev->bdev, b2));
1138 goto abort;
1139 }
1140 ev1 = md_event(sb);
1141 ev2 = md_event(refsb);
1142 if (ev1 > ev2)
1143 ret = 1;
1144 else
1145 ret = 0;
1146 }
NeilBrown8190e752009-06-18 08:48:58 +10001147 rdev->sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001148 /* Limit to 4TB as metadata cannot record more than that */
1149 if (rdev->sectors >= (2ULL << 32))
1150 rdev->sectors = (2ULL << 32) - 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
NeilBrown27a7b262011-09-10 17:21:28 +10001152 if (rdev->sectors < ((sector_t)sb->size) * 2 && sb->level >= 1)
NeilBrown2bf071b2006-01-06 00:20:55 -08001153 /* "this cannot possibly happen" ... */
1154 ret = -EINVAL;
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 abort:
1157 return ret;
1158}
1159
1160/*
1161 * validate_super for 0.90.0
1162 */
NeilBrownfd01b882011-10-11 16:47:53 +11001163static int super_90_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 mdp_disk_t *desc;
Namhyung Kim65a06f02011-07-27 11:00:36 +10001166 mdp_super_t *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001167 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
NeilBrown41158c72005-06-21 17:17:25 -07001169 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001170 clear_bit(Faulty, &rdev->flags);
1171 clear_bit(In_sync, &rdev->flags);
1172 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (mddev->raid_disks == 0) {
1175 mddev->major_version = 0;
1176 mddev->minor_version = sb->minor_version;
1177 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001178 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001179 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 mddev->ctime = sb->ctime;
1181 mddev->utime = sb->utime;
1182 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001183 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 mddev->layout = sb->layout;
1185 mddev->raid_disks = sb->raid_disks;
NeilBrown27a7b262011-09-10 17:21:28 +10001186 mddev->dev_sectors = ((sector_t)sb->size) * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001187 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001188 mddev->bitmap_info.offset = 0;
1189 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
NeilBrownf6705572006-03-27 01:18:11 -08001191 if (mddev->minor_version >= 91) {
1192 mddev->reshape_position = sb->reshape_position;
1193 mddev->delta_disks = sb->delta_disks;
1194 mddev->new_level = sb->new_level;
1195 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001196 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001197 } else {
1198 mddev->reshape_position = MaxSector;
1199 mddev->delta_disks = 0;
1200 mddev->new_level = mddev->level;
1201 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001202 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001203 }
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 if (sb->state & (1<<MD_SB_CLEAN))
1206 mddev->recovery_cp = MaxSector;
1207 else {
1208 if (sb->events_hi == sb->cp_events_hi &&
1209 sb->events_lo == sb->cp_events_lo) {
1210 mddev->recovery_cp = sb->recovery_cp;
1211 } else
1212 mddev->recovery_cp = 0;
1213 }
1214
1215 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1216 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1217 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1218 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1219
1220 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001221
1222 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001223 mddev->bitmap_info.file == NULL)
1224 mddev->bitmap_info.offset =
1225 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001226
NeilBrown41158c72005-06-21 17:17:25 -07001227 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001228 /* Insist on good event counter while assembling, except
1229 * for spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001231 if (sb->disks[rdev->desc_nr].state & (
1232 (1<<MD_DISK_SYNC) | (1 << MD_DISK_ACTIVE)))
1233 if (ev1 < mddev->events)
1234 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001235 } else if (mddev->bitmap) {
1236 /* if adding to array with a bitmap, then we can accept an
1237 * older device ... but not too old.
1238 */
NeilBrown41158c72005-06-21 17:17:25 -07001239 if (ev1 < mddev->bitmap->events_cleared)
1240 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001241 } else {
1242 if (ev1 < mddev->events)
1243 /* just a hot-add of a new device, leave raid_disk at -1 */
1244 return 0;
1245 }
NeilBrown41158c72005-06-21 17:17:25 -07001246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 desc = sb->disks + rdev->desc_nr;
1249
1250 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001251 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001252 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1253 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001254 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd92009-11-13 17:40:48 +11001256 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1257 /* active but not in sync implies recovery up to
1258 * reshape position. We don't know exactly where
1259 * that is, so set to zero for now */
1260 if (mddev->minor_version >= 91) {
1261 rdev->recovery_offset = 0;
1262 rdev->raid_disk = desc->raid_disk;
1263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001265 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1266 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001267 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001268 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return 0;
1270}
1271
1272/*
1273 * sync_super for 0.90.0
1274 */
NeilBrownfd01b882011-10-11 16:47:53 +11001275static void super_90_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 mdp_super_t *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001278 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 /* make rdev->sb match mddev data..
1283 *
1284 * 1/ zero out disks
1285 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1286 * 3/ any empty disks < next_spare become removed
1287 *
1288 * disks[0] gets initialised to REMOVED because
1289 * we cannot be sure from other fields if it has
1290 * been initialised or not.
1291 */
1292 int i;
1293 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1294
NeilBrown61181562005-09-09 16:24:02 -07001295 rdev->sb_size = MD_SB_BYTES;
1296
Namhyung Kim65a06f02011-07-27 11:00:36 +10001297 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 memset(sb, 0, sizeof(*sb));
1300
1301 sb->md_magic = MD_SB_MAGIC;
1302 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 sb->patch_version = mddev->patch_version;
1304 sb->gvalid_words = 0; /* ignored */
1305 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1306 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1307 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1308 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1309
1310 sb->ctime = mddev->ctime;
1311 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001312 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 sb->raid_disks = mddev->raid_disks;
1314 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001315 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 sb->utime = mddev->utime;
1317 sb->state = 0;
1318 sb->events_hi = (mddev->events>>32);
1319 sb->events_lo = (u32)mddev->events;
1320
NeilBrownf6705572006-03-27 01:18:11 -08001321 if (mddev->reshape_position == MaxSector)
1322 sb->minor_version = 90;
1323 else {
1324 sb->minor_version = 91;
1325 sb->reshape_position = mddev->reshape_position;
1326 sb->new_level = mddev->new_level;
1327 sb->delta_disks = mddev->delta_disks;
1328 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001329 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001330 }
1331 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (mddev->in_sync)
1333 {
1334 sb->recovery_cp = mddev->recovery_cp;
1335 sb->cp_events_hi = (mddev->events>>32);
1336 sb->cp_events_lo = (u32)mddev->events;
1337 if (mddev->recovery_cp == MaxSector)
1338 sb->state = (1<< MD_SB_CLEAN);
1339 } else
1340 sb->recovery_cp = 0;
1341
1342 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001343 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
NeilBrownc3d97142009-12-14 12:49:52 +11001345 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001346 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001349 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001351 int desc_nr;
NeilBrown0261cd92009-11-13 17:40:48 +11001352 int is_active = test_bit(In_sync, &rdev2->flags);
1353
1354 if (rdev2->raid_disk >= 0 &&
1355 sb->minor_version >= 91)
1356 /* we have nowhere to store the recovery_offset,
1357 * but if it is not below the reshape_position,
1358 * we can piggy-back on that.
1359 */
1360 is_active = 1;
1361 if (rdev2->raid_disk < 0 ||
1362 test_bit(Faulty, &rdev2->flags))
1363 is_active = 0;
1364 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001365 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001367 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001368 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 d = &sb->disks[rdev2->desc_nr];
1370 nr_disks++;
1371 d->number = rdev2->desc_nr;
1372 d->major = MAJOR(rdev2->bdev->bd_dev);
1373 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd92009-11-13 17:40:48 +11001374 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 d->raid_disk = rdev2->raid_disk;
1376 else
1377 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001378 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd92009-11-13 17:40:48 +11001380 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd92009-11-13 17:40:48 +11001382 if (test_bit(In_sync, &rdev2->flags))
1383 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 active++;
1385 working++;
1386 } else {
1387 d->state = 0;
1388 spare++;
1389 working++;
1390 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001391 if (test_bit(WriteMostly, &rdev2->flags))
1392 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 /* now set the "removed" and "faulty" bits on any missing devices */
1395 for (i=0 ; i < mddev->raid_disks ; i++) {
1396 mdp_disk_t *d = &sb->disks[i];
1397 if (d->state == 0 && d->number == 0) {
1398 d->number = i;
1399 d->raid_disk = i;
1400 d->state = (1<<MD_DISK_REMOVED);
1401 d->state |= (1<<MD_DISK_FAULTY);
1402 failed++;
1403 }
1404 }
1405 sb->nr_disks = nr_disks;
1406 sb->active_disks = active;
1407 sb->working_disks = working;
1408 sb->failed_disks = failed;
1409 sb->spare_disks = spare;
1410
1411 sb->this_disk = sb->disks[rdev->desc_nr];
1412 sb->sb_csum = calc_sb_csum(sb);
1413}
1414
1415/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001416 * rdev_size_change for 0.90.0
1417 */
1418static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001419super_90_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001420{
Andre Noll58c0fed2009-03-31 14:33:13 +11001421 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001422 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001423 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001424 return 0; /* can't move bitmap */
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11001425 rdev->sb_start = calc_dev_sboffset(rdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001426 if (!num_sectors || num_sectors > rdev->sb_start)
1427 num_sectors = rdev->sb_start;
NeilBrown27a7b262011-09-10 17:21:28 +10001428 /* Limit to 4TB as metadata cannot record more than that.
1429 * 4TB == 2^32 KB, or 2*2^32 sectors.
1430 */
1431 if (num_sectors >= (2ULL << 32))
1432 num_sectors = (2ULL << 32) - 2;
Andre Noll0f420352008-07-11 22:02:23 +10001433 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001434 rdev->sb_page);
1435 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001436 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001437}
1438
1439
1440/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 * version 1 superblock
1442 */
1443
NeilBrown1c05b4b2006-10-21 10:24:08 -07001444static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001446 __le32 disk_csum;
1447 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 unsigned long long newcsum;
1449 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001450 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 int i;
1452
1453 disk_csum = sb->sb_csum;
1454 sb->sb_csum = 0;
1455 newcsum = 0;
1456 for (i=0; size>=4; size -= 4 )
1457 newcsum += le32_to_cpu(*isuper++);
1458
1459 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001460 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1463 sb->sb_csum = disk_csum;
1464 return cpu_to_le32(csum);
1465}
1466
NeilBrown2699b672011-07-28 11:31:47 +10001467static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
1468 int acknowledged);
NeilBrown3cb03002011-10-11 16:45:26 +11001469static int super_1_load(struct md_rdev *rdev, struct md_rdev *refdev, int minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
1471 struct mdp_superblock_1 *sb;
1472 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001473 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001475 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 /*
Andre Noll0f420352008-07-11 22:02:23 +10001478 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 * It is always aligned to a 4K boundary and
1480 * depeding on minor_version, it can be:
1481 * 0: At least 8K, but less than 12K, from end of device
1482 * 1: At start of device
1483 * 2: 4K from start of device.
1484 */
1485 switch(minor_version) {
1486 case 0:
Mike Snitzer77304d22010-11-08 14:39:12 +01001487 sb_start = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll0f420352008-07-11 22:02:23 +10001488 sb_start -= 8*2;
1489 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 break;
1491 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001492 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 break;
1494 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001495 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 break;
1497 default:
1498 return -EINVAL;
1499 }
Andre Noll0f420352008-07-11 22:02:23 +10001500 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
NeilBrown0002b272005-09-09 16:23:53 -07001502 /* superblock is rarely larger than 1K, but it can be larger,
1503 * and it is safe to read 4k, so we do that
1504 */
1505 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (ret) return ret;
1507
1508
Namhyung Kim65a06f02011-07-27 11:00:36 +10001509 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1512 sb->major_version != cpu_to_le32(1) ||
1513 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001514 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001515 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 return -EINVAL;
1517
1518 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1519 printk("md: invalid superblock checksum on %s\n",
1520 bdevname(rdev->bdev,b));
1521 return -EINVAL;
1522 }
1523 if (le64_to_cpu(sb->data_size) < 10) {
1524 printk("md: data_size too small on %s\n",
1525 bdevname(rdev->bdev,b));
1526 return -EINVAL;
1527 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 rdev->preferred_minor = 0xffff;
1530 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001531 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
NeilBrown0002b272005-09-09 16:23:53 -07001533 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001534 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001535 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001536 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1537
1538 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001539 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001540 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001541
NeilBrown31b65a02006-07-10 04:44:14 -07001542 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1543 rdev->desc_nr = -1;
1544 else
1545 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1546
NeilBrown2699b672011-07-28 11:31:47 +10001547 if (!rdev->bb_page) {
1548 rdev->bb_page = alloc_page(GFP_KERNEL);
1549 if (!rdev->bb_page)
1550 return -ENOMEM;
1551 }
1552 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BAD_BLOCKS) &&
1553 rdev->badblocks.count == 0) {
1554 /* need to load the bad block list.
1555 * Currently we limit it to one page.
1556 */
1557 s32 offset;
1558 sector_t bb_sector;
1559 u64 *bbp;
1560 int i;
1561 int sectors = le16_to_cpu(sb->bblog_size);
1562 if (sectors > (PAGE_SIZE / 512))
1563 return -EINVAL;
1564 offset = le32_to_cpu(sb->bblog_offset);
1565 if (offset == 0)
1566 return -EINVAL;
1567 bb_sector = (long long)offset;
1568 if (!sync_page_io(rdev, bb_sector, sectors << 9,
1569 rdev->bb_page, READ, true))
1570 return -EIO;
1571 bbp = (u64 *)page_address(rdev->bb_page);
1572 rdev->badblocks.shift = sb->bblog_shift;
1573 for (i = 0 ; i < (sectors << (9-3)) ; i++, bbp++) {
1574 u64 bb = le64_to_cpu(*bbp);
1575 int count = bb & (0x3ff);
1576 u64 sector = bb >> 10;
1577 sector <<= sb->bblog_shift;
1578 count <<= sb->bblog_shift;
1579 if (bb + 1 == 0)
1580 break;
1581 if (md_set_badblocks(&rdev->badblocks,
1582 sector, count, 1) == 0)
1583 return -EINVAL;
1584 }
1585 } else if (sb->bblog_offset == 0)
1586 rdev->badblocks.shift = -1;
1587
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001588 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001589 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001590 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 __u64 ev1, ev2;
Namhyung Kim65a06f02011-07-27 11:00:36 +10001592 struct mdp_superblock_1 *refsb = page_address(refdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1595 sb->level != refsb->level ||
1596 sb->layout != refsb->layout ||
1597 sb->chunksize != refsb->chunksize) {
1598 printk(KERN_WARNING "md: %s has strangely different"
1599 " superblock to %s\n",
1600 bdevname(rdev->bdev,b),
1601 bdevname(refdev->bdev,b2));
1602 return -EINVAL;
1603 }
1604 ev1 = le64_to_cpu(sb->events);
1605 ev2 = le64_to_cpu(refsb->events);
1606
1607 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001608 ret = 1;
1609 else
1610 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 }
NeilBrowna1801f82008-03-04 14:29:31 -08001612 if (minor_version)
Mike Snitzer77304d22010-11-08 14:39:12 +01001613 rdev->sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11001614 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001616 rdev->sectors = rdev->sb_start;
1617 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001619 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001620 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001621 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001622 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623}
1624
NeilBrownfd01b882011-10-11 16:47:53 +11001625static int super_1_validate(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
Namhyung Kim65a06f02011-07-27 11:00:36 +10001627 struct mdp_superblock_1 *sb = page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001628 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
NeilBrown41158c72005-06-21 17:17:25 -07001630 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001631 clear_bit(Faulty, &rdev->flags);
1632 clear_bit(In_sync, &rdev->flags);
1633 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 if (mddev->raid_disks == 0) {
1636 mddev->major_version = 1;
1637 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001638 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001639 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1641 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1642 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001643 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 mddev->layout = le32_to_cpu(sb->layout);
1645 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001646 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001647 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001648 mddev->bitmap_info.offset = 0;
1649 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1652 memcpy(mddev->uuid, sb->set_uuid, 16);
1653
1654 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001655
NeilBrown71c08052005-09-09 16:23:51 -07001656 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001657 mddev->bitmap_info.file == NULL )
1658 mddev->bitmap_info.offset =
1659 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001660
NeilBrownf6705572006-03-27 01:18:11 -08001661 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1662 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1663 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1664 mddev->new_level = le32_to_cpu(sb->new_level);
1665 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001666 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001667 } else {
1668 mddev->reshape_position = MaxSector;
1669 mddev->delta_disks = 0;
1670 mddev->new_level = mddev->level;
1671 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001672 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001673 }
1674
NeilBrown41158c72005-06-21 17:17:25 -07001675 } else if (mddev->pers == NULL) {
NeilBrownbe6800a2010-05-18 10:17:09 +10001676 /* Insist of good event counter while assembling, except for
1677 * spares (which don't need an event count) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 ++ev1;
NeilBrownbe6800a2010-05-18 10:17:09 +10001679 if (rdev->desc_nr >= 0 &&
1680 rdev->desc_nr < le32_to_cpu(sb->max_dev) &&
1681 le16_to_cpu(sb->dev_roles[rdev->desc_nr]) < 0xfffe)
1682 if (ev1 < mddev->events)
1683 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001684 } else if (mddev->bitmap) {
1685 /* If adding to array with a bitmap, then we can accept an
1686 * older device, but not too old.
1687 */
NeilBrown41158c72005-06-21 17:17:25 -07001688 if (ev1 < mddev->bitmap->events_cleared)
1689 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001690 } else {
1691 if (ev1 < mddev->events)
1692 /* just a hot-add of a new device, leave raid_disk at -1 */
1693 return 0;
1694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 if (mddev->level != LEVEL_MULTIPATH) {
1696 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001697 if (rdev->desc_nr < 0 ||
1698 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1699 role = 0xffff;
1700 rdev->desc_nr = -1;
1701 } else
1702 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 switch(role) {
1704 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 break;
1706 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001707 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 break;
1709 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001710 if ((le32_to_cpu(sb->feature_map) &
1711 MD_FEATURE_RECOVERY_OFFSET))
1712 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1713 else
1714 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 rdev->raid_disk = role;
1716 break;
1717 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001718 if (sb->devflags & WriteMostly1)
1719 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001720 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001721 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return 0;
1724}
1725
NeilBrownfd01b882011-10-11 16:47:53 +11001726static void super_1_sync(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
1728 struct mdp_superblock_1 *sb;
NeilBrown3cb03002011-10-11 16:45:26 +11001729 struct md_rdev *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 int max_dev, i;
1731 /* make rdev->sb match mddev and rdev data. */
1732
Namhyung Kim65a06f02011-07-27 11:00:36 +10001733 sb = page_address(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 sb->feature_map = 0;
1736 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001737 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 memset(sb->pad1, 0, sizeof(sb->pad1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 memset(sb->pad3, 0, sizeof(sb->pad3));
1740
1741 sb->utime = cpu_to_le64((__u64)mddev->utime);
1742 sb->events = cpu_to_le64(mddev->events);
1743 if (mddev->in_sync)
1744 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1745 else
1746 sb->resync_offset = cpu_to_le64(0);
1747
NeilBrown1c05b4b2006-10-21 10:24:08 -07001748 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001749
NeilBrownf0ca3402006-02-02 14:28:04 -08001750 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001751 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001752 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001753 sb->level = cpu_to_le32(mddev->level);
1754 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001755
NeilBrownaeb9b212011-08-25 14:43:08 +10001756 if (test_bit(WriteMostly, &rdev->flags))
1757 sb->devflags |= WriteMostly1;
1758 else
1759 sb->devflags &= ~WriteMostly1;
1760
NeilBrownc3d97142009-12-14 12:49:52 +11001761 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1762 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001763 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001764 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001765
1766 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001767 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001768 sb->feature_map |=
1769 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1770 sb->recovery_offset =
1771 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001772 }
1773
NeilBrownf6705572006-03-27 01:18:11 -08001774 if (mddev->reshape_position != MaxSector) {
1775 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1776 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1777 sb->new_layout = cpu_to_le32(mddev->new_layout);
1778 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1779 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001780 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001781 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001782
NeilBrown2699b672011-07-28 11:31:47 +10001783 if (rdev->badblocks.count == 0)
1784 /* Nothing to do for bad blocks*/ ;
1785 else if (sb->bblog_offset == 0)
1786 /* Cannot record bad blocks on this device */
1787 md_error(mddev, rdev);
1788 else {
1789 struct badblocks *bb = &rdev->badblocks;
1790 u64 *bbp = (u64 *)page_address(rdev->bb_page);
1791 u64 *p = bb->page;
1792 sb->feature_map |= cpu_to_le32(MD_FEATURE_BAD_BLOCKS);
1793 if (bb->changed) {
1794 unsigned seq;
1795
1796retry:
1797 seq = read_seqbegin(&bb->lock);
1798
1799 memset(bbp, 0xff, PAGE_SIZE);
1800
1801 for (i = 0 ; i < bb->count ; i++) {
1802 u64 internal_bb = *p++;
1803 u64 store_bb = ((BB_OFFSET(internal_bb) << 10)
1804 | BB_LEN(internal_bb));
1805 *bbp++ = cpu_to_le64(store_bb);
1806 }
1807 if (read_seqretry(&bb->lock, seq))
1808 goto retry;
1809
1810 bb->sector = (rdev->sb_start +
1811 (int)le32_to_cpu(sb->bblog_offset));
1812 bb->size = le16_to_cpu(sb->bblog_size);
1813 bb->changed = 0;
1814 }
1815 }
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001818 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (rdev2->desc_nr+1 > max_dev)
1820 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001821
NeilBrown70471da2009-08-03 10:59:57 +10001822 if (max_dev > le32_to_cpu(sb->max_dev)) {
1823 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001824 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001825 rdev->sb_size = max_dev * 2 + 256;
1826 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1827 if (rdev->sb_size & bmask)
1828 rdev->sb_size = (rdev->sb_size | bmask) + 1;
NeilBrownddcf3522010-09-08 16:48:17 +10001829 } else
1830 max_dev = le32_to_cpu(sb->max_dev);
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 for (i=0; i<max_dev;i++)
1833 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1834
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001835 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001837 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001839 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001841 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001842 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 else
1844 sb->dev_roles[i] = cpu_to_le16(0xffff);
1845 }
1846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 sb->sb_csum = calc_sb_1_csum(sb);
1848}
1849
Chris Webb0cd17fe2008-06-28 08:31:46 +10001850static unsigned long long
NeilBrown3cb03002011-10-11 16:45:26 +11001851super_1_rdev_size_change(struct md_rdev *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001852{
1853 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001854 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001855 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001856 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001857 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001858 /* minor versions 1 and 2; superblock before data */
Mike Snitzer77304d22010-11-08 14:39:12 +01001859 max_sectors = i_size_read(rdev->bdev->bd_inode) >> 9;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001860 max_sectors -= rdev->data_offset;
1861 if (!num_sectors || num_sectors > max_sectors)
1862 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001863 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001864 /* minor version 0 with bitmap we can't move */
1865 return 0;
1866 } else {
1867 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001868 sector_t sb_start;
Mike Snitzer77304d22010-11-08 14:39:12 +01001869 sb_start = (i_size_read(rdev->bdev->bd_inode) >> 9) - 8*2;
Andre Noll0f420352008-07-11 22:02:23 +10001870 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001871 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001872 if (!num_sectors || num_sectors > max_sectors)
1873 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001874 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001875 }
Namhyung Kim65a06f02011-07-27 11:00:36 +10001876 sb = page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001877 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001878 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001879 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001880 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001881 rdev->sb_page);
1882 md_super_wait(rdev->mddev);
Justin Maggardc26a44e2010-11-24 16:36:17 +11001883 return num_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001884}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Adrian Bunk75c96f82005-05-05 16:16:09 -07001886static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 [0] = {
1888 .name = "0.90.0",
1889 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001890 .load_super = super_90_load,
1891 .validate_super = super_90_validate,
1892 .sync_super = super_90_sync,
1893 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 },
1895 [1] = {
1896 .name = "md-1",
1897 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001898 .load_super = super_1_load,
1899 .validate_super = super_1_validate,
1900 .sync_super = super_1_sync,
1901 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 },
1903};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
NeilBrownfd01b882011-10-11 16:47:53 +11001905static void sync_super(struct mddev *mddev, struct md_rdev *rdev)
Jonathan Brassow076f9682011-06-07 17:51:30 -05001906{
1907 if (mddev->sync_super) {
1908 mddev->sync_super(mddev, rdev);
1909 return;
1910 }
1911
1912 BUG_ON(mddev->major_version >= ARRAY_SIZE(super_types));
1913
1914 super_types[mddev->major_version].sync_super(mddev, rdev);
1915}
1916
NeilBrownfd01b882011-10-11 16:47:53 +11001917static int match_mddev_units(struct mddev *mddev1, struct mddev *mddev2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
NeilBrown3cb03002011-10-11 16:45:26 +11001919 struct md_rdev *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
NeilBrown4b809912008-07-21 17:05:25 +10001921 rcu_read_lock();
1922 rdev_for_each_rcu(rdev, mddev1)
1923 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001924 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001925 rdev2->bdev->bd_contains) {
1926 rcu_read_unlock();
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001927 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001928 }
1929 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 return 0;
1931}
1932
1933static LIST_HEAD(pending_raid_disks);
1934
Andre Nollac5e7112009-08-03 10:59:47 +10001935/*
1936 * Try to register data integrity profile for an mddev
1937 *
1938 * This is called when an array is started and after a disk has been kicked
1939 * from the array. It only succeeds if all working and active component devices
1940 * are integrity capable with matching profiles.
1941 */
NeilBrownfd01b882011-10-11 16:47:53 +11001942int md_integrity_register(struct mddev *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001943{
NeilBrown3cb03002011-10-11 16:45:26 +11001944 struct md_rdev *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001945
Andre Nollac5e7112009-08-03 10:59:47 +10001946 if (list_empty(&mddev->disks))
1947 return 0; /* nothing to do */
Jonathan Brassow629acb62011-06-08 15:10:08 +10001948 if (!mddev->gendisk || blk_get_integrity(mddev->gendisk))
1949 return 0; /* shouldn't register, or already is */
Andre Nollac5e7112009-08-03 10:59:47 +10001950 list_for_each_entry(rdev, &mddev->disks, same_set) {
1951 /* skip spares and non-functional disks */
1952 if (test_bit(Faulty, &rdev->flags))
1953 continue;
1954 if (rdev->raid_disk < 0)
1955 continue;
Andre Nollac5e7112009-08-03 10:59:47 +10001956 if (!reference) {
1957 /* Use the first rdev as the reference */
1958 reference = rdev;
1959 continue;
1960 }
1961 /* does this rdev's profile match the reference profile? */
1962 if (blk_integrity_compare(reference->bdev->bd_disk,
1963 rdev->bdev->bd_disk) < 0)
1964 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001965 }
Martin K. Petersen89078d52011-03-28 20:09:12 -04001966 if (!reference || !bdev_get_integrity(reference->bdev))
1967 return 0;
Andre Nollac5e7112009-08-03 10:59:47 +10001968 /*
1969 * All component devices are integrity capable and have matching
1970 * profiles, register the common profile for the md device.
1971 */
1972 if (blk_integrity_register(mddev->gendisk,
1973 bdev_get_integrity(reference->bdev)) != 0) {
1974 printk(KERN_ERR "md: failed to register integrity for %s\n",
1975 mdname(mddev));
1976 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001977 }
Martin K. Petersena91a2782011-03-17 11:11:05 +01001978 printk(KERN_NOTICE "md: data integrity enabled on %s\n", mdname(mddev));
1979 if (bioset_integrity_create(mddev->bio_set, BIO_POOL_SIZE)) {
1980 printk(KERN_ERR "md: failed to create integrity pool for %s\n",
1981 mdname(mddev));
1982 return -EINVAL;
1983 }
Andre Nollac5e7112009-08-03 10:59:47 +10001984 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001985}
Andre Nollac5e7112009-08-03 10:59:47 +10001986EXPORT_SYMBOL(md_integrity_register);
1987
1988/* Disable data integrity if non-capable/non-matching disk is being added */
NeilBrownfd01b882011-10-11 16:47:53 +11001989void md_integrity_add_rdev(struct md_rdev *rdev, struct mddev *mddev)
Andre Nollac5e7112009-08-03 10:59:47 +10001990{
1991 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1992 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1993
1994 if (!bi_mddev) /* nothing to do */
1995 return;
1996 if (rdev->raid_disk < 0) /* skip spares */
1997 return;
1998 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1999 rdev->bdev->bd_disk) >= 0)
2000 return;
2001 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
2002 blk_integrity_unregister(mddev->gendisk);
2003}
2004EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002005
NeilBrownfd01b882011-10-11 16:47:53 +11002006static int bind_rdev_to_array(struct md_rdev * rdev, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08002008 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08002009 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08002010 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002011 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 if (rdev->mddev) {
2014 MD_BUG();
2015 return -EINVAL;
2016 }
Dan Williams11e2ede2008-04-30 00:52:32 -07002017
2018 /* prevent duplicates */
2019 if (find_rdev(mddev, rdev->bdev->bd_dev))
2020 return -EEXIST;
2021
Andre Nolldd8ac332009-03-31 14:33:13 +11002022 /* make sure rdev->sectors exceeds mddev->dev_sectors */
2023 if (rdev->sectors && (mddev->dev_sectors == 0 ||
2024 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07002025 if (mddev->pers) {
2026 /* Cannot change size, so fail
2027 * If mddev->level <= 0, then we don't care
2028 * about aligning sizes (e.g. linear)
2029 */
2030 if (mddev->level > 0)
2031 return -ENOSPC;
2032 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11002033 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08002034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
2036 /* Verify rdev->desc_nr is unique.
2037 * If it is -1, assign a free number, else
2038 * check number is not in use
2039 */
2040 if (rdev->desc_nr < 0) {
2041 int choice = 0;
2042 if (mddev->pers) choice = mddev->raid_disks;
2043 while (find_rdev_nr(mddev, choice))
2044 choice++;
2045 rdev->desc_nr = choice;
2046 } else {
2047 if (find_rdev_nr(mddev, rdev->desc_nr))
2048 return -EBUSY;
2049 }
NeilBrownde01dfa2009-02-06 18:02:46 +11002050 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
2051 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
2052 mdname(mddev), mddev->max_disks);
2053 return -EBUSY;
2054 }
NeilBrown19133a42005-11-08 21:39:35 -08002055 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002056 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08002057 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07002058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08002060 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002061
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07002062 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08002063 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002064
Tejun Heo0762b8b2008-08-25 19:56:12 +09002065 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown00bcb4a2010-06-01 19:37:23 +10002066 if (sysfs_create_link(&rdev->kobj, ko, "block"))
2067 /* failure here is OK */;
2068 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
NeilBrown3c0ee632008-10-21 13:25:28 +11002069
NeilBrown4b809912008-07-21 17:05:25 +10002070 list_add_rcu(&rdev->same_set, &mddev->disks);
Tejun Heoe09b4572010-11-13 11:55:17 +01002071 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11002072
2073 /* May as well allow recovery to be retried once */
NeilBrown53890422011-07-27 11:00:36 +10002074 mddev->recovery_disabled++;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11002075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08002077
2078 fail:
2079 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
2080 b, mdname(mddev));
2081 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082}
2083
NeilBrown177a99b2008-02-06 01:39:56 -08002084static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07002085{
NeilBrown3cb03002011-10-11 16:45:26 +11002086 struct md_rdev *rdev = container_of(ws, struct md_rdev, del_work);
NeilBrown5792a282007-04-04 19:08:18 -07002087 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08002088 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07002089}
2090
NeilBrown3cb03002011-10-11 16:45:26 +11002091static void unbind_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 char b[BDEVNAME_SIZE];
2094 if (!rdev->mddev) {
2095 MD_BUG();
2096 return;
2097 }
Tejun Heo49731ba2011-01-14 18:43:57 +01002098 bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10002099 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
2101 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002102 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11002103 sysfs_put(rdev->sysfs_state);
2104 rdev->sysfs_state = NULL;
NeilBrown2230dfe2011-07-28 11:31:46 +10002105 kfree(rdev->badblocks.page);
2106 rdev->badblocks.count = 0;
2107 rdev->badblocks.page = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07002108 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10002109 * writing to 'remove' to "dev/state". We also need
2110 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07002111 */
NeilBrown4b809912008-07-21 17:05:25 +10002112 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08002113 INIT_WORK(&rdev->del_work, md_delayed_delete);
2114 kobject_get(&rdev->kobj);
Tejun Heoe804ac72010-10-15 15:36:08 +02002115 queue_work(md_misc_wq, &rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116}
2117
2118/*
2119 * prevent the device from being mounted, repartitioned or
2120 * otherwise reused by a RAID array (or any other kernel
2121 * subsystem), by bd_claiming the device.
2122 */
NeilBrown3cb03002011-10-11 16:45:26 +11002123static int lock_rdev(struct md_rdev *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
2125 int err = 0;
2126 struct block_device *bdev;
2127 char b[BDEVNAME_SIZE];
2128
Tejun Heod4d77622010-11-13 11:55:18 +01002129 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
NeilBrown3cb03002011-10-11 16:45:26 +11002130 shared ? (struct md_rdev *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (IS_ERR(bdev)) {
2132 printk(KERN_ERR "md: could not open %s.\n",
2133 __bdevname(dev, b));
2134 return PTR_ERR(bdev);
2135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 rdev->bdev = bdev;
2137 return err;
2138}
2139
NeilBrown3cb03002011-10-11 16:45:26 +11002140static void unlock_rdev(struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
2142 struct block_device *bdev = rdev->bdev;
2143 rdev->bdev = NULL;
2144 if (!bdev)
2145 MD_BUG();
Tejun Heoe525fd82010-11-13 11:55:17 +01002146 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
2149void md_autodetect_dev(dev_t dev);
2150
NeilBrown3cb03002011-10-11 16:45:26 +11002151static void export_rdev(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
2153 char b[BDEVNAME_SIZE];
2154 printk(KERN_INFO "md: export_rdev(%s)\n",
2155 bdevname(rdev->bdev,b));
2156 if (rdev->mddev)
2157 MD_BUG();
2158 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08002160 if (test_bit(AutoDetected, &rdev->flags))
2161 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162#endif
2163 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002164 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
2166
NeilBrown3cb03002011-10-11 16:45:26 +11002167static void kick_rdev_from_array(struct md_rdev * rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
2169 unbind_rdev_from_array(rdev);
2170 export_rdev(rdev);
2171}
2172
NeilBrownfd01b882011-10-11 16:47:53 +11002173static void export_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
NeilBrown3cb03002011-10-11 16:45:26 +11002175 struct md_rdev *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
NeilBrownd089c6a2008-02-06 01:39:59 -08002177 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 if (!rdev->mddev) {
2179 MD_BUG();
2180 continue;
2181 }
2182 kick_rdev_from_array(rdev);
2183 }
2184 if (!list_empty(&mddev->disks))
2185 MD_BUG();
2186 mddev->raid_disks = 0;
2187 mddev->major_version = 0;
2188}
2189
2190static void print_desc(mdp_disk_t *desc)
2191{
2192 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
2193 desc->major,desc->minor,desc->raid_disk,desc->state);
2194}
2195
Cheng Renquancd2ac932009-01-09 08:31:08 +11002196static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
2198 int i;
2199
2200 printk(KERN_INFO
2201 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
2202 sb->major_version, sb->minor_version, sb->patch_version,
2203 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
2204 sb->ctime);
2205 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
2206 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
2207 sb->md_minor, sb->layout, sb->chunk_size);
2208 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
2209 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
2210 sb->utime, sb->state, sb->active_disks, sb->working_disks,
2211 sb->failed_disks, sb->spare_disks,
2212 sb->sb_csum, (unsigned long)sb->events_lo);
2213
2214 printk(KERN_INFO);
2215 for (i = 0; i < MD_SB_DISKS; i++) {
2216 mdp_disk_t *desc;
2217
2218 desc = sb->disks + i;
2219 if (desc->number || desc->major || desc->minor ||
2220 desc->raid_disk || (desc->state && (desc->state != 4))) {
2221 printk(" D %2d: ", i);
2222 print_desc(desc);
2223 }
2224 }
2225 printk(KERN_INFO "md: THIS: ");
2226 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227}
2228
Cheng Renquancd2ac932009-01-09 08:31:08 +11002229static void print_sb_1(struct mdp_superblock_1 *sb)
2230{
2231 __u8 *uuid;
2232
2233 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002234 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08002235 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002236 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002237 le32_to_cpu(sb->major_version),
2238 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002239 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002240 sb->set_name,
2241 (unsigned long long)le64_to_cpu(sb->ctime)
2242 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
2243
2244 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07002245 printk(KERN_INFO
2246 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11002247 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08002248 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07002249 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
2250 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11002251 le32_to_cpu(sb->level),
2252 (unsigned long long)le64_to_cpu(sb->size),
2253 le32_to_cpu(sb->raid_disks),
2254 le32_to_cpu(sb->layout),
2255 le32_to_cpu(sb->chunksize),
2256 (unsigned long long)le64_to_cpu(sb->data_offset),
2257 (unsigned long long)le64_to_cpu(sb->data_size),
2258 (unsigned long long)le64_to_cpu(sb->super_offset),
2259 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2260 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002261 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002262 sb->devflags,
2263 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2264 (unsigned long long)le64_to_cpu(sb->events),
2265 (unsigned long long)le64_to_cpu(sb->resync_offset),
2266 le32_to_cpu(sb->sb_csum),
2267 le32_to_cpu(sb->max_dev)
2268 );
2269}
2270
NeilBrown3cb03002011-10-11 16:45:26 +11002271static void print_rdev(struct md_rdev *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
2273 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002274 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2275 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002276 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2277 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002279 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2280 switch (major_version) {
2281 case 0:
Namhyung Kim65a06f02011-07-27 11:00:36 +10002282 print_sb_90(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002283 break;
2284 case 1:
Namhyung Kim65a06f02011-07-27 11:00:36 +10002285 print_sb_1(page_address(rdev->sb_page));
Cheng Renquancd2ac932009-01-09 08:31:08 +11002286 break;
2287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 } else
2289 printk(KERN_INFO "md: no rdev superblock!\n");
2290}
2291
Adrian Bunk5e563412006-06-26 00:27:42 -07002292static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002294 struct list_head *tmp;
NeilBrown3cb03002011-10-11 16:45:26 +11002295 struct md_rdev *rdev;
NeilBrownfd01b882011-10-11 16:47:53 +11002296 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 char b[BDEVNAME_SIZE];
2298
2299 printk("\n");
2300 printk("md: **********************************\n");
2301 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2302 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002303 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
NeilBrown32a76272005-06-21 17:17:14 -07002305 if (mddev->bitmap)
2306 bitmap_print_sb(mddev->bitmap);
2307 else
2308 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002309 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 printk("<%s>", bdevname(rdev->bdev,b));
2311 printk("\n");
2312
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002313 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002314 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 }
2316 printk("md: **********************************\n");
2317 printk("\n");
2318}
2319
2320
NeilBrownfd01b882011-10-11 16:47:53 +11002321static void sync_sbs(struct mddev * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
NeilBrown42543762006-06-26 00:27:57 -07002323 /* Update each superblock (in-memory image), but
2324 * if we are allowed to, skip spares which already
2325 * have the right event counter, or have one earlier
2326 * (which would mean they aren't being marked as dirty
2327 * with the rest of the array)
2328 */
NeilBrown3cb03002011-10-11 16:45:26 +11002329 struct md_rdev *rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002330 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002331 if (rdev->sb_events == mddev->events ||
2332 (nospares &&
2333 rdev->raid_disk < 0 &&
NeilBrown42543762006-06-26 00:27:57 -07002334 rdev->sb_events+1 == mddev->events)) {
2335 /* Don't update this superblock */
2336 rdev->sb_loaded = 2;
2337 } else {
Jonathan Brassow076f9682011-06-07 17:51:30 -05002338 sync_super(mddev, rdev);
NeilBrown42543762006-06-26 00:27:57 -07002339 rdev->sb_loaded = 1;
2340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 }
2342}
2343
NeilBrownfd01b882011-10-11 16:47:53 +11002344static void md_update_sb(struct mddev * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
NeilBrown3cb03002011-10-11 16:45:26 +11002346 struct md_rdev *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002347 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002348 int nospares = 0;
NeilBrown2699b672011-07-28 11:31:47 +10002349 int any_badblocks_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351repeat:
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002352 /* First make sure individual recovery_offsets are correct */
2353 list_for_each_entry(rdev, &mddev->disks, same_set) {
2354 if (rdev->raid_disk >= 0 &&
2355 mddev->delta_disks >= 0 &&
2356 !test_bit(In_sync, &rdev->flags) &&
2357 mddev->curr_resync_completed > rdev->recovery_offset)
2358 rdev->recovery_offset = mddev->curr_resync_completed;
2359
2360 }
Dan Williamsbd52b742010-08-30 17:33:33 +10002361 if (!mddev->persistent) {
NeilBrown070dc6d2010-08-30 17:33:34 +10002362 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002363 clear_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002364 if (!mddev->external) {
NeilBrownd97a41d2010-10-28 17:30:20 +11002365 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002366 list_for_each_entry(rdev, &mddev->disks, same_set) {
2367 if (rdev->badblocks.changed) {
2368 md_ack_all_badblocks(&rdev->badblocks);
2369 md_error(mddev, rdev);
2370 }
2371 clear_bit(Blocked, &rdev->flags);
2372 clear_bit(BlockedBadBlocks, &rdev->flags);
2373 wake_up(&rdev->blocked_wait);
2374 }
2375 }
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002376 wake_up(&mddev->sb_wait);
2377 return;
2378 }
2379
NeilBrowna9701a32005-11-08 21:39:34 -08002380 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002381
NeilBrown3a3a5dd2010-08-16 18:09:31 +10002382 mddev->utime = get_seconds();
2383
NeilBrown850b2b42006-10-03 01:15:46 -07002384 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2385 force_change = 1;
2386 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2387 /* just a clean<-> dirty transition, possibly leave spares alone,
2388 * though if events isn't the right even/odd, we will have to do
2389 * spares after all
2390 */
2391 nospares = 1;
2392 if (force_change)
2393 nospares = 0;
2394 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002395 /* If the array is degraded, then skipping spares is both
2396 * dangerous and fairly pointless.
2397 * Dangerous because a device that was removed from the array
2398 * might have a event_count that still looks up-to-date,
2399 * so it can be re-added without a resync.
2400 * Pointless because if there are any spares to skip,
2401 * then a recovery will happen and soon that array won't
2402 * be degraded any more and the spare can go back to sleep then.
2403 */
NeilBrown850b2b42006-10-03 01:15:46 -07002404 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002405
NeilBrown06d91a52005-06-21 17:17:12 -07002406 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002407
2408 /* If this is just a dirty<->clean transition, and the array is clean
2409 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002410 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002411 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrowna8707c02010-05-18 09:28:43 +10002412 && mddev->can_decrease_events
2413 && mddev->events != 1) {
NeilBrown42543762006-06-26 00:27:57 -07002414 mddev->events--;
NeilBrowna8707c02010-05-18 09:28:43 +10002415 mddev->can_decrease_events = 0;
2416 } else {
NeilBrown42543762006-06-26 00:27:57 -07002417 /* otherwise we have to go forward and ... */
2418 mddev->events ++;
NeilBrowna8707c02010-05-18 09:28:43 +10002419 mddev->can_decrease_events = nospares;
NeilBrown42543762006-06-26 00:27:57 -07002420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
2422 if (!mddev->events) {
2423 /*
2424 * oops, this 64-bit counter should never wrap.
2425 * Either we are in around ~1 trillion A.C., assuming
2426 * 1 reboot per second, or we have a bug:
2427 */
2428 MD_BUG();
2429 mddev->events --;
2430 }
NeilBrown2699b672011-07-28 11:31:47 +10002431
NeilBrownde393cd2011-07-28 11:31:48 +10002432 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown2699b672011-07-28 11:31:47 +10002433 if (rdev->badblocks.changed)
2434 any_badblocks_changed++;
NeilBrownde393cd2011-07-28 11:31:48 +10002435 if (test_bit(Faulty, &rdev->flags))
2436 set_bit(FaultRecorded, &rdev->flags);
2437 }
NeilBrown2699b672011-07-28 11:31:47 +10002438
NeilBrowne6910632008-02-06 01:39:51 -08002439 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002440 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
NeilBrown36a4e1f2011-10-07 14:23:17 +11002442 pr_debug("md: updating %s RAID superblock on device (in sync %d)\n",
2443 mdname(mddev), mddev->in_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
NeilBrown4ad13662007-07-17 04:06:13 -07002445 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002446 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 char b[BDEVNAME_SIZE];
NeilBrown36a4e1f2011-10-07 14:23:17 +11002448
NeilBrown42543762006-06-26 00:27:57 -07002449 if (rdev->sb_loaded != 1)
2450 continue; /* no noise on spare devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
NeilBrownb2d444d2005-11-08 21:39:31 -08002452 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002453 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002454 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002455 rdev->sb_page);
NeilBrown36a4e1f2011-10-07 14:23:17 +11002456 pr_debug("md: (write) %s's sb offset: %llu\n",
2457 bdevname(rdev->bdev, b),
2458 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002459 rdev->sb_events = mddev->events;
NeilBrown2699b672011-07-28 11:31:47 +10002460 if (rdev->badblocks.size) {
2461 md_super_write(mddev, rdev,
2462 rdev->badblocks.sector,
2463 rdev->badblocks.size << 9,
2464 rdev->bb_page);
2465 rdev->badblocks.size = 0;
2466 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07002467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 } else
NeilBrown36a4e1f2011-10-07 14:23:17 +11002469 pr_debug("md: %s (skipping faulty)\n",
2470 bdevname(rdev->bdev, b));
NeilBrown7bfa19f2005-06-21 17:17:28 -07002471 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 /* only need to write one superblock... */
2473 break;
2474 }
NeilBrowna9701a32005-11-08 21:39:34 -08002475 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002476 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002477
NeilBrowna9701a32005-11-08 21:39:34 -08002478 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002479 if (mddev->in_sync != sync_req ||
2480 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002481 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002482 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002483 goto repeat;
2484 }
NeilBrown850b2b42006-10-03 01:15:46 -07002485 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002486 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002487 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002488 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2489 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002490
NeilBrownde393cd2011-07-28 11:31:48 +10002491 list_for_each_entry(rdev, &mddev->disks, same_set) {
2492 if (test_and_clear_bit(FaultRecorded, &rdev->flags))
2493 clear_bit(Blocked, &rdev->flags);
2494
2495 if (any_badblocks_changed)
NeilBrown2699b672011-07-28 11:31:47 +10002496 md_ack_all_badblocks(&rdev->badblocks);
NeilBrownde393cd2011-07-28 11:31:48 +10002497 clear_bit(BlockedBadBlocks, &rdev->flags);
2498 wake_up(&rdev->blocked_wait);
2499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500}
2501
Andre Noll7f6ce762008-03-23 18:34:54 +01002502/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002503 * We want to accept with case. For this we use cmd_match.
2504 */
2505static int cmd_match(const char *cmd, const char *str)
2506{
2507 /* See if cmd, written into a sysfs file, matches
2508 * str. They must either be the same, or cmd can
2509 * have a trailing newline
2510 */
2511 while (*cmd && *str && *cmd == *str) {
2512 cmd++;
2513 str++;
2514 }
2515 if (*cmd == '\n')
2516 cmd++;
2517 if (*str || *cmd)
2518 return 0;
2519 return 1;
2520}
2521
NeilBrown86e6ffd2005-11-08 21:39:24 -08002522struct rdev_sysfs_entry {
2523 struct attribute attr;
NeilBrown3cb03002011-10-11 16:45:26 +11002524 ssize_t (*show)(struct md_rdev *, char *);
2525 ssize_t (*store)(struct md_rdev *, const char *, size_t);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002526};
2527
2528static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002529state_show(struct md_rdev *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002530{
2531 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002532 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002533
NeilBrownde393cd2011-07-28 11:31:48 +10002534 if (test_bit(Faulty, &rdev->flags) ||
2535 rdev->badblocks.unacked_exist) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002536 len+= sprintf(page+len, "%sfaulty",sep);
2537 sep = ",";
2538 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002539 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002540 len += sprintf(page+len, "%sin_sync",sep);
2541 sep = ",";
2542 }
NeilBrownf6556752006-06-26 00:28:01 -07002543 if (test_bit(WriteMostly, &rdev->flags)) {
2544 len += sprintf(page+len, "%swrite_mostly",sep);
2545 sep = ",";
2546 }
NeilBrownde393cd2011-07-28 11:31:48 +10002547 if (test_bit(Blocked, &rdev->flags) ||
2548 rdev->badblocks.unacked_exist) {
Dan Williams6bfe0b42008-04-30 00:52:32 -07002549 len += sprintf(page+len, "%sblocked", sep);
2550 sep = ",";
2551 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002552 if (!test_bit(Faulty, &rdev->flags) &&
2553 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002554 len += sprintf(page+len, "%sspare", sep);
2555 sep = ",";
2556 }
NeilBrownd7a9d442011-07-28 11:31:48 +10002557 if (test_bit(WriteErrorSeen, &rdev->flags)) {
2558 len += sprintf(page+len, "%swrite_error", sep);
2559 sep = ",";
2560 }
NeilBrown86e6ffd2005-11-08 21:39:24 -08002561 return len+sprintf(page+len, "\n");
2562}
2563
NeilBrown45dc2de2006-06-26 00:27:58 -07002564static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002565state_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown45dc2de2006-06-26 00:27:58 -07002566{
2567 /* can write
NeilBrownde393cd2011-07-28 11:31:48 +10002568 * faulty - simulates an error
NeilBrown45dc2de2006-06-26 00:27:58 -07002569 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002570 * writemostly - sets write_mostly
2571 * -writemostly - clears write_mostly
NeilBrownde393cd2011-07-28 11:31:48 +10002572 * blocked - sets the Blocked flags
2573 * -blocked - clears the Blocked and possibly simulates an error
NeilBrown6d56e272009-04-14 12:01:57 +10002574 * insync - sets Insync providing device isn't active
NeilBrownd7a9d442011-07-28 11:31:48 +10002575 * write_error - sets WriteErrorSeen
2576 * -write_error - clears WriteErrorSeen
NeilBrown45dc2de2006-06-26 00:27:58 -07002577 */
2578 int err = -EINVAL;
2579 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2580 md_error(rdev->mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10002581 if (test_bit(Faulty, &rdev->flags))
2582 err = 0;
2583 else
2584 err = -EBUSY;
NeilBrown45dc2de2006-06-26 00:27:58 -07002585 } else if (cmd_match(buf, "remove")) {
2586 if (rdev->raid_disk >= 0)
2587 err = -EBUSY;
2588 else {
NeilBrownfd01b882011-10-11 16:47:53 +11002589 struct mddev *mddev = rdev->mddev;
NeilBrown45dc2de2006-06-26 00:27:58 -07002590 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002591 if (mddev->pers)
2592 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002593 md_new_event(mddev);
2594 err = 0;
2595 }
NeilBrownf6556752006-06-26 00:28:01 -07002596 } else if (cmd_match(buf, "writemostly")) {
2597 set_bit(WriteMostly, &rdev->flags);
2598 err = 0;
2599 } else if (cmd_match(buf, "-writemostly")) {
2600 clear_bit(WriteMostly, &rdev->flags);
2601 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002602 } else if (cmd_match(buf, "blocked")) {
2603 set_bit(Blocked, &rdev->flags);
2604 err = 0;
2605 } else if (cmd_match(buf, "-blocked")) {
NeilBrownde393cd2011-07-28 11:31:48 +10002606 if (!test_bit(Faulty, &rdev->flags) &&
NeilBrown7da64a02011-08-30 16:20:17 +10002607 rdev->badblocks.unacked_exist) {
NeilBrownde393cd2011-07-28 11:31:48 +10002608 /* metadata handler doesn't understand badblocks,
2609 * so we need to fail the device
2610 */
2611 md_error(rdev->mddev, rdev);
2612 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002613 clear_bit(Blocked, &rdev->flags);
NeilBrownde393cd2011-07-28 11:31:48 +10002614 clear_bit(BlockedBadBlocks, &rdev->flags);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002615 wake_up(&rdev->blocked_wait);
2616 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2617 md_wakeup_thread(rdev->mddev->thread);
2618
2619 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002620 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2621 set_bit(In_sync, &rdev->flags);
2622 err = 0;
NeilBrownd7a9d442011-07-28 11:31:48 +10002623 } else if (cmd_match(buf, "write_error")) {
2624 set_bit(WriteErrorSeen, &rdev->flags);
2625 err = 0;
2626 } else if (cmd_match(buf, "-write_error")) {
2627 clear_bit(WriteErrorSeen, &rdev->flags);
2628 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002629 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10002630 if (!err)
2631 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002632 return err ? err : len;
2633}
NeilBrown80ca3a42006-07-10 04:44:18 -07002634static struct rdev_sysfs_entry rdev_state =
2635__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002636
2637static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002638errors_show(struct md_rdev *rdev, char *page)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002639{
2640 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2641}
2642
2643static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002644errors_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown4dbcdc72006-01-06 00:20:52 -08002645{
2646 char *e;
2647 unsigned long n = simple_strtoul(buf, &e, 10);
2648 if (*buf && (*e == 0 || *e == '\n')) {
2649 atomic_set(&rdev->corrected_errors, n);
2650 return len;
2651 }
2652 return -EINVAL;
2653}
2654static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002655__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002656
NeilBrown014236d2006-01-06 00:20:55 -08002657static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002658slot_show(struct md_rdev *rdev, char *page)
NeilBrown014236d2006-01-06 00:20:55 -08002659{
2660 if (rdev->raid_disk < 0)
2661 return sprintf(page, "none\n");
2662 else
2663 return sprintf(page, "%d\n", rdev->raid_disk);
2664}
2665
2666static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002667slot_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown014236d2006-01-06 00:20:55 -08002668{
2669 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002670 int err;
NeilBrown014236d2006-01-06 00:20:55 -08002671 int slot = simple_strtoul(buf, &e, 10);
2672 if (strncmp(buf, "none", 4)==0)
2673 slot = -1;
2674 else if (e==buf || (*e && *e!= '\n'))
2675 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002676 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002677 /* Setting 'slot' on an active array requires also
2678 * updating the 'rd%d' link, and communicating
2679 * with the personality with ->hot_*_disk.
2680 * For now we only support removing
2681 * failed/spare devices. This normally happens automatically,
2682 * but not when the metadata is externally managed.
2683 */
NeilBrownc303da62008-02-06 01:39:51 -08002684 if (rdev->raid_disk == -1)
2685 return -EEXIST;
2686 /* personality does all needed checks */
Namhyung Kim01393f32011-06-09 11:42:54 +10002687 if (rdev->mddev->pers->hot_remove_disk == NULL)
NeilBrownc303da62008-02-06 01:39:51 -08002688 return -EINVAL;
2689 err = rdev->mddev->pers->
2690 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2691 if (err)
2692 return err;
Namhyung Kim36fad852011-07-27 11:00:36 +10002693 sysfs_unlink_rdev(rdev->mddev, rdev);
Maciej Trelab7103102010-04-14 16:58:16 +10002694 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002695 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2696 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002697 } else if (rdev->mddev->pers) {
NeilBrown3cb03002011-10-11 16:45:26 +11002698 struct md_rdev *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002699 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002700 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002701 */
2702
2703 if (rdev->raid_disk != -1)
2704 return -EBUSY;
2705
NeilBrownc6751b22011-02-02 11:57:13 +11002706 if (test_bit(MD_RECOVERY_RUNNING, &rdev->mddev->recovery))
2707 return -EBUSY;
2708
Neil Brown6c2fce22008-06-28 08:31:31 +10002709 if (rdev->mddev->pers->hot_add_disk == NULL)
2710 return -EINVAL;
2711
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002712 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002713 if (rdev2->raid_disk == slot)
2714 return -EEXIST;
2715
NeilBrownba1b41b2011-01-14 09:14:34 +11002716 if (slot >= rdev->mddev->raid_disks &&
2717 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
2718 return -ENOSPC;
2719
Neil Brown6c2fce22008-06-28 08:31:31 +10002720 rdev->raid_disk = slot;
2721 if (test_bit(In_sync, &rdev->flags))
2722 rdev->saved_raid_disk = slot;
2723 else
2724 rdev->saved_raid_disk = -1;
2725 err = rdev->mddev->pers->
2726 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002727 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002728 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002729 return err;
Neil Brown52664732008-06-28 08:31:44 +10002730 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002731 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002732 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002733 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002734 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002735 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002736 if (slot >= rdev->mddev->raid_disks &&
2737 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002738 return -ENOSPC;
2739 rdev->raid_disk = slot;
2740 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002741 clear_bit(Faulty, &rdev->flags);
2742 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002743 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002744 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002745 }
NeilBrown014236d2006-01-06 00:20:55 -08002746 return len;
2747}
2748
2749
2750static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002751__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002752
NeilBrown93c8cad2006-01-06 00:20:56 -08002753static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002754offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002755{
Andrew Morton6961ece2006-01-06 00:20:59 -08002756 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002757}
2758
2759static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002760offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002761{
2762 char *e;
2763 unsigned long long offset = simple_strtoull(buf, &e, 10);
2764 if (e==buf || (*e && *e != '\n'))
2765 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002766 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002767 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002768 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002769 /* Must set offset before size, so overlap checks
2770 * can be sane */
2771 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002772 rdev->data_offset = offset;
2773 return len;
2774}
2775
2776static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002777__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002778
NeilBrown83303b62006-01-06 00:21:06 -08002779static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002780rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002781{
Andre Nolldd8ac332009-03-31 14:33:13 +11002782 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002783}
2784
NeilBrownc5d79ad2008-02-06 01:39:54 -08002785static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2786{
2787 /* check if two start/length pairs overlap */
2788 if (s1+l1 <= s2)
2789 return 0;
2790 if (s2+l2 <= s1)
2791 return 0;
2792 return 1;
2793}
2794
Dan Williamsb522adc2009-03-31 15:00:31 +11002795static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2796{
2797 unsigned long long blocks;
2798 sector_t new;
2799
2800 if (strict_strtoull(buf, 10, &blocks) < 0)
2801 return -EINVAL;
2802
2803 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2804 return -EINVAL; /* sector conversion overflow */
2805
2806 new = blocks * 2;
2807 if (new != blocks * 2)
2808 return -EINVAL; /* unsigned long long to sector_t overflow */
2809
2810 *sectors = new;
2811 return 0;
2812}
2813
NeilBrown83303b62006-01-06 00:21:06 -08002814static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002815rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002816{
NeilBrownfd01b882011-10-11 16:47:53 +11002817 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002818 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002819 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002820
Dan Williamsb522adc2009-03-31 15:00:31 +11002821 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002822 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002823 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002824 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002825 sectors = super_types[my_mddev->major_version].
2826 rdev_size_change(rdev, sectors);
2827 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002828 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002829 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002830 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002831 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002832 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002833 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002834 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002835
Andre Nolldd8ac332009-03-31 14:33:13 +11002836 rdev->sectors = sectors;
2837 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002838 /* need to check that all other rdevs with the same ->bdev
2839 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002840 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002841 * we have to change it back, we will have the lock again.
2842 */
NeilBrownfd01b882011-10-11 16:47:53 +11002843 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002844 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002845 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002846
NeilBrown27c529b2008-03-04 14:29:33 -08002847 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002848 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002849 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002850
2851 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002852 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002853 if (rdev->bdev == rdev2->bdev &&
2854 rdev != rdev2 &&
2855 overlaps(rdev->data_offset, rdev->sectors,
2856 rdev2->data_offset,
2857 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002858 overlap = 1;
2859 break;
2860 }
2861 mddev_unlock(mddev);
2862 if (overlap) {
2863 mddev_put(mddev);
2864 break;
2865 }
2866 }
NeilBrown27c529b2008-03-04 14:29:33 -08002867 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002868 if (overlap) {
2869 /* Someone else could have slipped in a size
2870 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002871 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002872 * safe, and trust userspace not to race with
2873 * itself
2874 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002875 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002876 return -EBUSY;
2877 }
2878 }
NeilBrown83303b62006-01-06 00:21:06 -08002879 return len;
2880}
2881
2882static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002883__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002884
Dan Williams06e3c812009-12-12 21:17:12 -07002885
NeilBrown3cb03002011-10-11 16:45:26 +11002886static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07002887{
2888 unsigned long long recovery_start = rdev->recovery_offset;
2889
2890 if (test_bit(In_sync, &rdev->flags) ||
2891 recovery_start == MaxSector)
2892 return sprintf(page, "none\n");
2893
2894 return sprintf(page, "%llu\n", recovery_start);
2895}
2896
NeilBrown3cb03002011-10-11 16:45:26 +11002897static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07002898{
2899 unsigned long long recovery_start;
2900
2901 if (cmd_match(buf, "none"))
2902 recovery_start = MaxSector;
2903 else if (strict_strtoull(buf, 10, &recovery_start))
2904 return -EINVAL;
2905
2906 if (rdev->mddev->pers &&
2907 rdev->raid_disk >= 0)
2908 return -EBUSY;
2909
2910 rdev->recovery_offset = recovery_start;
2911 if (recovery_start == MaxSector)
2912 set_bit(In_sync, &rdev->flags);
2913 else
2914 clear_bit(In_sync, &rdev->flags);
2915 return len;
2916}
2917
2918static struct rdev_sysfs_entry rdev_recovery_start =
2919__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2920
NeilBrown16c791a2011-07-28 11:31:47 +10002921
2922static ssize_t
2923badblocks_show(struct badblocks *bb, char *page, int unack);
2924static ssize_t
2925badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
2926
NeilBrown3cb03002011-10-11 16:45:26 +11002927static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002928{
2929 return badblocks_show(&rdev->badblocks, page, 0);
2930}
NeilBrown3cb03002011-10-11 16:45:26 +11002931static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002932{
NeilBrownde393cd2011-07-28 11:31:48 +10002933 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
2934 /* Maybe that ack was all we needed */
2935 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
2936 wake_up(&rdev->blocked_wait);
2937 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10002938}
2939static struct rdev_sysfs_entry rdev_bad_blocks =
2940__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
2941
2942
NeilBrown3cb03002011-10-11 16:45:26 +11002943static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002944{
2945 return badblocks_show(&rdev->badblocks, page, 1);
2946}
NeilBrown3cb03002011-10-11 16:45:26 +11002947static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002948{
2949 return badblocks_store(&rdev->badblocks, page, len, 1);
2950}
2951static struct rdev_sysfs_entry rdev_unack_bad_blocks =
2952__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
2953
NeilBrown86e6ffd2005-11-08 21:39:24 -08002954static struct attribute *rdev_default_attrs[] = {
2955 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002956 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002957 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002958 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002959 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002960 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10002961 &rdev_bad_blocks.attr,
2962 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002963 NULL,
2964};
2965static ssize_t
2966rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2967{
2968 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11002969 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11002970 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08002971 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002972
2973 if (!entry->show)
2974 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002975
2976 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2977 if (!rv) {
2978 if (rdev->mddev == NULL)
2979 rv = -EBUSY;
2980 else
2981 rv = entry->show(rdev, page);
2982 mddev_unlock(mddev);
2983 }
2984 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002985}
2986
2987static ssize_t
2988rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2989 const char *page, size_t length)
2990{
2991 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11002992 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002993 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11002994 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002995
2996 if (!entry->store)
2997 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002998 if (!capable(CAP_SYS_ADMIN))
2999 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003000 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003001 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003002 if (rdev->mddev == NULL)
3003 rv = -EBUSY;
3004 else
3005 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003006 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003007 }
3008 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003009}
3010
3011static void rdev_free(struct kobject *ko)
3012{
NeilBrown3cb03002011-10-11 16:45:26 +11003013 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003014 kfree(rdev);
3015}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003016static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003017 .show = rdev_attr_show,
3018 .store = rdev_attr_store,
3019};
3020static struct kobj_type rdev_ktype = {
3021 .release = rdev_free,
3022 .sysfs_ops = &rdev_sysfs_ops,
3023 .default_attrs = rdev_default_attrs,
3024};
3025
NeilBrown3cb03002011-10-11 16:45:26 +11003026int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003027{
3028 rdev->desc_nr = -1;
3029 rdev->saved_raid_disk = -1;
3030 rdev->raid_disk = -1;
3031 rdev->flags = 0;
3032 rdev->data_offset = 0;
3033 rdev->sb_events = 0;
3034 rdev->last_read_error.tv_sec = 0;
3035 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003036 rdev->sb_loaded = 0;
3037 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003038 atomic_set(&rdev->nr_pending, 0);
3039 atomic_set(&rdev->read_errors, 0);
3040 atomic_set(&rdev->corrected_errors, 0);
3041
3042 INIT_LIST_HEAD(&rdev->same_set);
3043 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003044
3045 /* Add space to store bad block list.
3046 * This reserves the space even on arrays where it cannot
3047 * be used - I wonder if that matters
3048 */
3049 rdev->badblocks.count = 0;
3050 rdev->badblocks.shift = 0;
3051 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3052 seqlock_init(&rdev->badblocks.lock);
3053 if (rdev->badblocks.page == NULL)
3054 return -ENOMEM;
3055
3056 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003057}
3058EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059/*
3060 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3061 *
3062 * mark the device faulty if:
3063 *
3064 * - the device is nonexistent (zero size)
3065 * - the device has no valid superblock
3066 *
3067 * a faulty rdev _never_ has rdev->sb set.
3068 */
NeilBrown3cb03002011-10-11 16:45:26 +11003069static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070{
3071 char b[BDEVNAME_SIZE];
3072 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003073 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 sector_t size;
3075
NeilBrown9ffae0c2006-01-06 00:20:32 -08003076 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 if (!rdev) {
3078 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3079 return ERR_PTR(-ENOMEM);
3080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
NeilBrown2230dfe2011-07-28 11:31:46 +10003082 err = md_rdev_init(rdev);
3083 if (err)
3084 goto abort_free;
3085 err = alloc_disk_sb(rdev);
3086 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 goto abort_free;
3088
NeilBrownc5d79ad2008-02-06 01:39:54 -08003089 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 if (err)
3091 goto abort_free;
3092
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003093 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003094
Mike Snitzer77304d22010-11-08 14:39:12 +01003095 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 if (!size) {
3097 printk(KERN_WARNING
3098 "md: %s has zero or unknown size, marking faulty!\n",
3099 bdevname(rdev->bdev,b));
3100 err = -EINVAL;
3101 goto abort_free;
3102 }
3103
3104 if (super_format >= 0) {
3105 err = super_types[super_format].
3106 load_super(rdev, NULL, super_minor);
3107 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003108 printk(KERN_WARNING
3109 "md: %s does not have a valid v%d.%d "
3110 "superblock, not importing!\n",
3111 bdevname(rdev->bdev,b),
3112 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 goto abort_free;
3114 }
3115 if (err < 0) {
3116 printk(KERN_WARNING
3117 "md: could not read %s's sb, not importing!\n",
3118 bdevname(rdev->bdev,b));
3119 goto abort_free;
3120 }
3121 }
NeilBrown9f2f383072011-07-28 11:31:47 +10003122 if (super_format == -1)
3123 /* hot-add for 0.90, or non-persistent: so no badblocks */
3124 rdev->badblocks.shift = -1;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003125
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 return rdev;
3127
3128abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003129 if (rdev->bdev)
3130 unlock_rdev(rdev);
3131 free_disk_sb(rdev);
NeilBrown2230dfe2011-07-28 11:31:46 +10003132 kfree(rdev->badblocks.page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 kfree(rdev);
3134 return ERR_PTR(err);
3135}
3136
3137/*
3138 * Check a full RAID array for plausibility
3139 */
3140
3141
NeilBrownfd01b882011-10-11 16:47:53 +11003142static void analyze_sbs(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
3144 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003145 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 char b[BDEVNAME_SIZE];
3147
3148 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08003149 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 switch (super_types[mddev->major_version].
3151 load_super(rdev, freshest, mddev->minor_version)) {
3152 case 1:
3153 freshest = rdev;
3154 break;
3155 case 0:
3156 break;
3157 default:
3158 printk( KERN_ERR \
3159 "md: fatal superblock inconsistency in %s"
3160 " -- removing from array\n",
3161 bdevname(rdev->bdev,b));
3162 kick_rdev_from_array(rdev);
3163 }
3164
3165
3166 super_types[mddev->major_version].
3167 validate_super(mddev, freshest);
3168
3169 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003170 rdev_for_each(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003171 if (mddev->max_disks &&
3172 (rdev->desc_nr >= mddev->max_disks ||
3173 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003174 printk(KERN_WARNING
3175 "md: %s: %s: only %d devices permitted\n",
3176 mdname(mddev), bdevname(rdev->bdev, b),
3177 mddev->max_disks);
3178 kick_rdev_from_array(rdev);
3179 continue;
3180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 if (rdev != freshest)
3182 if (super_types[mddev->major_version].
3183 validate_super(mddev, rdev)) {
3184 printk(KERN_WARNING "md: kicking non-fresh %s"
3185 " from array!\n",
3186 bdevname(rdev->bdev,b));
3187 kick_rdev_from_array(rdev);
3188 continue;
3189 }
3190 if (mddev->level == LEVEL_MULTIPATH) {
3191 rdev->desc_nr = i++;
3192 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003193 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003194 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003195 rdev->raid_disk = -1;
3196 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 }
3198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199}
3200
NeilBrown72e02072009-12-14 12:49:55 +11003201/* Read a fixed-point number.
3202 * Numbers in sysfs attributes should be in "standard" units where
3203 * possible, so time should be in seconds.
3204 * However we internally use a a much smaller unit such as
3205 * milliseconds or jiffies.
3206 * This function takes a decimal number with a possible fractional
3207 * component, and produces an integer which is the result of
3208 * multiplying that number by 10^'scale'.
3209 * all without any floating-point arithmetic.
3210 */
3211int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3212{
3213 unsigned long result = 0;
3214 long decimals = -1;
3215 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3216 if (*cp == '.')
3217 decimals = 0;
3218 else if (decimals < scale) {
3219 unsigned int value;
3220 value = *cp - '0';
3221 result = result * 10 + value;
3222 if (decimals >= 0)
3223 decimals++;
3224 }
3225 cp++;
3226 }
3227 if (*cp == '\n')
3228 cp++;
3229 if (*cp)
3230 return -EINVAL;
3231 if (decimals < 0)
3232 decimals = 0;
3233 while (decimals < scale) {
3234 result *= 10;
3235 decimals ++;
3236 }
3237 *res = result;
3238 return 0;
3239}
3240
3241
NeilBrown19052c02008-08-05 15:54:13 +10003242static void md_safemode_timeout(unsigned long data);
3243
NeilBrowneae17012005-11-08 21:39:23 -08003244static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003245safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003246{
3247 int msec = (mddev->safemode_delay*1000)/HZ;
3248 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3249}
3250static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003251safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003252{
NeilBrown16f17b32006-06-26 00:27:37 -07003253 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003254
NeilBrown72e02072009-12-14 12:49:55 +11003255 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003256 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003257 if (msec == 0)
3258 mddev->safemode_delay = 0;
3259 else {
NeilBrown19052c02008-08-05 15:54:13 +10003260 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003261 mddev->safemode_delay = (msec*HZ)/1000;
3262 if (mddev->safemode_delay == 0)
3263 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10003264 if (mddev->safemode_delay < old_delay)
3265 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003266 }
3267 return len;
3268}
3269static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003270__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003271
3272static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003273level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003274{
NeilBrown2604b702006-01-06 00:20:36 -08003275 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003276 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003277 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003278 else if (mddev->clevel[0])
3279 return sprintf(page, "%s\n", mddev->clevel);
3280 else if (mddev->level != LEVEL_NONE)
3281 return sprintf(page, "%d\n", mddev->level);
3282 else
3283 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003284}
3285
NeilBrownd9d166c2006-01-06 00:20:51 -08003286static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003287level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003288{
Dan Williamsf2859af2010-05-02 10:04:16 -07003289 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003290 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11003291 struct mdk_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003292 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003293 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003294 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003295
3296 if (mddev->pers == NULL) {
3297 if (len == 0)
3298 return 0;
3299 if (len >= sizeof(mddev->clevel))
3300 return -ENOSPC;
3301 strncpy(mddev->clevel, buf, len);
3302 if (mddev->clevel[len-1] == '\n')
3303 len--;
3304 mddev->clevel[len] = 0;
3305 mddev->level = LEVEL_NONE;
3306 return rv;
3307 }
3308
3309 /* request to change the personality. Need to ensure:
3310 * - array is not engaged in resync/recovery/reshape
3311 * - old personality can be suspended
3312 * - new personality will access other array.
3313 */
3314
NeilBrownbb4f1e92010-08-08 21:18:03 +10003315 if (mddev->sync_thread ||
3316 mddev->reshape_position != MaxSector ||
3317 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003318 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003319
3320 if (!mddev->pers->quiesce) {
3321 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3322 mdname(mddev), mddev->pers->name);
3323 return -EINVAL;
3324 }
3325
3326 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003327 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003328 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003329 strncpy(clevel, buf, len);
3330 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003331 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003332 clevel[len] = 0;
3333 if (strict_strtol(clevel, 10, &level))
3334 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003335
Dan Williamsf2859af2010-05-02 10:04:16 -07003336 if (request_module("md-%s", clevel) != 0)
3337 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003338 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003339 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003340 if (!pers || !try_module_get(pers->owner)) {
3341 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003342 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003343 return -EINVAL;
3344 }
3345 spin_unlock(&pers_lock);
3346
3347 if (pers == mddev->pers) {
3348 /* Nothing to do! */
3349 module_put(pers->owner);
3350 return rv;
3351 }
3352 if (!pers->takeover) {
3353 module_put(pers->owner);
3354 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003355 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003356 return -EINVAL;
3357 }
3358
NeilBrowne93f68a2010-06-15 09:36:03 +01003359 list_for_each_entry(rdev, &mddev->disks, same_set)
3360 rdev->new_raid_disk = rdev->raid_disk;
3361
NeilBrown245f46c2009-03-31 14:39:39 +11003362 /* ->takeover must set new_* and/or delta_disks
3363 * if it succeeds, and may set them when it fails.
3364 */
3365 priv = pers->takeover(mddev);
3366 if (IS_ERR(priv)) {
3367 mddev->new_level = mddev->level;
3368 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003369 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003370 mddev->raid_disks -= mddev->delta_disks;
3371 mddev->delta_disks = 0;
3372 module_put(pers->owner);
3373 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003374 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003375 return PTR_ERR(priv);
3376 }
3377
3378 /* Looks like we have a winner */
3379 mddev_suspend(mddev);
3380 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003381
3382 if (mddev->pers->sync_request == NULL &&
3383 pers->sync_request != NULL) {
3384 /* need to add the md_redundancy_group */
3385 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3386 printk(KERN_WARNING
3387 "md: cannot register extra attributes for %s\n",
3388 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003389 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003390 }
3391 if (mddev->pers->sync_request != NULL &&
3392 pers->sync_request == NULL) {
3393 /* need to remove the md_redundancy_group */
3394 if (mddev->to_remove == NULL)
3395 mddev->to_remove = &md_redundancy_group;
3396 }
3397
Trela Maciej54071b32010-03-08 16:02:42 +11003398 if (mddev->pers->sync_request == NULL &&
3399 mddev->external) {
3400 /* We are converting from a no-redundancy array
3401 * to a redundancy array and metadata is managed
3402 * externally so we need to be sure that writes
3403 * won't block due to a need to transition
3404 * clean->dirty
3405 * until external management is started.
3406 */
3407 mddev->in_sync = 0;
3408 mddev->safemode_delay = 0;
3409 mddev->safemode = 0;
3410 }
3411
NeilBrowne93f68a2010-06-15 09:36:03 +01003412 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003413 if (rdev->raid_disk < 0)
3414 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003415 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003416 rdev->new_raid_disk = -1;
3417 if (rdev->new_raid_disk == rdev->raid_disk)
3418 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003419 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003420 }
3421 list_for_each_entry(rdev, &mddev->disks, same_set) {
3422 if (rdev->raid_disk < 0)
3423 continue;
3424 if (rdev->new_raid_disk == rdev->raid_disk)
3425 continue;
3426 rdev->raid_disk = rdev->new_raid_disk;
3427 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003428 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003429 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003430 if (sysfs_link_rdev(mddev, rdev))
3431 printk(KERN_WARNING "md: cannot register rd%d"
3432 " for %s after level change\n",
3433 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003434 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003435 }
3436
3437 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003438 mddev->pers = pers;
3439 mddev->private = priv;
3440 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3441 mddev->level = mddev->new_level;
3442 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003443 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003444 mddev->delta_disks = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003445 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003446 if (mddev->pers->sync_request == NULL) {
3447 /* this is now an array without redundancy, so
3448 * it must always be in_sync
3449 */
3450 mddev->in_sync = 1;
3451 del_timer_sync(&mddev->safemode_timer);
3452 }
NeilBrown245f46c2009-03-31 14:39:39 +11003453 pers->run(mddev);
3454 mddev_resume(mddev);
3455 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3456 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3457 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003458 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003459 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003460 return rv;
3461}
3462
3463static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003464__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003465
NeilBrownd4dbd022006-06-26 00:27:59 -07003466
3467static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003468layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003469{
3470 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003471 if (mddev->reshape_position != MaxSector &&
3472 mddev->layout != mddev->new_layout)
3473 return sprintf(page, "%d (%d)\n",
3474 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003475 return sprintf(page, "%d\n", mddev->layout);
3476}
3477
3478static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003479layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003480{
3481 char *e;
3482 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003483
3484 if (!*buf || (*e && *e != '\n'))
3485 return -EINVAL;
3486
NeilBrownb3546032009-03-31 14:56:41 +11003487 if (mddev->pers) {
3488 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003489 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003490 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003491 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003492 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003493 if (err) {
3494 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003495 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003496 }
NeilBrownb3546032009-03-31 14:56:41 +11003497 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003498 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003499 if (mddev->reshape_position == MaxSector)
3500 mddev->layout = n;
3501 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003502 return len;
3503}
3504static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003505__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003506
3507
NeilBrowneae17012005-11-08 21:39:23 -08003508static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003509raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003510{
NeilBrownbb636542005-11-08 21:39:45 -08003511 if (mddev->raid_disks == 0)
3512 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003513 if (mddev->reshape_position != MaxSector &&
3514 mddev->delta_disks != 0)
3515 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3516 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003517 return sprintf(page, "%d\n", mddev->raid_disks);
3518}
3519
NeilBrownfd01b882011-10-11 16:47:53 +11003520static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b92006-01-06 00:20:54 -08003521
3522static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003523raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b92006-01-06 00:20:54 -08003524{
NeilBrownda943b92006-01-06 00:20:54 -08003525 char *e;
3526 int rv = 0;
3527 unsigned long n = simple_strtoul(buf, &e, 10);
3528
3529 if (!*buf || (*e && *e != '\n'))
3530 return -EINVAL;
3531
3532 if (mddev->pers)
3533 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003534 else if (mddev->reshape_position != MaxSector) {
3535 int olddisks = mddev->raid_disks - mddev->delta_disks;
3536 mddev->delta_disks = n - olddisks;
3537 mddev->raid_disks = n;
3538 } else
NeilBrownda943b92006-01-06 00:20:54 -08003539 mddev->raid_disks = n;
3540 return rv ? rv : len;
3541}
3542static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003543__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003544
NeilBrown24dd4692005-11-08 21:39:26 -08003545static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003546chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003547{
NeilBrown08a02ec2007-05-09 02:35:38 -07003548 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003549 mddev->chunk_sectors != mddev->new_chunk_sectors)
3550 return sprintf(page, "%d (%d)\n",
3551 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003552 mddev->chunk_sectors << 9);
3553 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003554}
3555
3556static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003557chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003558{
NeilBrown3b343802006-01-06 00:20:47 -08003559 char *e;
3560 unsigned long n = simple_strtoul(buf, &e, 10);
3561
NeilBrown3b343802006-01-06 00:20:47 -08003562 if (!*buf || (*e && *e != '\n'))
3563 return -EINVAL;
3564
NeilBrownb3546032009-03-31 14:56:41 +11003565 if (mddev->pers) {
3566 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003567 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003568 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003569 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003570 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003571 if (err) {
3572 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003573 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003574 }
NeilBrownb3546032009-03-31 14:56:41 +11003575 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003576 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003577 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003578 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003579 }
NeilBrown3b343802006-01-06 00:20:47 -08003580 return len;
3581}
3582static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003583__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003584
NeilBrowna94213b2006-06-26 00:28:00 -07003585static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003586resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003587{
NeilBrownd1a7c502009-03-31 15:24:32 +11003588 if (mddev->recovery_cp == MaxSector)
3589 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003590 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3591}
3592
3593static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003594resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003595{
NeilBrowna94213b2006-06-26 00:28:00 -07003596 char *e;
3597 unsigned long long n = simple_strtoull(buf, &e, 10);
3598
NeilBrownb0986362011-05-11 15:52:21 +10003599 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003600 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003601 if (cmd_match(buf, "none"))
3602 n = MaxSector;
3603 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003604 return -EINVAL;
3605
3606 mddev->recovery_cp = n;
3607 return len;
3608}
3609static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003610__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003611
NeilBrown9e653b62006-06-26 00:27:58 -07003612/*
3613 * The array state can be:
3614 *
3615 * clear
3616 * No devices, no size, no level
3617 * Equivalent to STOP_ARRAY ioctl
3618 * inactive
3619 * May have some settings, but array is not active
3620 * all IO results in error
3621 * When written, doesn't tear down array, but just stops it
3622 * suspended (not supported yet)
3623 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003624 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003625 * readonly
3626 * no resync can happen. no superblocks get written.
3627 * write requests fail
3628 * read-auto
3629 * like readonly, but behaves like 'clean' on a write request.
3630 *
3631 * clean - no pending writes, but otherwise active.
3632 * When written to inactive array, starts without resync
3633 * If a write request arrives then
3634 * if metadata is known, mark 'dirty' and switch to 'active'.
3635 * if not known, block and switch to write-pending
3636 * If written to an active array that has pending writes, then fails.
3637 * active
3638 * fully active: IO and resync can be happening.
3639 * When written to inactive array, starts with resync
3640 *
3641 * write-pending
3642 * clean, but writes are blocked waiting for 'active' to be written.
3643 *
3644 * active-idle
3645 * like active, but no writes have been seen for a while (100msec).
3646 *
3647 */
3648enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3649 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003650static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003651 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3652 "write-pending", "active-idle", NULL };
3653
3654static int match_word(const char *word, char **list)
3655{
3656 int n;
3657 for (n=0; list[n]; n++)
3658 if (cmd_match(word, list[n]))
3659 break;
3660 return n;
3661}
3662
3663static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003664array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003665{
3666 enum array_state st = inactive;
3667
3668 if (mddev->pers)
3669 switch(mddev->ro) {
3670 case 1:
3671 st = readonly;
3672 break;
3673 case 2:
3674 st = read_auto;
3675 break;
3676 case 0:
3677 if (mddev->in_sync)
3678 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003679 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003680 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003681 else if (mddev->safemode)
3682 st = active_idle;
3683 else
3684 st = active;
3685 }
3686 else {
3687 if (list_empty(&mddev->disks) &&
3688 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003689 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003690 st = clear;
3691 else
3692 st = inactive;
3693 }
3694 return sprintf(page, "%s\n", array_states[st]);
3695}
3696
NeilBrownfd01b882011-10-11 16:47:53 +11003697static int do_md_stop(struct mddev * mddev, int ro, int is_open);
3698static int md_set_readonly(struct mddev * mddev, int is_open);
3699static int do_md_run(struct mddev * mddev);
3700static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003701
3702static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003703array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003704{
3705 int err = -EINVAL;
3706 enum array_state st = match_word(buf, array_states);
3707 switch(st) {
3708 case bad_word:
3709 break;
3710 case clear:
3711 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003712 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003713 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003714 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003715 break;
3716 case inactive:
3717 /* stopping an active array */
3718 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003719 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003720 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003721 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003722 } else
3723 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003724 break;
3725 case suspended:
3726 break; /* not supported yet */
3727 case readonly:
3728 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003729 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003730 else {
3731 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003732 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003733 err = do_md_run(mddev);
3734 }
3735 break;
3736 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003737 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003738 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003739 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003740 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003741 err = restart_array(mddev);
3742 if (err == 0) {
3743 mddev->ro = 2;
3744 set_disk_ro(mddev->gendisk, 0);
3745 }
NeilBrown9e653b62006-06-26 00:27:58 -07003746 } else {
3747 mddev->ro = 2;
3748 err = do_md_run(mddev);
3749 }
3750 break;
3751 case clean:
3752 if (mddev->pers) {
3753 restart_array(mddev);
3754 spin_lock_irq(&mddev->write_lock);
3755 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003756 if (mddev->in_sync == 0) {
3757 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003758 if (mddev->safemode == 1)
3759 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003760 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003761 }
3762 err = 0;
3763 } else
3764 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003765 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003766 } else
3767 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003768 break;
3769 case active:
3770 if (mddev->pers) {
3771 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003772 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003773 wake_up(&mddev->sb_wait);
3774 err = 0;
3775 } else {
3776 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003777 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003778 err = do_md_run(mddev);
3779 }
3780 break;
3781 case write_pending:
3782 case active_idle:
3783 /* these cannot be set */
3784 break;
3785 }
3786 if (err)
3787 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003788 else {
NeilBrown00bcb4a2010-06-01 19:37:23 +10003789 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003790 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003791 }
NeilBrown9e653b62006-06-26 00:27:58 -07003792}
NeilBrown80ca3a42006-07-10 04:44:18 -07003793static struct md_sysfs_entry md_array_state =
3794__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003795
NeilBrown6d7ff732006-01-06 00:21:16 -08003796static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003797max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003798 return sprintf(page, "%d\n",
3799 atomic_read(&mddev->max_corr_read_errors));
3800}
3801
3802static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003803max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003804{
3805 char *e;
3806 unsigned long n = simple_strtoul(buf, &e, 10);
3807
3808 if (*buf && (*e == 0 || *e == '\n')) {
3809 atomic_set(&mddev->max_corr_read_errors, n);
3810 return len;
3811 }
3812 return -EINVAL;
3813}
3814
3815static struct md_sysfs_entry max_corr_read_errors =
3816__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3817 max_corrected_read_errors_store);
3818
3819static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003820null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff732006-01-06 00:21:16 -08003821{
3822 return -EINVAL;
3823}
3824
3825static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003826new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff732006-01-06 00:21:16 -08003827{
3828 /* buf must be %d:%d\n? giving major and minor numbers */
3829 /* The new device is added to the array.
3830 * If the array has a persistent superblock, we read the
3831 * superblock to initialise info and check validity.
3832 * Otherwise, only checking done is that in bind_rdev_to_array,
3833 * which mainly checks size.
3834 */
3835 char *e;
3836 int major = simple_strtoul(buf, &e, 10);
3837 int minor;
3838 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003839 struct md_rdev *rdev;
NeilBrown6d7ff732006-01-06 00:21:16 -08003840 int err;
3841
3842 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3843 return -EINVAL;
3844 minor = simple_strtoul(e+1, &e, 10);
3845 if (*e && *e != '\n')
3846 return -EINVAL;
3847 dev = MKDEV(major, minor);
3848 if (major != MAJOR(dev) ||
3849 minor != MINOR(dev))
3850 return -EOVERFLOW;
3851
3852
3853 if (mddev->persistent) {
3854 rdev = md_import_device(dev, mddev->major_version,
3855 mddev->minor_version);
3856 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003857 struct md_rdev *rdev0
3858 = list_entry(mddev->disks.next,
3859 struct md_rdev, same_set);
NeilBrown6d7ff732006-01-06 00:21:16 -08003860 err = super_types[mddev->major_version]
3861 .load_super(rdev, rdev0, mddev->minor_version);
3862 if (err < 0)
3863 goto out;
3864 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003865 } else if (mddev->external)
3866 rdev = md_import_device(dev, -2, -1);
3867 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003868 rdev = md_import_device(dev, -1, -1);
3869
3870 if (IS_ERR(rdev))
3871 return PTR_ERR(rdev);
3872 err = bind_rdev_to_array(rdev, mddev);
3873 out:
3874 if (err)
3875 export_rdev(rdev);
3876 return err ? err : len;
3877}
3878
3879static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003880__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003881
3882static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003883bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07003884{
3885 char *end;
3886 unsigned long chunk, end_chunk;
3887
3888 if (!mddev->bitmap)
3889 goto out;
3890 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3891 while (*buf) {
3892 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3893 if (buf == end) break;
3894 if (*end == '-') { /* range */
3895 buf = end + 1;
3896 end_chunk = simple_strtoul(buf, &end, 0);
3897 if (buf == end) break;
3898 }
3899 if (*end && !isspace(*end)) break;
3900 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003901 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003902 }
3903 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3904out:
3905 return len;
3906}
3907
3908static struct md_sysfs_entry md_bitmap =
3909__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3910
3911static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003912size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08003913{
Andre Noll58c0fed2009-03-31 14:33:13 +11003914 return sprintf(page, "%llu\n",
3915 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003916}
3917
NeilBrownfd01b882011-10-11 16:47:53 +11003918static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003919
3920static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003921size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08003922{
3923 /* If array is inactive, we can reduce the component size, but
3924 * not increase it (except from 0).
3925 * If array is active, we can try an on-line resize
3926 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003927 sector_t sectors;
3928 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003929
Andre Noll58c0fed2009-03-31 14:33:13 +11003930 if (err < 0)
3931 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003932 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003933 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003934 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003935 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003936 if (mddev->dev_sectors == 0 ||
3937 mddev->dev_sectors > sectors)
3938 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003939 else
3940 err = -ENOSPC;
3941 }
3942 return err ? err : len;
3943}
3944
3945static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003946__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003947
NeilBrown8bb93aa2006-01-06 00:20:50 -08003948
3949/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003950 * This is one of
3951 * 'none' for arrays with no metadata (good luck...)
3952 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003953 * or N.M for internally known formats
3954 */
3955static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003956metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003957{
3958 if (mddev->persistent)
3959 return sprintf(page, "%d.%d\n",
3960 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003961 else if (mddev->external)
3962 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003963 else
3964 return sprintf(page, "none\n");
3965}
3966
3967static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003968metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003969{
3970 int major, minor;
3971 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003972 /* Changing the details of 'external' metadata is
3973 * always permitted. Otherwise there must be
3974 * no devices attached to the array.
3975 */
3976 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3977 ;
3978 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003979 return -EBUSY;
3980
3981 if (cmd_match(buf, "none")) {
3982 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003983 mddev->external = 0;
3984 mddev->major_version = 0;
3985 mddev->minor_version = 90;
3986 return len;
3987 }
3988 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003989 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003990 if (namelen >= sizeof(mddev->metadata_type))
3991 namelen = sizeof(mddev->metadata_type)-1;
3992 strncpy(mddev->metadata_type, buf+9, namelen);
3993 mddev->metadata_type[namelen] = 0;
3994 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3995 mddev->metadata_type[--namelen] = 0;
3996 mddev->persistent = 0;
3997 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003998 mddev->major_version = 0;
3999 mddev->minor_version = 90;
4000 return len;
4001 }
4002 major = simple_strtoul(buf, &e, 10);
4003 if (e==buf || *e != '.')
4004 return -EINVAL;
4005 buf = e+1;
4006 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004007 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004008 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004009 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004010 return -ENOENT;
4011 mddev->major_version = major;
4012 mddev->minor_version = minor;
4013 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004014 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004015 return len;
4016}
4017
4018static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004019__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004020
NeilBrowna35b0d62006-01-06 00:20:49 -08004021static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004022action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004023{
NeilBrown7eec3142005-11-08 21:39:44 -08004024 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004025 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4026 type = "frozen";
4027 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004028 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004029 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4030 type = "reshape";
4031 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004032 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4033 type = "resync";
4034 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4035 type = "check";
4036 else
4037 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004038 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004039 type = "recover";
4040 }
4041 return sprintf(page, "%s\n", type);
4042}
4043
NeilBrownfd01b882011-10-11 16:47:53 +11004044static void reap_sync_thread(struct mddev *mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004045
NeilBrown24dd4692005-11-08 21:39:26 -08004046static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004047action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004048{
NeilBrown7eec3142005-11-08 21:39:44 -08004049 if (!mddev->pers || !mddev->pers->sync_request)
4050 return -EINVAL;
4051
NeilBrownb6a9ce62009-05-26 09:41:17 +10004052 if (cmd_match(page, "frozen"))
4053 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4054 else
4055 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4056
4057 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004058 if (mddev->sync_thread) {
4059 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004060 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004061 }
NeilBrown03c902e2006-01-06 00:20:46 -08004062 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4063 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004064 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004065 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004066 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004067 else if (cmd_match(page, "recover")) {
4068 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4069 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4070 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004071 int err;
4072 if (mddev->pers->start_reshape == NULL)
4073 return -EINVAL;
4074 err = mddev->pers->start_reshape(mddev);
4075 if (err)
4076 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004077 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004078 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004079 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004080 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004081 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004082 return -EINVAL;
4083 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4084 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004085 }
NeilBrown03c902e2006-01-06 00:20:46 -08004086 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004087 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004088 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004089 return len;
4090}
4091
NeilBrown9d888832005-11-08 21:39:26 -08004092static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004093mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004094{
4095 return sprintf(page, "%llu\n",
4096 (unsigned long long) mddev->resync_mismatches);
4097}
4098
NeilBrown80ca3a42006-07-10 04:44:18 -07004099static struct md_sysfs_entry md_scan_mode =
4100__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08004101
NeilBrown96de1e62005-11-08 21:39:39 -08004102
NeilBrown80ca3a42006-07-10 04:44:18 -07004103static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004104
NeilBrown88202a02006-01-06 00:21:36 -08004105static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004106sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004107{
4108 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4109 mddev->sync_speed_min ? "local": "system");
4110}
4111
4112static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004113sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004114{
4115 int min;
4116 char *e;
4117 if (strncmp(buf, "system", 6)==0) {
4118 mddev->sync_speed_min = 0;
4119 return len;
4120 }
4121 min = simple_strtoul(buf, &e, 10);
4122 if (buf == e || (*e && *e != '\n') || min <= 0)
4123 return -EINVAL;
4124 mddev->sync_speed_min = min;
4125 return len;
4126}
4127
4128static struct md_sysfs_entry md_sync_min =
4129__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4130
4131static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004132sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004133{
4134 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4135 mddev->sync_speed_max ? "local": "system");
4136}
4137
4138static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004139sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004140{
4141 int max;
4142 char *e;
4143 if (strncmp(buf, "system", 6)==0) {
4144 mddev->sync_speed_max = 0;
4145 return len;
4146 }
4147 max = simple_strtoul(buf, &e, 10);
4148 if (buf == e || (*e && *e != '\n') || max <= 0)
4149 return -EINVAL;
4150 mddev->sync_speed_max = max;
4151 return len;
4152}
4153
4154static struct md_sysfs_entry md_sync_max =
4155__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4156
Iustin Popd7f3d292007-10-16 23:30:54 -07004157static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004158degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004159{
4160 return sprintf(page, "%d\n", mddev->degraded);
4161}
4162static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004163
4164static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004165sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004166{
4167 return sprintf(page, "%d\n", mddev->parallel_resync);
4168}
4169
4170static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004171sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004172{
4173 long n;
4174
4175 if (strict_strtol(buf, 10, &n))
4176 return -EINVAL;
4177
4178 if (n != 0 && n != 1)
4179 return -EINVAL;
4180
4181 mddev->parallel_resync = n;
4182
4183 if (mddev->sync_thread)
4184 wake_up(&resync_wait);
4185
4186 return len;
4187}
4188
4189/* force parallel resync, even with shared block devices */
4190static struct md_sysfs_entry md_sync_force_parallel =
4191__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4192 sync_force_parallel_show, sync_force_parallel_store);
4193
4194static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004195sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004196{
4197 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004198 if (mddev->curr_resync == 0)
4199 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004200 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4201 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004202 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004203 db = resync - mddev->resync_mark_cnt;
4204 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004205}
4206
NeilBrown80ca3a42006-07-10 04:44:18 -07004207static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004208
4209static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004210sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004211{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004212 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004213
NeilBrownacb180b2009-04-14 16:28:34 +10004214 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4215 return sprintf(page, "none\n");
4216
NeilBrown88202a02006-01-06 00:21:36 -08004217 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004218 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004219 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004220 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004221
NeilBrownacb180b2009-04-14 16:28:34 +10004222 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004223 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004224}
4225
NeilBrown80ca3a42006-07-10 04:44:18 -07004226static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004227
NeilBrowne464eaf2006-03-27 01:18:14 -08004228static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004229min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004230{
4231 return sprintf(page, "%llu\n",
4232 (unsigned long long)mddev->resync_min);
4233}
4234static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004235min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004236{
4237 unsigned long long min;
4238 if (strict_strtoull(buf, 10, &min))
4239 return -EINVAL;
4240 if (min > mddev->resync_max)
4241 return -EINVAL;
4242 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4243 return -EBUSY;
4244
4245 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004246 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004247 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004248 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004249 return -EINVAL;
4250 }
4251 mddev->resync_min = min;
4252
4253 return len;
4254}
4255
4256static struct md_sysfs_entry md_min_sync =
4257__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4258
4259static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004260max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004261{
4262 if (mddev->resync_max == MaxSector)
4263 return sprintf(page, "max\n");
4264 else
4265 return sprintf(page, "%llu\n",
4266 (unsigned long long)mddev->resync_max);
4267}
4268static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004269max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004270{
4271 if (strncmp(buf, "max", 3) == 0)
4272 mddev->resync_max = MaxSector;
4273 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004274 unsigned long long max;
4275 if (strict_strtoull(buf, 10, &max))
4276 return -EINVAL;
4277 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004278 return -EINVAL;
4279 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004280 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004281 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4282 return -EBUSY;
4283
4284 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004285 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004286 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004287 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004288 return -EINVAL;
4289 }
4290 mddev->resync_max = max;
4291 }
4292 wake_up(&mddev->recovery_wait);
4293 return len;
4294}
4295
4296static struct md_sysfs_entry md_max_sync =
4297__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4298
4299static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004300suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004301{
4302 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4303}
4304
4305static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004306suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004307{
4308 char *e;
4309 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004310 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004311
NeilBrownb8d966e2009-07-01 11:14:04 +10004312 if (mddev->pers == NULL ||
4313 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004314 return -EINVAL;
4315 if (buf == e || (*e && *e != '\n'))
4316 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004317
4318 mddev->suspend_lo = new;
4319 if (new >= old)
4320 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004321 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004322 else {
4323 /* Expanding suspended region - need to wait */
4324 mddev->pers->quiesce(mddev, 1);
4325 mddev->pers->quiesce(mddev, 0);
4326 }
4327 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004328}
4329static struct md_sysfs_entry md_suspend_lo =
4330__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4331
4332
4333static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004334suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004335{
4336 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4337}
4338
4339static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004340suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004341{
4342 char *e;
4343 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004344 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004345
NeilBrownb8d966e2009-07-01 11:14:04 +10004346 if (mddev->pers == NULL ||
4347 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004348 return -EINVAL;
4349 if (buf == e || (*e && *e != '\n'))
4350 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004351
4352 mddev->suspend_hi = new;
4353 if (new <= old)
4354 /* Shrinking suspended region */
4355 mddev->pers->quiesce(mddev, 2);
4356 else {
4357 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004358 mddev->pers->quiesce(mddev, 1);
4359 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004360 }
4361 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004362}
4363static struct md_sysfs_entry md_suspend_hi =
4364__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4365
NeilBrown08a02ec2007-05-09 02:35:38 -07004366static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004367reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004368{
4369 if (mddev->reshape_position != MaxSector)
4370 return sprintf(page, "%llu\n",
4371 (unsigned long long)mddev->reshape_position);
4372 strcpy(page, "none\n");
4373 return 5;
4374}
4375
4376static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004377reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004378{
4379 char *e;
4380 unsigned long long new = simple_strtoull(buf, &e, 10);
4381 if (mddev->pers)
4382 return -EBUSY;
4383 if (buf == e || (*e && *e != '\n'))
4384 return -EINVAL;
4385 mddev->reshape_position = new;
4386 mddev->delta_disks = 0;
4387 mddev->new_level = mddev->level;
4388 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004389 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004390 return len;
4391}
4392
4393static struct md_sysfs_entry md_reshape_position =
4394__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4395 reshape_position_store);
4396
Dan Williamsb522adc2009-03-31 15:00:31 +11004397static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004398array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004399{
4400 if (mddev->external_size)
4401 return sprintf(page, "%llu\n",
4402 (unsigned long long)mddev->array_sectors/2);
4403 else
4404 return sprintf(page, "default\n");
4405}
4406
4407static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004408array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004409{
4410 sector_t sectors;
4411
4412 if (strncmp(buf, "default", 7) == 0) {
4413 if (mddev->pers)
4414 sectors = mddev->pers->size(mddev, 0, 0);
4415 else
4416 sectors = mddev->array_sectors;
4417
4418 mddev->external_size = 0;
4419 } else {
4420 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4421 return -EINVAL;
4422 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004423 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004424
4425 mddev->external_size = 1;
4426 }
4427
4428 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004429 if (mddev->pers) {
4430 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004431 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004432 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004433 return len;
4434}
4435
4436static struct md_sysfs_entry md_array_size =
4437__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4438 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004439
NeilBrowneae17012005-11-08 21:39:23 -08004440static struct attribute *md_default_attrs[] = {
4441 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004442 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004443 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004444 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004445 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004446 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004447 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004448 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004449 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004450 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004451 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004452 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004453 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004454 NULL,
4455};
4456
4457static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004458 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004459 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004460 &md_sync_min.attr,
4461 &md_sync_max.attr,
4462 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004463 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004464 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004465 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004466 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004467 &md_suspend_lo.attr,
4468 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004469 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004470 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004471 NULL,
4472};
NeilBrown411036f2005-11-08 21:39:40 -08004473static struct attribute_group md_redundancy_group = {
4474 .name = NULL,
4475 .attrs = md_redundancy_attrs,
4476};
4477
NeilBrowneae17012005-11-08 21:39:23 -08004478
4479static ssize_t
4480md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4481{
4482 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004483 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004484 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004485
4486 if (!entry->show)
4487 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004488 rv = mddev_lock(mddev);
4489 if (!rv) {
4490 rv = entry->show(mddev, page);
4491 mddev_unlock(mddev);
4492 }
NeilBrown96de1e62005-11-08 21:39:39 -08004493 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004494}
4495
4496static ssize_t
4497md_attr_store(struct kobject *kobj, struct attribute *attr,
4498 const char *page, size_t length)
4499{
4500 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004501 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004502 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004503
4504 if (!entry->store)
4505 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004506 if (!capable(CAP_SYS_ADMIN))
4507 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004508 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004509 if (mddev->hold_active == UNTIL_IOCTL)
4510 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004511 if (!rv) {
4512 rv = entry->store(mddev, page, length);
4513 mddev_unlock(mddev);
4514 }
NeilBrown96de1e62005-11-08 21:39:39 -08004515 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004516}
4517
4518static void md_free(struct kobject *ko)
4519{
NeilBrownfd01b882011-10-11 16:47:53 +11004520 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004521
4522 if (mddev->sysfs_state)
4523 sysfs_put(mddev->sysfs_state);
4524
4525 if (mddev->gendisk) {
4526 del_gendisk(mddev->gendisk);
4527 put_disk(mddev->gendisk);
4528 }
4529 if (mddev->queue)
4530 blk_cleanup_queue(mddev->queue);
4531
NeilBrowneae17012005-11-08 21:39:23 -08004532 kfree(mddev);
4533}
4534
Emese Revfy52cf25d2010-01-19 02:58:23 +01004535static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004536 .show = md_attr_show,
4537 .store = md_attr_store,
4538};
4539static struct kobj_type md_ktype = {
4540 .release = md_free,
4541 .sysfs_ops = &md_sysfs_ops,
4542 .default_attrs = md_default_attrs,
4543};
4544
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545int mdp_major = 0;
4546
Dan Williams5fd3a172009-03-04 00:57:25 -07004547static void mddev_delayed_delete(struct work_struct *ws)
4548{
NeilBrownfd01b882011-10-11 16:47:53 +11004549 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004550
NeilBrown43a70502009-12-14 12:49:55 +11004551 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004552 kobject_del(&mddev->kobj);
4553 kobject_put(&mddev->kobj);
4554}
4555
NeilBrownefeb53c2009-01-09 08:31:10 +11004556static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004558 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004559 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004561 int partitioned;
4562 int shift;
4563 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004564 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565
4566 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004567 return -ENODEV;
4568
4569 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4570 shift = partitioned ? MdpMinorShift : 0;
4571 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572
Tejun Heoe804ac72010-10-15 15:36:08 +02004573 /* wait for any previous instance of this device to be
4574 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004575 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004576 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004577
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004578 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004579 error = -EEXIST;
4580 if (mddev->gendisk)
4581 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004582
4583 if (name) {
4584 /* Need to ensure that 'name' is not a duplicate.
4585 */
NeilBrownfd01b882011-10-11 16:47:53 +11004586 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004587 spin_lock(&all_mddevs_lock);
4588
4589 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4590 if (mddev2->gendisk &&
4591 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4592 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004593 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004594 }
4595 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 }
NeilBrown8b765392009-01-09 08:31:08 +11004597
NeilBrown0909dc42009-07-01 12:27:21 +10004598 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004599 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004600 if (!mddev->queue)
4601 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004602 mddev->queue->queuedata = mddev;
4603
NeilBrown409c57f2009-03-31 14:39:39 +11004604 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004605
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 disk = alloc_disk(1 << shift);
4607 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004608 blk_cleanup_queue(mddev->queue);
4609 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004610 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004612 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004614 if (name)
4615 strcpy(disk->disk_name, name);
4616 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004618 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 disk->fops = &md_fops;
4621 disk->private_data = mddev;
4622 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004623 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004624 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004625 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004626 * remove it now.
4627 */
4628 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004630 /* As soon as we call add_disk(), another thread could get
4631 * through to md_open, so make sure it doesn't get too far
4632 */
4633 mutex_lock(&mddev->open_mutex);
4634 add_disk(disk);
4635
Tejun Heoed9e1982008-08-25 19:56:05 +09004636 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4637 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004638 if (error) {
4639 /* This isn't possible, but as kobject_init_and_add is marked
4640 * __must_check, we must do something with the result
4641 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004642 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4643 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004644 error = 0;
4645 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004646 if (mddev->kobj.sd &&
4647 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004648 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004649 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004650 abort:
4651 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004652 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004653 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004654 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004655 }
NeilBrownd3374822009-01-09 08:31:10 +11004656 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004657 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004658}
4659
4660static struct kobject *md_probe(dev_t dev, int *part, void *data)
4661{
4662 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 return NULL;
4664}
4665
NeilBrownefeb53c2009-01-09 08:31:10 +11004666static int add_named_array(const char *val, struct kernel_param *kp)
4667{
4668 /* val must be "md_*" where * is not all digits.
4669 * We allocate an array with a large free minor number, and
4670 * set the name to val. val must not already be an active name.
4671 */
4672 int len = strlen(val);
4673 char buf[DISK_NAME_LEN];
4674
4675 while (len && val[len-1] == '\n')
4676 len--;
4677 if (len >= DISK_NAME_LEN)
4678 return -E2BIG;
4679 strlcpy(buf, val, len+1);
4680 if (strncmp(buf, "md_", 3) != 0)
4681 return -EINVAL;
4682 return md_alloc(0, buf);
4683}
4684
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685static void md_safemode_timeout(unsigned long data)
4686{
NeilBrownfd01b882011-10-11 16:47:53 +11004687 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688
Neil Brown0fd62b82008-06-28 08:31:36 +10004689 if (!atomic_read(&mddev->writes_pending)) {
4690 mddev->safemode = 1;
4691 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004692 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 md_wakeup_thread(mddev->thread);
4695}
4696
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004697static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698
NeilBrownfd01b882011-10-11 16:47:53 +11004699int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700{
NeilBrown2604b702006-01-06 00:20:36 -08004701 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004702 struct md_rdev *rdev;
NeilBrown2604b702006-01-06 00:20:36 -08004703 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704
NeilBrowna757e642005-04-16 15:26:42 -07004705 if (list_empty(&mddev->disks))
4706 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
4709 if (mddev->pers)
4710 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004711 /* Cannot run until previous stop completes properly */
4712 if (mddev->sysfs_active)
4713 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004714
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 /*
4716 * Analyze all RAID superblock(s)
4717 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004718 if (!mddev->raid_disks) {
4719 if (!mddev->persistent)
4720 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004721 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723
NeilBrownd9d166c2006-01-06 00:20:51 -08004724 if (mddev->level != LEVEL_NONE)
4725 request_module("md-level-%d", mddev->level);
4726 else if (mddev->clevel[0])
4727 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728
4729 /*
4730 * Drop all container device buffers, from now on
4731 * the only valid external interface is through the md
4732 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004734 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004735 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 continue;
4737 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004738 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004739
4740 /* perform some consistency tests on the device.
4741 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004742 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004743 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004744 if (rdev->meta_bdev) {
4745 /* Nothing to check */;
4746 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004747 if (mddev->dev_sectors &&
4748 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004749 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004750 printk("md: %s: data overlaps metadata\n",
4751 mdname(mddev));
4752 return -EINVAL;
4753 }
4754 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004755 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004756 > rdev->data_offset) {
4757 printk("md: %s: metadata overlaps data\n",
4758 mdname(mddev));
4759 return -EINVAL;
4760 }
4761 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004762 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 }
4764
NeilBrowna167f662010-10-26 18:31:13 +11004765 if (mddev->bio_set == NULL)
NeilBrowna519b262011-07-28 07:56:24 +10004766 mddev->bio_set = bioset_create(BIO_POOL_SIZE,
NeilBrownfd01b882011-10-11 16:47:53 +11004767 sizeof(struct mddev *));
NeilBrowna167f662010-10-26 18:31:13 +11004768
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004770 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004771 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004773 if (mddev->level != LEVEL_NONE)
4774 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4775 mddev->level);
4776 else
4777 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4778 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 return -EINVAL;
4780 }
NeilBrown2604b702006-01-06 00:20:36 -08004781 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004783 if (mddev->level != pers->level) {
4784 mddev->level = pers->level;
4785 mddev->new_level = pers->level;
4786 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004787 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788
NeilBrownf6705572006-03-27 01:18:11 -08004789 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004790 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004791 /* This personality cannot handle reshaping... */
4792 mddev->pers = NULL;
4793 module_put(pers->owner);
4794 return -EINVAL;
4795 }
4796
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004797 if (pers->sync_request) {
4798 /* Warn if this is a potentially silly
4799 * configuration.
4800 */
4801 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11004802 struct md_rdev *rdev2;
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004803 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004804
4805 list_for_each_entry(rdev, &mddev->disks, same_set)
4806 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004807 if (rdev < rdev2 &&
4808 rdev->bdev->bd_contains ==
4809 rdev2->bdev->bd_contains) {
4810 printk(KERN_WARNING
4811 "%s: WARNING: %s appears to be"
4812 " on the same physical disk as"
4813 " %s.\n",
4814 mdname(mddev),
4815 bdevname(rdev->bdev,b),
4816 bdevname(rdev2->bdev,b2));
4817 warned = 1;
4818 }
4819 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004820
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004821 if (warned)
4822 printk(KERN_WARNING
4823 "True protection against single-disk"
4824 " failure might be compromised.\n");
4825 }
4826
NeilBrown657390d2005-08-26 18:34:16 -07004827 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004828 /* may be over-ridden by personality */
4829 mddev->resync_max_sectors = mddev->dev_sectors;
4830
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004831 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832
NeilBrown0f9552b2009-12-30 12:08:50 +11004833 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004834 mddev->ro = 2; /* read-only, but switch on first write */
4835
NeilBrownb15c2e52006-01-06 00:20:16 -08004836 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004837 if (err)
4838 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004839 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4840 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4841 " but 'external_size' not in effect?\n", __func__);
4842 printk(KERN_ERR
4843 "md: invalid array_size %llu > default size %llu\n",
4844 (unsigned long long)mddev->array_sectors / 2,
4845 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4846 err = -EINVAL;
4847 mddev->pers->stop(mddev);
4848 }
4849 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004850 err = bitmap_create(mddev);
4851 if (err) {
4852 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4853 mdname(mddev), err);
4854 mddev->pers->stop(mddev);
4855 }
4856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 module_put(mddev->pers->owner);
4859 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004860 bitmap_destroy(mddev);
4861 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004863 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004864 if (mddev->kobj.sd &&
4865 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004866 printk(KERN_WARNING
4867 "md: cannot register extra attributes for %s\n",
4868 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004869 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004870 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004871 mddev->ro = 0;
4872
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004874 atomic_set(&mddev->max_corr_read_errors,
4875 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 mddev->safemode = 0;
4877 mddev->safemode_timer.function = md_safemode_timeout;
4878 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004879 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11004881 smp_wmb();
4882 mddev->ready = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004883 list_for_each_entry(rdev, &mddev->disks, same_set)
Namhyung Kim36fad852011-07-27 11:00:36 +10004884 if (rdev->raid_disk >= 0)
4885 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004886 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887
4888 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4889
NeilBrown850b2b42006-10-03 01:15:46 -07004890 if (mddev->flags)
4891 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892
NeilBrownd7603b72006-01-06 00:20:30 -08004893 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004894 sysfs_notify_dirent_safe(mddev->sysfs_state);
4895 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004896 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 return 0;
4898}
NeilBrown390ee602010-06-01 19:37:27 +10004899EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900
NeilBrownfd01b882011-10-11 16:47:53 +11004901static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11004902{
4903 int err;
4904
4905 err = md_run(mddev);
4906 if (err)
4907 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10004908 err = bitmap_load(mddev);
4909 if (err) {
4910 bitmap_destroy(mddev);
4911 goto out;
4912 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05004913
4914 md_wakeup_thread(mddev->thread);
4915 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4916
NeilBrownfe60b012010-03-29 11:10:42 +11004917 set_capacity(mddev->gendisk, mddev->array_sectors);
4918 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004919 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11004920 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4921out:
4922 return err;
4923}
4924
NeilBrownfd01b882011-10-11 16:47:53 +11004925static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926{
4927 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928
Andre Noll80fab1d2008-07-11 22:02:21 +10004929 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004931 return -ENXIO;
4932 if (!mddev->pers)
4933 return -EINVAL;
4934 if (!mddev->ro)
4935 return -EBUSY;
4936 mddev->safemode = 0;
4937 mddev->ro = 0;
4938 set_disk_ro(disk, 0);
4939 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4940 mdname(mddev));
4941 /* Kick recovery or resync if necessary */
4942 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4943 md_wakeup_thread(mddev->thread);
4944 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004945 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004946 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947}
4948
NeilBrownacc55e22006-06-26 00:27:47 -07004949/* similar to deny_write_access, but accounts for our holding a reference
4950 * to the file ourselves */
4951static int deny_bitmap_write_access(struct file * file)
4952{
4953 struct inode *inode = file->f_mapping->host;
4954
4955 spin_lock(&inode->i_lock);
4956 if (atomic_read(&inode->i_writecount) > 1) {
4957 spin_unlock(&inode->i_lock);
4958 return -ETXTBSY;
4959 }
4960 atomic_set(&inode->i_writecount, -1);
4961 spin_unlock(&inode->i_lock);
4962
4963 return 0;
4964}
4965
NeilBrown43a70502009-12-14 12:49:55 +11004966void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004967{
4968 struct inode *inode = file->f_mapping->host;
4969
4970 spin_lock(&inode->i_lock);
4971 atomic_set(&inode->i_writecount, 1);
4972 spin_unlock(&inode->i_lock);
4973}
4974
NeilBrownfd01b882011-10-11 16:47:53 +11004975static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11004976{
4977 mddev->array_sectors = 0;
4978 mddev->external_size = 0;
4979 mddev->dev_sectors = 0;
4980 mddev->raid_disks = 0;
4981 mddev->recovery_cp = 0;
4982 mddev->resync_min = 0;
4983 mddev->resync_max = MaxSector;
4984 mddev->reshape_position = MaxSector;
4985 mddev->external = 0;
4986 mddev->persistent = 0;
4987 mddev->level = LEVEL_NONE;
4988 mddev->clevel[0] = 0;
4989 mddev->flags = 0;
4990 mddev->ro = 0;
4991 mddev->metadata_type[0] = 0;
4992 mddev->chunk_sectors = 0;
4993 mddev->ctime = mddev->utime = 0;
4994 mddev->layout = 0;
4995 mddev->max_disks = 0;
4996 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10004997 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004998 mddev->delta_disks = 0;
4999 mddev->new_level = LEVEL_NONE;
5000 mddev->new_layout = 0;
5001 mddev->new_chunk_sectors = 0;
5002 mddev->curr_resync = 0;
5003 mddev->resync_mismatches = 0;
5004 mddev->suspend_lo = mddev->suspend_hi = 0;
5005 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5006 mddev->recovery = 0;
5007 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005008 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005009 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005010 mddev->safemode = 0;
5011 mddev->bitmap_info.offset = 0;
5012 mddev->bitmap_info.default_offset = 0;
5013 mddev->bitmap_info.chunksize = 0;
5014 mddev->bitmap_info.daemon_sleep = 0;
5015 mddev->bitmap_info.max_write_behind = 0;
5016}
5017
NeilBrownfd01b882011-10-11 16:47:53 +11005018static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005019{
5020 if (mddev->sync_thread) {
5021 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5022 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11005023 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005024 }
5025
5026 del_timer_sync(&mddev->safemode_timer);
5027
5028 bitmap_flush(mddev);
5029 md_super_wait(mddev);
5030
5031 if (!mddev->in_sync || mddev->flags) {
5032 /* mark array as shutdown cleanly */
5033 mddev->in_sync = 1;
5034 md_update_sb(mddev, 1);
5035 }
5036}
NeilBrowndefad612011-01-14 09:14:33 +11005037
NeilBrownfd01b882011-10-11 16:47:53 +11005038void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005039{
5040 mddev_lock(mddev);
5041 __md_stop_writes(mddev);
5042 mddev_unlock(mddev);
5043}
NeilBrown390ee602010-06-01 19:37:27 +10005044EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005045
NeilBrownfd01b882011-10-11 16:47:53 +11005046void md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005047{
NeilBrown0ca69882011-01-14 09:14:33 +11005048 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005049 mddev->pers->stop(mddev);
5050 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5051 mddev->to_remove = &md_redundancy_group;
5052 module_put(mddev->pers->owner);
5053 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005054 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005055}
NeilBrown390ee602010-06-01 19:37:27 +10005056EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005057
NeilBrownfd01b882011-10-11 16:47:53 +11005058static int md_set_readonly(struct mddev *mddev, int is_open)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005059{
5060 int err = 0;
5061 mutex_lock(&mddev->open_mutex);
5062 if (atomic_read(&mddev->openers) > is_open) {
5063 printk("md: %s still in use.\n",mdname(mddev));
5064 err = -EBUSY;
5065 goto out;
5066 }
5067 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005068 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005069
5070 err = -ENXIO;
5071 if (mddev->ro==1)
5072 goto out;
5073 mddev->ro = 1;
5074 set_disk_ro(mddev->gendisk, 1);
5075 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005076 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005077 err = 0;
5078 }
5079out:
5080 mutex_unlock(&mddev->open_mutex);
5081 return err;
5082}
5083
NeilBrown9e653b62006-06-26 00:27:58 -07005084/* mode:
5085 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005086 * 2 - stop but do not disassemble array
5087 */
NeilBrownfd01b882011-10-11 16:47:53 +11005088static int do_md_stop(struct mddev * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005091 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092
NeilBrownc8c00a62009-08-10 12:50:52 +10005093 mutex_lock(&mddev->open_mutex);
NeilBrownbb4f1e92010-08-08 21:18:03 +10005094 if (atomic_read(&mddev->openers) > is_open ||
5095 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005096 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005097 mutex_unlock(&mddev->open_mutex);
5098 return -EBUSY;
5099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100
NeilBrown6e17b022010-08-07 21:41:19 +10005101 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005102 if (mddev->ro)
5103 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005104
NeilBrowndefad612011-01-14 09:14:33 +11005105 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005106 md_stop(mddev);
5107 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005108 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005109
NeilBrowna4bd82d2010-03-29 13:23:10 +11005110 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005111 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005112
NeilBrowna4bd82d2010-03-29 13:23:10 +11005113 list_for_each_entry(rdev, &mddev->disks, same_set)
Namhyung Kim36fad852011-07-27 11:00:36 +10005114 if (rdev->raid_disk >= 0)
5115 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005116
NeilBrowna4bd82d2010-03-29 13:23:10 +11005117 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005118 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005119 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005120 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005121
NeilBrowna4bd82d2010-03-29 13:23:10 +11005122 if (mddev->ro)
5123 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005124 } else
5125 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 /*
5127 * Free resources if final stop
5128 */
NeilBrown9e653b62006-06-26 00:27:58 -07005129 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5131
NeilBrown978f9462006-02-02 14:28:05 -08005132 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005133 if (mddev->bitmap_info.file) {
5134 restore_bitmap_write_access(mddev->bitmap_info.file);
5135 fput(mddev->bitmap_info.file);
5136 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005137 }
NeilBrownc3d97142009-12-14 12:49:52 +11005138 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005139
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 export_array(mddev);
5141
NeilBrown6177b472010-03-29 11:37:13 +11005142 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005143 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005144 if (mddev->hold_active == UNTIL_STOP)
5145 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005146 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005147 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005148 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005149 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005150 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151}
5152
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005153#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005154static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155{
NeilBrown3cb03002011-10-11 16:45:26 +11005156 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 int err;
5158
NeilBrowna757e642005-04-16 15:26:42 -07005159 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161
5162 printk(KERN_INFO "md: running: ");
5163
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005164 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 char b[BDEVNAME_SIZE];
5166 printk("<%s>", bdevname(rdev->bdev,b));
5167 }
5168 printk("\n");
5169
NeilBrownd710e132008-10-13 11:55:12 +11005170 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 if (err) {
5172 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11005173 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 }
5175}
5176
5177/*
5178 * lets try to run arrays based on all disks that have arrived
5179 * until now. (those are in pending_raid_disks)
5180 *
5181 * the method: pick the first pending disk, collect all disks with
5182 * the same UUID, remove all from the pending list and put them into
5183 * the 'same_array' list. Then order this list based on superblock
5184 * update time (freshest comes first), kick out 'old' disks and
5185 * compare superblocks. If everything's fine then run it.
5186 *
5187 * If "unit" is allocated, then bump its reference count
5188 */
5189static void autorun_devices(int part)
5190{
NeilBrown3cb03002011-10-11 16:45:26 +11005191 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005192 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 char b[BDEVNAME_SIZE];
5194
5195 printk(KERN_INFO "md: autorun ...\n");
5196 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005197 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005199 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005201 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202
5203 printk(KERN_INFO "md: considering %s ...\n",
5204 bdevname(rdev0->bdev,b));
5205 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005206 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 if (super_90_load(rdev, rdev0, 0) >= 0) {
5208 printk(KERN_INFO "md: adding %s ...\n",
5209 bdevname(rdev->bdev,b));
5210 list_move(&rdev->same_set, &candidates);
5211 }
5212 /*
5213 * now we have a set of devices, with all of them having
5214 * mostly sane superblocks. It's time to allocate the
5215 * mddev.
5216 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005217 if (part) {
5218 dev = MKDEV(mdp_major,
5219 rdev0->preferred_minor << MdpMinorShift);
5220 unit = MINOR(dev) >> MdpMinorShift;
5221 } else {
5222 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5223 unit = MINOR(dev);
5224 }
5225 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5227 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5228 break;
5229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230
5231 md_probe(dev, NULL, NULL);
5232 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005233 if (!mddev || !mddev->gendisk) {
5234 if (mddev)
5235 mddev_put(mddev);
5236 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 "md: cannot allocate memory for md drive.\n");
5238 break;
5239 }
5240 if (mddev_lock(mddev))
5241 printk(KERN_WARNING "md: %s locked, cannot run\n",
5242 mdname(mddev));
5243 else if (mddev->raid_disks || mddev->major_version
5244 || !list_empty(&mddev->disks)) {
5245 printk(KERN_WARNING
5246 "md: %s already running, cannot run %s\n",
5247 mdname(mddev), bdevname(rdev0->bdev,b));
5248 mddev_unlock(mddev);
5249 } else {
5250 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005251 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005252 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 list_del_init(&rdev->same_set);
5254 if (bind_rdev_to_array(rdev, mddev))
5255 export_rdev(rdev);
5256 }
5257 autorun_array(mddev);
5258 mddev_unlock(mddev);
5259 }
5260 /* on success, candidates will be empty, on error
5261 * it won't...
5262 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005263 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005264 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 mddev_put(mddev);
5268 }
5269 printk(KERN_INFO "md: ... autorun DONE.\n");
5270}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005271#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273static int get_version(void __user * arg)
5274{
5275 mdu_version_t ver;
5276
5277 ver.major = MD_MAJOR_VERSION;
5278 ver.minor = MD_MINOR_VERSION;
5279 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5280
5281 if (copy_to_user(arg, &ver, sizeof(ver)))
5282 return -EFAULT;
5283
5284 return 0;
5285}
5286
NeilBrownfd01b882011-10-11 16:47:53 +11005287static int get_array_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288{
5289 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005290 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005291 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292
NeilBrowna9f326e2009-09-23 18:06:41 +10005293 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005294 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005296 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 failed++;
5298 else {
5299 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005300 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005301 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 else
5303 spare++;
5304 }
5305 }
5306
5307 info.major_version = mddev->major_version;
5308 info.minor_version = mddev->minor_version;
5309 info.patch_version = MD_PATCHLEVEL_VERSION;
5310 info.ctime = mddev->ctime;
5311 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005312 info.size = mddev->dev_sectors / 2;
5313 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005314 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 info.nr_disks = nr;
5316 info.raid_disks = mddev->raid_disks;
5317 info.md_minor = mddev->md_minor;
5318 info.not_persistent= !mddev->persistent;
5319
5320 info.utime = mddev->utime;
5321 info.state = 0;
5322 if (mddev->in_sync)
5323 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005324 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005325 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005326 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 info.working_disks = working;
5328 info.failed_disks = failed;
5329 info.spare_disks = spare;
5330
5331 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005332 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333
5334 if (copy_to_user(arg, &info, sizeof(info)))
5335 return -EFAULT;
5336
5337 return 0;
5338}
5339
NeilBrownfd01b882011-10-11 16:47:53 +11005340static int get_bitmap_file(struct mddev * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005341{
5342 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5343 char *ptr, *buf = NULL;
5344 int err = -ENOMEM;
5345
Dan Williamsb5470dc2008-06-27 21:44:04 -07005346 if (md_allow_write(mddev))
5347 file = kmalloc(sizeof(*file), GFP_NOIO);
5348 else
5349 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005350
NeilBrown32a76272005-06-21 17:17:14 -07005351 if (!file)
5352 goto out;
5353
5354 /* bitmap disabled, zero the first byte and copy out */
5355 if (!mddev->bitmap || !mddev->bitmap->file) {
5356 file->pathname[0] = '\0';
5357 goto copy_out;
5358 }
5359
5360 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5361 if (!buf)
5362 goto out;
5363
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005364 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5365 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005366 goto out;
5367
5368 strcpy(file->pathname, ptr);
5369
5370copy_out:
5371 err = 0;
5372 if (copy_to_user(arg, file, sizeof(*file)))
5373 err = -EFAULT;
5374out:
5375 kfree(buf);
5376 kfree(file);
5377 return err;
5378}
5379
NeilBrownfd01b882011-10-11 16:47:53 +11005380static int get_disk_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381{
5382 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005383 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384
5385 if (copy_from_user(&info, arg, sizeof(info)))
5386 return -EFAULT;
5387
Andre Noll26ef3792008-07-11 22:02:21 +10005388 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 if (rdev) {
5390 info.major = MAJOR(rdev->bdev->bd_dev);
5391 info.minor = MINOR(rdev->bdev->bd_dev);
5392 info.raid_disk = rdev->raid_disk;
5393 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005394 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005396 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 info.state |= (1<<MD_DISK_ACTIVE);
5398 info.state |= (1<<MD_DISK_SYNC);
5399 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005400 if (test_bit(WriteMostly, &rdev->flags))
5401 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 } else {
5403 info.major = info.minor = 0;
5404 info.raid_disk = -1;
5405 info.state = (1<<MD_DISK_REMOVED);
5406 }
5407
5408 if (copy_to_user(arg, &info, sizeof(info)))
5409 return -EFAULT;
5410
5411 return 0;
5412}
5413
NeilBrownfd01b882011-10-11 16:47:53 +11005414static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415{
5416 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005417 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 dev_t dev = MKDEV(info->major,info->minor);
5419
5420 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5421 return -EOVERFLOW;
5422
5423 if (!mddev->raid_disks) {
5424 int err;
5425 /* expecting a device which has a superblock */
5426 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5427 if (IS_ERR(rdev)) {
5428 printk(KERN_WARNING
5429 "md: md_import_device returned %ld\n",
5430 PTR_ERR(rdev));
5431 return PTR_ERR(rdev);
5432 }
5433 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005434 struct md_rdev *rdev0
5435 = list_entry(mddev->disks.next,
5436 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005437 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 .load_super(rdev, rdev0, mddev->minor_version);
5439 if (err < 0) {
5440 printk(KERN_WARNING
5441 "md: %s has different UUID to %s\n",
5442 bdevname(rdev->bdev,b),
5443 bdevname(rdev0->bdev,b2));
5444 export_rdev(rdev);
5445 return -EINVAL;
5446 }
5447 }
5448 err = bind_rdev_to_array(rdev, mddev);
5449 if (err)
5450 export_rdev(rdev);
5451 return err;
5452 }
5453
5454 /*
5455 * add_new_disk can be used once the array is assembled
5456 * to add "hot spares". They must already have a superblock
5457 * written
5458 */
5459 if (mddev->pers) {
5460 int err;
5461 if (!mddev->pers->hot_add_disk) {
5462 printk(KERN_WARNING
5463 "%s: personality does not support diskops!\n",
5464 mdname(mddev));
5465 return -EINVAL;
5466 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005467 if (mddev->persistent)
5468 rdev = md_import_device(dev, mddev->major_version,
5469 mddev->minor_version);
5470 else
5471 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 if (IS_ERR(rdev)) {
5473 printk(KERN_WARNING
5474 "md: md_import_device returned %ld\n",
5475 PTR_ERR(rdev));
5476 return PTR_ERR(rdev);
5477 }
NeilBrown1a855a02010-12-09 16:36:28 +11005478 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005479 if (!mddev->persistent) {
5480 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005481 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005482 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005483 set_bit(In_sync, &rdev->flags);
5484 } else
NeilBrown41158c72005-06-21 17:17:25 -07005485 rdev->raid_disk = -1;
5486 } else
5487 super_types[mddev->major_version].
5488 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005489 if ((info->state & (1<<MD_DISK_SYNC)) &&
5490 (!test_bit(In_sync, &rdev->flags) ||
5491 rdev->raid_disk != info->raid_disk)) {
5492 /* This was a hot-add request, but events doesn't
5493 * match, so reject it.
5494 */
5495 export_rdev(rdev);
5496 return -EINVAL;
5497 }
5498
NeilBrown1a855a02010-12-09 16:36:28 +11005499 if (test_bit(In_sync, &rdev->flags))
5500 rdev->saved_raid_disk = rdev->raid_disk;
5501 else
5502 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005503
NeilBrownb2d444d2005-11-08 21:39:31 -08005504 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005505 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5506 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005507 else
5508 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005509
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 rdev->raid_disk = -1;
5511 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005512 if (!err && !mddev->pers->hot_remove_disk) {
5513 /* If there is hot_add_disk but no hot_remove_disk
5514 * then added disks for geometry changes,
5515 * and should be added immediately.
5516 */
5517 super_types[mddev->major_version].
5518 validate_super(mddev, rdev);
5519 err = mddev->pers->hot_add_disk(mddev, rdev);
5520 if (err)
5521 unbind_rdev_from_array(rdev);
5522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 if (err)
5524 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005525 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005526 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005527
NeilBrown17571282006-12-10 02:20:52 -08005528 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005529 if (mddev->degraded)
5530 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005531 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005532 if (!err)
5533 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005534 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 return err;
5536 }
5537
5538 /* otherwise, add_new_disk is only allowed
5539 * for major_version==0 superblocks
5540 */
5541 if (mddev->major_version != 0) {
5542 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5543 mdname(mddev));
5544 return -EINVAL;
5545 }
5546
5547 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5548 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005549 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 if (IS_ERR(rdev)) {
5551 printk(KERN_WARNING
5552 "md: error, md_import_device() returned %ld\n",
5553 PTR_ERR(rdev));
5554 return PTR_ERR(rdev);
5555 }
5556 rdev->desc_nr = info->number;
5557 if (info->raid_disk < mddev->raid_disks)
5558 rdev->raid_disk = info->raid_disk;
5559 else
5560 rdev->raid_disk = -1;
5561
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005563 if (info->state & (1<<MD_DISK_SYNC))
5564 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005566 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5567 set_bit(WriteMostly, &rdev->flags);
5568
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 if (!mddev->persistent) {
5570 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005571 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5572 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005573 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005574 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575
NeilBrown2bf071b2006-01-06 00:20:55 -08005576 err = bind_rdev_to_array(rdev, mddev);
5577 if (err) {
5578 export_rdev(rdev);
5579 return err;
5580 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 }
5582
5583 return 0;
5584}
5585
NeilBrownfd01b882011-10-11 16:47:53 +11005586static int hot_remove_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587{
5588 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005589 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 rdev = find_rdev(mddev, dev);
5592 if (!rdev)
5593 return -ENXIO;
5594
5595 if (rdev->raid_disk >= 0)
5596 goto busy;
5597
5598 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005599 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005600 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601
5602 return 0;
5603busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005604 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 bdevname(rdev->bdev,b), mdname(mddev));
5606 return -EBUSY;
5607}
5608
NeilBrownfd01b882011-10-11 16:47:53 +11005609static int hot_add_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610{
5611 char b[BDEVNAME_SIZE];
5612 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005613 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614
5615 if (!mddev->pers)
5616 return -ENODEV;
5617
5618 if (mddev->major_version != 0) {
5619 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5620 " version-0 superblocks.\n",
5621 mdname(mddev));
5622 return -EINVAL;
5623 }
5624 if (!mddev->pers->hot_add_disk) {
5625 printk(KERN_WARNING
5626 "%s: personality does not support diskops!\n",
5627 mdname(mddev));
5628 return -EINVAL;
5629 }
5630
NeilBrownd710e132008-10-13 11:55:12 +11005631 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 if (IS_ERR(rdev)) {
5633 printk(KERN_WARNING
5634 "md: error, md_import_device() returned %ld\n",
5635 PTR_ERR(rdev));
5636 return -EINVAL;
5637 }
5638
5639 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005640 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005642 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643
NeilBrown8190e752009-06-18 08:48:58 +10005644 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645
NeilBrownb2d444d2005-11-08 21:39:31 -08005646 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 printk(KERN_WARNING
5648 "md: can not hot-add faulty %s disk to %s!\n",
5649 bdevname(rdev->bdev,b), mdname(mddev));
5650 err = -EINVAL;
5651 goto abort_export;
5652 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005653 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005655 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005656 err = bind_rdev_to_array(rdev, mddev);
5657 if (err)
5658 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659
5660 /*
5661 * The rest should better be atomic, we can have disk failures
5662 * noticed in interrupt contexts ...
5663 */
5664
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 rdev->raid_disk = -1;
5666
NeilBrown850b2b42006-10-03 01:15:46 -07005667 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668
5669 /*
5670 * Kick recovery, maybe this spare has to be added to the
5671 * array immediately.
5672 */
5673 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5674 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005675 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 return 0;
5677
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678abort_export:
5679 export_rdev(rdev);
5680 return err;
5681}
5682
NeilBrownfd01b882011-10-11 16:47:53 +11005683static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005684{
5685 int err;
5686
NeilBrown36fa3062005-09-09 16:23:45 -07005687 if (mddev->pers) {
5688 if (!mddev->pers->quiesce)
5689 return -EBUSY;
5690 if (mddev->recovery || mddev->sync_thread)
5691 return -EBUSY;
5692 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005693 }
5694
NeilBrown36fa3062005-09-09 16:23:45 -07005695
5696 if (fd >= 0) {
5697 if (mddev->bitmap)
5698 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005699 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005700
NeilBrownc3d97142009-12-14 12:49:52 +11005701 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005702 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5703 mdname(mddev));
5704 return -EBADF;
5705 }
5706
NeilBrownc3d97142009-12-14 12:49:52 +11005707 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005708 if (err) {
5709 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5710 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005711 fput(mddev->bitmap_info.file);
5712 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005713 return err;
5714 }
NeilBrownc3d97142009-12-14 12:49:52 +11005715 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005716 } else if (mddev->bitmap == NULL)
5717 return -ENOENT; /* cannot remove what isn't there */
5718 err = 0;
5719 if (mddev->pers) {
5720 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005721 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005722 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005723 if (!err)
5724 err = bitmap_load(mddev);
5725 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005726 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005727 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005728 fd = -1; /* make sure to put the file */
5729 }
NeilBrown36fa3062005-09-09 16:23:45 -07005730 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005731 }
5732 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005733 if (mddev->bitmap_info.file) {
5734 restore_bitmap_write_access(mddev->bitmap_info.file);
5735 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005736 }
NeilBrownc3d97142009-12-14 12:49:52 +11005737 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005738 }
5739
NeilBrown32a76272005-06-21 17:17:14 -07005740 return err;
5741}
5742
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743/*
5744 * set_array_info is used two different ways
5745 * The original usage is when creating a new array.
5746 * In this usage, raid_disks is > 0 and it together with
5747 * level, size, not_persistent,layout,chunksize determine the
5748 * shape of the array.
5749 * This will always create an array with a type-0.90.0 superblock.
5750 * The newer usage is when assembling an array.
5751 * In this case raid_disks will be 0, and the major_version field is
5752 * use to determine which style super-blocks are to be found on the devices.
5753 * The minor and patch _version numbers are also kept incase the
5754 * super_block handler wishes to interpret them.
5755 */
NeilBrownfd01b882011-10-11 16:47:53 +11005756static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757{
5758
5759 if (info->raid_disks == 0) {
5760 /* just setting version number for superblock loading */
5761 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005762 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 super_types[info->major_version].name == NULL) {
5764 /* maybe try to auto-load a module? */
5765 printk(KERN_INFO
5766 "md: superblock version %d not known\n",
5767 info->major_version);
5768 return -EINVAL;
5769 }
5770 mddev->major_version = info->major_version;
5771 mddev->minor_version = info->minor_version;
5772 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005773 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005774 /* ensure mddev_put doesn't delete this now that there
5775 * is some minimal configuration.
5776 */
5777 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 return 0;
5779 }
5780 mddev->major_version = MD_MAJOR_VERSION;
5781 mddev->minor_version = MD_MINOR_VERSION;
5782 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5783 mddev->ctime = get_seconds();
5784
5785 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005786 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005787 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 mddev->raid_disks = info->raid_disks;
5789 /* don't set md_minor, it is determined by which /dev/md* was
5790 * openned
5791 */
5792 if (info->state & (1<<MD_SB_CLEAN))
5793 mddev->recovery_cp = MaxSector;
5794 else
5795 mddev->recovery_cp = 0;
5796 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005797 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798
5799 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005800 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801
5802 mddev->max_disks = MD_SB_DISKS;
5803
NeilBrowne6910632008-02-06 01:39:51 -08005804 if (mddev->persistent)
5805 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005806 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807
NeilBrownc3d97142009-12-14 12:49:52 +11005808 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5809 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005810
NeilBrownf6705572006-03-27 01:18:11 -08005811 mddev->reshape_position = MaxSector;
5812
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 /*
5814 * Generate a 128 bit UUID
5815 */
5816 get_random_bytes(mddev->uuid, 16);
5817
NeilBrownf6705572006-03-27 01:18:11 -08005818 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005819 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005820 mddev->new_layout = mddev->layout;
5821 mddev->delta_disks = 0;
5822
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 return 0;
5824}
5825
NeilBrownfd01b882011-10-11 16:47:53 +11005826void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11005827{
Dan Williamsb522adc2009-03-31 15:00:31 +11005828 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5829
5830 if (mddev->external_size)
5831 return;
5832
Dan Williams1f403622009-03-31 14:59:03 +11005833 mddev->array_sectors = array_sectors;
5834}
5835EXPORT_SYMBOL(md_set_array_sectors);
5836
NeilBrownfd01b882011-10-11 16:47:53 +11005837static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005838{
NeilBrown3cb03002011-10-11 16:45:26 +11005839 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005840 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005841 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005842
5843 if (mddev->pers->resize == NULL)
5844 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005845 /* The "num_sectors" is the number of sectors of each device that
5846 * is used. This can only make sense for arrays with redundancy.
5847 * linear and raid0 always use whatever space is available. We can only
5848 * consider changing this number if no resync or reconstruction is
5849 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005850 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005851 * of each device. If num_sectors is zero, we find the largest size
5852 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08005853 */
5854 if (mddev->sync_thread)
5855 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005856 if (mddev->bitmap)
5857 /* Sorry, cannot grow a bitmap yet, just remove it,
5858 * grow, and re-add.
5859 */
5860 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005861 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005862 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005863
Andre Nolld71f9f82008-07-11 22:02:22 +10005864 if (fit && (num_sectors == 0 || num_sectors > avail))
5865 num_sectors = avail;
5866 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005867 return -ENOSPC;
5868 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005869 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005870 if (!rv)
5871 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005872 return rv;
5873}
5874
NeilBrownfd01b882011-10-11 16:47:53 +11005875static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b92006-01-06 00:20:54 -08005876{
5877 int rv;
5878 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005879 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b92006-01-06 00:20:54 -08005880 return -EINVAL;
5881 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005882 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b92006-01-06 00:20:54 -08005883 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005884 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b92006-01-06 00:20:54 -08005885 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005886 mddev->delta_disks = raid_disks - mddev->raid_disks;
5887
5888 rv = mddev->pers->check_reshape(mddev);
NeilBrownde171cb2011-01-31 11:57:42 +11005889 if (rv < 0)
5890 mddev->delta_disks = 0;
NeilBrownda943b92006-01-06 00:20:54 -08005891 return rv;
5892}
5893
5894
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895/*
5896 * update_array_info is used to change the configuration of an
5897 * on-line array.
5898 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5899 * fields in the info are checked against the array.
5900 * Any differences that cannot be handled will cause an error.
5901 * Normally, only one change can be managed at a time.
5902 */
NeilBrownfd01b882011-10-11 16:47:53 +11005903static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904{
5905 int rv = 0;
5906 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005907 int state = 0;
5908
5909 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005910 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005911 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912
5913 if (mddev->major_version != info->major_version ||
5914 mddev->minor_version != info->minor_version ||
5915/* mddev->patch_version != info->patch_version || */
5916 mddev->ctime != info->ctime ||
5917 mddev->level != info->level ||
5918/* mddev->layout != info->layout || */
5919 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005920 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005921 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5922 ((state^info->state) & 0xfffffe00)
5923 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 return -EINVAL;
5925 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005926 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5927 cnt++;
5928 if (mddev->raid_disks != info->raid_disks)
5929 cnt++;
5930 if (mddev->layout != info->layout)
5931 cnt++;
5932 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5933 cnt++;
5934 if (cnt == 0)
5935 return 0;
5936 if (cnt > 1)
5937 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938
5939 if (mddev->layout != info->layout) {
5940 /* Change layout
5941 * we don't need to do anything at the md level, the
5942 * personality will take care of it all.
5943 */
NeilBrown50ac1682009-06-18 08:47:55 +10005944 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005946 else {
5947 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005948 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005949 if (rv)
5950 mddev->new_layout = mddev->layout;
5951 return rv;
5952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005954 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005955 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956
NeilBrownda943b92006-01-06 00:20:54 -08005957 if (mddev->raid_disks != info->raid_disks)
5958 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959
NeilBrown36fa3062005-09-09 16:23:45 -07005960 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5961 if (mddev->pers->quiesce == NULL)
5962 return -EINVAL;
5963 if (mddev->recovery || mddev->sync_thread)
5964 return -EBUSY;
5965 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5966 /* add the bitmap */
5967 if (mddev->bitmap)
5968 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005969 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005970 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005971 mddev->bitmap_info.offset =
5972 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005973 mddev->pers->quiesce(mddev, 1);
5974 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005975 if (!rv)
5976 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07005977 if (rv)
5978 bitmap_destroy(mddev);
5979 mddev->pers->quiesce(mddev, 0);
5980 } else {
5981 /* remove the bitmap */
5982 if (!mddev->bitmap)
5983 return -ENOENT;
5984 if (mddev->bitmap->file)
5985 return -EINVAL;
5986 mddev->pers->quiesce(mddev, 1);
5987 bitmap_destroy(mddev);
5988 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005989 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005990 }
5991 }
NeilBrown850b2b42006-10-03 01:15:46 -07005992 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 return rv;
5994}
5995
NeilBrownfd01b882011-10-11 16:47:53 +11005996static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997{
NeilBrown3cb03002011-10-11 16:45:26 +11005998 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999
6000 if (mddev->pers == NULL)
6001 return -ENODEV;
6002
6003 rdev = find_rdev(mddev, dev);
6004 if (!rdev)
6005 return -ENODEV;
6006
6007 md_error(mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10006008 if (!test_bit(Faulty, &rdev->flags))
6009 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 return 0;
6011}
6012
Andre Noll2f9618c2008-04-25 18:57:58 +02006013/*
6014 * We have a problem here : there is no easy way to give a CHS
6015 * virtual geometry. We currently pretend that we have a 2 heads
6016 * 4 sectors (with a BIG number of cylinders...). This drives
6017 * dosfs just mad... ;-)
6018 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006019static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6020{
NeilBrownfd01b882011-10-11 16:47:53 +11006021 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006022
6023 geo->heads = 2;
6024 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006025 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006026 return 0;
6027}
6028
Al Viroa39907f2008-03-02 10:31:15 -05006029static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030 unsigned int cmd, unsigned long arg)
6031{
6032 int err = 0;
6033 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006034 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006035 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036
6037 if (!capable(CAP_SYS_ADMIN))
6038 return -EACCES;
6039
6040 /*
6041 * Commands dealing with the RAID driver but not any
6042 * particular array:
6043 */
6044 switch (cmd)
6045 {
6046 case RAID_VERSION:
6047 err = get_version(argp);
6048 goto done;
6049
6050 case PRINT_RAID_DEBUG:
6051 err = 0;
6052 md_print_devices();
6053 goto done;
6054
6055#ifndef MODULE
6056 case RAID_AUTORUN:
6057 err = 0;
6058 autostart_arrays(arg);
6059 goto done;
6060#endif
6061 default:;
6062 }
6063
6064 /*
6065 * Commands creating/starting a new array:
6066 */
6067
Al Viroa39907f2008-03-02 10:31:15 -05006068 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069
6070 if (!mddev) {
6071 BUG();
6072 goto abort;
6073 }
6074
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 err = mddev_lock(mddev);
6076 if (err) {
6077 printk(KERN_INFO
6078 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6079 err, cmd);
6080 goto abort;
6081 }
6082
6083 switch (cmd)
6084 {
6085 case SET_ARRAY_INFO:
6086 {
6087 mdu_array_info_t info;
6088 if (!arg)
6089 memset(&info, 0, sizeof(info));
6090 else if (copy_from_user(&info, argp, sizeof(info))) {
6091 err = -EFAULT;
6092 goto abort_unlock;
6093 }
6094 if (mddev->pers) {
6095 err = update_array_info(mddev, &info);
6096 if (err) {
6097 printk(KERN_WARNING "md: couldn't update"
6098 " array info. %d\n", err);
6099 goto abort_unlock;
6100 }
6101 goto done_unlock;
6102 }
6103 if (!list_empty(&mddev->disks)) {
6104 printk(KERN_WARNING
6105 "md: array %s already has disks!\n",
6106 mdname(mddev));
6107 err = -EBUSY;
6108 goto abort_unlock;
6109 }
6110 if (mddev->raid_disks) {
6111 printk(KERN_WARNING
6112 "md: array %s already initialised!\n",
6113 mdname(mddev));
6114 err = -EBUSY;
6115 goto abort_unlock;
6116 }
6117 err = set_array_info(mddev, &info);
6118 if (err) {
6119 printk(KERN_WARNING "md: couldn't set"
6120 " array info. %d\n", err);
6121 goto abort_unlock;
6122 }
6123 }
6124 goto done_unlock;
6125
6126 default:;
6127 }
6128
6129 /*
6130 * Commands querying/configuring an existing array:
6131 */
NeilBrown32a76272005-06-21 17:17:14 -07006132 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006133 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006134 if ((!mddev->raid_disks && !mddev->external)
6135 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6136 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6137 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 err = -ENODEV;
6139 goto abort_unlock;
6140 }
6141
6142 /*
6143 * Commands even a read-only array can execute:
6144 */
6145 switch (cmd)
6146 {
6147 case GET_ARRAY_INFO:
6148 err = get_array_info(mddev, argp);
6149 goto done_unlock;
6150
NeilBrown32a76272005-06-21 17:17:14 -07006151 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01006152 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07006153 goto done_unlock;
6154
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155 case GET_DISK_INFO:
6156 err = get_disk_info(mddev, argp);
6157 goto done_unlock;
6158
6159 case RESTART_ARRAY_RW:
6160 err = restart_array(mddev);
6161 goto done_unlock;
6162
6163 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006164 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 goto done_unlock;
6166
6167 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11006168 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 goto done_unlock;
6170
Dan Williamse2218352010-05-12 08:25:37 +10006171 case BLKROSET:
6172 if (get_user(ro, (int __user *)(arg))) {
6173 err = -EFAULT;
6174 goto done_unlock;
6175 }
6176 err = -EINVAL;
6177
6178 /* if the bdev is going readonly the value of mddev->ro
6179 * does not matter, no writes are coming
6180 */
6181 if (ro)
6182 goto done_unlock;
6183
6184 /* are we are already prepared for writes? */
6185 if (mddev->ro != 1)
6186 goto done_unlock;
6187
6188 /* transitioning to readauto need only happen for
6189 * arrays that call md_write_start
6190 */
6191 if (mddev->pers) {
6192 err = restart_array(mddev);
6193 if (err == 0) {
6194 mddev->ro = 2;
6195 set_disk_ro(mddev->gendisk, 0);
6196 }
6197 }
6198 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 }
6200
6201 /*
6202 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006203 * superblock, so we do not allow them on read-only arrays.
6204 * However non-MD ioctls (e.g. get-size) will still come through
6205 * here and hit the 'default' below, so only disallow
6206 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006208 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006209 if (mddev->ro == 2) {
6210 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006211 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006212 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6213 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08006214 } else {
6215 err = -EROFS;
6216 goto abort_unlock;
6217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 }
6219
6220 switch (cmd)
6221 {
6222 case ADD_NEW_DISK:
6223 {
6224 mdu_disk_info_t info;
6225 if (copy_from_user(&info, argp, sizeof(info)))
6226 err = -EFAULT;
6227 else
6228 err = add_new_disk(mddev, &info);
6229 goto done_unlock;
6230 }
6231
6232 case HOT_REMOVE_DISK:
6233 err = hot_remove_disk(mddev, new_decode_dev(arg));
6234 goto done_unlock;
6235
6236 case HOT_ADD_DISK:
6237 err = hot_add_disk(mddev, new_decode_dev(arg));
6238 goto done_unlock;
6239
6240 case SET_DISK_FAULTY:
6241 err = set_disk_faulty(mddev, new_decode_dev(arg));
6242 goto done_unlock;
6243
6244 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006245 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 goto done_unlock;
6247
NeilBrown32a76272005-06-21 17:17:14 -07006248 case SET_BITMAP_FILE:
6249 err = set_bitmap_file(mddev, (int)arg);
6250 goto done_unlock;
6251
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253 err = -EINVAL;
6254 goto abort_unlock;
6255 }
6256
6257done_unlock:
6258abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006259 if (mddev->hold_active == UNTIL_IOCTL &&
6260 err != -EINVAL)
6261 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 mddev_unlock(mddev);
6263
6264 return err;
6265done:
6266 if (err)
6267 MD_BUG();
6268abort:
6269 return err;
6270}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006271#ifdef CONFIG_COMPAT
6272static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6273 unsigned int cmd, unsigned long arg)
6274{
6275 switch (cmd) {
6276 case HOT_REMOVE_DISK:
6277 case HOT_ADD_DISK:
6278 case SET_DISK_FAULTY:
6279 case SET_BITMAP_FILE:
6280 /* These take in integer arg, do not convert */
6281 break;
6282 default:
6283 arg = (unsigned long)compat_ptr(arg);
6284 break;
6285 }
6286
6287 return md_ioctl(bdev, mode, cmd, arg);
6288}
6289#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290
Al Viroa39907f2008-03-02 10:31:15 -05006291static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292{
6293 /*
6294 * Succeed if we can lock the mddev, which confirms that
6295 * it isn't being stopped right now.
6296 */
NeilBrownfd01b882011-10-11 16:47:53 +11006297 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298 int err;
6299
NeilBrownd3374822009-01-09 08:31:10 +11006300 if (mddev->gendisk != bdev->bd_disk) {
6301 /* we are racing with mddev_put which is discarding this
6302 * bd_disk.
6303 */
6304 mddev_put(mddev);
6305 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006306 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006307 /* Then retry the open from the top */
6308 return -ERESTARTSYS;
6309 }
6310 BUG_ON(mddev != bdev->bd_disk->private_data);
6311
NeilBrownc8c00a62009-08-10 12:50:52 +10006312 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 goto out;
6314
6315 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006316 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006317 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006319 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320 out:
6321 return err;
6322}
6323
Al Viroa39907f2008-03-02 10:31:15 -05006324static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325{
NeilBrownfd01b882011-10-11 16:47:53 +11006326 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006328 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006329 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 mddev_put(mddev);
6331
6332 return 0;
6333}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006334
6335static int md_media_changed(struct gendisk *disk)
6336{
NeilBrownfd01b882011-10-11 16:47:53 +11006337 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006338
6339 return mddev->changed;
6340}
6341
6342static int md_revalidate(struct gendisk *disk)
6343{
NeilBrownfd01b882011-10-11 16:47:53 +11006344 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006345
6346 mddev->changed = 0;
6347 return 0;
6348}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006349static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350{
6351 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006352 .open = md_open,
6353 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006354 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006355#ifdef CONFIG_COMPAT
6356 .compat_ioctl = md_compat_ioctl,
6357#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006358 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006359 .media_changed = md_media_changed,
6360 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361};
6362
Adrian Bunk75c96f82005-05-05 16:16:09 -07006363static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364{
NeilBrown2b8bf342011-10-11 16:48:23 +11006365 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 /*
6368 * md_thread is a 'system-thread', it's priority should be very
6369 * high. We avoid resource deadlocks individually in each
6370 * raid personality. (RAID5 does preallocation) We also use RR and
6371 * the very same RT priority as kswapd, thus we will never get
6372 * into a priority inversion deadlock.
6373 *
6374 * we definitely have to have equal or higher priority than
6375 * bdflush, otherwise bdflush will deadlock if there are too
6376 * many dirty RAID5 blocks.
6377 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378
NeilBrown6985c432005-10-19 21:23:47 -07006379 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006380 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381
NeilBrown93588e22005-11-15 00:09:12 -08006382 /* We need to wait INTERRUPTIBLE so that
6383 * we don't add to the load-average.
6384 * That means we need to be sure no signals are
6385 * pending
6386 */
6387 if (signal_pending(current))
6388 flush_signals(current);
6389
6390 wait_event_interruptible_timeout
6391 (thread->wqueue,
6392 test_bit(THREAD_WAKEUP, &thread->flags)
6393 || kthread_should_stop(),
6394 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395
NeilBrown6c987912011-01-14 09:13:53 +11006396 clear_bit(THREAD_WAKEUP, &thread->flags);
6397 if (!kthread_should_stop())
NeilBrown589a5942010-12-09 17:02:14 +11006398 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006400
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401 return 0;
6402}
6403
NeilBrown2b8bf342011-10-11 16:48:23 +11006404void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405{
6406 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006407 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408 set_bit(THREAD_WAKEUP, &thread->flags);
6409 wake_up(&thread->wqueue);
6410 }
6411}
6412
NeilBrown2b8bf342011-10-11 16:48:23 +11006413struct md_thread *md_register_thread(void (*run) (struct mddev *), struct mddev *mddev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414 const char *name)
6415{
NeilBrown2b8bf342011-10-11 16:48:23 +11006416 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417
NeilBrown2b8bf342011-10-11 16:48:23 +11006418 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 if (!thread)
6420 return NULL;
6421
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422 init_waitqueue_head(&thread->wqueue);
6423
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424 thread->run = run;
6425 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006426 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006427 thread->tsk = kthread_run(md_thread, thread,
6428 "%s_%s",
6429 mdname(thread->mddev),
6430 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006431 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432 kfree(thread);
6433 return NULL;
6434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006435 return thread;
6436}
6437
NeilBrown2b8bf342011-10-11 16:48:23 +11006438void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006439{
NeilBrown2b8bf342011-10-11 16:48:23 +11006440 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006441 if (!thread)
6442 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006443 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006444 /* Locking ensures that mddev_unlock does not wake_up a
6445 * non-existent thread
6446 */
6447 spin_lock(&pers_lock);
6448 *threadp = NULL;
6449 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006450
6451 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452 kfree(thread);
6453}
6454
NeilBrownfd01b882011-10-11 16:47:53 +11006455void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456{
6457 if (!mddev) {
6458 MD_BUG();
6459 return;
6460 }
6461
NeilBrownb2d444d2005-11-08 21:39:31 -08006462 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006463 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006464
NeilBrownde393cd2011-07-28 11:31:48 +10006465 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466 return;
6467 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006468 if (mddev->degraded)
6469 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006470 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6472 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6473 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006474 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006475 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006476 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006477}
6478
6479/* seq_file implementation /proc/mdstat */
6480
6481static void status_unused(struct seq_file *seq)
6482{
6483 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006484 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485
6486 seq_printf(seq, "unused devices: ");
6487
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006488 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006489 char b[BDEVNAME_SIZE];
6490 i++;
6491 seq_printf(seq, "%s ",
6492 bdevname(rdev->bdev,b));
6493 }
6494 if (!i)
6495 seq_printf(seq, "<none>");
6496
6497 seq_printf(seq, "\n");
6498}
6499
6500
NeilBrownfd01b882011-10-11 16:47:53 +11006501static void status_resync(struct seq_file *seq, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502{
NeilBrowndd71cf62009-05-07 12:49:35 +10006503 sector_t max_sectors, resync, res;
6504 unsigned long dt, db;
6505 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006506 int scale;
6507 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508
NeilBrowndd71cf62009-05-07 12:49:35 +10006509 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510
6511 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006512 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006514 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515
6516 /*
6517 * Should not happen.
6518 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006519 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520 MD_BUG();
6521 return;
6522 }
NeilBrown4588b422006-03-27 01:18:04 -08006523 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006524 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006525 * u32, as those are the requirements for sector_div.
6526 * Thus 'scale' must be at least 10
6527 */
6528 scale = 10;
6529 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006530 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006531 scale++;
6532 }
6533 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006534 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006535
6536 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006537 {
NeilBrown4588b422006-03-27 01:18:04 -08006538 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539 seq_printf(seq, "[");
6540 for (i = 0; i < x; i++)
6541 seq_printf(seq, "=");
6542 seq_printf(seq, ">");
6543 for (i = 0; i < y; i++)
6544 seq_printf(seq, ".");
6545 seq_printf(seq, "] ");
6546 }
NeilBrown4588b422006-03-27 01:18:04 -08006547 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006548 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6549 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006550 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6551 "check" :
6552 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6553 "resync" : "recovery"))),
6554 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006555 (unsigned long long) resync/2,
6556 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006557
6558 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559 * dt: time from mark until now
6560 * db: blocks written from mark until now
6561 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006562 *
6563 * rt is a sector_t, so could be 32bit or 64bit.
6564 * So we divide before multiply in case it is 32bit and close
6565 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006566 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006567 * near the end of resync when the number of remaining sectors
6568 * is close to 'db'.
6569 * We then divide rt by 32 after multiplying by db to compensate.
6570 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571 */
6572 dt = ((jiffies - mddev->resync_mark) / HZ);
6573 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006574 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6575 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576
NeilBrowndd71cf62009-05-07 12:49:35 +10006577 rt = max_sectors - resync; /* number of remaining sectors */
6578 sector_div(rt, db/32+1);
6579 rt *= dt;
6580 rt >>= 5;
6581
6582 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6583 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584
NeilBrownff4e8d92006-07-10 04:44:16 -07006585 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586}
6587
6588static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6589{
6590 struct list_head *tmp;
6591 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006592 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006593
6594 if (l >= 0x10000)
6595 return NULL;
6596 if (!l--)
6597 /* header */
6598 return (void*)1;
6599
6600 spin_lock(&all_mddevs_lock);
6601 list_for_each(tmp,&all_mddevs)
6602 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006603 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604 mddev_get(mddev);
6605 spin_unlock(&all_mddevs_lock);
6606 return mddev;
6607 }
6608 spin_unlock(&all_mddevs_lock);
6609 if (!l--)
6610 return (void*)2;/* tail */
6611 return NULL;
6612}
6613
6614static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6615{
6616 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006617 struct mddev *next_mddev, *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006618
6619 ++*pos;
6620 if (v == (void*)2)
6621 return NULL;
6622
6623 spin_lock(&all_mddevs_lock);
6624 if (v == (void*)1)
6625 tmp = all_mddevs.next;
6626 else
6627 tmp = mddev->all_mddevs.next;
6628 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11006629 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630 else {
6631 next_mddev = (void*)2;
6632 *pos = 0x10000;
6633 }
6634 spin_unlock(&all_mddevs_lock);
6635
6636 if (v != (void*)1)
6637 mddev_put(mddev);
6638 return next_mddev;
6639
6640}
6641
6642static void md_seq_stop(struct seq_file *seq, void *v)
6643{
NeilBrownfd01b882011-10-11 16:47:53 +11006644 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645
6646 if (mddev && v != (void*)1 && v != (void*)2)
6647 mddev_put(mddev);
6648}
6649
6650static int md_seq_show(struct seq_file *seq, void *v)
6651{
NeilBrownfd01b882011-10-11 16:47:53 +11006652 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006653 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11006654 struct md_rdev *rdev;
NeilBrown32a76272005-06-21 17:17:14 -07006655 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656
6657 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006658 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006659 seq_printf(seq, "Personalities : ");
6660 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006661 list_for_each_entry(pers, &pers_list, list)
6662 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006663
6664 spin_unlock(&pers_lock);
6665 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02006666 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667 return 0;
6668 }
6669 if (v == (void*)2) {
6670 status_unused(seq);
6671 return 0;
6672 }
6673
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006674 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006676
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6678 seq_printf(seq, "%s : %sactive", mdname(mddev),
6679 mddev->pers ? "" : "in");
6680 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006681 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006683 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006684 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685 seq_printf(seq, " %s", mddev->pers->name);
6686 }
6687
Andre Nolldd8ac332009-03-31 14:33:13 +11006688 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006689 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690 char b[BDEVNAME_SIZE];
6691 seq_printf(seq, " %s[%d]",
6692 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006693 if (test_bit(WriteMostly, &rdev->flags))
6694 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006695 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006696 seq_printf(seq, "(F)");
6697 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006698 } else if (rdev->raid_disk < 0)
6699 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006700 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701 }
6702
6703 if (!list_empty(&mddev->disks)) {
6704 if (mddev->pers)
6705 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006706 (unsigned long long)
6707 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708 else
6709 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006710 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006712 if (mddev->persistent) {
6713 if (mddev->major_version != 0 ||
6714 mddev->minor_version != 90) {
6715 seq_printf(seq," super %d.%d",
6716 mddev->major_version,
6717 mddev->minor_version);
6718 }
NeilBrowne6910632008-02-06 01:39:51 -08006719 } else if (mddev->external)
6720 seq_printf(seq, " super external:%s",
6721 mddev->metadata_type);
6722 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006723 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724
6725 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006726 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006728 if (mddev->pers->sync_request) {
6729 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006730 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006731 seq_printf(seq, "\n ");
6732 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6733 seq_printf(seq, "\tresync=DELAYED\n ");
6734 else if (mddev->recovery_cp < MaxSector)
6735 seq_printf(seq, "\tresync=PENDING\n ");
6736 }
NeilBrown32a76272005-06-21 17:17:14 -07006737 } else
6738 seq_printf(seq, "\n ");
6739
6740 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006741 unsigned long chunk_kb;
6742 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006743 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006744 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006745 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6746 "%lu%s chunk",
6747 bitmap->pages - bitmap->missing_pages,
6748 bitmap->pages,
6749 (bitmap->pages - bitmap->missing_pages)
6750 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006751 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006752 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006753 if (bitmap->file) {
6754 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006755 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006756 }
NeilBrown78d742d2005-06-21 17:17:15 -07006757
NeilBrown32a76272005-06-21 17:17:14 -07006758 seq_printf(seq, "\n");
6759 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006760 }
6761
6762 seq_printf(seq, "\n");
6763 }
6764 mddev_unlock(mddev);
6765
6766 return 0;
6767}
6768
Jan Engelhardt110518b2009-05-07 12:49:37 +10006769static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770 .start = md_seq_start,
6771 .next = md_seq_next,
6772 .stop = md_seq_stop,
6773 .show = md_seq_show,
6774};
6775
6776static int md_seq_open(struct inode *inode, struct file *file)
6777{
Kay Sieversf1514632011-07-12 20:48:39 +02006778 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779 int error;
6780
6781 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006782 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02006783 return error;
6784
6785 seq = file->private_data;
6786 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787 return error;
6788}
6789
NeilBrownd7603b72006-01-06 00:20:30 -08006790static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6791{
Kay Sieversf1514632011-07-12 20:48:39 +02006792 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08006793 int mask;
6794
6795 poll_wait(filp, &md_event_waiters, wait);
6796
6797 /* always allow read */
6798 mask = POLLIN | POLLRDNORM;
6799
Kay Sieversf1514632011-07-12 20:48:39 +02006800 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08006801 mask |= POLLERR | POLLPRI;
6802 return mask;
6803}
6804
Arjan van de Venfa027c22007-02-12 00:55:33 -08006805static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006806 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807 .open = md_seq_open,
6808 .read = seq_read,
6809 .llseek = seq_lseek,
Martin Peschkec3f94b42007-05-09 02:35:35 -07006810 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006811 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812};
6813
NeilBrown2604b702006-01-06 00:20:36 -08006814int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006815{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006817 list_add_tail(&p->list, &pers_list);
6818 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 spin_unlock(&pers_lock);
6820 return 0;
6821}
6822
NeilBrown2604b702006-01-06 00:20:36 -08006823int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824{
NeilBrown2604b702006-01-06 00:20:36 -08006825 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006827 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828 spin_unlock(&pers_lock);
6829 return 0;
6830}
6831
NeilBrownfd01b882011-10-11 16:47:53 +11006832static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833{
NeilBrown3cb03002011-10-11 16:45:26 +11006834 struct md_rdev * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006836 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837
6838 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006839 rcu_read_lock();
6840 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006842 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6843 (int)part_stat_read(&disk->part0, sectors[1]) -
6844 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006845 /* sync IO will cause sync_io to increase before the disk_stats
6846 * as sync_io is counted when a request starts, and
6847 * disk_stats is counted when it completes.
6848 * So resync activity will cause curr_events to be smaller than
6849 * when there was no such activity.
6850 * non-sync IO will cause disk_stat to increase without
6851 * increasing sync_io so curr_events will (eventually)
6852 * be larger than it was before. Once it becomes
6853 * substantially larger, the test below will cause
6854 * the array to appear non-idle, and resync will slow
6855 * down.
6856 * If there is a lot of outstanding resync activity when
6857 * we set last_event to curr_events, then all that activity
6858 * completing might cause the array to appear non-idle
6859 * and resync will be slowed down even though there might
6860 * not have been non-resync activity. This will only
6861 * happen once though. 'last_events' will soon reflect
6862 * the state where there is little or no outstanding
6863 * resync requests, and further resync activity will
6864 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006865 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006867 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868 rdev->last_events = curr_events;
6869 idle = 0;
6870 }
6871 }
NeilBrown4b809912008-07-21 17:05:25 +10006872 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873 return idle;
6874}
6875
NeilBrownfd01b882011-10-11 16:47:53 +11006876void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877{
6878 /* another "blocks" (512byte) blocks have been synced */
6879 atomic_sub(blocks, &mddev->recovery_active);
6880 wake_up(&mddev->recovery_wait);
6881 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006882 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883 md_wakeup_thread(mddev->thread);
6884 // stop recovery, signal do_sync ....
6885 }
6886}
6887
6888
NeilBrown06d91a52005-06-21 17:17:12 -07006889/* md_write_start(mddev, bi)
6890 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006891 * in superblock) before writing, schedule a superblock update
6892 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006893 */
NeilBrownfd01b882011-10-11 16:47:53 +11006894void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895{
Neil Brown0fd62b82008-06-28 08:31:36 +10006896 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006897 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006898 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006899
NeilBrownf91de922005-11-08 21:39:36 -08006900 BUG_ON(mddev->ro == 1);
6901 if (mddev->ro == 2) {
6902 /* need to switch to read/write */
6903 mddev->ro = 0;
6904 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6905 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006906 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006907 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006908 }
NeilBrown06d91a52005-06-21 17:17:12 -07006909 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006910 if (mddev->safemode == 1)
6911 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006912 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006913 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006914 if (mddev->in_sync) {
6915 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006916 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006917 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006918 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006919 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006920 }
NeilBrowna9701a32005-11-08 21:39:34 -08006921 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006922 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006923 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10006924 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006925 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07006926 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006927}
6928
NeilBrownfd01b882011-10-11 16:47:53 +11006929void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006930{
6931 if (atomic_dec_and_test(&mddev->writes_pending)) {
6932 if (mddev->safemode == 2)
6933 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006934 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006935 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6936 }
6937}
6938
NeilBrown2a2275d2007-01-26 00:57:11 -08006939/* md_allow_write(mddev)
6940 * Calling this ensures that the array is marked 'active' so that writes
6941 * may proceed without blocking. It is important to call this before
6942 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6943 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006944 *
6945 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6946 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006947 */
NeilBrownfd01b882011-10-11 16:47:53 +11006948int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006949{
6950 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006951 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006952 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006953 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006954 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006955 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006956
6957 spin_lock_irq(&mddev->write_lock);
6958 if (mddev->in_sync) {
6959 mddev->in_sync = 0;
6960 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006961 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08006962 if (mddev->safemode_delay &&
6963 mddev->safemode == 0)
6964 mddev->safemode = 1;
6965 spin_unlock_irq(&mddev->write_lock);
6966 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006967 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006968 } else
6969 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006970
NeilBrown070dc6d2010-08-30 17:33:34 +10006971 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07006972 return -EAGAIN;
6973 else
6974 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006975}
6976EXPORT_SYMBOL_GPL(md_allow_write);
6977
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978#define SYNC_MARKS 10
6979#define SYNC_MARK_STEP (3*HZ)
NeilBrownfd01b882011-10-11 16:47:53 +11006980void md_do_sync(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981{
NeilBrownfd01b882011-10-11 16:47:53 +11006982 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006983 unsigned int currspeed = 0,
6984 window;
NeilBrown57afd892005-06-21 17:17:13 -07006985 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006986 unsigned long mark[SYNC_MARKS];
6987 sector_t mark_cnt[SYNC_MARKS];
6988 int last_mark,m;
6989 struct list_head *tmp;
6990 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006991 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006992 struct md_rdev *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006993 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994
6995 /* just incase thread restarts... */
6996 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6997 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006998 if (mddev->ro) /* never try to sync a read-only array */
6999 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000
NeilBrown61df9d92006-10-03 01:15:57 -07007001 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7002 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
7003 desc = "data-check";
7004 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7005 desc = "requested-resync";
7006 else
7007 desc = "resync";
7008 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7009 desc = "reshape";
7010 else
7011 desc = "recovery";
7012
Linus Torvalds1da177e2005-04-16 15:20:36 -07007013 /* we overload curr_resync somewhat here.
7014 * 0 == not engaged in resync at all
7015 * 2 == checking that there is no conflict with another sync
7016 * 1 == like 2, but have yielded to allow conflicting resync to
7017 * commense
7018 * other == active in resync - this many blocks
7019 *
7020 * Before starting a resync we must have set curr_resync to
7021 * 2, and then checked that every "conflicting" array has curr_resync
7022 * less than ours. When we find one that is the same or higher
7023 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7024 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7025 * This will mean we have to start checking from the beginning again.
7026 *
7027 */
7028
7029 do {
7030 mddev->curr_resync = 2;
7031
7032 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007033 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07007034 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11007035
7036 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007037 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007038 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039 if (mddev2 == mddev)
7040 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007041 if (!mddev->parallel_resync
7042 && mddev2->curr_resync
7043 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044 DEFINE_WAIT(wq);
7045 if (mddev < mddev2 && mddev->curr_resync == 2) {
7046 /* arbitrarily yield */
7047 mddev->curr_resync = 1;
7048 wake_up(&resync_wait);
7049 }
7050 if (mddev > mddev2 && mddev->curr_resync == 1)
7051 /* no need to wait here, we can wait the next
7052 * time 'round when curr_resync == 2
7053 */
7054 continue;
NeilBrown97441972008-09-19 11:49:54 +10007055 /* We need to wait 'interruptible' so as not to
7056 * contribute to the load average, and not to
7057 * be caught by 'softlockup'
7058 */
7059 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08007060 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07007061 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007062 printk(KERN_INFO "md: delaying %s of %s"
7063 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007065 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007067 if (signal_pending(current))
7068 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069 schedule();
7070 finish_wait(&resync_wait, &wq);
7071 goto try_again;
7072 }
7073 finish_wait(&resync_wait, &wq);
7074 }
7075 }
7076 } while (mddev->curr_resync < 2);
7077
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007078 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007079 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007080 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007081 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082 */
7083 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08007084 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007085 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007086 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7087 j = mddev->resync_min;
7088 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007089 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007090
NeilBrownccfcc3c2006-03-27 01:18:09 -08007091 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11007092 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007093 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007094 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007095 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007096 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007097 rcu_read_lock();
7098 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007099 if (rdev->raid_disk >= 0 &&
7100 !test_bit(Faulty, &rdev->flags) &&
7101 !test_bit(In_sync, &rdev->flags) &&
7102 rdev->recovery_offset < j)
7103 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007104 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007106
NeilBrown61df9d92006-10-03 01:15:57 -07007107 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7108 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7109 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007110 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007111 "(but not more than %d KB/sec) for %s.\n",
7112 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113
NeilBrowneea1bf32009-03-31 14:27:02 +11007114 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007115
NeilBrown57afd892005-06-21 17:17:13 -07007116 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007117 for (m = 0; m < SYNC_MARKS; m++) {
7118 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007119 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007120 }
7121 last_mark = 0;
7122 mddev->resync_mark = mark[last_mark];
7123 mddev->resync_mark_cnt = mark_cnt[last_mark];
7124
7125 /*
7126 * Tune reconstruction:
7127 */
7128 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007129 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7130 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131
7132 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007133 last_check = 0;
7134
7135 if (j>2) {
7136 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007137 "md: resuming %s of %s from checkpoint.\n",
7138 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139 mddev->curr_resync = j;
7140 }
NeilBrown75d3da42011-01-14 09:14:34 +11007141 mddev->curr_resync_completed = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007142
7143 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007144 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145
NeilBrown57afd892005-06-21 17:17:13 -07007146 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007147
NeilBrown7a91ee12009-05-26 12:57:21 +10007148 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7149 ((mddev->curr_resync > mddev->curr_resync_completed &&
7150 (mddev->curr_resync - mddev->curr_resync_completed)
7151 > (max_sectors >> 4)) ||
7152 (j - mddev->curr_resync_completed)*2
7153 >= mddev->resync_max - mddev->curr_resync_completed
7154 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007155 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007156 wait_event(mddev->recovery_wait,
7157 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007158 mddev->curr_resync_completed = j;
NeilBrown070dc6d2010-08-30 17:33:34 +10007159 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007160 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007161 }
NeilBrownacb180b2009-04-14 16:28:34 +10007162
NeilBrowne62e58a2009-07-01 13:15:35 +10007163 while (j >= mddev->resync_max && !kthread_should_stop()) {
7164 /* As this condition is controlled by user-space,
7165 * we can block indefinitely, so use '_interruptible'
7166 * to avoid triggering warnings.
7167 */
7168 flush_signals(current); /* just in case */
7169 wait_event_interruptible(mddev->recovery_wait,
7170 mddev->resync_max > j
7171 || kthread_should_stop());
7172 }
NeilBrownacb180b2009-04-14 16:28:34 +10007173
7174 if (kthread_should_stop())
7175 goto interrupted;
7176
NeilBrown57afd892005-06-21 17:17:13 -07007177 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007178 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007179 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007180 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181 goto out;
7182 }
NeilBrown57afd892005-06-21 17:17:13 -07007183
7184 if (!skipped) { /* actual IO requested */
7185 io_sectors += sectors;
7186 atomic_add(sectors, &mddev->recovery_active);
7187 }
7188
NeilBrowne875ece2011-07-28 11:39:24 +10007189 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7190 break;
7191
Linus Torvalds1da177e2005-04-16 15:20:36 -07007192 j += sectors;
7193 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007194 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007195 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007196 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007197 * visible in /proc/mdstat
7198 */
7199 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007200
7201 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007202 continue;
7203
NeilBrown57afd892005-06-21 17:17:13 -07007204 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007205 repeat:
7206 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7207 /* step marks */
7208 int next = (last_mark+1) % SYNC_MARKS;
7209
7210 mddev->resync_mark = mark[next];
7211 mddev->resync_mark_cnt = mark_cnt[next];
7212 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007213 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214 last_mark = next;
7215 }
7216
7217
NeilBrownc6207272008-02-06 01:39:52 -08007218 if (kthread_should_stop())
7219 goto interrupted;
7220
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221
7222 /*
7223 * this loop exits only if either when we are slower than
7224 * the 'hard' speed limit, or the system was IO-idle for
7225 * a jiffy.
7226 * the system might be non-idle CPU-wise, but we only care
7227 * about not overloading the IO subsystem. (things like an
7228 * e2fsck being done on the RAID array should execute fast)
7229 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230 cond_resched();
7231
NeilBrown57afd892005-06-21 17:17:13 -07007232 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7233 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234
NeilBrown88202a02006-01-06 00:21:36 -08007235 if (currspeed > speed_min(mddev)) {
7236 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007237 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007238 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239 goto repeat;
7240 }
7241 }
7242 }
NeilBrown61df9d92006-10-03 01:15:57 -07007243 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244 /*
7245 * this also signals 'finished resyncing' to md_stop
7246 */
7247 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7249
7250 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007251 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007252
NeilBrowndfc70642008-05-23 13:04:39 -07007253 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007254 mddev->curr_resync > 2) {
7255 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7256 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7257 if (mddev->curr_resync >= mddev->recovery_cp) {
7258 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007259 "md: checkpointing %s of %s.\n",
7260 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007261 mddev->recovery_cp = mddev->curr_resync;
7262 }
7263 } else
7264 mddev->recovery_cp = MaxSector;
7265 } else {
7266 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7267 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007268 rcu_read_lock();
7269 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007270 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007271 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007272 !test_bit(Faulty, &rdev->flags) &&
7273 !test_bit(In_sync, &rdev->flags) &&
7274 rdev->recovery_offset < mddev->curr_resync)
7275 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007276 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007278 }
NeilBrown17571282006-12-10 02:20:52 -08007279 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11007282 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7283 /* We completed so min/max setting can be forgotten if used. */
7284 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7285 mddev->resync_min = 0;
7286 mddev->resync_max = MaxSector;
7287 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7288 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007289 mddev->curr_resync = 0;
7290 wake_up(&resync_wait);
7291 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7292 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007293 return;
7294
7295 interrupted:
7296 /*
7297 * got a signal, exit.
7298 */
7299 printk(KERN_INFO
7300 "md: md_do_sync() got signal ... exiting\n");
7301 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7302 goto out;
7303
Linus Torvalds1da177e2005-04-16 15:20:36 -07007304}
NeilBrown29269552006-03-27 01:18:10 -08007305EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007306
NeilBrownfd01b882011-10-11 16:47:53 +11007307static int remove_and_add_spares(struct mddev *mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007308{
NeilBrown3cb03002011-10-11 16:45:26 +11007309 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007310 int spares = 0;
7311
NeilBrown97e4f422009-03-31 14:33:13 +11007312 mddev->curr_resync_completed = 0;
7313
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007314 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007315 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007316 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007317 (test_bit(Faulty, &rdev->flags) ||
7318 ! test_bit(In_sync, &rdev->flags)) &&
7319 atomic_read(&rdev->nr_pending)==0) {
7320 if (mddev->pers->hot_remove_disk(
7321 mddev, rdev->raid_disk)==0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007322 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007323 rdev->raid_disk = -1;
7324 }
7325 }
7326
NeilBrown53890422011-07-27 11:00:36 +10007327 if (mddev->degraded) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007328 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07007329 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07007330 !test_bit(In_sync, &rdev->flags) &&
NeilBrownde393cd2011-07-28 11:31:48 +10007331 !test_bit(Faulty, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07007332 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007333 if (rdev->raid_disk < 0
7334 && !test_bit(Faulty, &rdev->flags)) {
7335 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10007336 if (mddev->pers->
7337 hot_add_disk(mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007338 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10007339 /* failure here is OK */;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007340 spares++;
7341 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11007342 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007343 } else
7344 break;
7345 }
NeilBrowndfc70642008-05-23 13:04:39 -07007346 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007347 }
7348 return spares;
7349}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007350
NeilBrownfd01b882011-10-11 16:47:53 +11007351static void reap_sync_thread(struct mddev *mddev)
NeilBrown7ebc0be2011-01-14 09:14:33 +11007352{
NeilBrown3cb03002011-10-11 16:45:26 +11007353 struct md_rdev *rdev;
NeilBrown7ebc0be2011-01-14 09:14:33 +11007354
7355 /* resync has finished, collect result */
NeilBrown01f96c02011-09-21 15:30:20 +10007356 md_unregister_thread(&mddev->sync_thread);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007357 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7358 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7359 /* success...*/
7360 /* activate any spares */
7361 if (mddev->pers->spare_active(mddev))
7362 sysfs_notify(&mddev->kobj, NULL,
7363 "degraded");
7364 }
7365 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7366 mddev->pers->finish_reshape)
7367 mddev->pers->finish_reshape(mddev);
7368 md_update_sb(mddev, 1);
7369
7370 /* if array is no-longer degraded, then any saved_raid_disk
7371 * information must be scrapped
7372 */
7373 if (!mddev->degraded)
7374 list_for_each_entry(rdev, &mddev->disks, same_set)
7375 rdev->saved_raid_disk = -1;
7376
7377 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7378 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7379 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7380 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7381 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7382 /* flag recovery needed just to double check */
7383 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7384 sysfs_notify_dirent_safe(mddev->sysfs_action);
7385 md_new_event(mddev);
Jonathan Brassow768e5872011-07-27 11:00:37 +10007386 if (mddev->event_work.func)
7387 queue_work(md_misc_wq, &mddev->event_work);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007388}
7389
Linus Torvalds1da177e2005-04-16 15:20:36 -07007390/*
7391 * This routine is regularly called by all per-raid-array threads to
7392 * deal with generic issues like resync and super-block update.
7393 * Raid personalities that don't have a thread (linear/raid0) do not
7394 * need this as they never do any recovery or update the superblock.
7395 *
7396 * It does not do any resync itself, but rather "forks" off other threads
7397 * to do that as needed.
7398 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7399 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007400 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007401 * and wakeups up this thread which will reap the thread and finish up.
7402 * This thread also removes any faulty devices (with nr_pending == 0).
7403 *
7404 * The overall approach is:
7405 * 1/ if the superblock needs updating, update it.
7406 * 2/ If a recovery thread is running, don't do anything else.
7407 * 3/ If recovery has finished, clean up, possibly marking spares active.
7408 * 4/ If there are any faulty devices, remove them.
7409 * 5/ If array is degraded, try to add spares devices
7410 * 6/ If array has spares or is not in-sync, start a resync thread.
7411 */
NeilBrownfd01b882011-10-11 16:47:53 +11007412void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007413{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007414 if (mddev->suspended)
7415 return;
7416
NeilBrown5f404022005-06-21 17:17:16 -07007417 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007418 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007419
NeilBrownfca4d842005-06-21 17:17:11 -07007420 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007421 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007422 printk(KERN_INFO "md: %s in immediate safe mode\n",
7423 mdname(mddev));
7424 mddev->safemode = 2;
7425 }
7426 flush_signals(current);
7427 }
7428
NeilBrownc89a8ee2008-08-05 15:54:13 +10007429 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7430 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007431 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007432 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007433 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007434 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007435 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007436 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7437 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007438 ))
7439 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007440
NeilBrowndf5b89b2006-03-27 01:18:20 -08007441 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007442 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007443
NeilBrownc89a8ee2008-08-05 15:54:13 +10007444 if (mddev->ro) {
7445 /* Only thing we do on a ro array is remove
7446 * failed devices.
7447 */
NeilBrown3cb03002011-10-11 16:45:26 +11007448 struct md_rdev *rdev;
NeilBrowna8c42c72011-01-31 13:47:13 +11007449 list_for_each_entry(rdev, &mddev->disks, same_set)
7450 if (rdev->raid_disk >= 0 &&
7451 !test_bit(Blocked, &rdev->flags) &&
7452 test_bit(Faulty, &rdev->flags) &&
7453 atomic_read(&rdev->nr_pending)==0) {
7454 if (mddev->pers->hot_remove_disk(
7455 mddev, rdev->raid_disk)==0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007456 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna8c42c72011-01-31 13:47:13 +11007457 rdev->raid_disk = -1;
7458 }
7459 }
NeilBrownc89a8ee2008-08-05 15:54:13 +10007460 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7461 goto unlock;
7462 }
7463
NeilBrown31a59e32008-04-30 00:52:30 -07007464 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007465 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007466 spin_lock_irq(&mddev->write_lock);
7467 if (mddev->safemode &&
7468 !atomic_read(&mddev->writes_pending) &&
7469 !mddev->in_sync &&
7470 mddev->recovery_cp == MaxSector) {
7471 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007472 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007473 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007474 }
7475 if (mddev->safemode == 1)
7476 mddev->safemode = 0;
7477 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007478 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007479 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007480 }
NeilBrownfca4d842005-06-21 17:17:11 -07007481
NeilBrown850b2b42006-10-03 01:15:46 -07007482 if (mddev->flags)
7483 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007484
Linus Torvalds1da177e2005-04-16 15:20:36 -07007485 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7486 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7487 /* resync/recovery still happening */
7488 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7489 goto unlock;
7490 }
7491 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007492 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007493 goto unlock;
7494 }
Neil Brown72a23c22008-06-28 08:31:41 +10007495 /* Set RUNNING before clearing NEEDED to avoid
7496 * any transients in the value of "sync_action".
7497 */
7498 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7499 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007500 /* Clear some bits that don't mean anything, but
7501 * might be left set
7502 */
NeilBrown24dd4692005-11-08 21:39:26 -08007503 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7504 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007505
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007506 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7507 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007508 /* no recovery is running.
7509 * remove any failed drives, then
7510 * add spares if possible.
7511 * Spare are also removed and re-added, to allow
7512 * the personality to fail the re-add.
7513 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007514
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007515 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007516 if (mddev->pers->check_reshape == NULL ||
7517 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007518 /* Cannot proceed */
7519 goto unlock;
7520 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007521 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007522 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007523 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7524 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007525 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007526 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007527 } else if (mddev->recovery_cp < MaxSector) {
7528 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007529 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007530 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7531 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007532 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007533
Linus Torvalds1da177e2005-04-16 15:20:36 -07007534 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007535 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7536 /* We are adding a device or devices to an array
7537 * which has the bitmap stored on all devices.
7538 * So make sure all bitmap pages get written
7539 */
7540 bitmap_write_all(mddev->bitmap);
7541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007542 mddev->sync_thread = md_register_thread(md_do_sync,
7543 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007544 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007545 if (!mddev->sync_thread) {
7546 printk(KERN_ERR "%s: could not start resync"
7547 " thread...\n",
7548 mdname(mddev));
7549 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007550 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7551 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7552 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7553 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7554 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007555 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007556 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007557 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007558 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007559 }
7560 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007561 if (!mddev->sync_thread) {
7562 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7563 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7564 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007565 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007566 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007568 mddev_unlock(mddev);
7569 }
7570}
7571
NeilBrownfd01b882011-10-11 16:47:53 +11007572void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007573{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007574 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007575 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007576 !test_bit(Blocked, &rdev->flags) &&
7577 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007578 msecs_to_jiffies(5000));
7579 rdev_dec_pending(rdev, mddev);
7580}
7581EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7582
NeilBrown2230dfe2011-07-28 11:31:46 +10007583
7584/* Bad block management.
7585 * We can record which blocks on each device are 'bad' and so just
7586 * fail those blocks, or that stripe, rather than the whole device.
7587 * Entries in the bad-block table are 64bits wide. This comprises:
7588 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7589 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7590 * A 'shift' can be set so that larger blocks are tracked and
7591 * consequently larger devices can be covered.
7592 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7593 *
7594 * Locking of the bad-block table uses a seqlock so md_is_badblock
7595 * might need to retry if it is very unlucky.
7596 * We will sometimes want to check for bad blocks in a bi_end_io function,
7597 * so we use the write_seqlock_irq variant.
7598 *
7599 * When looking for a bad block we specify a range and want to
7600 * know if any block in the range is bad. So we binary-search
7601 * to the last range that starts at-or-before the given endpoint,
7602 * (or "before the sector after the target range")
7603 * then see if it ends after the given start.
7604 * We return
7605 * 0 if there are no known bad blocks in the range
7606 * 1 if there are known bad block which are all acknowledged
7607 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7608 * plus the start/length of the first bad section we overlap.
7609 */
7610int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7611 sector_t *first_bad, int *bad_sectors)
7612{
7613 int hi;
7614 int lo = 0;
7615 u64 *p = bb->page;
7616 int rv = 0;
7617 sector_t target = s + sectors;
7618 unsigned seq;
7619
7620 if (bb->shift > 0) {
7621 /* round the start down, and the end up */
7622 s >>= bb->shift;
7623 target += (1<<bb->shift) - 1;
7624 target >>= bb->shift;
7625 sectors = target - s;
7626 }
7627 /* 'target' is now the first block after the bad range */
7628
7629retry:
7630 seq = read_seqbegin(&bb->lock);
7631
7632 hi = bb->count;
7633
7634 /* Binary search between lo and hi for 'target'
7635 * i.e. for the last range that starts before 'target'
7636 */
7637 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7638 * are known not to be the last range before target.
7639 * VARIANT: hi-lo is the number of possible
7640 * ranges, and decreases until it reaches 1
7641 */
7642 while (hi - lo > 1) {
7643 int mid = (lo + hi) / 2;
7644 sector_t a = BB_OFFSET(p[mid]);
7645 if (a < target)
7646 /* This could still be the one, earlier ranges
7647 * could not. */
7648 lo = mid;
7649 else
7650 /* This and later ranges are definitely out. */
7651 hi = mid;
7652 }
7653 /* 'lo' might be the last that started before target, but 'hi' isn't */
7654 if (hi > lo) {
7655 /* need to check all range that end after 's' to see if
7656 * any are unacknowledged.
7657 */
7658 while (lo >= 0 &&
7659 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7660 if (BB_OFFSET(p[lo]) < target) {
7661 /* starts before the end, and finishes after
7662 * the start, so they must overlap
7663 */
7664 if (rv != -1 && BB_ACK(p[lo]))
7665 rv = 1;
7666 else
7667 rv = -1;
7668 *first_bad = BB_OFFSET(p[lo]);
7669 *bad_sectors = BB_LEN(p[lo]);
7670 }
7671 lo--;
7672 }
7673 }
7674
7675 if (read_seqretry(&bb->lock, seq))
7676 goto retry;
7677
7678 return rv;
7679}
7680EXPORT_SYMBOL_GPL(md_is_badblock);
7681
7682/*
7683 * Add a range of bad blocks to the table.
7684 * This might extend the table, or might contract it
7685 * if two adjacent ranges can be merged.
7686 * We binary-search to find the 'insertion' point, then
7687 * decide how best to handle it.
7688 */
7689static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
7690 int acknowledged)
7691{
7692 u64 *p;
7693 int lo, hi;
7694 int rv = 1;
7695
7696 if (bb->shift < 0)
7697 /* badblocks are disabled */
7698 return 0;
7699
7700 if (bb->shift) {
7701 /* round the start down, and the end up */
7702 sector_t next = s + sectors;
7703 s >>= bb->shift;
7704 next += (1<<bb->shift) - 1;
7705 next >>= bb->shift;
7706 sectors = next - s;
7707 }
7708
7709 write_seqlock_irq(&bb->lock);
7710
7711 p = bb->page;
7712 lo = 0;
7713 hi = bb->count;
7714 /* Find the last range that starts at-or-before 's' */
7715 while (hi - lo > 1) {
7716 int mid = (lo + hi) / 2;
7717 sector_t a = BB_OFFSET(p[mid]);
7718 if (a <= s)
7719 lo = mid;
7720 else
7721 hi = mid;
7722 }
7723 if (hi > lo && BB_OFFSET(p[lo]) > s)
7724 hi = lo;
7725
7726 if (hi > lo) {
7727 /* we found a range that might merge with the start
7728 * of our new range
7729 */
7730 sector_t a = BB_OFFSET(p[lo]);
7731 sector_t e = a + BB_LEN(p[lo]);
7732 int ack = BB_ACK(p[lo]);
7733 if (e >= s) {
7734 /* Yes, we can merge with a previous range */
7735 if (s == a && s + sectors >= e)
7736 /* new range covers old */
7737 ack = acknowledged;
7738 else
7739 ack = ack && acknowledged;
7740
7741 if (e < s + sectors)
7742 e = s + sectors;
7743 if (e - a <= BB_MAX_LEN) {
7744 p[lo] = BB_MAKE(a, e-a, ack);
7745 s = e;
7746 } else {
7747 /* does not all fit in one range,
7748 * make p[lo] maximal
7749 */
7750 if (BB_LEN(p[lo]) != BB_MAX_LEN)
7751 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
7752 s = a + BB_MAX_LEN;
7753 }
7754 sectors = e - s;
7755 }
7756 }
7757 if (sectors && hi < bb->count) {
7758 /* 'hi' points to the first range that starts after 's'.
7759 * Maybe we can merge with the start of that range */
7760 sector_t a = BB_OFFSET(p[hi]);
7761 sector_t e = a + BB_LEN(p[hi]);
7762 int ack = BB_ACK(p[hi]);
7763 if (a <= s + sectors) {
7764 /* merging is possible */
7765 if (e <= s + sectors) {
7766 /* full overlap */
7767 e = s + sectors;
7768 ack = acknowledged;
7769 } else
7770 ack = ack && acknowledged;
7771
7772 a = s;
7773 if (e - a <= BB_MAX_LEN) {
7774 p[hi] = BB_MAKE(a, e-a, ack);
7775 s = e;
7776 } else {
7777 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
7778 s = a + BB_MAX_LEN;
7779 }
7780 sectors = e - s;
7781 lo = hi;
7782 hi++;
7783 }
7784 }
7785 if (sectors == 0 && hi < bb->count) {
7786 /* we might be able to combine lo and hi */
7787 /* Note: 's' is at the end of 'lo' */
7788 sector_t a = BB_OFFSET(p[hi]);
7789 int lolen = BB_LEN(p[lo]);
7790 int hilen = BB_LEN(p[hi]);
7791 int newlen = lolen + hilen - (s - a);
7792 if (s >= a && newlen < BB_MAX_LEN) {
7793 /* yes, we can combine them */
7794 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
7795 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
7796 memmove(p + hi, p + hi + 1,
7797 (bb->count - hi - 1) * 8);
7798 bb->count--;
7799 }
7800 }
7801 while (sectors) {
7802 /* didn't merge (it all).
7803 * Need to add a range just before 'hi' */
7804 if (bb->count >= MD_MAX_BADBLOCKS) {
7805 /* No room for more */
7806 rv = 0;
7807 break;
7808 } else {
7809 int this_sectors = sectors;
7810 memmove(p + hi + 1, p + hi,
7811 (bb->count - hi) * 8);
7812 bb->count++;
7813
7814 if (this_sectors > BB_MAX_LEN)
7815 this_sectors = BB_MAX_LEN;
7816 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
7817 sectors -= this_sectors;
7818 s += this_sectors;
7819 }
7820 }
7821
7822 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10007823 if (!acknowledged)
7824 bb->unacked_exist = 1;
NeilBrown2230dfe2011-07-28 11:31:46 +10007825 write_sequnlock_irq(&bb->lock);
7826
7827 return rv;
7828}
7829
NeilBrown3cb03002011-10-11 16:45:26 +11007830int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrown2230dfe2011-07-28 11:31:46 +10007831 int acknowledged)
7832{
7833 int rv = md_set_badblocks(&rdev->badblocks,
7834 s + rdev->data_offset, sectors, acknowledged);
7835 if (rv) {
7836 /* Make sure they get written out promptly */
7837 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
7838 md_wakeup_thread(rdev->mddev->thread);
7839 }
7840 return rv;
7841}
7842EXPORT_SYMBOL_GPL(rdev_set_badblocks);
7843
7844/*
7845 * Remove a range of bad blocks from the table.
7846 * This may involve extending the table if we spilt a region,
7847 * but it must not fail. So if the table becomes full, we just
7848 * drop the remove request.
7849 */
7850static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
7851{
7852 u64 *p;
7853 int lo, hi;
7854 sector_t target = s + sectors;
7855 int rv = 0;
7856
7857 if (bb->shift > 0) {
7858 /* When clearing we round the start up and the end down.
7859 * This should not matter as the shift should align with
7860 * the block size and no rounding should ever be needed.
7861 * However it is better the think a block is bad when it
7862 * isn't than to think a block is not bad when it is.
7863 */
7864 s += (1<<bb->shift) - 1;
7865 s >>= bb->shift;
7866 target >>= bb->shift;
7867 sectors = target - s;
7868 }
7869
7870 write_seqlock_irq(&bb->lock);
7871
7872 p = bb->page;
7873 lo = 0;
7874 hi = bb->count;
7875 /* Find the last range that starts before 'target' */
7876 while (hi - lo > 1) {
7877 int mid = (lo + hi) / 2;
7878 sector_t a = BB_OFFSET(p[mid]);
7879 if (a < target)
7880 lo = mid;
7881 else
7882 hi = mid;
7883 }
7884 if (hi > lo) {
7885 /* p[lo] is the last range that could overlap the
7886 * current range. Earlier ranges could also overlap,
7887 * but only this one can overlap the end of the range.
7888 */
7889 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
7890 /* Partial overlap, leave the tail of this range */
7891 int ack = BB_ACK(p[lo]);
7892 sector_t a = BB_OFFSET(p[lo]);
7893 sector_t end = a + BB_LEN(p[lo]);
7894
7895 if (a < s) {
7896 /* we need to split this range */
7897 if (bb->count >= MD_MAX_BADBLOCKS) {
7898 rv = 0;
7899 goto out;
7900 }
7901 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
7902 bb->count++;
7903 p[lo] = BB_MAKE(a, s-a, ack);
7904 lo++;
7905 }
7906 p[lo] = BB_MAKE(target, end - target, ack);
7907 /* there is no longer an overlap */
7908 hi = lo;
7909 lo--;
7910 }
7911 while (lo >= 0 &&
7912 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7913 /* This range does overlap */
7914 if (BB_OFFSET(p[lo]) < s) {
7915 /* Keep the early parts of this range. */
7916 int ack = BB_ACK(p[lo]);
7917 sector_t start = BB_OFFSET(p[lo]);
7918 p[lo] = BB_MAKE(start, s - start, ack);
7919 /* now low doesn't overlap, so.. */
7920 break;
7921 }
7922 lo--;
7923 }
7924 /* 'lo' is strictly before, 'hi' is strictly after,
7925 * anything between needs to be discarded
7926 */
7927 if (hi - lo > 1) {
7928 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
7929 bb->count -= (hi - lo - 1);
7930 }
7931 }
7932
7933 bb->changed = 1;
7934out:
7935 write_sequnlock_irq(&bb->lock);
7936 return rv;
7937}
7938
NeilBrown3cb03002011-10-11 16:45:26 +11007939int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors)
NeilBrown2230dfe2011-07-28 11:31:46 +10007940{
7941 return md_clear_badblocks(&rdev->badblocks,
7942 s + rdev->data_offset,
7943 sectors);
7944}
7945EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
7946
7947/*
7948 * Acknowledge all bad blocks in a list.
7949 * This only succeeds if ->changed is clear. It is used by
7950 * in-kernel metadata updates
7951 */
7952void md_ack_all_badblocks(struct badblocks *bb)
7953{
7954 if (bb->page == NULL || bb->changed)
7955 /* no point even trying */
7956 return;
7957 write_seqlock_irq(&bb->lock);
7958
7959 if (bb->changed == 0) {
7960 u64 *p = bb->page;
7961 int i;
7962 for (i = 0; i < bb->count ; i++) {
7963 if (!BB_ACK(p[i])) {
7964 sector_t start = BB_OFFSET(p[i]);
7965 int len = BB_LEN(p[i]);
7966 p[i] = BB_MAKE(start, len, 1);
7967 }
7968 }
NeilBrownde393cd2011-07-28 11:31:48 +10007969 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10007970 }
7971 write_sequnlock_irq(&bb->lock);
7972}
7973EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
7974
NeilBrown16c791a2011-07-28 11:31:47 +10007975/* sysfs access to bad-blocks list.
7976 * We present two files.
7977 * 'bad-blocks' lists sector numbers and lengths of ranges that
7978 * are recorded as bad. The list is truncated to fit within
7979 * the one-page limit of sysfs.
7980 * Writing "sector length" to this file adds an acknowledged
7981 * bad block list.
7982 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
7983 * been acknowledged. Writing to this file adds bad blocks
7984 * without acknowledging them. This is largely for testing.
7985 */
7986
7987static ssize_t
7988badblocks_show(struct badblocks *bb, char *page, int unack)
7989{
7990 size_t len;
7991 int i;
7992 u64 *p = bb->page;
7993 unsigned seq;
7994
7995 if (bb->shift < 0)
7996 return 0;
7997
7998retry:
7999 seq = read_seqbegin(&bb->lock);
8000
8001 len = 0;
8002 i = 0;
8003
8004 while (len < PAGE_SIZE && i < bb->count) {
8005 sector_t s = BB_OFFSET(p[i]);
8006 unsigned int length = BB_LEN(p[i]);
8007 int ack = BB_ACK(p[i]);
8008 i++;
8009
8010 if (unack && ack)
8011 continue;
8012
8013 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8014 (unsigned long long)s << bb->shift,
8015 length << bb->shift);
8016 }
NeilBrownde393cd2011-07-28 11:31:48 +10008017 if (unack && len == 0)
8018 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008019
8020 if (read_seqretry(&bb->lock, seq))
8021 goto retry;
8022
8023 return len;
8024}
8025
8026#define DO_DEBUG 1
8027
8028static ssize_t
8029badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8030{
8031 unsigned long long sector;
8032 int length;
8033 char newline;
8034#ifdef DO_DEBUG
8035 /* Allow clearing via sysfs *only* for testing/debugging.
8036 * Normally only a successful write may clear a badblock
8037 */
8038 int clear = 0;
8039 if (page[0] == '-') {
8040 clear = 1;
8041 page++;
8042 }
8043#endif /* DO_DEBUG */
8044
8045 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8046 case 3:
8047 if (newline != '\n')
8048 return -EINVAL;
8049 case 2:
8050 if (length <= 0)
8051 return -EINVAL;
8052 break;
8053 default:
8054 return -EINVAL;
8055 }
8056
8057#ifdef DO_DEBUG
8058 if (clear) {
8059 md_clear_badblocks(bb, sector, length);
8060 return len;
8061 }
8062#endif /* DO_DEBUG */
8063 if (md_set_badblocks(bb, sector, length, !unack))
8064 return len;
8065 else
8066 return -ENOSPC;
8067}
8068
Adrian Bunk75c96f82005-05-05 16:16:09 -07008069static int md_notify_reboot(struct notifier_block *this,
8070 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008071{
8072 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008073 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008074 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008075
8076 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
8077
8078 printk(KERN_INFO "md: stopping all md devices.\n");
8079
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008080 for_each_mddev(mddev, tmp) {
Neil Brownc71d4882006-05-26 10:39:25 +10008081 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10008082 /* Force a switch to readonly even array
8083 * appears to still be in use. Hence
8084 * the '100'.
8085 */
NeilBrowna4bd82d2010-03-29 13:23:10 +11008086 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10008087 mddev_unlock(mddev);
8088 }
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008089 need_delay = 1;
8090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008091 /*
8092 * certain more exotic SCSI devices are known to be
8093 * volatile wrt too early system reboots. While the
8094 * right place to handle this issue is the given
8095 * driver, we do want to have a safe RAID driver ...
8096 */
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008097 if (need_delay)
8098 mdelay(1000*1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008099 }
8100 return NOTIFY_DONE;
8101}
8102
Adrian Bunk75c96f82005-05-05 16:16:09 -07008103static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008104 .notifier_call = md_notify_reboot,
8105 .next = NULL,
8106 .priority = INT_MAX, /* before any real devices */
8107};
8108
8109static void md_geninit(void)
8110{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008111 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008112
Denis V. Lunevc7705f32008-04-29 01:02:35 -07008113 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008114}
8115
Adrian Bunk75c96f82005-05-05 16:16:09 -07008116static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008117{
Tejun Heoe804ac72010-10-15 15:36:08 +02008118 int ret = -ENOMEM;
8119
Tejun Heoada609e2011-01-25 14:35:54 +01008120 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008121 if (!md_wq)
8122 goto err_wq;
8123
8124 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8125 if (!md_misc_wq)
8126 goto err_misc_wq;
8127
8128 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8129 goto err_md;
8130
8131 if ((ret = register_blkdev(0, "mdp")) < 0)
8132 goto err_mdp;
8133 mdp_major = ret;
8134
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008135 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008136 md_probe, NULL, NULL);
8137 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008138 md_probe, NULL, NULL);
8139
Linus Torvalds1da177e2005-04-16 15:20:36 -07008140 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008141 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008142
8143 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008145
Tejun Heoe804ac72010-10-15 15:36:08 +02008146err_mdp:
8147 unregister_blkdev(MD_MAJOR, "md");
8148err_md:
8149 destroy_workqueue(md_misc_wq);
8150err_misc_wq:
8151 destroy_workqueue(md_wq);
8152err_wq:
8153 return ret;
8154}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008155
8156#ifndef MODULE
8157
8158/*
8159 * Searches all registered partitions for autorun RAID arrays
8160 * at boot time.
8161 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008162
8163static LIST_HEAD(all_detected_devices);
8164struct detected_devices_node {
8165 struct list_head list;
8166 dev_t dev;
8167};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008168
8169void md_autodetect_dev(dev_t dev)
8170{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008171 struct detected_devices_node *node_detected_dev;
8172
8173 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8174 if (node_detected_dev) {
8175 node_detected_dev->dev = dev;
8176 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8177 } else {
8178 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8179 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008181}
8182
8183
8184static void autostart_arrays(int part)
8185{
NeilBrown3cb03002011-10-11 16:45:26 +11008186 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008187 struct detected_devices_node *node_detected_dev;
8188 dev_t dev;
8189 int i_scanned, i_passed;
8190
8191 i_scanned = 0;
8192 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008193
8194 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8195
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008196 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8197 i_scanned++;
8198 node_detected_dev = list_entry(all_detected_devices.next,
8199 struct detected_devices_node, list);
8200 list_del(&node_detected_dev->list);
8201 dev = node_detected_dev->dev;
8202 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008203 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008204 if (IS_ERR(rdev))
8205 continue;
8206
NeilBrownb2d444d2005-11-08 21:39:31 -08008207 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008208 MD_BUG();
8209 continue;
8210 }
NeilBrownd0fae182008-03-04 14:29:31 -08008211 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008212 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008213 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008214 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008215
8216 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8217 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008218
8219 autorun_devices(part);
8220}
8221
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008222#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008223
8224static __exit void md_exit(void)
8225{
NeilBrownfd01b882011-10-11 16:47:53 +11008226 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008227 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008228
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008229 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008230 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008231
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008232 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008233 unregister_blkdev(mdp_major, "mdp");
8234 unregister_reboot_notifier(&md_notifier);
8235 unregister_sysctl_table(raid_table_header);
8236 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008237 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008238 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008239 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008240 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008241 destroy_workqueue(md_misc_wq);
8242 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008243}
8244
Dan Williams685784a2007-07-09 11:56:42 -07008245subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008246module_exit(md_exit)
8247
NeilBrownf91de922005-11-08 21:39:36 -08008248static int get_ro(char *buffer, struct kernel_param *kp)
8249{
8250 return sprintf(buffer, "%d", start_readonly);
8251}
8252static int set_ro(const char *val, struct kernel_param *kp)
8253{
8254 char *e;
8255 int num = simple_strtoul(val, &e, 10);
8256 if (*val && (*e == '\0' || *e == '\n')) {
8257 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008258 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008259 }
8260 return -EINVAL;
8261}
8262
NeilBrown80ca3a42006-07-10 04:44:18 -07008263module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8264module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8e2006-01-06 00:20:15 -08008265
NeilBrownefeb53c2009-01-09 08:31:10 +11008266module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008267
Linus Torvalds1da177e2005-04-16 15:20:36 -07008268EXPORT_SYMBOL(register_md_personality);
8269EXPORT_SYMBOL(unregister_md_personality);
8270EXPORT_SYMBOL(md_error);
8271EXPORT_SYMBOL(md_done_sync);
8272EXPORT_SYMBOL(md_write_start);
8273EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008274EXPORT_SYMBOL(md_register_thread);
8275EXPORT_SYMBOL(md_unregister_thread);
8276EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008277EXPORT_SYMBOL(md_check_recovery);
8278MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008279MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008280MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008281MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);