blob: 0ea34858637a4e55bea516976de5f7a9a4e6ca90 [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
NeilBrown84fc4b52011-10-11 16:49:58 +1100774static struct md_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800775{
NeilBrown84fc4b52011-10-11 16:49:58 +1100776 struct md_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;
NeilBrownd30519f2011-10-18 12:13:47 +11002725 clear_bit(In_sync, &rdev->flags);
Neil Brown6c2fce22008-06-28 08:31:31 +10002726 err = rdev->mddev->pers->
2727 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002728 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002729 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002730 return err;
Neil Brown52664732008-06-28 08:31:44 +10002731 } else
NeilBrown00bcb4a2010-06-01 19:37:23 +10002732 sysfs_notify_dirent_safe(rdev->sysfs_state);
Namhyung Kim36fad852011-07-27 11:00:36 +10002733 if (sysfs_link_rdev(rdev->mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10002734 /* failure here is OK */;
Neil Brown6c2fce22008-06-28 08:31:31 +10002735 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002736 } else {
NeilBrownba1b41b2011-01-14 09:14:34 +11002737 if (slot >= rdev->mddev->raid_disks &&
2738 slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
NeilBrownc303da62008-02-06 01:39:51 -08002739 return -ENOSPC;
2740 rdev->raid_disk = slot;
2741 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002742 clear_bit(Faulty, &rdev->flags);
2743 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002744 set_bit(In_sync, &rdev->flags);
NeilBrown00bcb4a2010-06-01 19:37:23 +10002745 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002746 }
NeilBrown014236d2006-01-06 00:20:55 -08002747 return len;
2748}
2749
2750
2751static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002752__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002753
NeilBrown93c8cad2006-01-06 00:20:56 -08002754static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002755offset_show(struct md_rdev *rdev, char *page)
NeilBrown93c8cad2006-01-06 00:20:56 -08002756{
Andrew Morton6961ece2006-01-06 00:20:59 -08002757 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002758}
2759
2760static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002761offset_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown93c8cad2006-01-06 00:20:56 -08002762{
2763 char *e;
2764 unsigned long long offset = simple_strtoull(buf, &e, 10);
2765 if (e==buf || (*e && *e != '\n'))
2766 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002767 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002768 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002769 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002770 /* Must set offset before size, so overlap checks
2771 * can be sane */
2772 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002773 rdev->data_offset = offset;
2774 return len;
2775}
2776
2777static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002778__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002779
NeilBrown83303b62006-01-06 00:21:06 -08002780static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002781rdev_size_show(struct md_rdev *rdev, char *page)
NeilBrown83303b62006-01-06 00:21:06 -08002782{
Andre Nolldd8ac332009-03-31 14:33:13 +11002783 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002784}
2785
NeilBrownc5d79ad2008-02-06 01:39:54 -08002786static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2787{
2788 /* check if two start/length pairs overlap */
2789 if (s1+l1 <= s2)
2790 return 0;
2791 if (s2+l2 <= s1)
2792 return 0;
2793 return 1;
2794}
2795
Dan Williamsb522adc2009-03-31 15:00:31 +11002796static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2797{
2798 unsigned long long blocks;
2799 sector_t new;
2800
2801 if (strict_strtoull(buf, 10, &blocks) < 0)
2802 return -EINVAL;
2803
2804 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2805 return -EINVAL; /* sector conversion overflow */
2806
2807 new = blocks * 2;
2808 if (new != blocks * 2)
2809 return -EINVAL; /* unsigned long long to sector_t overflow */
2810
2811 *sectors = new;
2812 return 0;
2813}
2814
NeilBrown83303b62006-01-06 00:21:06 -08002815static ssize_t
NeilBrown3cb03002011-10-11 16:45:26 +11002816rdev_size_store(struct md_rdev *rdev, const char *buf, size_t len)
NeilBrown83303b62006-01-06 00:21:06 -08002817{
NeilBrownfd01b882011-10-11 16:47:53 +11002818 struct mddev *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002819 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002820 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002821
Dan Williamsb522adc2009-03-31 15:00:31 +11002822 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002823 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002824 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002825 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002826 sectors = super_types[my_mddev->major_version].
2827 rdev_size_change(rdev, sectors);
2828 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002829 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002830 } else if (!sectors)
Mike Snitzer77304d22010-11-08 14:39:12 +01002831 sectors = (i_size_read(rdev->bdev->bd_inode) >> 9) -
Andre Nolldd8ac332009-03-31 14:33:13 +11002832 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002833 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002834 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002835 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002836
Andre Nolldd8ac332009-03-31 14:33:13 +11002837 rdev->sectors = sectors;
2838 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002839 /* need to check that all other rdevs with the same ->bdev
2840 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002841 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002842 * we have to change it back, we will have the lock again.
2843 */
NeilBrownfd01b882011-10-11 16:47:53 +11002844 struct mddev *mddev;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002845 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002846 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002847
NeilBrown27c529b2008-03-04 14:29:33 -08002848 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002849 for_each_mddev(mddev, tmp) {
NeilBrown3cb03002011-10-11 16:45:26 +11002850 struct md_rdev *rdev2;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002851
2852 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002853 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownf21e9ff2011-01-31 12:10:09 +11002854 if (rdev->bdev == rdev2->bdev &&
2855 rdev != rdev2 &&
2856 overlaps(rdev->data_offset, rdev->sectors,
2857 rdev2->data_offset,
2858 rdev2->sectors)) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002859 overlap = 1;
2860 break;
2861 }
2862 mddev_unlock(mddev);
2863 if (overlap) {
2864 mddev_put(mddev);
2865 break;
2866 }
2867 }
NeilBrown27c529b2008-03-04 14:29:33 -08002868 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002869 if (overlap) {
2870 /* Someone else could have slipped in a size
2871 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002872 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002873 * safe, and trust userspace not to race with
2874 * itself
2875 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002876 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002877 return -EBUSY;
2878 }
2879 }
NeilBrown83303b62006-01-06 00:21:06 -08002880 return len;
2881}
2882
2883static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002884__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002885
Dan Williams06e3c812009-12-12 21:17:12 -07002886
NeilBrown3cb03002011-10-11 16:45:26 +11002887static ssize_t recovery_start_show(struct md_rdev *rdev, char *page)
Dan Williams06e3c812009-12-12 21:17:12 -07002888{
2889 unsigned long long recovery_start = rdev->recovery_offset;
2890
2891 if (test_bit(In_sync, &rdev->flags) ||
2892 recovery_start == MaxSector)
2893 return sprintf(page, "none\n");
2894
2895 return sprintf(page, "%llu\n", recovery_start);
2896}
2897
NeilBrown3cb03002011-10-11 16:45:26 +11002898static ssize_t recovery_start_store(struct md_rdev *rdev, const char *buf, size_t len)
Dan Williams06e3c812009-12-12 21:17:12 -07002899{
2900 unsigned long long recovery_start;
2901
2902 if (cmd_match(buf, "none"))
2903 recovery_start = MaxSector;
2904 else if (strict_strtoull(buf, 10, &recovery_start))
2905 return -EINVAL;
2906
2907 if (rdev->mddev->pers &&
2908 rdev->raid_disk >= 0)
2909 return -EBUSY;
2910
2911 rdev->recovery_offset = recovery_start;
2912 if (recovery_start == MaxSector)
2913 set_bit(In_sync, &rdev->flags);
2914 else
2915 clear_bit(In_sync, &rdev->flags);
2916 return len;
2917}
2918
2919static struct rdev_sysfs_entry rdev_recovery_start =
2920__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2921
NeilBrown16c791a2011-07-28 11:31:47 +10002922
2923static ssize_t
2924badblocks_show(struct badblocks *bb, char *page, int unack);
2925static ssize_t
2926badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack);
2927
NeilBrown3cb03002011-10-11 16:45:26 +11002928static ssize_t bb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002929{
2930 return badblocks_show(&rdev->badblocks, page, 0);
2931}
NeilBrown3cb03002011-10-11 16:45:26 +11002932static ssize_t bb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002933{
NeilBrownde393cd2011-07-28 11:31:48 +10002934 int rv = badblocks_store(&rdev->badblocks, page, len, 0);
2935 /* Maybe that ack was all we needed */
2936 if (test_and_clear_bit(BlockedBadBlocks, &rdev->flags))
2937 wake_up(&rdev->blocked_wait);
2938 return rv;
NeilBrown16c791a2011-07-28 11:31:47 +10002939}
2940static struct rdev_sysfs_entry rdev_bad_blocks =
2941__ATTR(bad_blocks, S_IRUGO|S_IWUSR, bb_show, bb_store);
2942
2943
NeilBrown3cb03002011-10-11 16:45:26 +11002944static ssize_t ubb_show(struct md_rdev *rdev, char *page)
NeilBrown16c791a2011-07-28 11:31:47 +10002945{
2946 return badblocks_show(&rdev->badblocks, page, 1);
2947}
NeilBrown3cb03002011-10-11 16:45:26 +11002948static ssize_t ubb_store(struct md_rdev *rdev, const char *page, size_t len)
NeilBrown16c791a2011-07-28 11:31:47 +10002949{
2950 return badblocks_store(&rdev->badblocks, page, len, 1);
2951}
2952static struct rdev_sysfs_entry rdev_unack_bad_blocks =
2953__ATTR(unacknowledged_bad_blocks, S_IRUGO|S_IWUSR, ubb_show, ubb_store);
2954
NeilBrown86e6ffd2005-11-08 21:39:24 -08002955static struct attribute *rdev_default_attrs[] = {
2956 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002957 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002958 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002959 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002960 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002961 &rdev_recovery_start.attr,
NeilBrown16c791a2011-07-28 11:31:47 +10002962 &rdev_bad_blocks.attr,
2963 &rdev_unack_bad_blocks.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002964 NULL,
2965};
2966static ssize_t
2967rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2968{
2969 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11002970 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrownfd01b882011-10-11 16:47:53 +11002971 struct mddev *mddev = rdev->mddev;
NeilBrown27c529b2008-03-04 14:29:33 -08002972 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002973
2974 if (!entry->show)
2975 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002976
2977 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2978 if (!rv) {
2979 if (rdev->mddev == NULL)
2980 rv = -EBUSY;
2981 else
2982 rv = entry->show(rdev, page);
2983 mddev_unlock(mddev);
2984 }
2985 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002986}
2987
2988static ssize_t
2989rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2990 const char *page, size_t length)
2991{
2992 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
NeilBrown3cb03002011-10-11 16:45:26 +11002993 struct md_rdev *rdev = container_of(kobj, struct md_rdev, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002994 ssize_t rv;
NeilBrownfd01b882011-10-11 16:47:53 +11002995 struct mddev *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002996
2997 if (!entry->store)
2998 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002999 if (!capable(CAP_SYS_ADMIN))
3000 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08003001 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08003002 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08003003 if (rdev->mddev == NULL)
3004 rv = -EBUSY;
3005 else
3006 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07003007 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08003008 }
3009 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003010}
3011
3012static void rdev_free(struct kobject *ko)
3013{
NeilBrown3cb03002011-10-11 16:45:26 +11003014 struct md_rdev *rdev = container_of(ko, struct md_rdev, kobj);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003015 kfree(rdev);
3016}
Emese Revfy52cf25d2010-01-19 02:58:23 +01003017static const struct sysfs_ops rdev_sysfs_ops = {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003018 .show = rdev_attr_show,
3019 .store = rdev_attr_store,
3020};
3021static struct kobj_type rdev_ktype = {
3022 .release = rdev_free,
3023 .sysfs_ops = &rdev_sysfs_ops,
3024 .default_attrs = rdev_default_attrs,
3025};
3026
NeilBrown3cb03002011-10-11 16:45:26 +11003027int md_rdev_init(struct md_rdev *rdev)
NeilBrowne8bb9a82010-06-01 19:37:26 +10003028{
3029 rdev->desc_nr = -1;
3030 rdev->saved_raid_disk = -1;
3031 rdev->raid_disk = -1;
3032 rdev->flags = 0;
3033 rdev->data_offset = 0;
3034 rdev->sb_events = 0;
3035 rdev->last_read_error.tv_sec = 0;
3036 rdev->last_read_error.tv_nsec = 0;
NeilBrown2699b672011-07-28 11:31:47 +10003037 rdev->sb_loaded = 0;
3038 rdev->bb_page = NULL;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003039 atomic_set(&rdev->nr_pending, 0);
3040 atomic_set(&rdev->read_errors, 0);
3041 atomic_set(&rdev->corrected_errors, 0);
3042
3043 INIT_LIST_HEAD(&rdev->same_set);
3044 init_waitqueue_head(&rdev->blocked_wait);
NeilBrown2230dfe2011-07-28 11:31:46 +10003045
3046 /* Add space to store bad block list.
3047 * This reserves the space even on arrays where it cannot
3048 * be used - I wonder if that matters
3049 */
3050 rdev->badblocks.count = 0;
3051 rdev->badblocks.shift = 0;
3052 rdev->badblocks.page = kmalloc(PAGE_SIZE, GFP_KERNEL);
3053 seqlock_init(&rdev->badblocks.lock);
3054 if (rdev->badblocks.page == NULL)
3055 return -ENOMEM;
3056
3057 return 0;
NeilBrowne8bb9a82010-06-01 19:37:26 +10003058}
3059EXPORT_SYMBOL_GPL(md_rdev_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060/*
3061 * Import a device. If 'super_format' >= 0, then sanity check the superblock
3062 *
3063 * mark the device faulty if:
3064 *
3065 * - the device is nonexistent (zero size)
3066 * - the device has no valid superblock
3067 *
3068 * a faulty rdev _never_ has rdev->sb set.
3069 */
NeilBrown3cb03002011-10-11 16:45:26 +11003070static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071{
3072 char b[BDEVNAME_SIZE];
3073 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11003074 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 sector_t size;
3076
NeilBrown9ffae0c2006-01-06 00:20:32 -08003077 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 if (!rdev) {
3079 printk(KERN_ERR "md: could not alloc mem for new device!\n");
3080 return ERR_PTR(-ENOMEM);
3081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
NeilBrown2230dfe2011-07-28 11:31:46 +10003083 err = md_rdev_init(rdev);
3084 if (err)
3085 goto abort_free;
3086 err = alloc_disk_sb(rdev);
3087 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 goto abort_free;
3089
NeilBrownc5d79ad2008-02-06 01:39:54 -08003090 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 if (err)
3092 goto abort_free;
3093
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07003094 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08003095
Mike Snitzer77304d22010-11-08 14:39:12 +01003096 size = i_size_read(rdev->bdev->bd_inode) >> BLOCK_SIZE_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 if (!size) {
3098 printk(KERN_WARNING
3099 "md: %s has zero or unknown size, marking faulty!\n",
3100 bdevname(rdev->bdev,b));
3101 err = -EINVAL;
3102 goto abort_free;
3103 }
3104
3105 if (super_format >= 0) {
3106 err = super_types[super_format].
3107 load_super(rdev, NULL, super_minor);
3108 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07003109 printk(KERN_WARNING
3110 "md: %s does not have a valid v%d.%d "
3111 "superblock, not importing!\n",
3112 bdevname(rdev->bdev,b),
3113 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 goto abort_free;
3115 }
3116 if (err < 0) {
3117 printk(KERN_WARNING
3118 "md: could not read %s's sb, not importing!\n",
3119 bdevname(rdev->bdev,b));
3120 goto abort_free;
3121 }
3122 }
NeilBrown9f2f383072011-07-28 11:31:47 +10003123 if (super_format == -1)
3124 /* hot-add for 0.90, or non-persistent: so no badblocks */
3125 rdev->badblocks.shift = -1;
Dan Williams6bfe0b42008-04-30 00:52:32 -07003126
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 return rdev;
3128
3129abort_free:
NeilBrown2699b672011-07-28 11:31:47 +10003130 if (rdev->bdev)
3131 unlock_rdev(rdev);
3132 free_disk_sb(rdev);
NeilBrown2230dfe2011-07-28 11:31:46 +10003133 kfree(rdev->badblocks.page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 kfree(rdev);
3135 return ERR_PTR(err);
3136}
3137
3138/*
3139 * Check a full RAID array for plausibility
3140 */
3141
3142
NeilBrownfd01b882011-10-11 16:47:53 +11003143static void analyze_sbs(struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144{
3145 int i;
NeilBrown3cb03002011-10-11 16:45:26 +11003146 struct md_rdev *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 char b[BDEVNAME_SIZE];
3148
3149 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08003150 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 switch (super_types[mddev->major_version].
3152 load_super(rdev, freshest, mddev->minor_version)) {
3153 case 1:
3154 freshest = rdev;
3155 break;
3156 case 0:
3157 break;
3158 default:
3159 printk( KERN_ERR \
3160 "md: fatal superblock inconsistency in %s"
3161 " -- removing from array\n",
3162 bdevname(rdev->bdev,b));
3163 kick_rdev_from_array(rdev);
3164 }
3165
3166
3167 super_types[mddev->major_version].
3168 validate_super(mddev, freshest);
3169
3170 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003171 rdev_for_each(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10003172 if (mddev->max_disks &&
3173 (rdev->desc_nr >= mddev->max_disks ||
3174 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11003175 printk(KERN_WARNING
3176 "md: %s: %s: only %d devices permitted\n",
3177 mdname(mddev), bdevname(rdev->bdev, b),
3178 mddev->max_disks);
3179 kick_rdev_from_array(rdev);
3180 continue;
3181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 if (rdev != freshest)
3183 if (super_types[mddev->major_version].
3184 validate_super(mddev, rdev)) {
3185 printk(KERN_WARNING "md: kicking non-fresh %s"
3186 " from array!\n",
3187 bdevname(rdev->bdev,b));
3188 kick_rdev_from_array(rdev);
3189 continue;
3190 }
3191 if (mddev->level == LEVEL_MULTIPATH) {
3192 rdev->desc_nr = i++;
3193 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08003194 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11003195 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07003196 rdev->raid_disk = -1;
3197 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 }
3199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200}
3201
NeilBrown72e02072009-12-14 12:49:55 +11003202/* Read a fixed-point number.
3203 * Numbers in sysfs attributes should be in "standard" units where
3204 * possible, so time should be in seconds.
3205 * However we internally use a a much smaller unit such as
3206 * milliseconds or jiffies.
3207 * This function takes a decimal number with a possible fractional
3208 * component, and produces an integer which is the result of
3209 * multiplying that number by 10^'scale'.
3210 * all without any floating-point arithmetic.
3211 */
3212int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
3213{
3214 unsigned long result = 0;
3215 long decimals = -1;
3216 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
3217 if (*cp == '.')
3218 decimals = 0;
3219 else if (decimals < scale) {
3220 unsigned int value;
3221 value = *cp - '0';
3222 result = result * 10 + value;
3223 if (decimals >= 0)
3224 decimals++;
3225 }
3226 cp++;
3227 }
3228 if (*cp == '\n')
3229 cp++;
3230 if (*cp)
3231 return -EINVAL;
3232 if (decimals < 0)
3233 decimals = 0;
3234 while (decimals < scale) {
3235 result *= 10;
3236 decimals ++;
3237 }
3238 *res = result;
3239 return 0;
3240}
3241
3242
NeilBrown19052c02008-08-05 15:54:13 +10003243static void md_safemode_timeout(unsigned long data);
3244
NeilBrowneae17012005-11-08 21:39:23 -08003245static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003246safe_delay_show(struct mddev *mddev, char *page)
NeilBrown16f17b32006-06-26 00:27:37 -07003247{
3248 int msec = (mddev->safemode_delay*1000)/HZ;
3249 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
3250}
3251static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003252safe_delay_store(struct mddev *mddev, const char *cbuf, size_t len)
NeilBrown16f17b32006-06-26 00:27:37 -07003253{
NeilBrown16f17b32006-06-26 00:27:37 -07003254 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07003255
NeilBrown72e02072009-12-14 12:49:55 +11003256 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07003257 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07003258 if (msec == 0)
3259 mddev->safemode_delay = 0;
3260 else {
NeilBrown19052c02008-08-05 15:54:13 +10003261 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07003262 mddev->safemode_delay = (msec*HZ)/1000;
3263 if (mddev->safemode_delay == 0)
3264 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10003265 if (mddev->safemode_delay < old_delay)
3266 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07003267 }
3268 return len;
3269}
3270static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07003271__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07003272
3273static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003274level_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003275{
NeilBrown84fc4b52011-10-11 16:49:58 +11003276 struct md_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08003277 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08003278 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08003279 else if (mddev->clevel[0])
3280 return sprintf(page, "%s\n", mddev->clevel);
3281 else if (mddev->level != LEVEL_NONE)
3282 return sprintf(page, "%d\n", mddev->level);
3283 else
3284 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08003285}
3286
NeilBrownd9d166c2006-01-06 00:20:51 -08003287static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003288level_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd9d166c2006-01-06 00:20:51 -08003289{
Dan Williamsf2859af2010-05-02 10:04:16 -07003290 char clevel[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08003291 ssize_t rv = len;
NeilBrown84fc4b52011-10-11 16:49:58 +11003292 struct md_personality *pers;
Dan Williamsf2859af2010-05-02 10:04:16 -07003293 long level;
NeilBrown245f46c2009-03-31 14:39:39 +11003294 void *priv;
NeilBrown3cb03002011-10-11 16:45:26 +11003295 struct md_rdev *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11003296
3297 if (mddev->pers == NULL) {
3298 if (len == 0)
3299 return 0;
3300 if (len >= sizeof(mddev->clevel))
3301 return -ENOSPC;
3302 strncpy(mddev->clevel, buf, len);
3303 if (mddev->clevel[len-1] == '\n')
3304 len--;
3305 mddev->clevel[len] = 0;
3306 mddev->level = LEVEL_NONE;
3307 return rv;
3308 }
3309
3310 /* request to change the personality. Need to ensure:
3311 * - array is not engaged in resync/recovery/reshape
3312 * - old personality can be suspended
3313 * - new personality will access other array.
3314 */
3315
NeilBrownbb4f1e92010-08-08 21:18:03 +10003316 if (mddev->sync_thread ||
3317 mddev->reshape_position != MaxSector ||
3318 mddev->sysfs_active)
NeilBrownd9d166c2006-01-06 00:20:51 -08003319 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11003320
3321 if (!mddev->pers->quiesce) {
3322 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
3323 mdname(mddev), mddev->pers->name);
3324 return -EINVAL;
3325 }
3326
3327 /* Now find the new personality */
Dan Williamsf2859af2010-05-02 10:04:16 -07003328 if (len == 0 || len >= sizeof(clevel))
NeilBrown245f46c2009-03-31 14:39:39 +11003329 return -EINVAL;
Dan Williamsf2859af2010-05-02 10:04:16 -07003330 strncpy(clevel, buf, len);
3331 if (clevel[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08003332 len--;
Dan Williamsf2859af2010-05-02 10:04:16 -07003333 clevel[len] = 0;
3334 if (strict_strtol(clevel, 10, &level))
3335 level = LEVEL_NONE;
NeilBrown245f46c2009-03-31 14:39:39 +11003336
Dan Williamsf2859af2010-05-02 10:04:16 -07003337 if (request_module("md-%s", clevel) != 0)
3338 request_module("md-level-%s", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003339 spin_lock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003340 pers = find_pers(level, clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003341 if (!pers || !try_module_get(pers->owner)) {
3342 spin_unlock(&pers_lock);
Dan Williamsf2859af2010-05-02 10:04:16 -07003343 printk(KERN_WARNING "md: personality %s not loaded\n", clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003344 return -EINVAL;
3345 }
3346 spin_unlock(&pers_lock);
3347
3348 if (pers == mddev->pers) {
3349 /* Nothing to do! */
3350 module_put(pers->owner);
3351 return rv;
3352 }
3353 if (!pers->takeover) {
3354 module_put(pers->owner);
3355 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003356 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003357 return -EINVAL;
3358 }
3359
NeilBrowne93f68a2010-06-15 09:36:03 +01003360 list_for_each_entry(rdev, &mddev->disks, same_set)
3361 rdev->new_raid_disk = rdev->raid_disk;
3362
NeilBrown245f46c2009-03-31 14:39:39 +11003363 /* ->takeover must set new_* and/or delta_disks
3364 * if it succeeds, and may set them when it fails.
3365 */
3366 priv = pers->takeover(mddev);
3367 if (IS_ERR(priv)) {
3368 mddev->new_level = mddev->level;
3369 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003370 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003371 mddev->raid_disks -= mddev->delta_disks;
3372 mddev->delta_disks = 0;
3373 module_put(pers->owner);
3374 printk(KERN_WARNING "md: %s: %s would not accept array\n",
Dan Williamsf2859af2010-05-02 10:04:16 -07003375 mdname(mddev), clevel);
NeilBrown245f46c2009-03-31 14:39:39 +11003376 return PTR_ERR(priv);
3377 }
3378
3379 /* Looks like we have a winner */
3380 mddev_suspend(mddev);
3381 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003382
3383 if (mddev->pers->sync_request == NULL &&
3384 pers->sync_request != NULL) {
3385 /* need to add the md_redundancy_group */
3386 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3387 printk(KERN_WARNING
3388 "md: cannot register extra attributes for %s\n",
3389 mdname(mddev));
NeilBrown19fdb9e2010-05-22 08:31:36 +10003390 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, NULL, "sync_action");
NeilBrowna64c8762010-04-14 17:15:37 +10003391 }
3392 if (mddev->pers->sync_request != NULL &&
3393 pers->sync_request == NULL) {
3394 /* need to remove the md_redundancy_group */
3395 if (mddev->to_remove == NULL)
3396 mddev->to_remove = &md_redundancy_group;
3397 }
3398
Trela Maciej54071b32010-03-08 16:02:42 +11003399 if (mddev->pers->sync_request == NULL &&
3400 mddev->external) {
3401 /* We are converting from a no-redundancy array
3402 * to a redundancy array and metadata is managed
3403 * externally so we need to be sure that writes
3404 * won't block due to a need to transition
3405 * clean->dirty
3406 * until external management is started.
3407 */
3408 mddev->in_sync = 0;
3409 mddev->safemode_delay = 0;
3410 mddev->safemode = 0;
3411 }
3412
NeilBrowne93f68a2010-06-15 09:36:03 +01003413 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowne93f68a2010-06-15 09:36:03 +01003414 if (rdev->raid_disk < 0)
3415 continue;
NeilBrownbf2cb0d2011-01-14 09:14:34 +11003416 if (rdev->new_raid_disk >= mddev->raid_disks)
NeilBrowne93f68a2010-06-15 09:36:03 +01003417 rdev->new_raid_disk = -1;
3418 if (rdev->new_raid_disk == rdev->raid_disk)
3419 continue;
Namhyung Kim36fad852011-07-27 11:00:36 +10003420 sysfs_unlink_rdev(mddev, rdev);
NeilBrowne93f68a2010-06-15 09:36:03 +01003421 }
3422 list_for_each_entry(rdev, &mddev->disks, same_set) {
3423 if (rdev->raid_disk < 0)
3424 continue;
3425 if (rdev->new_raid_disk == rdev->raid_disk)
3426 continue;
3427 rdev->raid_disk = rdev->new_raid_disk;
3428 if (rdev->raid_disk < 0)
NeilBrown3a981b02009-08-03 10:59:55 +10003429 clear_bit(In_sync, &rdev->flags);
NeilBrowne93f68a2010-06-15 09:36:03 +01003430 else {
Namhyung Kim36fad852011-07-27 11:00:36 +10003431 if (sysfs_link_rdev(mddev, rdev))
3432 printk(KERN_WARNING "md: cannot register rd%d"
3433 " for %s after level change\n",
3434 rdev->raid_disk, mdname(mddev));
NeilBrown3a981b02009-08-03 10:59:55 +10003435 }
NeilBrowne93f68a2010-06-15 09:36:03 +01003436 }
3437
3438 module_put(mddev->pers->owner);
NeilBrown245f46c2009-03-31 14:39:39 +11003439 mddev->pers = pers;
3440 mddev->private = priv;
3441 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3442 mddev->level = mddev->new_level;
3443 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003444 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003445 mddev->delta_disks = 0;
Krzysztof Wojcikfee68722011-04-20 15:39:53 +10003446 mddev->degraded = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003447 if (mddev->pers->sync_request == NULL) {
3448 /* this is now an array without redundancy, so
3449 * it must always be in_sync
3450 */
3451 mddev->in_sync = 1;
3452 del_timer_sync(&mddev->safemode_timer);
3453 }
NeilBrown245f46c2009-03-31 14:39:39 +11003454 pers->run(mddev);
3455 mddev_resume(mddev);
3456 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3457 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3458 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003459 sysfs_notify(&mddev->kobj, NULL, "level");
Dan Williamsbb7f8d22010-05-01 18:14:57 -07003460 md_new_event(mddev);
NeilBrownd9d166c2006-01-06 00:20:51 -08003461 return rv;
3462}
3463
3464static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003465__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003466
NeilBrownd4dbd022006-06-26 00:27:59 -07003467
3468static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003469layout_show(struct mddev *mddev, char *page)
NeilBrownd4dbd022006-06-26 00:27:59 -07003470{
3471 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003472 if (mddev->reshape_position != MaxSector &&
3473 mddev->layout != mddev->new_layout)
3474 return sprintf(page, "%d (%d)\n",
3475 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003476 return sprintf(page, "%d\n", mddev->layout);
3477}
3478
3479static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003480layout_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownd4dbd022006-06-26 00:27:59 -07003481{
3482 char *e;
3483 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003484
3485 if (!*buf || (*e && *e != '\n'))
3486 return -EINVAL;
3487
NeilBrownb3546032009-03-31 14:56:41 +11003488 if (mddev->pers) {
3489 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003490 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003491 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003492 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003493 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003494 if (err) {
3495 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003496 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003497 }
NeilBrownb3546032009-03-31 14:56:41 +11003498 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003499 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003500 if (mddev->reshape_position == MaxSector)
3501 mddev->layout = n;
3502 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003503 return len;
3504}
3505static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003506__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003507
3508
NeilBrowneae17012005-11-08 21:39:23 -08003509static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003510raid_disks_show(struct mddev *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003511{
NeilBrownbb636542005-11-08 21:39:45 -08003512 if (mddev->raid_disks == 0)
3513 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003514 if (mddev->reshape_position != MaxSector &&
3515 mddev->delta_disks != 0)
3516 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3517 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003518 return sprintf(page, "%d\n", mddev->raid_disks);
3519}
3520
NeilBrownfd01b882011-10-11 16:47:53 +11003521static int update_raid_disks(struct mddev *mddev, int raid_disks);
NeilBrownda943b92006-01-06 00:20:54 -08003522
3523static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003524raid_disks_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownda943b92006-01-06 00:20:54 -08003525{
NeilBrownda943b92006-01-06 00:20:54 -08003526 char *e;
3527 int rv = 0;
3528 unsigned long n = simple_strtoul(buf, &e, 10);
3529
3530 if (!*buf || (*e && *e != '\n'))
3531 return -EINVAL;
3532
3533 if (mddev->pers)
3534 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003535 else if (mddev->reshape_position != MaxSector) {
3536 int olddisks = mddev->raid_disks - mddev->delta_disks;
3537 mddev->delta_disks = n - olddisks;
3538 mddev->raid_disks = n;
3539 } else
NeilBrownda943b92006-01-06 00:20:54 -08003540 mddev->raid_disks = n;
3541 return rv ? rv : len;
3542}
3543static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003544__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003545
NeilBrown24dd4692005-11-08 21:39:26 -08003546static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003547chunk_size_show(struct mddev *mddev, char *page)
NeilBrown3b343802006-01-06 00:20:47 -08003548{
NeilBrown08a02ec2007-05-09 02:35:38 -07003549 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003550 mddev->chunk_sectors != mddev->new_chunk_sectors)
3551 return sprintf(page, "%d (%d)\n",
3552 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003553 mddev->chunk_sectors << 9);
3554 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003555}
3556
3557static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003558chunk_size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown3b343802006-01-06 00:20:47 -08003559{
NeilBrown3b343802006-01-06 00:20:47 -08003560 char *e;
3561 unsigned long n = simple_strtoul(buf, &e, 10);
3562
NeilBrown3b343802006-01-06 00:20:47 -08003563 if (!*buf || (*e && *e != '\n'))
3564 return -EINVAL;
3565
NeilBrownb3546032009-03-31 14:56:41 +11003566 if (mddev->pers) {
3567 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003568 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003569 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003570 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003571 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003572 if (err) {
3573 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003574 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003575 }
NeilBrownb3546032009-03-31 14:56:41 +11003576 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003577 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003578 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003579 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003580 }
NeilBrown3b343802006-01-06 00:20:47 -08003581 return len;
3582}
3583static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003584__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003585
NeilBrowna94213b2006-06-26 00:28:00 -07003586static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003587resync_start_show(struct mddev *mddev, char *page)
NeilBrowna94213b2006-06-26 00:28:00 -07003588{
NeilBrownd1a7c502009-03-31 15:24:32 +11003589 if (mddev->recovery_cp == MaxSector)
3590 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003591 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3592}
3593
3594static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003595resync_start_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna94213b2006-06-26 00:28:00 -07003596{
NeilBrowna94213b2006-06-26 00:28:00 -07003597 char *e;
3598 unsigned long long n = simple_strtoull(buf, &e, 10);
3599
NeilBrownb0986362011-05-11 15:52:21 +10003600 if (mddev->pers && !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
NeilBrowna94213b2006-06-26 00:28:00 -07003601 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003602 if (cmd_match(buf, "none"))
3603 n = MaxSector;
3604 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003605 return -EINVAL;
3606
3607 mddev->recovery_cp = n;
3608 return len;
3609}
3610static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003611__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003612
NeilBrown9e653b62006-06-26 00:27:58 -07003613/*
3614 * The array state can be:
3615 *
3616 * clear
3617 * No devices, no size, no level
3618 * Equivalent to STOP_ARRAY ioctl
3619 * inactive
3620 * May have some settings, but array is not active
3621 * all IO results in error
3622 * When written, doesn't tear down array, but just stops it
3623 * suspended (not supported yet)
3624 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003625 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003626 * readonly
3627 * no resync can happen. no superblocks get written.
3628 * write requests fail
3629 * read-auto
3630 * like readonly, but behaves like 'clean' on a write request.
3631 *
3632 * clean - no pending writes, but otherwise active.
3633 * When written to inactive array, starts without resync
3634 * If a write request arrives then
3635 * if metadata is known, mark 'dirty' and switch to 'active'.
3636 * if not known, block and switch to write-pending
3637 * If written to an active array that has pending writes, then fails.
3638 * active
3639 * fully active: IO and resync can be happening.
3640 * When written to inactive array, starts with resync
3641 *
3642 * write-pending
3643 * clean, but writes are blocked waiting for 'active' to be written.
3644 *
3645 * active-idle
3646 * like active, but no writes have been seen for a while (100msec).
3647 *
3648 */
3649enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3650 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003651static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003652 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3653 "write-pending", "active-idle", NULL };
3654
3655static int match_word(const char *word, char **list)
3656{
3657 int n;
3658 for (n=0; list[n]; n++)
3659 if (cmd_match(word, list[n]))
3660 break;
3661 return n;
3662}
3663
3664static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003665array_state_show(struct mddev *mddev, char *page)
NeilBrown9e653b62006-06-26 00:27:58 -07003666{
3667 enum array_state st = inactive;
3668
3669 if (mddev->pers)
3670 switch(mddev->ro) {
3671 case 1:
3672 st = readonly;
3673 break;
3674 case 2:
3675 st = read_auto;
3676 break;
3677 case 0:
3678 if (mddev->in_sync)
3679 st = clean;
NeilBrown070dc6d2010-08-30 17:33:34 +10003680 else if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
NeilBrowne6910632008-02-06 01:39:51 -08003681 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003682 else if (mddev->safemode)
3683 st = active_idle;
3684 else
3685 st = active;
3686 }
3687 else {
3688 if (list_empty(&mddev->disks) &&
3689 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003690 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003691 st = clear;
3692 else
3693 st = inactive;
3694 }
3695 return sprintf(page, "%s\n", array_states[st]);
3696}
3697
NeilBrownfd01b882011-10-11 16:47:53 +11003698static int do_md_stop(struct mddev * mddev, int ro, int is_open);
3699static int md_set_readonly(struct mddev * mddev, int is_open);
3700static int do_md_run(struct mddev * mddev);
3701static int restart_array(struct mddev *mddev);
NeilBrown9e653b62006-06-26 00:27:58 -07003702
3703static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003704array_state_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown9e653b62006-06-26 00:27:58 -07003705{
3706 int err = -EINVAL;
3707 enum array_state st = match_word(buf, array_states);
3708 switch(st) {
3709 case bad_word:
3710 break;
3711 case clear:
3712 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003713 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003714 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003715 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003716 break;
3717 case inactive:
3718 /* stopping an active array */
3719 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003720 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003721 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003722 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003723 } else
3724 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003725 break;
3726 case suspended:
3727 break; /* not supported yet */
3728 case readonly:
3729 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003730 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003731 else {
3732 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003733 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003734 err = do_md_run(mddev);
3735 }
3736 break;
3737 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003738 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003739 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003740 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003741 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003742 err = restart_array(mddev);
3743 if (err == 0) {
3744 mddev->ro = 2;
3745 set_disk_ro(mddev->gendisk, 0);
3746 }
NeilBrown9e653b62006-06-26 00:27:58 -07003747 } else {
3748 mddev->ro = 2;
3749 err = do_md_run(mddev);
3750 }
3751 break;
3752 case clean:
3753 if (mddev->pers) {
3754 restart_array(mddev);
3755 spin_lock_irq(&mddev->write_lock);
3756 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003757 if (mddev->in_sync == 0) {
3758 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003759 if (mddev->safemode == 1)
3760 mddev->safemode = 0;
NeilBrown070dc6d2010-08-30 17:33:34 +10003761 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrowne6910632008-02-06 01:39:51 -08003762 }
3763 err = 0;
3764 } else
3765 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003766 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003767 } else
3768 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003769 break;
3770 case active:
3771 if (mddev->pers) {
3772 restart_array(mddev);
NeilBrown070dc6d2010-08-30 17:33:34 +10003773 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003774 wake_up(&mddev->sb_wait);
3775 err = 0;
3776 } else {
3777 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003778 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003779 err = do_md_run(mddev);
3780 }
3781 break;
3782 case write_pending:
3783 case active_idle:
3784 /* these cannot be set */
3785 break;
3786 }
3787 if (err)
3788 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003789 else {
NeilBrown00bcb4a2010-06-01 19:37:23 +10003790 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003791 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003792 }
NeilBrown9e653b62006-06-26 00:27:58 -07003793}
NeilBrown80ca3a42006-07-10 04:44:18 -07003794static struct md_sysfs_entry md_array_state =
3795__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003796
NeilBrown6d7ff732006-01-06 00:21:16 -08003797static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003798max_corrected_read_errors_show(struct mddev *mddev, char *page) {
Robert Becker1e509152009-12-14 12:49:58 +11003799 return sprintf(page, "%d\n",
3800 atomic_read(&mddev->max_corr_read_errors));
3801}
3802
3803static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003804max_corrected_read_errors_store(struct mddev *mddev, const char *buf, size_t len)
Robert Becker1e509152009-12-14 12:49:58 +11003805{
3806 char *e;
3807 unsigned long n = simple_strtoul(buf, &e, 10);
3808
3809 if (*buf && (*e == 0 || *e == '\n')) {
3810 atomic_set(&mddev->max_corr_read_errors, n);
3811 return len;
3812 }
3813 return -EINVAL;
3814}
3815
3816static struct md_sysfs_entry max_corr_read_errors =
3817__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3818 max_corrected_read_errors_store);
3819
3820static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003821null_show(struct mddev *mddev, char *page)
NeilBrown6d7ff732006-01-06 00:21:16 -08003822{
3823 return -EINVAL;
3824}
3825
3826static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003827new_dev_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown6d7ff732006-01-06 00:21:16 -08003828{
3829 /* buf must be %d:%d\n? giving major and minor numbers */
3830 /* The new device is added to the array.
3831 * If the array has a persistent superblock, we read the
3832 * superblock to initialise info and check validity.
3833 * Otherwise, only checking done is that in bind_rdev_to_array,
3834 * which mainly checks size.
3835 */
3836 char *e;
3837 int major = simple_strtoul(buf, &e, 10);
3838 int minor;
3839 dev_t dev;
NeilBrown3cb03002011-10-11 16:45:26 +11003840 struct md_rdev *rdev;
NeilBrown6d7ff732006-01-06 00:21:16 -08003841 int err;
3842
3843 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3844 return -EINVAL;
3845 minor = simple_strtoul(e+1, &e, 10);
3846 if (*e && *e != '\n')
3847 return -EINVAL;
3848 dev = MKDEV(major, minor);
3849 if (major != MAJOR(dev) ||
3850 minor != MINOR(dev))
3851 return -EOVERFLOW;
3852
3853
3854 if (mddev->persistent) {
3855 rdev = md_import_device(dev, mddev->major_version,
3856 mddev->minor_version);
3857 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003858 struct md_rdev *rdev0
3859 = list_entry(mddev->disks.next,
3860 struct md_rdev, same_set);
NeilBrown6d7ff732006-01-06 00:21:16 -08003861 err = super_types[mddev->major_version]
3862 .load_super(rdev, rdev0, mddev->minor_version);
3863 if (err < 0)
3864 goto out;
3865 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003866 } else if (mddev->external)
3867 rdev = md_import_device(dev, -2, -1);
3868 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003869 rdev = md_import_device(dev, -1, -1);
3870
3871 if (IS_ERR(rdev))
3872 return PTR_ERR(rdev);
3873 err = bind_rdev_to_array(rdev, mddev);
3874 out:
3875 if (err)
3876 export_rdev(rdev);
3877 return err ? err : len;
3878}
3879
3880static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003881__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003882
3883static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003884bitmap_store(struct mddev *mddev, const char *buf, size_t len)
Paul Clements9b1d1da2006-10-03 01:15:49 -07003885{
3886 char *end;
3887 unsigned long chunk, end_chunk;
3888
3889 if (!mddev->bitmap)
3890 goto out;
3891 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3892 while (*buf) {
3893 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3894 if (buf == end) break;
3895 if (*end == '-') { /* range */
3896 buf = end + 1;
3897 end_chunk = simple_strtoul(buf, &end, 0);
3898 if (buf == end) break;
3899 }
3900 if (*end && !isspace(*end)) break;
3901 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003902 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003903 }
3904 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3905out:
3906 return len;
3907}
3908
3909static struct md_sysfs_entry md_bitmap =
3910__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3911
3912static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003913size_show(struct mddev *mddev, char *page)
NeilBrowna35b0d62006-01-06 00:20:49 -08003914{
Andre Noll58c0fed2009-03-31 14:33:13 +11003915 return sprintf(page, "%llu\n",
3916 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003917}
3918
NeilBrownfd01b882011-10-11 16:47:53 +11003919static int update_size(struct mddev *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003920
3921static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003922size_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowna35b0d62006-01-06 00:20:49 -08003923{
3924 /* If array is inactive, we can reduce the component size, but
3925 * not increase it (except from 0).
3926 * If array is active, we can try an on-line resize
3927 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003928 sector_t sectors;
3929 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003930
Andre Noll58c0fed2009-03-31 14:33:13 +11003931 if (err < 0)
3932 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003933 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003934 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003935 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003936 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003937 if (mddev->dev_sectors == 0 ||
3938 mddev->dev_sectors > sectors)
3939 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003940 else
3941 err = -ENOSPC;
3942 }
3943 return err ? err : len;
3944}
3945
3946static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003947__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003948
NeilBrown8bb93aa2006-01-06 00:20:50 -08003949
3950/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003951 * This is one of
3952 * 'none' for arrays with no metadata (good luck...)
3953 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003954 * or N.M for internally known formats
3955 */
3956static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003957metadata_show(struct mddev *mddev, char *page)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003958{
3959 if (mddev->persistent)
3960 return sprintf(page, "%d.%d\n",
3961 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003962 else if (mddev->external)
3963 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003964 else
3965 return sprintf(page, "none\n");
3966}
3967
3968static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11003969metadata_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003970{
3971 int major, minor;
3972 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003973 /* Changing the details of 'external' metadata is
3974 * always permitted. Otherwise there must be
3975 * no devices attached to the array.
3976 */
3977 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3978 ;
3979 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003980 return -EBUSY;
3981
3982 if (cmd_match(buf, "none")) {
3983 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003984 mddev->external = 0;
3985 mddev->major_version = 0;
3986 mddev->minor_version = 90;
3987 return len;
3988 }
3989 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003990 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003991 if (namelen >= sizeof(mddev->metadata_type))
3992 namelen = sizeof(mddev->metadata_type)-1;
3993 strncpy(mddev->metadata_type, buf+9, namelen);
3994 mddev->metadata_type[namelen] = 0;
3995 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3996 mddev->metadata_type[--namelen] = 0;
3997 mddev->persistent = 0;
3998 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003999 mddev->major_version = 0;
4000 mddev->minor_version = 90;
4001 return len;
4002 }
4003 major = simple_strtoul(buf, &e, 10);
4004 if (e==buf || *e != '.')
4005 return -EINVAL;
4006 buf = e+1;
4007 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08004008 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08004009 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004010 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08004011 return -ENOENT;
4012 mddev->major_version = major;
4013 mddev->minor_version = minor;
4014 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08004015 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08004016 return len;
4017}
4018
4019static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07004020__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08004021
NeilBrowna35b0d62006-01-06 00:20:49 -08004022static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004023action_show(struct mddev *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08004024{
NeilBrown7eec3142005-11-08 21:39:44 -08004025 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10004026 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
4027 type = "frozen";
4028 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07004029 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08004030 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4031 type = "reshape";
4032 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08004033 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4034 type = "resync";
4035 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
4036 type = "check";
4037 else
4038 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10004039 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004040 type = "recover";
4041 }
4042 return sprintf(page, "%s\n", type);
4043}
4044
NeilBrownfd01b882011-10-11 16:47:53 +11004045static void reap_sync_thread(struct mddev *mddev);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004046
NeilBrown24dd4692005-11-08 21:39:26 -08004047static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004048action_store(struct mddev *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08004049{
NeilBrown7eec3142005-11-08 21:39:44 -08004050 if (!mddev->pers || !mddev->pers->sync_request)
4051 return -EINVAL;
4052
NeilBrownb6a9ce62009-05-26 09:41:17 +10004053 if (cmd_match(page, "frozen"))
4054 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4055 else
4056 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4057
4058 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08004059 if (mddev->sync_thread) {
4060 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11004061 reap_sync_thread(mddev);
NeilBrown7eec3142005-11-08 21:39:44 -08004062 }
NeilBrown03c902e2006-01-06 00:20:46 -08004063 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
4064 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08004065 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10004066 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08004067 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10004068 else if (cmd_match(page, "recover")) {
4069 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
4070 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4071 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08004072 int err;
4073 if (mddev->pers->start_reshape == NULL)
4074 return -EINVAL;
4075 err = mddev->pers->start_reshape(mddev);
4076 if (err)
4077 return err;
Neil Browna99ac972008-06-28 08:31:43 +10004078 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08004079 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08004080 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08004081 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07004082 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08004083 return -EINVAL;
4084 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
4085 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08004086 }
NeilBrown03c902e2006-01-06 00:20:46 -08004087 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08004088 md_wakeup_thread(mddev->thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004089 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08004090 return len;
4091}
4092
NeilBrown9d888832005-11-08 21:39:26 -08004093static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004094mismatch_cnt_show(struct mddev *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08004095{
4096 return sprintf(page, "%llu\n",
4097 (unsigned long long) mddev->resync_mismatches);
4098}
4099
NeilBrown80ca3a42006-07-10 04:44:18 -07004100static struct md_sysfs_entry md_scan_mode =
4101__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08004102
NeilBrown96de1e62005-11-08 21:39:39 -08004103
NeilBrown80ca3a42006-07-10 04:44:18 -07004104static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08004105
NeilBrown88202a02006-01-06 00:21:36 -08004106static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004107sync_min_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004108{
4109 return sprintf(page, "%d (%s)\n", speed_min(mddev),
4110 mddev->sync_speed_min ? "local": "system");
4111}
4112
4113static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004114sync_min_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004115{
4116 int min;
4117 char *e;
4118 if (strncmp(buf, "system", 6)==0) {
4119 mddev->sync_speed_min = 0;
4120 return len;
4121 }
4122 min = simple_strtoul(buf, &e, 10);
4123 if (buf == e || (*e && *e != '\n') || min <= 0)
4124 return -EINVAL;
4125 mddev->sync_speed_min = min;
4126 return len;
4127}
4128
4129static struct md_sysfs_entry md_sync_min =
4130__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
4131
4132static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004133sync_max_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004134{
4135 return sprintf(page, "%d (%s)\n", speed_max(mddev),
4136 mddev->sync_speed_max ? "local": "system");
4137}
4138
4139static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004140sync_max_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown88202a02006-01-06 00:21:36 -08004141{
4142 int max;
4143 char *e;
4144 if (strncmp(buf, "system", 6)==0) {
4145 mddev->sync_speed_max = 0;
4146 return len;
4147 }
4148 max = simple_strtoul(buf, &e, 10);
4149 if (buf == e || (*e && *e != '\n') || max <= 0)
4150 return -EINVAL;
4151 mddev->sync_speed_max = max;
4152 return len;
4153}
4154
4155static struct md_sysfs_entry md_sync_max =
4156__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
4157
Iustin Popd7f3d292007-10-16 23:30:54 -07004158static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004159degraded_show(struct mddev *mddev, char *page)
Iustin Popd7f3d292007-10-16 23:30:54 -07004160{
4161 return sprintf(page, "%d\n", mddev->degraded);
4162}
4163static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08004164
4165static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004166sync_force_parallel_show(struct mddev *mddev, char *page)
Bernd Schubert90b08712008-05-23 13:04:38 -07004167{
4168 return sprintf(page, "%d\n", mddev->parallel_resync);
4169}
4170
4171static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004172sync_force_parallel_store(struct mddev *mddev, const char *buf, size_t len)
Bernd Schubert90b08712008-05-23 13:04:38 -07004173{
4174 long n;
4175
4176 if (strict_strtol(buf, 10, &n))
4177 return -EINVAL;
4178
4179 if (n != 0 && n != 1)
4180 return -EINVAL;
4181
4182 mddev->parallel_resync = n;
4183
4184 if (mddev->sync_thread)
4185 wake_up(&resync_wait);
4186
4187 return len;
4188}
4189
4190/* force parallel resync, even with shared block devices */
4191static struct md_sysfs_entry md_sync_force_parallel =
4192__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
4193 sync_force_parallel_show, sync_force_parallel_store);
4194
4195static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004196sync_speed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004197{
4198 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11004199 if (mddev->curr_resync == 0)
4200 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01004201 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
4202 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08004203 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01004204 db = resync - mddev->resync_mark_cnt;
4205 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08004206}
4207
NeilBrown80ca3a42006-07-10 04:44:18 -07004208static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08004209
4210static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004211sync_completed_show(struct mddev *mddev, char *page)
NeilBrown88202a02006-01-06 00:21:36 -08004212{
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004213 unsigned long long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08004214
NeilBrownacb180b2009-04-14 16:28:34 +10004215 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4216 return sprintf(page, "none\n");
4217
NeilBrown88202a02006-01-06 00:21:36 -08004218 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11004219 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004220 else
Andre Noll58c0fed2009-03-31 14:33:13 +11004221 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08004222
NeilBrownacb180b2009-04-14 16:28:34 +10004223 resync = mddev->curr_resync_completed;
RĂ©mi RĂ©rolle13ae8642011-01-14 09:14:34 +11004224 return sprintf(page, "%llu / %llu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08004225}
4226
NeilBrown80ca3a42006-07-10 04:44:18 -07004227static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08004228
NeilBrowne464eaf2006-03-27 01:18:14 -08004229static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004230min_sync_show(struct mddev *mddev, char *page)
Neil Brown5e96ee62008-06-28 08:31:24 +10004231{
4232 return sprintf(page, "%llu\n",
4233 (unsigned long long)mddev->resync_min);
4234}
4235static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004236min_sync_store(struct mddev *mddev, const char *buf, size_t len)
Neil Brown5e96ee62008-06-28 08:31:24 +10004237{
4238 unsigned long long min;
4239 if (strict_strtoull(buf, 10, &min))
4240 return -EINVAL;
4241 if (min > mddev->resync_max)
4242 return -EINVAL;
4243 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4244 return -EBUSY;
4245
4246 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004247 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004248 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10004249 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10004250 return -EINVAL;
4251 }
4252 mddev->resync_min = min;
4253
4254 return len;
4255}
4256
4257static struct md_sysfs_entry md_min_sync =
4258__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
4259
4260static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004261max_sync_show(struct mddev *mddev, char *page)
NeilBrownc6207272008-02-06 01:39:52 -08004262{
4263 if (mddev->resync_max == MaxSector)
4264 return sprintf(page, "max\n");
4265 else
4266 return sprintf(page, "%llu\n",
4267 (unsigned long long)mddev->resync_max);
4268}
4269static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004270max_sync_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrownc6207272008-02-06 01:39:52 -08004271{
4272 if (strncmp(buf, "max", 3) == 0)
4273 mddev->resync_max = MaxSector;
4274 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10004275 unsigned long long max;
4276 if (strict_strtoull(buf, 10, &max))
4277 return -EINVAL;
4278 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08004279 return -EINVAL;
4280 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10004281 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08004282 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4283 return -EBUSY;
4284
4285 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10004286 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10004287 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10004288 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08004289 return -EINVAL;
4290 }
4291 mddev->resync_max = max;
4292 }
4293 wake_up(&mddev->recovery_wait);
4294 return len;
4295}
4296
4297static struct md_sysfs_entry md_max_sync =
4298__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
4299
4300static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004301suspend_lo_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004302{
4303 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
4304}
4305
4306static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004307suspend_lo_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004308{
4309 char *e;
4310 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004311 unsigned long long old = mddev->suspend_lo;
NeilBrowne464eaf2006-03-27 01:18:14 -08004312
NeilBrownb8d966e2009-07-01 11:14:04 +10004313 if (mddev->pers == NULL ||
4314 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004315 return -EINVAL;
4316 if (buf == e || (*e && *e != '\n'))
4317 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004318
4319 mddev->suspend_lo = new;
4320 if (new >= old)
4321 /* Shrinking suspended region */
NeilBrowne464eaf2006-03-27 01:18:14 -08004322 mddev->pers->quiesce(mddev, 2);
NeilBrown23ddff32011-01-14 09:14:34 +11004323 else {
4324 /* Expanding suspended region - need to wait */
4325 mddev->pers->quiesce(mddev, 1);
4326 mddev->pers->quiesce(mddev, 0);
4327 }
4328 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004329}
4330static struct md_sysfs_entry md_suspend_lo =
4331__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
4332
4333
4334static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004335suspend_hi_show(struct mddev *mddev, char *page)
NeilBrowne464eaf2006-03-27 01:18:14 -08004336{
4337 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
4338}
4339
4340static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004341suspend_hi_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrowne464eaf2006-03-27 01:18:14 -08004342{
4343 char *e;
4344 unsigned long long new = simple_strtoull(buf, &e, 10);
NeilBrown23ddff32011-01-14 09:14:34 +11004345 unsigned long long old = mddev->suspend_hi;
NeilBrowne464eaf2006-03-27 01:18:14 -08004346
NeilBrownb8d966e2009-07-01 11:14:04 +10004347 if (mddev->pers == NULL ||
4348 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08004349 return -EINVAL;
4350 if (buf == e || (*e && *e != '\n'))
4351 return -EINVAL;
NeilBrown23ddff32011-01-14 09:14:34 +11004352
4353 mddev->suspend_hi = new;
4354 if (new <= old)
4355 /* Shrinking suspended region */
4356 mddev->pers->quiesce(mddev, 2);
4357 else {
4358 /* Expanding suspended region - need to wait */
NeilBrowne464eaf2006-03-27 01:18:14 -08004359 mddev->pers->quiesce(mddev, 1);
4360 mddev->pers->quiesce(mddev, 0);
NeilBrown23ddff32011-01-14 09:14:34 +11004361 }
4362 return len;
NeilBrowne464eaf2006-03-27 01:18:14 -08004363}
4364static struct md_sysfs_entry md_suspend_hi =
4365__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
4366
NeilBrown08a02ec2007-05-09 02:35:38 -07004367static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004368reshape_position_show(struct mddev *mddev, char *page)
NeilBrown08a02ec2007-05-09 02:35:38 -07004369{
4370 if (mddev->reshape_position != MaxSector)
4371 return sprintf(page, "%llu\n",
4372 (unsigned long long)mddev->reshape_position);
4373 strcpy(page, "none\n");
4374 return 5;
4375}
4376
4377static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004378reshape_position_store(struct mddev *mddev, const char *buf, size_t len)
NeilBrown08a02ec2007-05-09 02:35:38 -07004379{
4380 char *e;
4381 unsigned long long new = simple_strtoull(buf, &e, 10);
4382 if (mddev->pers)
4383 return -EBUSY;
4384 if (buf == e || (*e && *e != '\n'))
4385 return -EINVAL;
4386 mddev->reshape_position = new;
4387 mddev->delta_disks = 0;
4388 mddev->new_level = mddev->level;
4389 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10004390 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07004391 return len;
4392}
4393
4394static struct md_sysfs_entry md_reshape_position =
4395__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4396 reshape_position_store);
4397
Dan Williamsb522adc2009-03-31 15:00:31 +11004398static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004399array_size_show(struct mddev *mddev, char *page)
Dan Williamsb522adc2009-03-31 15:00:31 +11004400{
4401 if (mddev->external_size)
4402 return sprintf(page, "%llu\n",
4403 (unsigned long long)mddev->array_sectors/2);
4404 else
4405 return sprintf(page, "default\n");
4406}
4407
4408static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11004409array_size_store(struct mddev *mddev, const char *buf, size_t len)
Dan Williamsb522adc2009-03-31 15:00:31 +11004410{
4411 sector_t sectors;
4412
4413 if (strncmp(buf, "default", 7) == 0) {
4414 if (mddev->pers)
4415 sectors = mddev->pers->size(mddev, 0, 0);
4416 else
4417 sectors = mddev->array_sectors;
4418
4419 mddev->external_size = 0;
4420 } else {
4421 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4422 return -EINVAL;
4423 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004424 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004425
4426 mddev->external_size = 1;
4427 }
4428
4429 mddev->array_sectors = sectors;
NeilBrowncbe6ef12011-02-16 13:58:38 +11004430 if (mddev->pers) {
4431 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004432 revalidate_disk(mddev->gendisk);
NeilBrowncbe6ef12011-02-16 13:58:38 +11004433 }
Dan Williamsb522adc2009-03-31 15:00:31 +11004434 return len;
4435}
4436
4437static struct md_sysfs_entry md_array_size =
4438__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4439 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004440
NeilBrowneae17012005-11-08 21:39:23 -08004441static struct attribute *md_default_attrs[] = {
4442 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004443 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004444 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004445 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004446 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004447 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004448 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004449 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004450 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004451 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004452 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004453 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004454 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004455 NULL,
4456};
4457
4458static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004459 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004460 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004461 &md_sync_min.attr,
4462 &md_sync_max.attr,
4463 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004464 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004465 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004466 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004467 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004468 &md_suspend_lo.attr,
4469 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004470 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004471 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004472 NULL,
4473};
NeilBrown411036f2005-11-08 21:39:40 -08004474static struct attribute_group md_redundancy_group = {
4475 .name = NULL,
4476 .attrs = md_redundancy_attrs,
4477};
4478
NeilBrowneae17012005-11-08 21:39:23 -08004479
4480static ssize_t
4481md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4482{
4483 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004484 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004485 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004486
4487 if (!entry->show)
4488 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004489 rv = mddev_lock(mddev);
4490 if (!rv) {
4491 rv = entry->show(mddev, page);
4492 mddev_unlock(mddev);
4493 }
NeilBrown96de1e62005-11-08 21:39:39 -08004494 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004495}
4496
4497static ssize_t
4498md_attr_store(struct kobject *kobj, struct attribute *attr,
4499 const char *page, size_t length)
4500{
4501 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
NeilBrownfd01b882011-10-11 16:47:53 +11004502 struct mddev *mddev = container_of(kobj, struct mddev, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004503 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004504
4505 if (!entry->store)
4506 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004507 if (!capable(CAP_SYS_ADMIN))
4508 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004509 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004510 if (mddev->hold_active == UNTIL_IOCTL)
4511 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004512 if (!rv) {
4513 rv = entry->store(mddev, page, length);
4514 mddev_unlock(mddev);
4515 }
NeilBrown96de1e62005-11-08 21:39:39 -08004516 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004517}
4518
4519static void md_free(struct kobject *ko)
4520{
NeilBrownfd01b882011-10-11 16:47:53 +11004521 struct mddev *mddev = container_of(ko, struct mddev, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004522
4523 if (mddev->sysfs_state)
4524 sysfs_put(mddev->sysfs_state);
4525
4526 if (mddev->gendisk) {
4527 del_gendisk(mddev->gendisk);
4528 put_disk(mddev->gendisk);
4529 }
4530 if (mddev->queue)
4531 blk_cleanup_queue(mddev->queue);
4532
NeilBrowneae17012005-11-08 21:39:23 -08004533 kfree(mddev);
4534}
4535
Emese Revfy52cf25d2010-01-19 02:58:23 +01004536static const struct sysfs_ops md_sysfs_ops = {
NeilBrowneae17012005-11-08 21:39:23 -08004537 .show = md_attr_show,
4538 .store = md_attr_store,
4539};
4540static struct kobj_type md_ktype = {
4541 .release = md_free,
4542 .sysfs_ops = &md_sysfs_ops,
4543 .default_attrs = md_default_attrs,
4544};
4545
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546int mdp_major = 0;
4547
Dan Williams5fd3a172009-03-04 00:57:25 -07004548static void mddev_delayed_delete(struct work_struct *ws)
4549{
NeilBrownfd01b882011-10-11 16:47:53 +11004550 struct mddev *mddev = container_of(ws, struct mddev, del_work);
Dan Williams5fd3a172009-03-04 00:57:25 -07004551
NeilBrown43a70502009-12-14 12:49:55 +11004552 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004553 kobject_del(&mddev->kobj);
4554 kobject_put(&mddev->kobj);
4555}
4556
NeilBrownefeb53c2009-01-09 08:31:10 +11004557static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004559 static DEFINE_MUTEX(disks_mutex);
NeilBrownfd01b882011-10-11 16:47:53 +11004560 struct mddev *mddev = mddev_find(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004562 int partitioned;
4563 int shift;
4564 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004565 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566
4567 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004568 return -ENODEV;
4569
4570 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4571 shift = partitioned ? MdpMinorShift : 0;
4572 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573
Tejun Heoe804ac72010-10-15 15:36:08 +02004574 /* wait for any previous instance of this device to be
4575 * completely removed (mddev_delayed_delete).
NeilBrownd3374822009-01-09 08:31:10 +11004576 */
Tejun Heoe804ac72010-10-15 15:36:08 +02004577 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11004578
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004579 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004580 error = -EEXIST;
4581 if (mddev->gendisk)
4582 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004583
4584 if (name) {
4585 /* Need to ensure that 'name' is not a duplicate.
4586 */
NeilBrownfd01b882011-10-11 16:47:53 +11004587 struct mddev *mddev2;
NeilBrownefeb53c2009-01-09 08:31:10 +11004588 spin_lock(&all_mddevs_lock);
4589
4590 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4591 if (mddev2->gendisk &&
4592 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4593 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004594 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004595 }
4596 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 }
NeilBrown8b765392009-01-09 08:31:08 +11004598
NeilBrown0909dc42009-07-01 12:27:21 +10004599 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004600 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004601 if (!mddev->queue)
4602 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004603 mddev->queue->queuedata = mddev;
4604
NeilBrown409c57f2009-03-31 14:39:39 +11004605 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004606
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 disk = alloc_disk(1 << shift);
4608 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004609 blk_cleanup_queue(mddev->queue);
4610 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004611 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004613 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004615 if (name)
4616 strcpy(disk->disk_name, name);
4617 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004619 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 disk->fops = &md_fops;
4622 disk->private_data = mddev;
4623 disk->queue = mddev->queue;
NeilBrownb0140892011-05-10 17:49:01 +10004624 blk_queue_flush(mddev->queue, REQ_FLUSH | REQ_FUA);
NeilBrown92850bb2008-10-21 13:25:32 +11004625 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004626 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004627 * remove it now.
4628 */
4629 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 mddev->gendisk = disk;
NeilBrownb0140892011-05-10 17:49:01 +10004631 /* As soon as we call add_disk(), another thread could get
4632 * through to md_open, so make sure it doesn't get too far
4633 */
4634 mutex_lock(&mddev->open_mutex);
4635 add_disk(disk);
4636
Tejun Heoed9e1982008-08-25 19:56:05 +09004637 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4638 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004639 if (error) {
4640 /* This isn't possible, but as kobject_init_and_add is marked
4641 * __must_check, we must do something with the result
4642 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004643 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4644 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004645 error = 0;
4646 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004647 if (mddev->kobj.sd &&
4648 sysfs_create_group(&mddev->kobj, &md_bitmap_group))
NeilBrown43a70502009-12-14 12:49:55 +11004649 printk(KERN_DEBUG "pointless warning\n");
NeilBrownb0140892011-05-10 17:49:01 +10004650 mutex_unlock(&mddev->open_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004651 abort:
4652 mutex_unlock(&disks_mutex);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004653 if (!error && mddev->kobj.sd) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004654 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004655 mddev->sysfs_state = sysfs_get_dirent_safe(mddev->kobj.sd, "array_state");
NeilBrownb62b7592008-10-21 13:25:21 +11004656 }
NeilBrownd3374822009-01-09 08:31:10 +11004657 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004658 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004659}
4660
4661static struct kobject *md_probe(dev_t dev, int *part, void *data)
4662{
4663 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 return NULL;
4665}
4666
NeilBrownefeb53c2009-01-09 08:31:10 +11004667static int add_named_array(const char *val, struct kernel_param *kp)
4668{
4669 /* val must be "md_*" where * is not all digits.
4670 * We allocate an array with a large free minor number, and
4671 * set the name to val. val must not already be an active name.
4672 */
4673 int len = strlen(val);
4674 char buf[DISK_NAME_LEN];
4675
4676 while (len && val[len-1] == '\n')
4677 len--;
4678 if (len >= DISK_NAME_LEN)
4679 return -E2BIG;
4680 strlcpy(buf, val, len+1);
4681 if (strncmp(buf, "md_", 3) != 0)
4682 return -EINVAL;
4683 return md_alloc(0, buf);
4684}
4685
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686static void md_safemode_timeout(unsigned long data)
4687{
NeilBrownfd01b882011-10-11 16:47:53 +11004688 struct mddev *mddev = (struct mddev *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689
Neil Brown0fd62b82008-06-28 08:31:36 +10004690 if (!atomic_read(&mddev->writes_pending)) {
4691 mddev->safemode = 1;
4692 if (mddev->external)
NeilBrown00bcb4a2010-06-01 19:37:23 +10004693 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 md_wakeup_thread(mddev->thread);
4696}
4697
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004698static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699
NeilBrownfd01b882011-10-11 16:47:53 +11004700int md_run(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701{
NeilBrown2604b702006-01-06 00:20:36 -08004702 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11004703 struct md_rdev *rdev;
NeilBrown84fc4b52011-10-11 16:49:58 +11004704 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705
NeilBrowna757e642005-04-16 15:26:42 -07004706 if (list_empty(&mddev->disks))
4707 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709
4710 if (mddev->pers)
4711 return -EBUSY;
NeilBrownbb4f1e92010-08-08 21:18:03 +10004712 /* Cannot run until previous stop completes properly */
4713 if (mddev->sysfs_active)
4714 return -EBUSY;
NeilBrownb6eb1272010-04-15 10:13:47 +10004715
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 /*
4717 * Analyze all RAID superblock(s)
4718 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004719 if (!mddev->raid_disks) {
4720 if (!mddev->persistent)
4721 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004722 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724
NeilBrownd9d166c2006-01-06 00:20:51 -08004725 if (mddev->level != LEVEL_NONE)
4726 request_module("md-level-%d", mddev->level);
4727 else if (mddev->clevel[0])
4728 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729
4730 /*
4731 * Drop all container device buffers, from now on
4732 * the only valid external interface is through the md
4733 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004735 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004736 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 continue;
4738 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004739 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004740
4741 /* perform some consistency tests on the device.
4742 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004743 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004744 */
Jonathan Brassowa6ff7e02011-01-14 09:14:34 +11004745 if (rdev->meta_bdev) {
4746 /* Nothing to check */;
4747 } else if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004748 if (mddev->dev_sectors &&
4749 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004750 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004751 printk("md: %s: data overlaps metadata\n",
4752 mdname(mddev));
4753 return -EINVAL;
4754 }
4755 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004756 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004757 > rdev->data_offset) {
4758 printk("md: %s: metadata overlaps data\n",
4759 mdname(mddev));
4760 return -EINVAL;
4761 }
4762 }
NeilBrown00bcb4a2010-06-01 19:37:23 +10004763 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 }
4765
NeilBrowna167f662010-10-26 18:31:13 +11004766 if (mddev->bio_set == NULL)
NeilBrowna519b262011-07-28 07:56:24 +10004767 mddev->bio_set = bioset_create(BIO_POOL_SIZE,
NeilBrownfd01b882011-10-11 16:47:53 +11004768 sizeof(struct mddev *));
NeilBrowna167f662010-10-26 18:31:13 +11004769
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004771 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004772 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004774 if (mddev->level != LEVEL_NONE)
4775 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4776 mddev->level);
4777 else
4778 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4779 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 return -EINVAL;
4781 }
NeilBrown2604b702006-01-06 00:20:36 -08004782 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004784 if (mddev->level != pers->level) {
4785 mddev->level = pers->level;
4786 mddev->new_level = pers->level;
4787 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004788 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789
NeilBrownf6705572006-03-27 01:18:11 -08004790 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004791 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004792 /* This personality cannot handle reshaping... */
4793 mddev->pers = NULL;
4794 module_put(pers->owner);
4795 return -EINVAL;
4796 }
4797
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004798 if (pers->sync_request) {
4799 /* Warn if this is a potentially silly
4800 * configuration.
4801 */
4802 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11004803 struct md_rdev *rdev2;
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004804 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004805
4806 list_for_each_entry(rdev, &mddev->disks, same_set)
4807 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004808 if (rdev < rdev2 &&
4809 rdev->bdev->bd_contains ==
4810 rdev2->bdev->bd_contains) {
4811 printk(KERN_WARNING
4812 "%s: WARNING: %s appears to be"
4813 " on the same physical disk as"
4814 " %s.\n",
4815 mdname(mddev),
4816 bdevname(rdev->bdev,b),
4817 bdevname(rdev2->bdev,b2));
4818 warned = 1;
4819 }
4820 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004821
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004822 if (warned)
4823 printk(KERN_WARNING
4824 "True protection against single-disk"
4825 " failure might be compromised.\n");
4826 }
4827
NeilBrown657390d2005-08-26 18:34:16 -07004828 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004829 /* may be over-ridden by personality */
4830 mddev->resync_max_sectors = mddev->dev_sectors;
4831
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004832 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
NeilBrown0f9552b2009-12-30 12:08:50 +11004834 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004835 mddev->ro = 2; /* read-only, but switch on first write */
4836
NeilBrownb15c2e52006-01-06 00:20:16 -08004837 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004838 if (err)
4839 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004840 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4841 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4842 " but 'external_size' not in effect?\n", __func__);
4843 printk(KERN_ERR
4844 "md: invalid array_size %llu > default size %llu\n",
4845 (unsigned long long)mddev->array_sectors / 2,
4846 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4847 err = -EINVAL;
4848 mddev->pers->stop(mddev);
4849 }
4850 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004851 err = bitmap_create(mddev);
4852 if (err) {
4853 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4854 mdname(mddev), err);
4855 mddev->pers->stop(mddev);
4856 }
4857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 module_put(mddev->pers->owner);
4860 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004861 bitmap_destroy(mddev);
4862 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004864 if (mddev->pers->sync_request) {
NeilBrown00bcb4a2010-06-01 19:37:23 +10004865 if (mddev->kobj.sd &&
4866 sysfs_create_group(&mddev->kobj, &md_redundancy_group))
NeilBrown5e55e2f2007-03-26 21:32:14 -08004867 printk(KERN_WARNING
4868 "md: cannot register extra attributes for %s\n",
4869 mdname(mddev));
NeilBrown00bcb4a2010-06-01 19:37:23 +10004870 mddev->sysfs_action = sysfs_get_dirent_safe(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004871 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004872 mddev->ro = 0;
4873
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004875 atomic_set(&mddev->max_corr_read_errors,
4876 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877 mddev->safemode = 0;
4878 mddev->safemode_timer.function = md_safemode_timeout;
4879 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004880 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 mddev->in_sync = 1;
NeilBrown0ca69882011-01-14 09:14:33 +11004882 smp_wmb();
4883 mddev->ready = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004884 list_for_each_entry(rdev, &mddev->disks, same_set)
Namhyung Kim36fad852011-07-27 11:00:36 +10004885 if (rdev->raid_disk >= 0)
4886 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10004887 /* failure here is OK */;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888
4889 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4890
NeilBrown850b2b42006-10-03 01:15:46 -07004891 if (mddev->flags)
4892 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893
NeilBrownd7603b72006-01-06 00:20:30 -08004894 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004895 sysfs_notify_dirent_safe(mddev->sysfs_state);
4896 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004897 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 return 0;
4899}
NeilBrown390ee602010-06-01 19:37:27 +10004900EXPORT_SYMBOL_GPL(md_run);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901
NeilBrownfd01b882011-10-11 16:47:53 +11004902static int do_md_run(struct mddev *mddev)
NeilBrownfe60b012010-03-29 11:10:42 +11004903{
4904 int err;
4905
4906 err = md_run(mddev);
4907 if (err)
4908 goto out;
NeilBrown69e51b42010-06-01 19:37:35 +10004909 err = bitmap_load(mddev);
4910 if (err) {
4911 bitmap_destroy(mddev);
4912 goto out;
4913 }
Jonathan Brassow0fd018a2011-06-07 17:49:36 -05004914
4915 md_wakeup_thread(mddev->thread);
4916 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
4917
NeilBrownfe60b012010-03-29 11:10:42 +11004918 set_capacity(mddev->gendisk, mddev->array_sectors);
4919 revalidate_disk(mddev->gendisk);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11004920 mddev->changed = 1;
NeilBrownfe60b012010-03-29 11:10:42 +11004921 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4922out:
4923 return err;
4924}
4925
NeilBrownfd01b882011-10-11 16:47:53 +11004926static int restart_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927{
4928 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929
Andre Noll80fab1d2008-07-11 22:02:21 +10004930 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004932 return -ENXIO;
4933 if (!mddev->pers)
4934 return -EINVAL;
4935 if (!mddev->ro)
4936 return -EBUSY;
4937 mddev->safemode = 0;
4938 mddev->ro = 0;
4939 set_disk_ro(disk, 0);
4940 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4941 mdname(mddev));
4942 /* Kick recovery or resync if necessary */
4943 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4944 md_wakeup_thread(mddev->thread);
4945 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10004946 sysfs_notify_dirent_safe(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004947 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948}
4949
NeilBrownacc55e22006-06-26 00:27:47 -07004950/* similar to deny_write_access, but accounts for our holding a reference
4951 * to the file ourselves */
4952static int deny_bitmap_write_access(struct file * file)
4953{
4954 struct inode *inode = file->f_mapping->host;
4955
4956 spin_lock(&inode->i_lock);
4957 if (atomic_read(&inode->i_writecount) > 1) {
4958 spin_unlock(&inode->i_lock);
4959 return -ETXTBSY;
4960 }
4961 atomic_set(&inode->i_writecount, -1);
4962 spin_unlock(&inode->i_lock);
4963
4964 return 0;
4965}
4966
NeilBrown43a70502009-12-14 12:49:55 +11004967void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004968{
4969 struct inode *inode = file->f_mapping->host;
4970
4971 spin_lock(&inode->i_lock);
4972 atomic_set(&inode->i_writecount, 1);
4973 spin_unlock(&inode->i_lock);
4974}
4975
NeilBrownfd01b882011-10-11 16:47:53 +11004976static void md_clean(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11004977{
4978 mddev->array_sectors = 0;
4979 mddev->external_size = 0;
4980 mddev->dev_sectors = 0;
4981 mddev->raid_disks = 0;
4982 mddev->recovery_cp = 0;
4983 mddev->resync_min = 0;
4984 mddev->resync_max = MaxSector;
4985 mddev->reshape_position = MaxSector;
4986 mddev->external = 0;
4987 mddev->persistent = 0;
4988 mddev->level = LEVEL_NONE;
4989 mddev->clevel[0] = 0;
4990 mddev->flags = 0;
4991 mddev->ro = 0;
4992 mddev->metadata_type[0] = 0;
4993 mddev->chunk_sectors = 0;
4994 mddev->ctime = mddev->utime = 0;
4995 mddev->layout = 0;
4996 mddev->max_disks = 0;
4997 mddev->events = 0;
NeilBrowna8707c02010-05-18 09:28:43 +10004998 mddev->can_decrease_events = 0;
NeilBrown6177b472010-03-29 11:37:13 +11004999 mddev->delta_disks = 0;
5000 mddev->new_level = LEVEL_NONE;
5001 mddev->new_layout = 0;
5002 mddev->new_chunk_sectors = 0;
5003 mddev->curr_resync = 0;
5004 mddev->resync_mismatches = 0;
5005 mddev->suspend_lo = mddev->suspend_hi = 0;
5006 mddev->sync_speed_min = mddev->sync_speed_max = 0;
5007 mddev->recovery = 0;
5008 mddev->in_sync = 0;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005009 mddev->changed = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005010 mddev->degraded = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005011 mddev->safemode = 0;
5012 mddev->bitmap_info.offset = 0;
5013 mddev->bitmap_info.default_offset = 0;
5014 mddev->bitmap_info.chunksize = 0;
5015 mddev->bitmap_info.daemon_sleep = 0;
5016 mddev->bitmap_info.max_write_behind = 0;
5017}
5018
NeilBrownfd01b882011-10-11 16:47:53 +11005019static void __md_stop_writes(struct mddev *mddev)
NeilBrowna047e122010-03-29 12:07:53 +11005020{
5021 if (mddev->sync_thread) {
5022 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
5023 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown7ebc0be2011-01-14 09:14:33 +11005024 reap_sync_thread(mddev);
NeilBrowna047e122010-03-29 12:07:53 +11005025 }
5026
5027 del_timer_sync(&mddev->safemode_timer);
5028
5029 bitmap_flush(mddev);
5030 md_super_wait(mddev);
5031
5032 if (!mddev->in_sync || mddev->flags) {
5033 /* mark array as shutdown cleanly */
5034 mddev->in_sync = 1;
5035 md_update_sb(mddev, 1);
5036 }
5037}
NeilBrowndefad612011-01-14 09:14:33 +11005038
NeilBrownfd01b882011-10-11 16:47:53 +11005039void md_stop_writes(struct mddev *mddev)
NeilBrowndefad612011-01-14 09:14:33 +11005040{
5041 mddev_lock(mddev);
5042 __md_stop_writes(mddev);
5043 mddev_unlock(mddev);
5044}
NeilBrown390ee602010-06-01 19:37:27 +10005045EXPORT_SYMBOL_GPL(md_stop_writes);
NeilBrowna047e122010-03-29 12:07:53 +11005046
NeilBrownfd01b882011-10-11 16:47:53 +11005047void md_stop(struct mddev *mddev)
NeilBrown6177b472010-03-29 11:37:13 +11005048{
NeilBrown0ca69882011-01-14 09:14:33 +11005049 mddev->ready = 0;
NeilBrown6177b472010-03-29 11:37:13 +11005050 mddev->pers->stop(mddev);
5051 if (mddev->pers->sync_request && mddev->to_remove == NULL)
5052 mddev->to_remove = &md_redundancy_group;
5053 module_put(mddev->pers->owner);
5054 mddev->pers = NULL;
NeilBrowncca9cf92010-04-01 12:08:16 +11005055 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown6177b472010-03-29 11:37:13 +11005056}
NeilBrown390ee602010-06-01 19:37:27 +10005057EXPORT_SYMBOL_GPL(md_stop);
NeilBrown6177b472010-03-29 11:37:13 +11005058
NeilBrownfd01b882011-10-11 16:47:53 +11005059static int md_set_readonly(struct mddev *mddev, int is_open)
NeilBrowna4bd82d2010-03-29 13:23:10 +11005060{
5061 int err = 0;
5062 mutex_lock(&mddev->open_mutex);
5063 if (atomic_read(&mddev->openers) > is_open) {
5064 printk("md: %s still in use.\n",mdname(mddev));
5065 err = -EBUSY;
5066 goto out;
5067 }
5068 if (mddev->pers) {
NeilBrowndefad612011-01-14 09:14:33 +11005069 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005070
5071 err = -ENXIO;
5072 if (mddev->ro==1)
5073 goto out;
5074 mddev->ro = 1;
5075 set_disk_ro(mddev->gendisk, 1);
5076 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005077 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005078 err = 0;
5079 }
5080out:
5081 mutex_unlock(&mddev->open_mutex);
5082 return err;
5083}
5084
NeilBrown9e653b62006-06-26 00:27:58 -07005085/* mode:
5086 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07005087 * 2 - stop but do not disassemble array
5088 */
NeilBrownfd01b882011-10-11 16:47:53 +11005089static int do_md_stop(struct mddev * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 struct gendisk *disk = mddev->gendisk;
NeilBrown3cb03002011-10-11 16:45:26 +11005092 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093
NeilBrownc8c00a62009-08-10 12:50:52 +10005094 mutex_lock(&mddev->open_mutex);
NeilBrownbb4f1e92010-08-08 21:18:03 +10005095 if (atomic_read(&mddev->openers) > is_open ||
5096 mddev->sysfs_active) {
Neil Browndf5b20c2008-07-11 22:02:22 +10005097 printk("md: %s still in use.\n",mdname(mddev));
NeilBrown6e17b022010-08-07 21:41:19 +10005098 mutex_unlock(&mddev->open_mutex);
5099 return -EBUSY;
5100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101
NeilBrown6e17b022010-08-07 21:41:19 +10005102 if (mddev->pers) {
NeilBrowna4bd82d2010-03-29 13:23:10 +11005103 if (mddev->ro)
5104 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11005105
NeilBrowndefad612011-01-14 09:14:33 +11005106 __md_stop_writes(mddev);
NeilBrowna4bd82d2010-03-29 13:23:10 +11005107 md_stop(mddev);
5108 mddev->queue->merge_bvec_fn = NULL;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005109 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11005110
NeilBrowna4bd82d2010-03-29 13:23:10 +11005111 /* tell userspace to handle 'inactive' */
NeilBrown00bcb4a2010-06-01 19:37:23 +10005112 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08005113
NeilBrowna4bd82d2010-03-29 13:23:10 +11005114 list_for_each_entry(rdev, &mddev->disks, same_set)
Namhyung Kim36fad852011-07-27 11:00:36 +10005115 if (rdev->raid_disk >= 0)
5116 sysfs_unlink_rdev(mddev, rdev);
NeilBrownc4647292009-05-07 12:51:06 +10005117
NeilBrowna4bd82d2010-03-29 13:23:10 +11005118 set_capacity(disk, 0);
NeilBrown6e17b022010-08-07 21:41:19 +10005119 mutex_unlock(&mddev->open_mutex);
NeilBrownf0b4f7e2011-02-24 17:26:41 +11005120 mddev->changed = 1;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005121 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08005122
NeilBrowna4bd82d2010-03-29 13:23:10 +11005123 if (mddev->ro)
5124 mddev->ro = 0;
NeilBrown6e17b022010-08-07 21:41:19 +10005125 } else
5126 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 /*
5128 * Free resources if final stop
5129 */
NeilBrown9e653b62006-06-26 00:27:58 -07005130 if (mode == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
5132
NeilBrown978f9462006-02-02 14:28:05 -08005133 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11005134 if (mddev->bitmap_info.file) {
5135 restore_bitmap_write_access(mddev->bitmap_info.file);
5136 fput(mddev->bitmap_info.file);
5137 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08005138 }
NeilBrownc3d97142009-12-14 12:49:52 +11005139 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08005140
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 export_array(mddev);
5142
NeilBrown6177b472010-03-29 11:37:13 +11005143 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11005144 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11005145 if (mddev->hold_active == UNTIL_STOP)
5146 mddev->hold_active = 0;
NeilBrowna4bd82d2010-03-29 13:23:10 +11005147 }
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11005148 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08005149 md_new_event(mddev);
NeilBrown00bcb4a2010-06-01 19:37:23 +10005150 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown6e17b022010-08-07 21:41:19 +10005151 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152}
5153
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005154#ifndef MODULE
NeilBrownfd01b882011-10-11 16:47:53 +11005155static void autorun_array(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156{
NeilBrown3cb03002011-10-11 16:45:26 +11005157 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 int err;
5159
NeilBrowna757e642005-04-16 15:26:42 -07005160 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162
5163 printk(KERN_INFO "md: running: ");
5164
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005165 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 char b[BDEVNAME_SIZE];
5167 printk("<%s>", bdevname(rdev->bdev,b));
5168 }
5169 printk("\n");
5170
NeilBrownd710e132008-10-13 11:55:12 +11005171 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 if (err) {
5173 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11005174 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 }
5176}
5177
5178/*
5179 * lets try to run arrays based on all disks that have arrived
5180 * until now. (those are in pending_raid_disks)
5181 *
5182 * the method: pick the first pending disk, collect all disks with
5183 * the same UUID, remove all from the pending list and put them into
5184 * the 'same_array' list. Then order this list based on superblock
5185 * update time (freshest comes first), kick out 'old' disks and
5186 * compare superblocks. If everything's fine then run it.
5187 *
5188 * If "unit" is allocated, then bump its reference count
5189 */
5190static void autorun_devices(int part)
5191{
NeilBrown3cb03002011-10-11 16:45:26 +11005192 struct md_rdev *rdev0, *rdev, *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11005193 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 char b[BDEVNAME_SIZE];
5195
5196 printk(KERN_INFO "md: autorun ...\n");
5197 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07005198 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08005200 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 rdev0 = list_entry(pending_raid_disks.next,
NeilBrown3cb03002011-10-11 16:45:26 +11005202 struct md_rdev, same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203
5204 printk(KERN_INFO "md: considering %s ...\n",
5205 bdevname(rdev0->bdev,b));
5206 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005207 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 if (super_90_load(rdev, rdev0, 0) >= 0) {
5209 printk(KERN_INFO "md: adding %s ...\n",
5210 bdevname(rdev->bdev,b));
5211 list_move(&rdev->same_set, &candidates);
5212 }
5213 /*
5214 * now we have a set of devices, with all of them having
5215 * mostly sane superblocks. It's time to allocate the
5216 * mddev.
5217 */
NeilBrowne8703fe2006-10-03 01:15:59 -07005218 if (part) {
5219 dev = MKDEV(mdp_major,
5220 rdev0->preferred_minor << MdpMinorShift);
5221 unit = MINOR(dev) >> MdpMinorShift;
5222 } else {
5223 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
5224 unit = MINOR(dev);
5225 }
5226 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
5228 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
5229 break;
5230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231
5232 md_probe(dev, NULL, NULL);
5233 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10005234 if (!mddev || !mddev->gendisk) {
5235 if (mddev)
5236 mddev_put(mddev);
5237 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 "md: cannot allocate memory for md drive.\n");
5239 break;
5240 }
5241 if (mddev_lock(mddev))
5242 printk(KERN_WARNING "md: %s locked, cannot run\n",
5243 mdname(mddev));
5244 else if (mddev->raid_disks || mddev->major_version
5245 || !list_empty(&mddev->disks)) {
5246 printk(KERN_WARNING
5247 "md: %s already running, cannot run %s\n",
5248 mdname(mddev), bdevname(rdev0->bdev,b));
5249 mddev_unlock(mddev);
5250 } else {
5251 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08005252 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005253 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 list_del_init(&rdev->same_set);
5255 if (bind_rdev_to_array(rdev, mddev))
5256 export_rdev(rdev);
5257 }
5258 autorun_array(mddev);
5259 mddev_unlock(mddev);
5260 }
5261 /* on success, candidates will be empty, on error
5262 * it won't...
5263 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005264 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10005265 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10005267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 mddev_put(mddev);
5269 }
5270 printk(KERN_INFO "md: ... autorun DONE.\n");
5271}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005272#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274static int get_version(void __user * arg)
5275{
5276 mdu_version_t ver;
5277
5278 ver.major = MD_MAJOR_VERSION;
5279 ver.minor = MD_MINOR_VERSION;
5280 ver.patchlevel = MD_PATCHLEVEL_VERSION;
5281
5282 if (copy_to_user(arg, &ver, sizeof(ver)))
5283 return -EFAULT;
5284
5285 return 0;
5286}
5287
NeilBrownfd01b882011-10-11 16:47:53 +11005288static int get_array_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289{
5290 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10005291 int nr,working,insync,failed,spare;
NeilBrown3cb03002011-10-11 16:45:26 +11005292 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293
NeilBrowna9f326e2009-09-23 18:06:41 +10005294 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005295 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005297 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 failed++;
5299 else {
5300 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08005301 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10005302 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 else
5304 spare++;
5305 }
5306 }
5307
5308 info.major_version = mddev->major_version;
5309 info.minor_version = mddev->minor_version;
5310 info.patch_version = MD_PATCHLEVEL_VERSION;
5311 info.ctime = mddev->ctime;
5312 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11005313 info.size = mddev->dev_sectors / 2;
5314 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08005315 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 info.nr_disks = nr;
5317 info.raid_disks = mddev->raid_disks;
5318 info.md_minor = mddev->md_minor;
5319 info.not_persistent= !mddev->persistent;
5320
5321 info.utime = mddev->utime;
5322 info.state = 0;
5323 if (mddev->in_sync)
5324 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11005325 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005326 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10005327 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 info.working_disks = working;
5329 info.failed_disks = failed;
5330 info.spare_disks = spare;
5331
5332 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005333 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334
5335 if (copy_to_user(arg, &info, sizeof(info)))
5336 return -EFAULT;
5337
5338 return 0;
5339}
5340
NeilBrownfd01b882011-10-11 16:47:53 +11005341static int get_bitmap_file(struct mddev * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07005342{
5343 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
5344 char *ptr, *buf = NULL;
5345 int err = -ENOMEM;
5346
Dan Williamsb5470dc2008-06-27 21:44:04 -07005347 if (md_allow_write(mddev))
5348 file = kmalloc(sizeof(*file), GFP_NOIO);
5349 else
5350 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08005351
NeilBrown32a76272005-06-21 17:17:14 -07005352 if (!file)
5353 goto out;
5354
5355 /* bitmap disabled, zero the first byte and copy out */
5356 if (!mddev->bitmap || !mddev->bitmap->file) {
5357 file->pathname[0] = '\0';
5358 goto copy_out;
5359 }
5360
5361 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
5362 if (!buf)
5363 goto out;
5364
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07005365 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
5366 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07005367 goto out;
5368
5369 strcpy(file->pathname, ptr);
5370
5371copy_out:
5372 err = 0;
5373 if (copy_to_user(arg, file, sizeof(*file)))
5374 err = -EFAULT;
5375out:
5376 kfree(buf);
5377 kfree(file);
5378 return err;
5379}
5380
NeilBrownfd01b882011-10-11 16:47:53 +11005381static int get_disk_info(struct mddev * mddev, void __user * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382{
5383 mdu_disk_info_t info;
NeilBrown3cb03002011-10-11 16:45:26 +11005384 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385
5386 if (copy_from_user(&info, arg, sizeof(info)))
5387 return -EFAULT;
5388
Andre Noll26ef3792008-07-11 22:02:21 +10005389 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 if (rdev) {
5391 info.major = MAJOR(rdev->bdev->bd_dev);
5392 info.minor = MINOR(rdev->bdev->bd_dev);
5393 info.raid_disk = rdev->raid_disk;
5394 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08005395 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08005397 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 info.state |= (1<<MD_DISK_ACTIVE);
5399 info.state |= (1<<MD_DISK_SYNC);
5400 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005401 if (test_bit(WriteMostly, &rdev->flags))
5402 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 } else {
5404 info.major = info.minor = 0;
5405 info.raid_disk = -1;
5406 info.state = (1<<MD_DISK_REMOVED);
5407 }
5408
5409 if (copy_to_user(arg, &info, sizeof(info)))
5410 return -EFAULT;
5411
5412 return 0;
5413}
5414
NeilBrownfd01b882011-10-11 16:47:53 +11005415static int add_new_disk(struct mddev * mddev, mdu_disk_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416{
5417 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005418 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 dev_t dev = MKDEV(info->major,info->minor);
5420
5421 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5422 return -EOVERFLOW;
5423
5424 if (!mddev->raid_disks) {
5425 int err;
5426 /* expecting a device which has a superblock */
5427 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5428 if (IS_ERR(rdev)) {
5429 printk(KERN_WARNING
5430 "md: md_import_device returned %ld\n",
5431 PTR_ERR(rdev));
5432 return PTR_ERR(rdev);
5433 }
5434 if (!list_empty(&mddev->disks)) {
NeilBrown3cb03002011-10-11 16:45:26 +11005435 struct md_rdev *rdev0
5436 = list_entry(mddev->disks.next,
5437 struct md_rdev, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005438 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 .load_super(rdev, rdev0, mddev->minor_version);
5440 if (err < 0) {
5441 printk(KERN_WARNING
5442 "md: %s has different UUID to %s\n",
5443 bdevname(rdev->bdev,b),
5444 bdevname(rdev0->bdev,b2));
5445 export_rdev(rdev);
5446 return -EINVAL;
5447 }
5448 }
5449 err = bind_rdev_to_array(rdev, mddev);
5450 if (err)
5451 export_rdev(rdev);
5452 return err;
5453 }
5454
5455 /*
5456 * add_new_disk can be used once the array is assembled
5457 * to add "hot spares". They must already have a superblock
5458 * written
5459 */
5460 if (mddev->pers) {
5461 int err;
5462 if (!mddev->pers->hot_add_disk) {
5463 printk(KERN_WARNING
5464 "%s: personality does not support diskops!\n",
5465 mdname(mddev));
5466 return -EINVAL;
5467 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005468 if (mddev->persistent)
5469 rdev = md_import_device(dev, mddev->major_version,
5470 mddev->minor_version);
5471 else
5472 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 if (IS_ERR(rdev)) {
5474 printk(KERN_WARNING
5475 "md: md_import_device returned %ld\n",
5476 PTR_ERR(rdev));
5477 return PTR_ERR(rdev);
5478 }
NeilBrown1a855a02010-12-09 16:36:28 +11005479 /* set saved_raid_disk if appropriate */
NeilBrown41158c72005-06-21 17:17:25 -07005480 if (!mddev->persistent) {
5481 if (info->state & (1<<MD_DISK_SYNC) &&
NeilBrownbf572542011-01-12 09:03:35 +11005482 info->raid_disk < mddev->raid_disks) {
NeilBrown41158c72005-06-21 17:17:25 -07005483 rdev->raid_disk = info->raid_disk;
NeilBrownbf572542011-01-12 09:03:35 +11005484 set_bit(In_sync, &rdev->flags);
5485 } else
NeilBrown41158c72005-06-21 17:17:25 -07005486 rdev->raid_disk = -1;
5487 } else
5488 super_types[mddev->major_version].
5489 validate_super(mddev, rdev);
NeilBrownbedd86b2011-05-11 14:26:20 +10005490 if ((info->state & (1<<MD_DISK_SYNC)) &&
5491 (!test_bit(In_sync, &rdev->flags) ||
5492 rdev->raid_disk != info->raid_disk)) {
5493 /* This was a hot-add request, but events doesn't
5494 * match, so reject it.
5495 */
5496 export_rdev(rdev);
5497 return -EINVAL;
5498 }
5499
NeilBrown1a855a02010-12-09 16:36:28 +11005500 if (test_bit(In_sync, &rdev->flags))
5501 rdev->saved_raid_disk = rdev->raid_disk;
5502 else
5503 rdev->saved_raid_disk = -1;
NeilBrown41158c72005-06-21 17:17:25 -07005504
NeilBrownb2d444d2005-11-08 21:39:31 -08005505 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005506 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5507 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005508 else
5509 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005510
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 rdev->raid_disk = -1;
5512 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005513 if (!err && !mddev->pers->hot_remove_disk) {
5514 /* If there is hot_add_disk but no hot_remove_disk
5515 * then added disks for geometry changes,
5516 * and should be added immediately.
5517 */
5518 super_types[mddev->major_version].
5519 validate_super(mddev, rdev);
5520 err = mddev->pers->hot_add_disk(mddev, rdev);
5521 if (err)
5522 unbind_rdev_from_array(rdev);
5523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 if (err)
5525 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005526 else
NeilBrown00bcb4a2010-06-01 19:37:23 +10005527 sysfs_notify_dirent_safe(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005528
NeilBrown17571282006-12-10 02:20:52 -08005529 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005530 if (mddev->degraded)
5531 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005532 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
马建朋9864c002011-06-09 11:42:48 +10005533 if (!err)
5534 md_new_event(mddev);
NeilBrown005eca52005-08-22 13:11:08 -07005535 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 return err;
5537 }
5538
5539 /* otherwise, add_new_disk is only allowed
5540 * for major_version==0 superblocks
5541 */
5542 if (mddev->major_version != 0) {
5543 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5544 mdname(mddev));
5545 return -EINVAL;
5546 }
5547
5548 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5549 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005550 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 if (IS_ERR(rdev)) {
5552 printk(KERN_WARNING
5553 "md: error, md_import_device() returned %ld\n",
5554 PTR_ERR(rdev));
5555 return PTR_ERR(rdev);
5556 }
5557 rdev->desc_nr = info->number;
5558 if (info->raid_disk < mddev->raid_disks)
5559 rdev->raid_disk = info->raid_disk;
5560 else
5561 rdev->raid_disk = -1;
5562
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005564 if (info->state & (1<<MD_DISK_SYNC))
5565 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005567 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5568 set_bit(WriteMostly, &rdev->flags);
5569
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 if (!mddev->persistent) {
5571 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Mike Snitzer77304d22010-11-08 14:39:12 +01005572 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
5573 } else
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005574 rdev->sb_start = calc_dev_sboffset(rdev);
NeilBrown8190e752009-06-18 08:48:58 +10005575 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576
NeilBrown2bf071b2006-01-06 00:20:55 -08005577 err = bind_rdev_to_array(rdev, mddev);
5578 if (err) {
5579 export_rdev(rdev);
5580 return err;
5581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 }
5583
5584 return 0;
5585}
5586
NeilBrownfd01b882011-10-11 16:47:53 +11005587static int hot_remove_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588{
5589 char b[BDEVNAME_SIZE];
NeilBrown3cb03002011-10-11 16:45:26 +11005590 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 rdev = find_rdev(mddev, dev);
5593 if (!rdev)
5594 return -ENXIO;
5595
5596 if (rdev->raid_disk >= 0)
5597 goto busy;
5598
5599 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005600 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005601 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602
5603 return 0;
5604busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005605 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 bdevname(rdev->bdev,b), mdname(mddev));
5607 return -EBUSY;
5608}
5609
NeilBrownfd01b882011-10-11 16:47:53 +11005610static int hot_add_disk(struct mddev * mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611{
5612 char b[BDEVNAME_SIZE];
5613 int err;
NeilBrown3cb03002011-10-11 16:45:26 +11005614 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615
5616 if (!mddev->pers)
5617 return -ENODEV;
5618
5619 if (mddev->major_version != 0) {
5620 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5621 " version-0 superblocks.\n",
5622 mdname(mddev));
5623 return -EINVAL;
5624 }
5625 if (!mddev->pers->hot_add_disk) {
5626 printk(KERN_WARNING
5627 "%s: personality does not support diskops!\n",
5628 mdname(mddev));
5629 return -EINVAL;
5630 }
5631
NeilBrownd710e132008-10-13 11:55:12 +11005632 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 if (IS_ERR(rdev)) {
5634 printk(KERN_WARNING
5635 "md: error, md_import_device() returned %ld\n",
5636 PTR_ERR(rdev));
5637 return -EINVAL;
5638 }
5639
5640 if (mddev->persistent)
Jonathan Brassow57b2caa2011-01-14 09:14:33 +11005641 rdev->sb_start = calc_dev_sboffset(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 else
Mike Snitzer77304d22010-11-08 14:39:12 +01005643 rdev->sb_start = i_size_read(rdev->bdev->bd_inode) / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644
NeilBrown8190e752009-06-18 08:48:58 +10005645 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646
NeilBrownb2d444d2005-11-08 21:39:31 -08005647 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 printk(KERN_WARNING
5649 "md: can not hot-add faulty %s disk to %s!\n",
5650 bdevname(rdev->bdev,b), mdname(mddev));
5651 err = -EINVAL;
5652 goto abort_export;
5653 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005654 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005656 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005657 err = bind_rdev_to_array(rdev, mddev);
5658 if (err)
5659 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660
5661 /*
5662 * The rest should better be atomic, we can have disk failures
5663 * noticed in interrupt contexts ...
5664 */
5665
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 rdev->raid_disk = -1;
5667
NeilBrown850b2b42006-10-03 01:15:46 -07005668 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669
5670 /*
5671 * Kick recovery, maybe this spare has to be added to the
5672 * array immediately.
5673 */
5674 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5675 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005676 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 return 0;
5678
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679abort_export:
5680 export_rdev(rdev);
5681 return err;
5682}
5683
NeilBrownfd01b882011-10-11 16:47:53 +11005684static int set_bitmap_file(struct mddev *mddev, int fd)
NeilBrown32a76272005-06-21 17:17:14 -07005685{
5686 int err;
5687
NeilBrown36fa3062005-09-09 16:23:45 -07005688 if (mddev->pers) {
5689 if (!mddev->pers->quiesce)
5690 return -EBUSY;
5691 if (mddev->recovery || mddev->sync_thread)
5692 return -EBUSY;
5693 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005694 }
5695
NeilBrown36fa3062005-09-09 16:23:45 -07005696
5697 if (fd >= 0) {
5698 if (mddev->bitmap)
5699 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005700 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005701
NeilBrownc3d97142009-12-14 12:49:52 +11005702 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005703 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5704 mdname(mddev));
5705 return -EBADF;
5706 }
5707
NeilBrownc3d97142009-12-14 12:49:52 +11005708 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005709 if (err) {
5710 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5711 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005712 fput(mddev->bitmap_info.file);
5713 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005714 return err;
5715 }
NeilBrownc3d97142009-12-14 12:49:52 +11005716 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005717 } else if (mddev->bitmap == NULL)
5718 return -ENOENT; /* cannot remove what isn't there */
5719 err = 0;
5720 if (mddev->pers) {
5721 mddev->pers->quiesce(mddev, 1);
NeilBrown69e51b42010-06-01 19:37:35 +10005722 if (fd >= 0) {
NeilBrown36fa3062005-09-09 16:23:45 -07005723 err = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005724 if (!err)
5725 err = bitmap_load(mddev);
5726 }
NeilBrownd7375ab2006-06-26 00:27:43 -07005727 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005728 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005729 fd = -1; /* make sure to put the file */
5730 }
NeilBrown36fa3062005-09-09 16:23:45 -07005731 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005732 }
5733 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005734 if (mddev->bitmap_info.file) {
5735 restore_bitmap_write_access(mddev->bitmap_info.file);
5736 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005737 }
NeilBrownc3d97142009-12-14 12:49:52 +11005738 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005739 }
5740
NeilBrown32a76272005-06-21 17:17:14 -07005741 return err;
5742}
5743
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744/*
5745 * set_array_info is used two different ways
5746 * The original usage is when creating a new array.
5747 * In this usage, raid_disks is > 0 and it together with
5748 * level, size, not_persistent,layout,chunksize determine the
5749 * shape of the array.
5750 * This will always create an array with a type-0.90.0 superblock.
5751 * The newer usage is when assembling an array.
5752 * In this case raid_disks will be 0, and the major_version field is
5753 * use to determine which style super-blocks are to be found on the devices.
5754 * The minor and patch _version numbers are also kept incase the
5755 * super_block handler wishes to interpret them.
5756 */
NeilBrownfd01b882011-10-11 16:47:53 +11005757static int set_array_info(struct mddev * mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758{
5759
5760 if (info->raid_disks == 0) {
5761 /* just setting version number for superblock loading */
5762 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005763 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 super_types[info->major_version].name == NULL) {
5765 /* maybe try to auto-load a module? */
5766 printk(KERN_INFO
5767 "md: superblock version %d not known\n",
5768 info->major_version);
5769 return -EINVAL;
5770 }
5771 mddev->major_version = info->major_version;
5772 mddev->minor_version = info->minor_version;
5773 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005774 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005775 /* ensure mddev_put doesn't delete this now that there
5776 * is some minimal configuration.
5777 */
5778 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 return 0;
5780 }
5781 mddev->major_version = MD_MAJOR_VERSION;
5782 mddev->minor_version = MD_MINOR_VERSION;
5783 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5784 mddev->ctime = get_seconds();
5785
5786 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005787 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005788 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 mddev->raid_disks = info->raid_disks;
5790 /* don't set md_minor, it is determined by which /dev/md* was
5791 * openned
5792 */
5793 if (info->state & (1<<MD_SB_CLEAN))
5794 mddev->recovery_cp = MaxSector;
5795 else
5796 mddev->recovery_cp = 0;
5797 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005798 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799
5800 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005801 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802
5803 mddev->max_disks = MD_SB_DISKS;
5804
NeilBrowne6910632008-02-06 01:39:51 -08005805 if (mddev->persistent)
5806 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005807 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808
NeilBrownc3d97142009-12-14 12:49:52 +11005809 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5810 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005811
NeilBrownf6705572006-03-27 01:18:11 -08005812 mddev->reshape_position = MaxSector;
5813
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 /*
5815 * Generate a 128 bit UUID
5816 */
5817 get_random_bytes(mddev->uuid, 16);
5818
NeilBrownf6705572006-03-27 01:18:11 -08005819 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005820 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005821 mddev->new_layout = mddev->layout;
5822 mddev->delta_disks = 0;
5823
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824 return 0;
5825}
5826
NeilBrownfd01b882011-10-11 16:47:53 +11005827void md_set_array_sectors(struct mddev *mddev, sector_t array_sectors)
Dan Williams1f403622009-03-31 14:59:03 +11005828{
Dan Williamsb522adc2009-03-31 15:00:31 +11005829 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5830
5831 if (mddev->external_size)
5832 return;
5833
Dan Williams1f403622009-03-31 14:59:03 +11005834 mddev->array_sectors = array_sectors;
5835}
5836EXPORT_SYMBOL(md_set_array_sectors);
5837
NeilBrownfd01b882011-10-11 16:47:53 +11005838static int update_size(struct mddev *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005839{
NeilBrown3cb03002011-10-11 16:45:26 +11005840 struct md_rdev *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005841 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005842 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005843
5844 if (mddev->pers->resize == NULL)
5845 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005846 /* The "num_sectors" is the number of sectors of each device that
5847 * is used. This can only make sense for arrays with redundancy.
5848 * linear and raid0 always use whatever space is available. We can only
5849 * consider changing this number if no resync or reconstruction is
5850 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005851 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005852 * of each device. If num_sectors is zero, we find the largest size
5853 * that fits.
NeilBrowna35b0d62006-01-06 00:20:49 -08005854 */
5855 if (mddev->sync_thread)
5856 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005857 if (mddev->bitmap)
5858 /* Sorry, cannot grow a bitmap yet, just remove it,
5859 * grow, and re-add.
5860 */
5861 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005862 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005863 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005864
Andre Nolld71f9f82008-07-11 22:02:22 +10005865 if (fit && (num_sectors == 0 || num_sectors > avail))
5866 num_sectors = avail;
5867 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005868 return -ENOSPC;
5869 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005870 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005871 if (!rv)
5872 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005873 return rv;
5874}
5875
NeilBrownfd01b882011-10-11 16:47:53 +11005876static int update_raid_disks(struct mddev *mddev, int raid_disks)
NeilBrownda943b92006-01-06 00:20:54 -08005877{
5878 int rv;
5879 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005880 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b92006-01-06 00:20:54 -08005881 return -EINVAL;
5882 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005883 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b92006-01-06 00:20:54 -08005884 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005885 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b92006-01-06 00:20:54 -08005886 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005887 mddev->delta_disks = raid_disks - mddev->raid_disks;
5888
5889 rv = mddev->pers->check_reshape(mddev);
NeilBrownde171cb2011-01-31 11:57:42 +11005890 if (rv < 0)
5891 mddev->delta_disks = 0;
NeilBrownda943b92006-01-06 00:20:54 -08005892 return rv;
5893}
5894
5895
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896/*
5897 * update_array_info is used to change the configuration of an
5898 * on-line array.
5899 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5900 * fields in the info are checked against the array.
5901 * Any differences that cannot be handled will cause an error.
5902 * Normally, only one change can be managed at a time.
5903 */
NeilBrownfd01b882011-10-11 16:47:53 +11005904static int update_array_info(struct mddev *mddev, mdu_array_info_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905{
5906 int rv = 0;
5907 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005908 int state = 0;
5909
5910 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005911 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005912 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913
5914 if (mddev->major_version != info->major_version ||
5915 mddev->minor_version != info->minor_version ||
5916/* mddev->patch_version != info->patch_version || */
5917 mddev->ctime != info->ctime ||
5918 mddev->level != info->level ||
5919/* mddev->layout != info->layout || */
5920 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005921 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005922 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5923 ((state^info->state) & 0xfffffe00)
5924 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 return -EINVAL;
5926 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005927 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5928 cnt++;
5929 if (mddev->raid_disks != info->raid_disks)
5930 cnt++;
5931 if (mddev->layout != info->layout)
5932 cnt++;
5933 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5934 cnt++;
5935 if (cnt == 0)
5936 return 0;
5937 if (cnt > 1)
5938 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939
5940 if (mddev->layout != info->layout) {
5941 /* Change layout
5942 * we don't need to do anything at the md level, the
5943 * personality will take care of it all.
5944 */
NeilBrown50ac1682009-06-18 08:47:55 +10005945 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005947 else {
5948 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005949 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005950 if (rv)
5951 mddev->new_layout = mddev->layout;
5952 return rv;
5953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005955 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005956 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957
NeilBrownda943b92006-01-06 00:20:54 -08005958 if (mddev->raid_disks != info->raid_disks)
5959 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960
NeilBrown36fa3062005-09-09 16:23:45 -07005961 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5962 if (mddev->pers->quiesce == NULL)
5963 return -EINVAL;
5964 if (mddev->recovery || mddev->sync_thread)
5965 return -EBUSY;
5966 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5967 /* add the bitmap */
5968 if (mddev->bitmap)
5969 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005970 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005971 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005972 mddev->bitmap_info.offset =
5973 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005974 mddev->pers->quiesce(mddev, 1);
5975 rv = bitmap_create(mddev);
NeilBrown69e51b42010-06-01 19:37:35 +10005976 if (!rv)
5977 rv = bitmap_load(mddev);
NeilBrown36fa3062005-09-09 16:23:45 -07005978 if (rv)
5979 bitmap_destroy(mddev);
5980 mddev->pers->quiesce(mddev, 0);
5981 } else {
5982 /* remove the bitmap */
5983 if (!mddev->bitmap)
5984 return -ENOENT;
5985 if (mddev->bitmap->file)
5986 return -EINVAL;
5987 mddev->pers->quiesce(mddev, 1);
5988 bitmap_destroy(mddev);
5989 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005990 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005991 }
5992 }
NeilBrown850b2b42006-10-03 01:15:46 -07005993 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994 return rv;
5995}
5996
NeilBrownfd01b882011-10-11 16:47:53 +11005997static int set_disk_faulty(struct mddev *mddev, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998{
NeilBrown3cb03002011-10-11 16:45:26 +11005999 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000
6001 if (mddev->pers == NULL)
6002 return -ENODEV;
6003
6004 rdev = find_rdev(mddev, dev);
6005 if (!rdev)
6006 return -ENODEV;
6007
6008 md_error(mddev, rdev);
NeilBrown5ef56c82011-08-25 14:42:51 +10006009 if (!test_bit(Faulty, &rdev->flags))
6010 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 return 0;
6012}
6013
Andre Noll2f9618c2008-04-25 18:57:58 +02006014/*
6015 * We have a problem here : there is no easy way to give a CHS
6016 * virtual geometry. We currently pretend that we have a 2 heads
6017 * 4 sectors (with a BIG number of cylinders...). This drives
6018 * dosfs just mad... ;-)
6019 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006020static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
6021{
NeilBrownfd01b882011-10-11 16:47:53 +11006022 struct mddev *mddev = bdev->bd_disk->private_data;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006023
6024 geo->heads = 2;
6025 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11006026 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006027 return 0;
6028}
6029
Al Viroa39907f2008-03-02 10:31:15 -05006030static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 unsigned int cmd, unsigned long arg)
6032{
6033 int err = 0;
6034 void __user *argp = (void __user *)arg;
NeilBrownfd01b882011-10-11 16:47:53 +11006035 struct mddev *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10006036 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037
6038 if (!capable(CAP_SYS_ADMIN))
6039 return -EACCES;
6040
6041 /*
6042 * Commands dealing with the RAID driver but not any
6043 * particular array:
6044 */
6045 switch (cmd)
6046 {
6047 case RAID_VERSION:
6048 err = get_version(argp);
6049 goto done;
6050
6051 case PRINT_RAID_DEBUG:
6052 err = 0;
6053 md_print_devices();
6054 goto done;
6055
6056#ifndef MODULE
6057 case RAID_AUTORUN:
6058 err = 0;
6059 autostart_arrays(arg);
6060 goto done;
6061#endif
6062 default:;
6063 }
6064
6065 /*
6066 * Commands creating/starting a new array:
6067 */
6068
Al Viroa39907f2008-03-02 10:31:15 -05006069 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070
6071 if (!mddev) {
6072 BUG();
6073 goto abort;
6074 }
6075
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 err = mddev_lock(mddev);
6077 if (err) {
6078 printk(KERN_INFO
6079 "md: ioctl lock interrupted, reason %d, cmd %d\n",
6080 err, cmd);
6081 goto abort;
6082 }
6083
6084 switch (cmd)
6085 {
6086 case SET_ARRAY_INFO:
6087 {
6088 mdu_array_info_t info;
6089 if (!arg)
6090 memset(&info, 0, sizeof(info));
6091 else if (copy_from_user(&info, argp, sizeof(info))) {
6092 err = -EFAULT;
6093 goto abort_unlock;
6094 }
6095 if (mddev->pers) {
6096 err = update_array_info(mddev, &info);
6097 if (err) {
6098 printk(KERN_WARNING "md: couldn't update"
6099 " array info. %d\n", err);
6100 goto abort_unlock;
6101 }
6102 goto done_unlock;
6103 }
6104 if (!list_empty(&mddev->disks)) {
6105 printk(KERN_WARNING
6106 "md: array %s already has disks!\n",
6107 mdname(mddev));
6108 err = -EBUSY;
6109 goto abort_unlock;
6110 }
6111 if (mddev->raid_disks) {
6112 printk(KERN_WARNING
6113 "md: array %s already initialised!\n",
6114 mdname(mddev));
6115 err = -EBUSY;
6116 goto abort_unlock;
6117 }
6118 err = set_array_info(mddev, &info);
6119 if (err) {
6120 printk(KERN_WARNING "md: couldn't set"
6121 " array info. %d\n", err);
6122 goto abort_unlock;
6123 }
6124 }
6125 goto done_unlock;
6126
6127 default:;
6128 }
6129
6130 /*
6131 * Commands querying/configuring an existing array:
6132 */
NeilBrown32a76272005-06-21 17:17:14 -07006133 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08006134 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08006135 if ((!mddev->raid_disks && !mddev->external)
6136 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
6137 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
6138 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139 err = -ENODEV;
6140 goto abort_unlock;
6141 }
6142
6143 /*
6144 * Commands even a read-only array can execute:
6145 */
6146 switch (cmd)
6147 {
6148 case GET_ARRAY_INFO:
6149 err = get_array_info(mddev, argp);
6150 goto done_unlock;
6151
NeilBrown32a76272005-06-21 17:17:14 -07006152 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01006153 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07006154 goto done_unlock;
6155
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 case GET_DISK_INFO:
6157 err = get_disk_info(mddev, argp);
6158 goto done_unlock;
6159
6160 case RESTART_ARRAY_RW:
6161 err = restart_array(mddev);
6162 goto done_unlock;
6163
6164 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006165 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 goto done_unlock;
6167
6168 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11006169 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 goto done_unlock;
6171
Dan Williamse2218352010-05-12 08:25:37 +10006172 case BLKROSET:
6173 if (get_user(ro, (int __user *)(arg))) {
6174 err = -EFAULT;
6175 goto done_unlock;
6176 }
6177 err = -EINVAL;
6178
6179 /* if the bdev is going readonly the value of mddev->ro
6180 * does not matter, no writes are coming
6181 */
6182 if (ro)
6183 goto done_unlock;
6184
6185 /* are we are already prepared for writes? */
6186 if (mddev->ro != 1)
6187 goto done_unlock;
6188
6189 /* transitioning to readauto need only happen for
6190 * arrays that call md_write_start
6191 */
6192 if (mddev->pers) {
6193 err = restart_array(mddev);
6194 if (err == 0) {
6195 mddev->ro = 2;
6196 set_disk_ro(mddev->gendisk, 0);
6197 }
6198 }
6199 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200 }
6201
6202 /*
6203 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08006204 * superblock, so we do not allow them on read-only arrays.
6205 * However non-MD ioctls (e.g. get-size) will still come through
6206 * here and hit the 'default' below, so only disallow
6207 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208 */
Andre Nollbb57fc62008-04-25 19:06:35 +02006209 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006210 if (mddev->ro == 2) {
6211 mddev->ro = 0;
NeilBrown00bcb4a2010-06-01 19:37:23 +10006212 sysfs_notify_dirent_safe(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10006213 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6214 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08006215 } else {
6216 err = -EROFS;
6217 goto abort_unlock;
6218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 }
6220
6221 switch (cmd)
6222 {
6223 case ADD_NEW_DISK:
6224 {
6225 mdu_disk_info_t info;
6226 if (copy_from_user(&info, argp, sizeof(info)))
6227 err = -EFAULT;
6228 else
6229 err = add_new_disk(mddev, &info);
6230 goto done_unlock;
6231 }
6232
6233 case HOT_REMOVE_DISK:
6234 err = hot_remove_disk(mddev, new_decode_dev(arg));
6235 goto done_unlock;
6236
6237 case HOT_ADD_DISK:
6238 err = hot_add_disk(mddev, new_decode_dev(arg));
6239 goto done_unlock;
6240
6241 case SET_DISK_FAULTY:
6242 err = set_disk_faulty(mddev, new_decode_dev(arg));
6243 goto done_unlock;
6244
6245 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11006246 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247 goto done_unlock;
6248
NeilBrown32a76272005-06-21 17:17:14 -07006249 case SET_BITMAP_FILE:
6250 err = set_bitmap_file(mddev, (int)arg);
6251 goto done_unlock;
6252
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254 err = -EINVAL;
6255 goto abort_unlock;
6256 }
6257
6258done_unlock:
6259abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11006260 if (mddev->hold_active == UNTIL_IOCTL &&
6261 err != -EINVAL)
6262 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 mddev_unlock(mddev);
6264
6265 return err;
6266done:
6267 if (err)
6268 MD_BUG();
6269abort:
6270 return err;
6271}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006272#ifdef CONFIG_COMPAT
6273static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
6274 unsigned int cmd, unsigned long arg)
6275{
6276 switch (cmd) {
6277 case HOT_REMOVE_DISK:
6278 case HOT_ADD_DISK:
6279 case SET_DISK_FAULTY:
6280 case SET_BITMAP_FILE:
6281 /* These take in integer arg, do not convert */
6282 break;
6283 default:
6284 arg = (unsigned long)compat_ptr(arg);
6285 break;
6286 }
6287
6288 return md_ioctl(bdev, mode, cmd, arg);
6289}
6290#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291
Al Viroa39907f2008-03-02 10:31:15 -05006292static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293{
6294 /*
6295 * Succeed if we can lock the mddev, which confirms that
6296 * it isn't being stopped right now.
6297 */
NeilBrownfd01b882011-10-11 16:47:53 +11006298 struct mddev *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299 int err;
6300
NeilBrownd3374822009-01-09 08:31:10 +11006301 if (mddev->gendisk != bdev->bd_disk) {
6302 /* we are racing with mddev_put which is discarding this
6303 * bd_disk.
6304 */
6305 mddev_put(mddev);
6306 /* Wait until bdev->bd_disk is definitely gone */
Tejun Heoe804ac72010-10-15 15:36:08 +02006307 flush_workqueue(md_misc_wq);
NeilBrownd3374822009-01-09 08:31:10 +11006308 /* Then retry the open from the top */
6309 return -ERESTARTSYS;
6310 }
6311 BUG_ON(mddev != bdev->bd_disk->private_data);
6312
NeilBrownc8c00a62009-08-10 12:50:52 +10006313 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 goto out;
6315
6316 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10006317 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10006318 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006320 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321 out:
6322 return err;
6323}
6324
Al Viroa39907f2008-03-02 10:31:15 -05006325static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326{
NeilBrownfd01b882011-10-11 16:47:53 +11006327 struct mddev *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02006329 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10006330 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331 mddev_put(mddev);
6332
6333 return 0;
6334}
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006335
6336static int md_media_changed(struct gendisk *disk)
6337{
NeilBrownfd01b882011-10-11 16:47:53 +11006338 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006339
6340 return mddev->changed;
6341}
6342
6343static int md_revalidate(struct gendisk *disk)
6344{
NeilBrownfd01b882011-10-11 16:47:53 +11006345 struct mddev *mddev = disk->private_data;
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006346
6347 mddev->changed = 0;
6348 return 0;
6349}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07006350static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351{
6352 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05006353 .open = md_open,
6354 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10006355 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11006356#ifdef CONFIG_COMPAT
6357 .compat_ioctl = md_compat_ioctl,
6358#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08006359 .getgeo = md_getgeo,
NeilBrownf0b4f7e2011-02-24 17:26:41 +11006360 .media_changed = md_media_changed,
6361 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362};
6363
Adrian Bunk75c96f82005-05-05 16:16:09 -07006364static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365{
NeilBrown2b8bf342011-10-11 16:48:23 +11006366 struct md_thread *thread = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368 /*
6369 * md_thread is a 'system-thread', it's priority should be very
6370 * high. We avoid resource deadlocks individually in each
6371 * raid personality. (RAID5 does preallocation) We also use RR and
6372 * the very same RT priority as kswapd, thus we will never get
6373 * into a priority inversion deadlock.
6374 *
6375 * we definitely have to have equal or higher priority than
6376 * bdflush, otherwise bdflush will deadlock if there are too
6377 * many dirty RAID5 blocks.
6378 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379
NeilBrown6985c432005-10-19 21:23:47 -07006380 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07006381 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382
NeilBrown93588e22005-11-15 00:09:12 -08006383 /* We need to wait INTERRUPTIBLE so that
6384 * we don't add to the load-average.
6385 * That means we need to be sure no signals are
6386 * pending
6387 */
6388 if (signal_pending(current))
6389 flush_signals(current);
6390
6391 wait_event_interruptible_timeout
6392 (thread->wqueue,
6393 test_bit(THREAD_WAKEUP, &thread->flags)
6394 || kthread_should_stop(),
6395 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396
NeilBrown6c987912011-01-14 09:13:53 +11006397 clear_bit(THREAD_WAKEUP, &thread->flags);
6398 if (!kthread_should_stop())
NeilBrown589a5942010-12-09 17:02:14 +11006399 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400 }
NeilBrowna6fb0932005-09-09 16:23:56 -07006401
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402 return 0;
6403}
6404
NeilBrown2b8bf342011-10-11 16:48:23 +11006405void md_wakeup_thread(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406{
6407 if (thread) {
NeilBrown36a4e1f2011-10-07 14:23:17 +11006408 pr_debug("md: waking up MD thread %s.\n", thread->tsk->comm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 set_bit(THREAD_WAKEUP, &thread->flags);
6410 wake_up(&thread->wqueue);
6411 }
6412}
6413
NeilBrown2b8bf342011-10-11 16:48:23 +11006414struct md_thread *md_register_thread(void (*run) (struct mddev *), struct mddev *mddev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415 const char *name)
6416{
NeilBrown2b8bf342011-10-11 16:48:23 +11006417 struct md_thread *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418
NeilBrown2b8bf342011-10-11 16:48:23 +11006419 thread = kzalloc(sizeof(struct md_thread), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420 if (!thread)
6421 return NULL;
6422
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423 init_waitqueue_head(&thread->wqueue);
6424
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425 thread->run = run;
6426 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07006427 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10006428 thread->tsk = kthread_run(md_thread, thread,
6429 "%s_%s",
6430 mdname(thread->mddev),
6431 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07006432 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433 kfree(thread);
6434 return NULL;
6435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436 return thread;
6437}
6438
NeilBrown2b8bf342011-10-11 16:48:23 +11006439void md_unregister_thread(struct md_thread **threadp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006440{
NeilBrown2b8bf342011-10-11 16:48:23 +11006441 struct md_thread *thread = *threadp;
NeilBrowne0cf8f02009-03-31 14:39:39 +11006442 if (!thread)
6443 return;
NeilBrown36a4e1f2011-10-07 14:23:17 +11006444 pr_debug("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrown01f96c02011-09-21 15:30:20 +10006445 /* Locking ensures that mddev_unlock does not wake_up a
6446 * non-existent thread
6447 */
6448 spin_lock(&pers_lock);
6449 *threadp = NULL;
6450 spin_unlock(&pers_lock);
NeilBrowna6fb0932005-09-09 16:23:56 -07006451
6452 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453 kfree(thread);
6454}
6455
NeilBrownfd01b882011-10-11 16:47:53 +11006456void md_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006457{
6458 if (!mddev) {
6459 MD_BUG();
6460 return;
6461 }
6462
NeilBrownb2d444d2005-11-08 21:39:31 -08006463 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006465
NeilBrownde393cd2011-07-28 11:31:48 +10006466 if (!mddev->pers || !mddev->pers->error_handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006467 return;
6468 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006469 if (mddev->degraded)
6470 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006471 sysfs_notify_dirent_safe(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6473 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6474 md_wakeup_thread(mddev->thread);
NeilBrown768a4182010-07-26 11:49:55 +10006475 if (mddev->event_work.func)
Tejun Heoe804ac72010-10-15 15:36:08 +02006476 queue_work(md_misc_wq, &mddev->event_work);
NeilBrownc331eb02006-05-30 21:27:13 -07006477 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478}
6479
6480/* seq_file implementation /proc/mdstat */
6481
6482static void status_unused(struct seq_file *seq)
6483{
6484 int i = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006485 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486
6487 seq_printf(seq, "unused devices: ");
6488
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006489 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490 char b[BDEVNAME_SIZE];
6491 i++;
6492 seq_printf(seq, "%s ",
6493 bdevname(rdev->bdev,b));
6494 }
6495 if (!i)
6496 seq_printf(seq, "<none>");
6497
6498 seq_printf(seq, "\n");
6499}
6500
6501
NeilBrownfd01b882011-10-11 16:47:53 +11006502static void status_resync(struct seq_file *seq, struct mddev * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503{
NeilBrowndd71cf62009-05-07 12:49:35 +10006504 sector_t max_sectors, resync, res;
6505 unsigned long dt, db;
6506 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006507 int scale;
6508 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509
NeilBrowndd71cf62009-05-07 12:49:35 +10006510 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511
6512 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006513 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006515 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006516
6517 /*
6518 * Should not happen.
6519 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006520 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521 MD_BUG();
6522 return;
6523 }
NeilBrown4588b422006-03-27 01:18:04 -08006524 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006525 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006526 * u32, as those are the requirements for sector_div.
6527 * Thus 'scale' must be at least 10
6528 */
6529 scale = 10;
6530 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006531 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006532 scale++;
6533 }
6534 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006535 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006536
6537 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538 {
NeilBrown4588b422006-03-27 01:18:04 -08006539 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540 seq_printf(seq, "[");
6541 for (i = 0; i < x; i++)
6542 seq_printf(seq, "=");
6543 seq_printf(seq, ">");
6544 for (i = 0; i < y; i++)
6545 seq_printf(seq, ".");
6546 seq_printf(seq, "] ");
6547 }
NeilBrown4588b422006-03-27 01:18:04 -08006548 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006549 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6550 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006551 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6552 "check" :
6553 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6554 "resync" : "recovery"))),
6555 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006556 (unsigned long long) resync/2,
6557 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558
6559 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 * dt: time from mark until now
6561 * db: blocks written from mark until now
6562 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006563 *
6564 * rt is a sector_t, so could be 32bit or 64bit.
6565 * So we divide before multiply in case it is 32bit and close
6566 * to the limit.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006567 * We scale the divisor (db) by 32 to avoid losing precision
NeilBrowndd71cf62009-05-07 12:49:35 +10006568 * near the end of resync when the number of remaining sectors
6569 * is close to 'db'.
6570 * We then divide rt by 32 after multiplying by db to compensate.
6571 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572 */
6573 dt = ((jiffies - mddev->resync_mark) / HZ);
6574 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006575 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6576 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577
NeilBrowndd71cf62009-05-07 12:49:35 +10006578 rt = max_sectors - resync; /* number of remaining sectors */
6579 sector_div(rt, db/32+1);
6580 rt *= dt;
6581 rt >>= 5;
6582
6583 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6584 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585
NeilBrownff4e8d92006-07-10 04:44:16 -07006586 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587}
6588
6589static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6590{
6591 struct list_head *tmp;
6592 loff_t l = *pos;
NeilBrownfd01b882011-10-11 16:47:53 +11006593 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594
6595 if (l >= 0x10000)
6596 return NULL;
6597 if (!l--)
6598 /* header */
6599 return (void*)1;
6600
6601 spin_lock(&all_mddevs_lock);
6602 list_for_each(tmp,&all_mddevs)
6603 if (!l--) {
NeilBrownfd01b882011-10-11 16:47:53 +11006604 mddev = list_entry(tmp, struct mddev, all_mddevs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 mddev_get(mddev);
6606 spin_unlock(&all_mddevs_lock);
6607 return mddev;
6608 }
6609 spin_unlock(&all_mddevs_lock);
6610 if (!l--)
6611 return (void*)2;/* tail */
6612 return NULL;
6613}
6614
6615static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6616{
6617 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11006618 struct mddev *next_mddev, *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619
6620 ++*pos;
6621 if (v == (void*)2)
6622 return NULL;
6623
6624 spin_lock(&all_mddevs_lock);
6625 if (v == (void*)1)
6626 tmp = all_mddevs.next;
6627 else
6628 tmp = mddev->all_mddevs.next;
6629 if (tmp != &all_mddevs)
NeilBrownfd01b882011-10-11 16:47:53 +11006630 next_mddev = mddev_get(list_entry(tmp,struct mddev,all_mddevs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631 else {
6632 next_mddev = (void*)2;
6633 *pos = 0x10000;
6634 }
6635 spin_unlock(&all_mddevs_lock);
6636
6637 if (v != (void*)1)
6638 mddev_put(mddev);
6639 return next_mddev;
6640
6641}
6642
6643static void md_seq_stop(struct seq_file *seq, void *v)
6644{
NeilBrownfd01b882011-10-11 16:47:53 +11006645 struct mddev *mddev = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646
6647 if (mddev && v != (void*)1 && v != (void*)2)
6648 mddev_put(mddev);
6649}
6650
6651static int md_seq_show(struct seq_file *seq, void *v)
6652{
NeilBrownfd01b882011-10-11 16:47:53 +11006653 struct mddev *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006654 sector_t sectors;
NeilBrown3cb03002011-10-11 16:45:26 +11006655 struct md_rdev *rdev;
NeilBrown32a76272005-06-21 17:17:14 -07006656 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006657
6658 if (v == (void*)1) {
NeilBrown84fc4b52011-10-11 16:49:58 +11006659 struct md_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660 seq_printf(seq, "Personalities : ");
6661 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006662 list_for_each_entry(pers, &pers_list, list)
6663 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664
6665 spin_unlock(&pers_lock);
6666 seq_printf(seq, "\n");
Kay Sieversf1514632011-07-12 20:48:39 +02006667 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006668 return 0;
6669 }
6670 if (v == (void*)2) {
6671 status_unused(seq);
6672 return 0;
6673 }
6674
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006675 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006677
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6679 seq_printf(seq, "%s : %sactive", mdname(mddev),
6680 mddev->pers ? "" : "in");
6681 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006682 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006683 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006684 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006685 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006686 seq_printf(seq, " %s", mddev->pers->name);
6687 }
6688
Andre Nolldd8ac332009-03-31 14:33:13 +11006689 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006690 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006691 char b[BDEVNAME_SIZE];
6692 seq_printf(seq, " %s[%d]",
6693 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006694 if (test_bit(WriteMostly, &rdev->flags))
6695 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006696 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697 seq_printf(seq, "(F)");
6698 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006699 } else if (rdev->raid_disk < 0)
6700 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006701 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006702 }
6703
6704 if (!list_empty(&mddev->disks)) {
6705 if (mddev->pers)
6706 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006707 (unsigned long long)
6708 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709 else
6710 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006711 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006713 if (mddev->persistent) {
6714 if (mddev->major_version != 0 ||
6715 mddev->minor_version != 90) {
6716 seq_printf(seq," super %d.%d",
6717 mddev->major_version,
6718 mddev->minor_version);
6719 }
NeilBrowne6910632008-02-06 01:39:51 -08006720 } else if (mddev->external)
6721 seq_printf(seq, " super external:%s",
6722 mddev->metadata_type);
6723 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006724 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725
6726 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006727 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006729 if (mddev->pers->sync_request) {
6730 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006731 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006732 seq_printf(seq, "\n ");
6733 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6734 seq_printf(seq, "\tresync=DELAYED\n ");
6735 else if (mddev->recovery_cp < MaxSector)
6736 seq_printf(seq, "\tresync=PENDING\n ");
6737 }
NeilBrown32a76272005-06-21 17:17:14 -07006738 } else
6739 seq_printf(seq, "\n ");
6740
6741 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006742 unsigned long chunk_kb;
6743 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006744 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006745 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006746 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6747 "%lu%s chunk",
6748 bitmap->pages - bitmap->missing_pages,
6749 bitmap->pages,
6750 (bitmap->pages - bitmap->missing_pages)
6751 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006752 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006753 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006754 if (bitmap->file) {
6755 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006756 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006757 }
NeilBrown78d742d2005-06-21 17:17:15 -07006758
NeilBrown32a76272005-06-21 17:17:14 -07006759 seq_printf(seq, "\n");
6760 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006761 }
6762
6763 seq_printf(seq, "\n");
6764 }
6765 mddev_unlock(mddev);
6766
6767 return 0;
6768}
6769
Jan Engelhardt110518b2009-05-07 12:49:37 +10006770static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771 .start = md_seq_start,
6772 .next = md_seq_next,
6773 .stop = md_seq_stop,
6774 .show = md_seq_show,
6775};
6776
6777static int md_seq_open(struct inode *inode, struct file *file)
6778{
Kay Sieversf1514632011-07-12 20:48:39 +02006779 struct seq_file *seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006780 int error;
6781
6782 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006783 if (error)
Kay Sieversf1514632011-07-12 20:48:39 +02006784 return error;
6785
6786 seq = file->private_data;
6787 seq->poll_event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788 return error;
6789}
6790
NeilBrownd7603b72006-01-06 00:20:30 -08006791static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6792{
Kay Sieversf1514632011-07-12 20:48:39 +02006793 struct seq_file *seq = filp->private_data;
NeilBrownd7603b72006-01-06 00:20:30 -08006794 int mask;
6795
6796 poll_wait(filp, &md_event_waiters, wait);
6797
6798 /* always allow read */
6799 mask = POLLIN | POLLRDNORM;
6800
Kay Sieversf1514632011-07-12 20:48:39 +02006801 if (seq->poll_event != atomic_read(&md_event_count))
NeilBrownd7603b72006-01-06 00:20:30 -08006802 mask |= POLLERR | POLLPRI;
6803 return mask;
6804}
6805
Arjan van de Venfa027c22007-02-12 00:55:33 -08006806static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006807 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808 .open = md_seq_open,
6809 .read = seq_read,
6810 .llseek = seq_lseek,
Martin Peschkec3f94b42007-05-09 02:35:35 -07006811 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006812 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813};
6814
NeilBrown84fc4b52011-10-11 16:49:58 +11006815int register_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006818 list_add_tail(&p->list, &pers_list);
6819 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 spin_unlock(&pers_lock);
6821 return 0;
6822}
6823
NeilBrown84fc4b52011-10-11 16:49:58 +11006824int unregister_md_personality(struct md_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825{
NeilBrown2604b702006-01-06 00:20:36 -08006826 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006828 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829 spin_unlock(&pers_lock);
6830 return 0;
6831}
6832
NeilBrownfd01b882011-10-11 16:47:53 +11006833static int is_mddev_idle(struct mddev *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834{
NeilBrown3cb03002011-10-11 16:45:26 +11006835 struct md_rdev * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006837 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838
6839 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006840 rcu_read_lock();
6841 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006843 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6844 (int)part_stat_read(&disk->part0, sectors[1]) -
6845 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006846 /* sync IO will cause sync_io to increase before the disk_stats
6847 * as sync_io is counted when a request starts, and
6848 * disk_stats is counted when it completes.
6849 * So resync activity will cause curr_events to be smaller than
6850 * when there was no such activity.
6851 * non-sync IO will cause disk_stat to increase without
6852 * increasing sync_io so curr_events will (eventually)
6853 * be larger than it was before. Once it becomes
6854 * substantially larger, the test below will cause
6855 * the array to appear non-idle, and resync will slow
6856 * down.
6857 * If there is a lot of outstanding resync activity when
6858 * we set last_event to curr_events, then all that activity
6859 * completing might cause the array to appear non-idle
6860 * and resync will be slowed down even though there might
6861 * not have been non-resync activity. This will only
6862 * happen once though. 'last_events' will soon reflect
6863 * the state where there is little or no outstanding
6864 * resync requests, and further resync activity will
6865 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006866 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006868 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869 rdev->last_events = curr_events;
6870 idle = 0;
6871 }
6872 }
NeilBrown4b809912008-07-21 17:05:25 +10006873 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874 return idle;
6875}
6876
NeilBrownfd01b882011-10-11 16:47:53 +11006877void md_done_sync(struct mddev *mddev, int blocks, int ok)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878{
6879 /* another "blocks" (512byte) blocks have been synced */
6880 atomic_sub(blocks, &mddev->recovery_active);
6881 wake_up(&mddev->recovery_wait);
6882 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006883 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884 md_wakeup_thread(mddev->thread);
6885 // stop recovery, signal do_sync ....
6886 }
6887}
6888
6889
NeilBrown06d91a52005-06-21 17:17:12 -07006890/* md_write_start(mddev, bi)
6891 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006892 * in superblock) before writing, schedule a superblock update
6893 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006894 */
NeilBrownfd01b882011-10-11 16:47:53 +11006895void md_write_start(struct mddev *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896{
Neil Brown0fd62b82008-06-28 08:31:36 +10006897 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006898 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006899 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006900
NeilBrownf91de922005-11-08 21:39:36 -08006901 BUG_ON(mddev->ro == 1);
6902 if (mddev->ro == 2) {
6903 /* need to switch to read/write */
6904 mddev->ro = 0;
6905 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6906 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006907 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006908 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006909 }
NeilBrown06d91a52005-06-21 17:17:12 -07006910 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006911 if (mddev->safemode == 1)
6912 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006913 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006914 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006915 if (mddev->in_sync) {
6916 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006917 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006918 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006919 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006920 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006921 }
NeilBrowna9701a32005-11-08 21:39:34 -08006922 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006923 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006924 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10006925 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006926 wait_event(mddev->sb_wait,
NeilBrown09a44cc2008-05-23 13:04:36 -07006927 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928}
6929
NeilBrownfd01b882011-10-11 16:47:53 +11006930void md_write_end(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931{
6932 if (atomic_dec_and_test(&mddev->writes_pending)) {
6933 if (mddev->safemode == 2)
6934 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006935 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6937 }
6938}
6939
NeilBrown2a2275d2007-01-26 00:57:11 -08006940/* md_allow_write(mddev)
6941 * Calling this ensures that the array is marked 'active' so that writes
6942 * may proceed without blocking. It is important to call this before
6943 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6944 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006945 *
6946 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6947 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006948 */
NeilBrownfd01b882011-10-11 16:47:53 +11006949int md_allow_write(struct mddev *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006950{
6951 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006952 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006953 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006954 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006955 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006956 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006957
6958 spin_lock_irq(&mddev->write_lock);
6959 if (mddev->in_sync) {
6960 mddev->in_sync = 0;
6961 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown070dc6d2010-08-30 17:33:34 +10006962 set_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrown2a2275d2007-01-26 00:57:11 -08006963 if (mddev->safemode_delay &&
6964 mddev->safemode == 0)
6965 mddev->safemode = 1;
6966 spin_unlock_irq(&mddev->write_lock);
6967 md_update_sb(mddev, 0);
NeilBrown00bcb4a2010-06-01 19:37:23 +10006968 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006969 } else
6970 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006971
NeilBrown070dc6d2010-08-30 17:33:34 +10006972 if (test_bit(MD_CHANGE_PENDING, &mddev->flags))
Dan Williamsb5470dc2008-06-27 21:44:04 -07006973 return -EAGAIN;
6974 else
6975 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006976}
6977EXPORT_SYMBOL_GPL(md_allow_write);
6978
Linus Torvalds1da177e2005-04-16 15:20:36 -07006979#define SYNC_MARKS 10
6980#define SYNC_MARK_STEP (3*HZ)
NeilBrownfd01b882011-10-11 16:47:53 +11006981void md_do_sync(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982{
NeilBrownfd01b882011-10-11 16:47:53 +11006983 struct mddev *mddev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006984 unsigned int currspeed = 0,
6985 window;
NeilBrown57afd892005-06-21 17:17:13 -07006986 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006987 unsigned long mark[SYNC_MARKS];
6988 sector_t mark_cnt[SYNC_MARKS];
6989 int last_mark,m;
6990 struct list_head *tmp;
6991 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006992 int skipped = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11006993 struct md_rdev *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006994 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995
6996 /* just incase thread restarts... */
6997 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6998 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006999 if (mddev->ro) /* never try to sync a read-only array */
7000 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001
NeilBrown61df9d92006-10-03 01:15:57 -07007002 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7003 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
7004 desc = "data-check";
7005 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7006 desc = "requested-resync";
7007 else
7008 desc = "resync";
7009 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
7010 desc = "reshape";
7011 else
7012 desc = "recovery";
7013
Linus Torvalds1da177e2005-04-16 15:20:36 -07007014 /* we overload curr_resync somewhat here.
7015 * 0 == not engaged in resync at all
7016 * 2 == checking that there is no conflict with another sync
7017 * 1 == like 2, but have yielded to allow conflicting resync to
7018 * commense
7019 * other == active in resync - this many blocks
7020 *
7021 * Before starting a resync we must have set curr_resync to
7022 * 2, and then checked that every "conflicting" array has curr_resync
7023 * less than ours. When we find one that is the same or higher
7024 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
7025 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
7026 * This will mean we have to start checking from the beginning again.
7027 *
7028 */
7029
7030 do {
7031 mddev->curr_resync = 2;
7032
7033 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11007034 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07007035 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11007036
7037 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08007039 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040 if (mddev2 == mddev)
7041 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07007042 if (!mddev->parallel_resync
7043 && mddev2->curr_resync
7044 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045 DEFINE_WAIT(wq);
7046 if (mddev < mddev2 && mddev->curr_resync == 2) {
7047 /* arbitrarily yield */
7048 mddev->curr_resync = 1;
7049 wake_up(&resync_wait);
7050 }
7051 if (mddev > mddev2 && mddev->curr_resync == 1)
7052 /* no need to wait here, we can wait the next
7053 * time 'round when curr_resync == 2
7054 */
7055 continue;
NeilBrown97441972008-09-19 11:49:54 +10007056 /* We need to wait 'interruptible' so as not to
7057 * contribute to the load average, and not to
7058 * be caught by 'softlockup'
7059 */
7060 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08007061 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07007062 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07007063 printk(KERN_INFO "md: delaying %s of %s"
7064 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07007066 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10007068 if (signal_pending(current))
7069 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007070 schedule();
7071 finish_wait(&resync_wait, &wq);
7072 goto try_again;
7073 }
7074 finish_wait(&resync_wait, &wq);
7075 }
7076 }
7077 } while (mddev->curr_resync < 2);
7078
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007079 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08007080 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007081 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07007082 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083 */
7084 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08007085 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007086 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10007087 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7088 j = mddev->resync_min;
7089 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007090 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10007091
NeilBrownccfcc3c2006-03-27 01:18:09 -08007092 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11007093 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007094 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11007096 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007097 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007098 rcu_read_lock();
7099 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007100 if (rdev->raid_disk >= 0 &&
7101 !test_bit(Faulty, &rdev->flags) &&
7102 !test_bit(In_sync, &rdev->flags) &&
7103 rdev->recovery_offset < j)
7104 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07007105 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107
NeilBrown61df9d92006-10-03 01:15:57 -07007108 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
7109 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
7110 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07007111 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07007112 "(but not more than %d KB/sec) for %s.\n",
7113 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114
NeilBrowneea1bf32009-03-31 14:27:02 +11007115 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007116
NeilBrown57afd892005-06-21 17:17:13 -07007117 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007118 for (m = 0; m < SYNC_MARKS; m++) {
7119 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007120 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121 }
7122 last_mark = 0;
7123 mddev->resync_mark = mark[last_mark];
7124 mddev->resync_mark_cnt = mark_cnt[last_mark];
7125
7126 /*
7127 * Tune reconstruction:
7128 */
7129 window = 32*(PAGE_SIZE/512);
Jonathan Brassowac424502011-06-07 17:48:35 -05007130 printk(KERN_INFO "md: using %dk window, over a total of %lluk.\n",
7131 window/2, (unsigned long long)max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007132
7133 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134 last_check = 0;
7135
7136 if (j>2) {
7137 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007138 "md: resuming %s of %s from checkpoint.\n",
7139 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140 mddev->curr_resync = j;
7141 }
NeilBrown75d3da42011-01-14 09:14:34 +11007142 mddev->curr_resync_completed = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143
7144 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07007145 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146
NeilBrown57afd892005-06-21 17:17:13 -07007147 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11007148
NeilBrown7a91ee12009-05-26 12:57:21 +10007149 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7150 ((mddev->curr_resync > mddev->curr_resync_completed &&
7151 (mddev->curr_resync - mddev->curr_resync_completed)
7152 > (max_sectors >> 4)) ||
7153 (j - mddev->curr_resync_completed)*2
7154 >= mddev->resync_max - mddev->curr_resync_completed
7155 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11007156 /* time to update curr_resync_completed */
NeilBrown97e4f422009-03-31 14:33:13 +11007157 wait_event(mddev->recovery_wait,
7158 atomic_read(&mddev->recovery_active) == 0);
NeilBrown75d3da42011-01-14 09:14:34 +11007159 mddev->curr_resync_completed = j;
NeilBrown070dc6d2010-08-30 17:33:34 +10007160 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10007161 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11007162 }
NeilBrownacb180b2009-04-14 16:28:34 +10007163
NeilBrowne62e58a2009-07-01 13:15:35 +10007164 while (j >= mddev->resync_max && !kthread_should_stop()) {
7165 /* As this condition is controlled by user-space,
7166 * we can block indefinitely, so use '_interruptible'
7167 * to avoid triggering warnings.
7168 */
7169 flush_signals(current); /* just in case */
7170 wait_event_interruptible(mddev->recovery_wait,
7171 mddev->resync_max > j
7172 || kthread_should_stop());
7173 }
NeilBrownacb180b2009-04-14 16:28:34 +10007174
7175 if (kthread_should_stop())
7176 goto interrupted;
7177
NeilBrown57afd892005-06-21 17:17:13 -07007178 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08007179 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07007180 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07007181 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007182 goto out;
7183 }
NeilBrown57afd892005-06-21 17:17:13 -07007184
7185 if (!skipped) { /* actual IO requested */
7186 io_sectors += sectors;
7187 atomic_add(sectors, &mddev->recovery_active);
7188 }
7189
NeilBrowne875ece2011-07-28 11:39:24 +10007190 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7191 break;
7192
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193 j += sectors;
7194 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07007195 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08007196 if (last_check == 0)
NeilBrowne875ece2011-07-28 11:39:24 +10007197 /* this is the earliest that rebuild will be
NeilBrownd7603b72006-01-06 00:20:30 -08007198 * visible in /proc/mdstat
7199 */
7200 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07007201
7202 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203 continue;
7204
NeilBrown57afd892005-06-21 17:17:13 -07007205 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206 repeat:
7207 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
7208 /* step marks */
7209 int next = (last_mark+1) % SYNC_MARKS;
7210
7211 mddev->resync_mark = mark[next];
7212 mddev->resync_mark_cnt = mark_cnt[next];
7213 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07007214 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007215 last_mark = next;
7216 }
7217
7218
NeilBrownc6207272008-02-06 01:39:52 -08007219 if (kthread_should_stop())
7220 goto interrupted;
7221
Linus Torvalds1da177e2005-04-16 15:20:36 -07007222
7223 /*
7224 * this loop exits only if either when we are slower than
7225 * the 'hard' speed limit, or the system was IO-idle for
7226 * a jiffy.
7227 * the system might be non-idle CPU-wise, but we only care
7228 * about not overloading the IO subsystem. (things like an
7229 * e2fsck being done on the RAID array should execute fast)
7230 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231 cond_resched();
7232
NeilBrown57afd892005-06-21 17:17:13 -07007233 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
7234 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235
NeilBrown88202a02006-01-06 00:21:36 -08007236 if (currspeed > speed_min(mddev)) {
7237 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11007238 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08007239 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240 goto repeat;
7241 }
7242 }
7243 }
NeilBrown61df9d92006-10-03 01:15:57 -07007244 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007245 /*
7246 * this also signals 'finished resyncing' to md_stop
7247 */
7248 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
7250
7251 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07007252 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007253
NeilBrowndfc70642008-05-23 13:04:39 -07007254 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007255 mddev->curr_resync > 2) {
7256 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
7257 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7258 if (mddev->curr_resync >= mddev->recovery_cp) {
7259 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07007260 "md: checkpointing %s of %s.\n",
7261 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007262 mddev->recovery_cp = mddev->curr_resync;
7263 }
7264 } else
7265 mddev->recovery_cp = MaxSector;
7266 } else {
7267 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
7268 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07007269 rcu_read_lock();
7270 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007271 if (rdev->raid_disk >= 0 &&
NeilBrown70fffd02010-06-16 17:01:25 +10007272 mddev->delta_disks >= 0 &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007273 !test_bit(Faulty, &rdev->flags) &&
7274 !test_bit(In_sync, &rdev->flags) &&
7275 rdev->recovery_offset < mddev->curr_resync)
7276 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07007277 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007279 }
NeilBrown17571282006-12-10 02:20:52 -08007280 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007281
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11007283 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
7284 /* We completed so min/max setting can be forgotten if used. */
7285 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7286 mddev->resync_min = 0;
7287 mddev->resync_max = MaxSector;
7288 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
7289 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007290 mddev->curr_resync = 0;
7291 wake_up(&resync_wait);
7292 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
7293 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08007294 return;
7295
7296 interrupted:
7297 /*
7298 * got a signal, exit.
7299 */
7300 printk(KERN_INFO
7301 "md: md_do_sync() got signal ... exiting\n");
7302 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
7303 goto out;
7304
Linus Torvalds1da177e2005-04-16 15:20:36 -07007305}
NeilBrown29269552006-03-27 01:18:10 -08007306EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007307
NeilBrownfd01b882011-10-11 16:47:53 +11007308static int remove_and_add_spares(struct mddev *mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007309{
NeilBrown3cb03002011-10-11 16:45:26 +11007310 struct md_rdev *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007311 int spares = 0;
7312
NeilBrown97e4f422009-03-31 14:33:13 +11007313 mddev->curr_resync_completed = 0;
7314
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007315 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007316 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07007317 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007318 (test_bit(Faulty, &rdev->flags) ||
7319 ! test_bit(In_sync, &rdev->flags)) &&
7320 atomic_read(&rdev->nr_pending)==0) {
7321 if (mddev->pers->hot_remove_disk(
7322 mddev, rdev->raid_disk)==0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007323 sysfs_unlink_rdev(mddev, rdev);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007324 rdev->raid_disk = -1;
7325 }
7326 }
7327
NeilBrown53890422011-07-27 11:00:36 +10007328 if (mddev->degraded) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007329 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07007330 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07007331 !test_bit(In_sync, &rdev->flags) &&
NeilBrownde393cd2011-07-28 11:31:48 +10007332 !test_bit(Faulty, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07007333 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007334 if (rdev->raid_disk < 0
7335 && !test_bit(Faulty, &rdev->flags)) {
7336 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10007337 if (mddev->pers->
7338 hot_add_disk(mddev, rdev) == 0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007339 if (sysfs_link_rdev(mddev, rdev))
NeilBrown00bcb4a2010-06-01 19:37:23 +10007340 /* failure here is OK */;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007341 spares++;
7342 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11007343 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007344 } else
7345 break;
7346 }
NeilBrowndfc70642008-05-23 13:04:39 -07007347 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007348 }
7349 return spares;
7350}
NeilBrown7ebc0be2011-01-14 09:14:33 +11007351
NeilBrownfd01b882011-10-11 16:47:53 +11007352static void reap_sync_thread(struct mddev *mddev)
NeilBrown7ebc0be2011-01-14 09:14:33 +11007353{
NeilBrown3cb03002011-10-11 16:45:26 +11007354 struct md_rdev *rdev;
NeilBrown7ebc0be2011-01-14 09:14:33 +11007355
7356 /* resync has finished, collect result */
NeilBrown01f96c02011-09-21 15:30:20 +10007357 md_unregister_thread(&mddev->sync_thread);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007358 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7359 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
7360 /* success...*/
7361 /* activate any spares */
7362 if (mddev->pers->spare_active(mddev))
7363 sysfs_notify(&mddev->kobj, NULL,
7364 "degraded");
7365 }
7366 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7367 mddev->pers->finish_reshape)
7368 mddev->pers->finish_reshape(mddev);
7369 md_update_sb(mddev, 1);
7370
7371 /* if array is no-longer degraded, then any saved_raid_disk
7372 * information must be scrapped
7373 */
7374 if (!mddev->degraded)
7375 list_for_each_entry(rdev, &mddev->disks, same_set)
7376 rdev->saved_raid_disk = -1;
7377
7378 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7379 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7380 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7381 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7382 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7383 /* flag recovery needed just to double check */
7384 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7385 sysfs_notify_dirent_safe(mddev->sysfs_action);
7386 md_new_event(mddev);
Jonathan Brassow768e5872011-07-27 11:00:37 +10007387 if (mddev->event_work.func)
7388 queue_work(md_misc_wq, &mddev->event_work);
NeilBrown7ebc0be2011-01-14 09:14:33 +11007389}
7390
Linus Torvalds1da177e2005-04-16 15:20:36 -07007391/*
7392 * This routine is regularly called by all per-raid-array threads to
7393 * deal with generic issues like resync and super-block update.
7394 * Raid personalities that don't have a thread (linear/raid0) do not
7395 * need this as they never do any recovery or update the superblock.
7396 *
7397 * It does not do any resync itself, but rather "forks" off other threads
7398 * to do that as needed.
7399 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
7400 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07007401 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07007402 * and wakeups up this thread which will reap the thread and finish up.
7403 * This thread also removes any faulty devices (with nr_pending == 0).
7404 *
7405 * The overall approach is:
7406 * 1/ if the superblock needs updating, update it.
7407 * 2/ If a recovery thread is running, don't do anything else.
7408 * 3/ If recovery has finished, clean up, possibly marking spares active.
7409 * 4/ If there are any faulty devices, remove them.
7410 * 5/ If array is degraded, try to add spares devices
7411 * 6/ If array has spares or is not in-sync, start a resync thread.
7412 */
NeilBrownfd01b882011-10-11 16:47:53 +11007413void md_check_recovery(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007414{
Jonathan Brassow68866e42011-06-08 15:10:08 +10007415 if (mddev->suspended)
7416 return;
7417
NeilBrown5f404022005-06-21 17:17:16 -07007418 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11007419 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007420
NeilBrownfca4d842005-06-21 17:17:11 -07007421 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07007422 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07007423 printk(KERN_INFO "md: %s in immediate safe mode\n",
7424 mdname(mddev));
7425 mddev->safemode = 2;
7426 }
7427 flush_signals(current);
7428 }
7429
NeilBrownc89a8ee2008-08-05 15:54:13 +10007430 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
7431 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007432 if ( ! (
NeilBrown126925c2010-09-07 17:02:47 +10007433 (mddev->flags & ~ (1<<MD_CHANGE_PENDING)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07007434 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007435 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07007436 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07007437 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
7438 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007439 ))
7440 return;
NeilBrownfca4d842005-06-21 17:17:11 -07007441
NeilBrowndf5b89b2006-03-27 01:18:20 -08007442 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007443 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07007444
NeilBrownc89a8ee2008-08-05 15:54:13 +10007445 if (mddev->ro) {
7446 /* Only thing we do on a ro array is remove
7447 * failed devices.
7448 */
NeilBrown3cb03002011-10-11 16:45:26 +11007449 struct md_rdev *rdev;
NeilBrowna8c42c72011-01-31 13:47:13 +11007450 list_for_each_entry(rdev, &mddev->disks, same_set)
7451 if (rdev->raid_disk >= 0 &&
7452 !test_bit(Blocked, &rdev->flags) &&
7453 test_bit(Faulty, &rdev->flags) &&
7454 atomic_read(&rdev->nr_pending)==0) {
7455 if (mddev->pers->hot_remove_disk(
7456 mddev, rdev->raid_disk)==0) {
Namhyung Kim36fad852011-07-27 11:00:36 +10007457 sysfs_unlink_rdev(mddev, rdev);
NeilBrowna8c42c72011-01-31 13:47:13 +11007458 rdev->raid_disk = -1;
7459 }
7460 }
NeilBrownc89a8ee2008-08-05 15:54:13 +10007461 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7462 goto unlock;
7463 }
7464
NeilBrown31a59e32008-04-30 00:52:30 -07007465 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10007466 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07007467 spin_lock_irq(&mddev->write_lock);
7468 if (mddev->safemode &&
7469 !atomic_read(&mddev->writes_pending) &&
7470 !mddev->in_sync &&
7471 mddev->recovery_cp == MaxSector) {
7472 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007473 did_change = 1;
NeilBrown070dc6d2010-08-30 17:33:34 +10007474 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown31a59e32008-04-30 00:52:30 -07007475 }
7476 if (mddev->safemode == 1)
7477 mddev->safemode = 0;
7478 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007479 if (did_change)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007480 sysfs_notify_dirent_safe(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007481 }
NeilBrownfca4d842005-06-21 17:17:11 -07007482
NeilBrown850b2b42006-10-03 01:15:46 -07007483 if (mddev->flags)
7484 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007485
Linus Torvalds1da177e2005-04-16 15:20:36 -07007486 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7487 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7488 /* resync/recovery still happening */
7489 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7490 goto unlock;
7491 }
7492 if (mddev->sync_thread) {
NeilBrown7ebc0be2011-01-14 09:14:33 +11007493 reap_sync_thread(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007494 goto unlock;
7495 }
Neil Brown72a23c22008-06-28 08:31:41 +10007496 /* Set RUNNING before clearing NEEDED to avoid
7497 * any transients in the value of "sync_action".
7498 */
7499 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7500 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007501 /* Clear some bits that don't mean anything, but
7502 * might be left set
7503 */
NeilBrown24dd4692005-11-08 21:39:26 -08007504 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7505 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007506
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007507 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7508 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007509 /* no recovery is running.
7510 * remove any failed drives, then
7511 * add spares if possible.
7512 * Spare are also removed and re-added, to allow
7513 * the personality to fail the re-add.
7514 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007516 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007517 if (mddev->pers->check_reshape == NULL ||
7518 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007519 /* Cannot proceed */
7520 goto unlock;
7521 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007522 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007523 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007524 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7525 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007526 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007527 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007528 } else if (mddev->recovery_cp < MaxSector) {
7529 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007530 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007531 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7532 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007533 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007534
Linus Torvalds1da177e2005-04-16 15:20:36 -07007535 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007536 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7537 /* We are adding a device or devices to an array
7538 * which has the bitmap stored on all devices.
7539 * So make sure all bitmap pages get written
7540 */
7541 bitmap_write_all(mddev->bitmap);
7542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007543 mddev->sync_thread = md_register_thread(md_do_sync,
7544 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007545 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007546 if (!mddev->sync_thread) {
7547 printk(KERN_ERR "%s: could not start resync"
7548 " thread...\n",
7549 mdname(mddev));
7550 /* leave the spares where they are, it shouldn't hurt */
NeilBrown7ebc0be2011-01-14 09:14:33 +11007551 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7552 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7553 clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7554 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
7555 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08007556 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007557 md_wakeup_thread(mddev->sync_thread);
NeilBrown00bcb4a2010-06-01 19:37:23 +10007558 sysfs_notify_dirent_safe(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007559 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007560 }
7561 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007562 if (!mddev->sync_thread) {
7563 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7564 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7565 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007566 if (mddev->sysfs_action)
NeilBrown00bcb4a2010-06-01 19:37:23 +10007567 sysfs_notify_dirent_safe(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007569 mddev_unlock(mddev);
7570 }
7571}
7572
NeilBrownfd01b882011-10-11 16:47:53 +11007573void md_wait_for_blocked_rdev(struct md_rdev *rdev, struct mddev *mddev)
Dan Williams6bfe0b42008-04-30 00:52:32 -07007574{
NeilBrown00bcb4a2010-06-01 19:37:23 +10007575 sysfs_notify_dirent_safe(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007576 wait_event_timeout(rdev->blocked_wait,
NeilBrownde393cd2011-07-28 11:31:48 +10007577 !test_bit(Blocked, &rdev->flags) &&
7578 !test_bit(BlockedBadBlocks, &rdev->flags),
Dan Williams6bfe0b42008-04-30 00:52:32 -07007579 msecs_to_jiffies(5000));
7580 rdev_dec_pending(rdev, mddev);
7581}
7582EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7583
NeilBrown2230dfe2011-07-28 11:31:46 +10007584
7585/* Bad block management.
7586 * We can record which blocks on each device are 'bad' and so just
7587 * fail those blocks, or that stripe, rather than the whole device.
7588 * Entries in the bad-block table are 64bits wide. This comprises:
7589 * Length of bad-range, in sectors: 0-511 for lengths 1-512
7590 * Start of bad-range, sector offset, 54 bits (allows 8 exbibytes)
7591 * A 'shift' can be set so that larger blocks are tracked and
7592 * consequently larger devices can be covered.
7593 * 'Acknowledged' flag - 1 bit. - the most significant bit.
7594 *
7595 * Locking of the bad-block table uses a seqlock so md_is_badblock
7596 * might need to retry if it is very unlucky.
7597 * We will sometimes want to check for bad blocks in a bi_end_io function,
7598 * so we use the write_seqlock_irq variant.
7599 *
7600 * When looking for a bad block we specify a range and want to
7601 * know if any block in the range is bad. So we binary-search
7602 * to the last range that starts at-or-before the given endpoint,
7603 * (or "before the sector after the target range")
7604 * then see if it ends after the given start.
7605 * We return
7606 * 0 if there are no known bad blocks in the range
7607 * 1 if there are known bad block which are all acknowledged
7608 * -1 if there are bad blocks which have not yet been acknowledged in metadata.
7609 * plus the start/length of the first bad section we overlap.
7610 */
7611int md_is_badblock(struct badblocks *bb, sector_t s, int sectors,
7612 sector_t *first_bad, int *bad_sectors)
7613{
7614 int hi;
7615 int lo = 0;
7616 u64 *p = bb->page;
7617 int rv = 0;
7618 sector_t target = s + sectors;
7619 unsigned seq;
7620
7621 if (bb->shift > 0) {
7622 /* round the start down, and the end up */
7623 s >>= bb->shift;
7624 target += (1<<bb->shift) - 1;
7625 target >>= bb->shift;
7626 sectors = target - s;
7627 }
7628 /* 'target' is now the first block after the bad range */
7629
7630retry:
7631 seq = read_seqbegin(&bb->lock);
7632
7633 hi = bb->count;
7634
7635 /* Binary search between lo and hi for 'target'
7636 * i.e. for the last range that starts before 'target'
7637 */
7638 /* INVARIANT: ranges before 'lo' and at-or-after 'hi'
7639 * are known not to be the last range before target.
7640 * VARIANT: hi-lo is the number of possible
7641 * ranges, and decreases until it reaches 1
7642 */
7643 while (hi - lo > 1) {
7644 int mid = (lo + hi) / 2;
7645 sector_t a = BB_OFFSET(p[mid]);
7646 if (a < target)
7647 /* This could still be the one, earlier ranges
7648 * could not. */
7649 lo = mid;
7650 else
7651 /* This and later ranges are definitely out. */
7652 hi = mid;
7653 }
7654 /* 'lo' might be the last that started before target, but 'hi' isn't */
7655 if (hi > lo) {
7656 /* need to check all range that end after 's' to see if
7657 * any are unacknowledged.
7658 */
7659 while (lo >= 0 &&
7660 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7661 if (BB_OFFSET(p[lo]) < target) {
7662 /* starts before the end, and finishes after
7663 * the start, so they must overlap
7664 */
7665 if (rv != -1 && BB_ACK(p[lo]))
7666 rv = 1;
7667 else
7668 rv = -1;
7669 *first_bad = BB_OFFSET(p[lo]);
7670 *bad_sectors = BB_LEN(p[lo]);
7671 }
7672 lo--;
7673 }
7674 }
7675
7676 if (read_seqretry(&bb->lock, seq))
7677 goto retry;
7678
7679 return rv;
7680}
7681EXPORT_SYMBOL_GPL(md_is_badblock);
7682
7683/*
7684 * Add a range of bad blocks to the table.
7685 * This might extend the table, or might contract it
7686 * if two adjacent ranges can be merged.
7687 * We binary-search to find the 'insertion' point, then
7688 * decide how best to handle it.
7689 */
7690static int md_set_badblocks(struct badblocks *bb, sector_t s, int sectors,
7691 int acknowledged)
7692{
7693 u64 *p;
7694 int lo, hi;
7695 int rv = 1;
7696
7697 if (bb->shift < 0)
7698 /* badblocks are disabled */
7699 return 0;
7700
7701 if (bb->shift) {
7702 /* round the start down, and the end up */
7703 sector_t next = s + sectors;
7704 s >>= bb->shift;
7705 next += (1<<bb->shift) - 1;
7706 next >>= bb->shift;
7707 sectors = next - s;
7708 }
7709
7710 write_seqlock_irq(&bb->lock);
7711
7712 p = bb->page;
7713 lo = 0;
7714 hi = bb->count;
7715 /* Find the last range that starts at-or-before 's' */
7716 while (hi - lo > 1) {
7717 int mid = (lo + hi) / 2;
7718 sector_t a = BB_OFFSET(p[mid]);
7719 if (a <= s)
7720 lo = mid;
7721 else
7722 hi = mid;
7723 }
7724 if (hi > lo && BB_OFFSET(p[lo]) > s)
7725 hi = lo;
7726
7727 if (hi > lo) {
7728 /* we found a range that might merge with the start
7729 * of our new range
7730 */
7731 sector_t a = BB_OFFSET(p[lo]);
7732 sector_t e = a + BB_LEN(p[lo]);
7733 int ack = BB_ACK(p[lo]);
7734 if (e >= s) {
7735 /* Yes, we can merge with a previous range */
7736 if (s == a && s + sectors >= e)
7737 /* new range covers old */
7738 ack = acknowledged;
7739 else
7740 ack = ack && acknowledged;
7741
7742 if (e < s + sectors)
7743 e = s + sectors;
7744 if (e - a <= BB_MAX_LEN) {
7745 p[lo] = BB_MAKE(a, e-a, ack);
7746 s = e;
7747 } else {
7748 /* does not all fit in one range,
7749 * make p[lo] maximal
7750 */
7751 if (BB_LEN(p[lo]) != BB_MAX_LEN)
7752 p[lo] = BB_MAKE(a, BB_MAX_LEN, ack);
7753 s = a + BB_MAX_LEN;
7754 }
7755 sectors = e - s;
7756 }
7757 }
7758 if (sectors && hi < bb->count) {
7759 /* 'hi' points to the first range that starts after 's'.
7760 * Maybe we can merge with the start of that range */
7761 sector_t a = BB_OFFSET(p[hi]);
7762 sector_t e = a + BB_LEN(p[hi]);
7763 int ack = BB_ACK(p[hi]);
7764 if (a <= s + sectors) {
7765 /* merging is possible */
7766 if (e <= s + sectors) {
7767 /* full overlap */
7768 e = s + sectors;
7769 ack = acknowledged;
7770 } else
7771 ack = ack && acknowledged;
7772
7773 a = s;
7774 if (e - a <= BB_MAX_LEN) {
7775 p[hi] = BB_MAKE(a, e-a, ack);
7776 s = e;
7777 } else {
7778 p[hi] = BB_MAKE(a, BB_MAX_LEN, ack);
7779 s = a + BB_MAX_LEN;
7780 }
7781 sectors = e - s;
7782 lo = hi;
7783 hi++;
7784 }
7785 }
7786 if (sectors == 0 && hi < bb->count) {
7787 /* we might be able to combine lo and hi */
7788 /* Note: 's' is at the end of 'lo' */
7789 sector_t a = BB_OFFSET(p[hi]);
7790 int lolen = BB_LEN(p[lo]);
7791 int hilen = BB_LEN(p[hi]);
7792 int newlen = lolen + hilen - (s - a);
7793 if (s >= a && newlen < BB_MAX_LEN) {
7794 /* yes, we can combine them */
7795 int ack = BB_ACK(p[lo]) && BB_ACK(p[hi]);
7796 p[lo] = BB_MAKE(BB_OFFSET(p[lo]), newlen, ack);
7797 memmove(p + hi, p + hi + 1,
7798 (bb->count - hi - 1) * 8);
7799 bb->count--;
7800 }
7801 }
7802 while (sectors) {
7803 /* didn't merge (it all).
7804 * Need to add a range just before 'hi' */
7805 if (bb->count >= MD_MAX_BADBLOCKS) {
7806 /* No room for more */
7807 rv = 0;
7808 break;
7809 } else {
7810 int this_sectors = sectors;
7811 memmove(p + hi + 1, p + hi,
7812 (bb->count - hi) * 8);
7813 bb->count++;
7814
7815 if (this_sectors > BB_MAX_LEN)
7816 this_sectors = BB_MAX_LEN;
7817 p[hi] = BB_MAKE(s, this_sectors, acknowledged);
7818 sectors -= this_sectors;
7819 s += this_sectors;
7820 }
7821 }
7822
7823 bb->changed = 1;
NeilBrownde393cd2011-07-28 11:31:48 +10007824 if (!acknowledged)
7825 bb->unacked_exist = 1;
NeilBrown2230dfe2011-07-28 11:31:46 +10007826 write_sequnlock_irq(&bb->lock);
7827
7828 return rv;
7829}
7830
NeilBrown3cb03002011-10-11 16:45:26 +11007831int rdev_set_badblocks(struct md_rdev *rdev, sector_t s, int sectors,
NeilBrown2230dfe2011-07-28 11:31:46 +10007832 int acknowledged)
7833{
7834 int rv = md_set_badblocks(&rdev->badblocks,
7835 s + rdev->data_offset, sectors, acknowledged);
7836 if (rv) {
7837 /* Make sure they get written out promptly */
7838 set_bit(MD_CHANGE_CLEAN, &rdev->mddev->flags);
7839 md_wakeup_thread(rdev->mddev->thread);
7840 }
7841 return rv;
7842}
7843EXPORT_SYMBOL_GPL(rdev_set_badblocks);
7844
7845/*
7846 * Remove a range of bad blocks from the table.
7847 * This may involve extending the table if we spilt a region,
7848 * but it must not fail. So if the table becomes full, we just
7849 * drop the remove request.
7850 */
7851static int md_clear_badblocks(struct badblocks *bb, sector_t s, int sectors)
7852{
7853 u64 *p;
7854 int lo, hi;
7855 sector_t target = s + sectors;
7856 int rv = 0;
7857
7858 if (bb->shift > 0) {
7859 /* When clearing we round the start up and the end down.
7860 * This should not matter as the shift should align with
7861 * the block size and no rounding should ever be needed.
7862 * However it is better the think a block is bad when it
7863 * isn't than to think a block is not bad when it is.
7864 */
7865 s += (1<<bb->shift) - 1;
7866 s >>= bb->shift;
7867 target >>= bb->shift;
7868 sectors = target - s;
7869 }
7870
7871 write_seqlock_irq(&bb->lock);
7872
7873 p = bb->page;
7874 lo = 0;
7875 hi = bb->count;
7876 /* Find the last range that starts before 'target' */
7877 while (hi - lo > 1) {
7878 int mid = (lo + hi) / 2;
7879 sector_t a = BB_OFFSET(p[mid]);
7880 if (a < target)
7881 lo = mid;
7882 else
7883 hi = mid;
7884 }
7885 if (hi > lo) {
7886 /* p[lo] is the last range that could overlap the
7887 * current range. Earlier ranges could also overlap,
7888 * but only this one can overlap the end of the range.
7889 */
7890 if (BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > target) {
7891 /* Partial overlap, leave the tail of this range */
7892 int ack = BB_ACK(p[lo]);
7893 sector_t a = BB_OFFSET(p[lo]);
7894 sector_t end = a + BB_LEN(p[lo]);
7895
7896 if (a < s) {
7897 /* we need to split this range */
7898 if (bb->count >= MD_MAX_BADBLOCKS) {
7899 rv = 0;
7900 goto out;
7901 }
7902 memmove(p+lo+1, p+lo, (bb->count - lo) * 8);
7903 bb->count++;
7904 p[lo] = BB_MAKE(a, s-a, ack);
7905 lo++;
7906 }
7907 p[lo] = BB_MAKE(target, end - target, ack);
7908 /* there is no longer an overlap */
7909 hi = lo;
7910 lo--;
7911 }
7912 while (lo >= 0 &&
7913 BB_OFFSET(p[lo]) + BB_LEN(p[lo]) > s) {
7914 /* This range does overlap */
7915 if (BB_OFFSET(p[lo]) < s) {
7916 /* Keep the early parts of this range. */
7917 int ack = BB_ACK(p[lo]);
7918 sector_t start = BB_OFFSET(p[lo]);
7919 p[lo] = BB_MAKE(start, s - start, ack);
7920 /* now low doesn't overlap, so.. */
7921 break;
7922 }
7923 lo--;
7924 }
7925 /* 'lo' is strictly before, 'hi' is strictly after,
7926 * anything between needs to be discarded
7927 */
7928 if (hi - lo > 1) {
7929 memmove(p+lo+1, p+hi, (bb->count - hi) * 8);
7930 bb->count -= (hi - lo - 1);
7931 }
7932 }
7933
7934 bb->changed = 1;
7935out:
7936 write_sequnlock_irq(&bb->lock);
7937 return rv;
7938}
7939
NeilBrown3cb03002011-10-11 16:45:26 +11007940int rdev_clear_badblocks(struct md_rdev *rdev, sector_t s, int sectors)
NeilBrown2230dfe2011-07-28 11:31:46 +10007941{
7942 return md_clear_badblocks(&rdev->badblocks,
7943 s + rdev->data_offset,
7944 sectors);
7945}
7946EXPORT_SYMBOL_GPL(rdev_clear_badblocks);
7947
7948/*
7949 * Acknowledge all bad blocks in a list.
7950 * This only succeeds if ->changed is clear. It is used by
7951 * in-kernel metadata updates
7952 */
7953void md_ack_all_badblocks(struct badblocks *bb)
7954{
7955 if (bb->page == NULL || bb->changed)
7956 /* no point even trying */
7957 return;
7958 write_seqlock_irq(&bb->lock);
7959
7960 if (bb->changed == 0) {
7961 u64 *p = bb->page;
7962 int i;
7963 for (i = 0; i < bb->count ; i++) {
7964 if (!BB_ACK(p[i])) {
7965 sector_t start = BB_OFFSET(p[i]);
7966 int len = BB_LEN(p[i]);
7967 p[i] = BB_MAKE(start, len, 1);
7968 }
7969 }
NeilBrownde393cd2011-07-28 11:31:48 +10007970 bb->unacked_exist = 0;
NeilBrown2230dfe2011-07-28 11:31:46 +10007971 }
7972 write_sequnlock_irq(&bb->lock);
7973}
7974EXPORT_SYMBOL_GPL(md_ack_all_badblocks);
7975
NeilBrown16c791a2011-07-28 11:31:47 +10007976/* sysfs access to bad-blocks list.
7977 * We present two files.
7978 * 'bad-blocks' lists sector numbers and lengths of ranges that
7979 * are recorded as bad. The list is truncated to fit within
7980 * the one-page limit of sysfs.
7981 * Writing "sector length" to this file adds an acknowledged
7982 * bad block list.
7983 * 'unacknowledged-bad-blocks' lists bad blocks that have not yet
7984 * been acknowledged. Writing to this file adds bad blocks
7985 * without acknowledging them. This is largely for testing.
7986 */
7987
7988static ssize_t
7989badblocks_show(struct badblocks *bb, char *page, int unack)
7990{
7991 size_t len;
7992 int i;
7993 u64 *p = bb->page;
7994 unsigned seq;
7995
7996 if (bb->shift < 0)
7997 return 0;
7998
7999retry:
8000 seq = read_seqbegin(&bb->lock);
8001
8002 len = 0;
8003 i = 0;
8004
8005 while (len < PAGE_SIZE && i < bb->count) {
8006 sector_t s = BB_OFFSET(p[i]);
8007 unsigned int length = BB_LEN(p[i]);
8008 int ack = BB_ACK(p[i]);
8009 i++;
8010
8011 if (unack && ack)
8012 continue;
8013
8014 len += snprintf(page+len, PAGE_SIZE-len, "%llu %u\n",
8015 (unsigned long long)s << bb->shift,
8016 length << bb->shift);
8017 }
NeilBrownde393cd2011-07-28 11:31:48 +10008018 if (unack && len == 0)
8019 bb->unacked_exist = 0;
NeilBrown16c791a2011-07-28 11:31:47 +10008020
8021 if (read_seqretry(&bb->lock, seq))
8022 goto retry;
8023
8024 return len;
8025}
8026
8027#define DO_DEBUG 1
8028
8029static ssize_t
8030badblocks_store(struct badblocks *bb, const char *page, size_t len, int unack)
8031{
8032 unsigned long long sector;
8033 int length;
8034 char newline;
8035#ifdef DO_DEBUG
8036 /* Allow clearing via sysfs *only* for testing/debugging.
8037 * Normally only a successful write may clear a badblock
8038 */
8039 int clear = 0;
8040 if (page[0] == '-') {
8041 clear = 1;
8042 page++;
8043 }
8044#endif /* DO_DEBUG */
8045
8046 switch (sscanf(page, "%llu %d%c", &sector, &length, &newline)) {
8047 case 3:
8048 if (newline != '\n')
8049 return -EINVAL;
8050 case 2:
8051 if (length <= 0)
8052 return -EINVAL;
8053 break;
8054 default:
8055 return -EINVAL;
8056 }
8057
8058#ifdef DO_DEBUG
8059 if (clear) {
8060 md_clear_badblocks(bb, sector, length);
8061 return len;
8062 }
8063#endif /* DO_DEBUG */
8064 if (md_set_badblocks(bb, sector, length, !unack))
8065 return len;
8066 else
8067 return -ENOSPC;
8068}
8069
Adrian Bunk75c96f82005-05-05 16:16:09 -07008070static int md_notify_reboot(struct notifier_block *this,
8071 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008072{
8073 struct list_head *tmp;
NeilBrownfd01b882011-10-11 16:47:53 +11008074 struct mddev *mddev;
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008075 int need_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008076
8077 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
8078
8079 printk(KERN_INFO "md: stopping all md devices.\n");
8080
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008081 for_each_mddev(mddev, tmp) {
Neil Brownc71d4882006-05-26 10:39:25 +10008082 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10008083 /* Force a switch to readonly even array
8084 * appears to still be in use. Hence
8085 * the '100'.
8086 */
NeilBrowna4bd82d2010-03-29 13:23:10 +11008087 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10008088 mddev_unlock(mddev);
8089 }
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008090 need_delay = 1;
8091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008092 /*
8093 * certain more exotic SCSI devices are known to be
8094 * volatile wrt too early system reboots. While the
8095 * right place to handle this issue is the given
8096 * driver, we do want to have a safe RAID driver ...
8097 */
Daniel P. Berrange2dba6a92011-09-23 10:40:45 +01008098 if (need_delay)
8099 mdelay(1000*1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008100 }
8101 return NOTIFY_DONE;
8102}
8103
Adrian Bunk75c96f82005-05-05 16:16:09 -07008104static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008105 .notifier_call = md_notify_reboot,
8106 .next = NULL,
8107 .priority = INT_MAX, /* before any real devices */
8108};
8109
8110static void md_geninit(void)
8111{
NeilBrown36a4e1f2011-10-07 14:23:17 +11008112 pr_debug("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07008113
Denis V. Lunevc7705f32008-04-29 01:02:35 -07008114 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008115}
8116
Adrian Bunk75c96f82005-05-05 16:16:09 -07008117static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008118{
Tejun Heoe804ac72010-10-15 15:36:08 +02008119 int ret = -ENOMEM;
8120
Tejun Heoada609e2011-01-25 14:35:54 +01008121 md_wq = alloc_workqueue("md", WQ_MEM_RECLAIM, 0);
Tejun Heoe804ac72010-10-15 15:36:08 +02008122 if (!md_wq)
8123 goto err_wq;
8124
8125 md_misc_wq = alloc_workqueue("md_misc", 0, 0);
8126 if (!md_misc_wq)
8127 goto err_misc_wq;
8128
8129 if ((ret = register_blkdev(MD_MAJOR, "md")) < 0)
8130 goto err_md;
8131
8132 if ((ret = register_blkdev(0, "mdp")) < 0)
8133 goto err_mdp;
8134 mdp_major = ret;
8135
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008136 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07008137 md_probe, NULL, NULL);
8138 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008139 md_probe, NULL, NULL);
8140
Linus Torvalds1da177e2005-04-16 15:20:36 -07008141 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08008142 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008143
8144 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11008145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008146
Tejun Heoe804ac72010-10-15 15:36:08 +02008147err_mdp:
8148 unregister_blkdev(MD_MAJOR, "md");
8149err_md:
8150 destroy_workqueue(md_misc_wq);
8151err_misc_wq:
8152 destroy_workqueue(md_wq);
8153err_wq:
8154 return ret;
8155}
Linus Torvalds1da177e2005-04-16 15:20:36 -07008156
8157#ifndef MODULE
8158
8159/*
8160 * Searches all registered partitions for autorun RAID arrays
8161 * at boot time.
8162 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008163
8164static LIST_HEAD(all_detected_devices);
8165struct detected_devices_node {
8166 struct list_head list;
8167 dev_t dev;
8168};
Linus Torvalds1da177e2005-04-16 15:20:36 -07008169
8170void md_autodetect_dev(dev_t dev)
8171{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008172 struct detected_devices_node *node_detected_dev;
8173
8174 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
8175 if (node_detected_dev) {
8176 node_detected_dev->dev = dev;
8177 list_add_tail(&node_detected_dev->list, &all_detected_devices);
8178 } else {
8179 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
8180 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
8181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07008182}
8183
8184
8185static void autostart_arrays(int part)
8186{
NeilBrown3cb03002011-10-11 16:45:26 +11008187 struct md_rdev *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008188 struct detected_devices_node *node_detected_dev;
8189 dev_t dev;
8190 int i_scanned, i_passed;
8191
8192 i_scanned = 0;
8193 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008194
8195 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
8196
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008197 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
8198 i_scanned++;
8199 node_detected_dev = list_entry(all_detected_devices.next,
8200 struct detected_devices_node, list);
8201 list_del(&node_detected_dev->list);
8202 dev = node_detected_dev->dev;
8203 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07008204 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008205 if (IS_ERR(rdev))
8206 continue;
8207
NeilBrownb2d444d2005-11-08 21:39:31 -08008208 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008209 MD_BUG();
8210 continue;
8211 }
NeilBrownd0fae182008-03-04 14:29:31 -08008212 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008213 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008214 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008215 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07008216
8217 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
8218 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008219
8220 autorun_devices(part);
8221}
8222
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08008223#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008224
8225static __exit void md_exit(void)
8226{
NeilBrownfd01b882011-10-11 16:47:53 +11008227 struct mddev *mddev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008228 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07008229
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008230 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07008231 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008232
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11008233 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008234 unregister_blkdev(mdp_major, "mdp");
8235 unregister_reboot_notifier(&md_notifier);
8236 unregister_sysctl_table(raid_table_header);
8237 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08008238 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07008239 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11008240 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008241 }
Tejun Heoe804ac72010-10-15 15:36:08 +02008242 destroy_workqueue(md_misc_wq);
8243 destroy_workqueue(md_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008244}
8245
Dan Williams685784a2007-07-09 11:56:42 -07008246subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008247module_exit(md_exit)
8248
NeilBrownf91de922005-11-08 21:39:36 -08008249static int get_ro(char *buffer, struct kernel_param *kp)
8250{
8251 return sprintf(buffer, "%d", start_readonly);
8252}
8253static int set_ro(const char *val, struct kernel_param *kp)
8254{
8255 char *e;
8256 int num = simple_strtoul(val, &e, 10);
8257 if (*val && (*e == '\0' || *e == '\n')) {
8258 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08008259 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08008260 }
8261 return -EINVAL;
8262}
8263
NeilBrown80ca3a42006-07-10 04:44:18 -07008264module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
8265module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8e2006-01-06 00:20:15 -08008266
NeilBrownefeb53c2009-01-09 08:31:10 +11008267module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08008268
Linus Torvalds1da177e2005-04-16 15:20:36 -07008269EXPORT_SYMBOL(register_md_personality);
8270EXPORT_SYMBOL(unregister_md_personality);
8271EXPORT_SYMBOL(md_error);
8272EXPORT_SYMBOL(md_done_sync);
8273EXPORT_SYMBOL(md_write_start);
8274EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008275EXPORT_SYMBOL(md_register_thread);
8276EXPORT_SYMBOL(md_unregister_thread);
8277EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008278EXPORT_SYMBOL(md_check_recovery);
8279MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008280MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07008281MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07008282MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);